Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | 3570a34 | 2000-09-25 21:45:58 +0000 | [diff] [blame] | 2 | #include "busybox.h" |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 3 | #include <stdlib.h> |
| 4 | #include <unistd.h> |
| 5 | #include <errno.h> |
| 6 | #include <string.h> |
| 7 | #include <stdio.h> |
| 8 | #include <mntent.h> |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 9 | #include <fstab.h> |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 10 | #include <sys/mount.h> |
| 11 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 12 | extern const char mtab_file[]; /* Defined in utility.c */ |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 13 | |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 14 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 15 | void erase_mtab(const char *name) |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 16 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 17 | struct mntent entries[20]; |
| 18 | int count = 0; |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 19 | FILE *mountTable = setmntent(mtab_file, "r"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 20 | struct mntent *m; |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 21 | |
Eric Andersen | 0ecb54a | 1999-12-05 23:24:55 +0000 | [diff] [blame] | 22 | /* Check if reading the mtab file failed */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 23 | if (mountTable == 0 |
Eric Andersen | af4ac77 | 2001-02-01 22:43:49 +0000 | [diff] [blame^] | 24 | /* Bummer. fall back on trying the /proc filesystem */ |
| 25 | && (mountTable = setmntent("/proc/mounts", "r")) == 0) { |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 26 | perror_msg("%s", mtab_file); |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 27 | return; |
| 28 | } |
| 29 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 30 | while ((m = getmntent(mountTable)) != 0) { |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 31 | entries[count].mnt_fsname = strdup(m->mnt_fsname); |
| 32 | entries[count].mnt_dir = strdup(m->mnt_dir); |
| 33 | entries[count].mnt_type = strdup(m->mnt_type); |
| 34 | entries[count].mnt_opts = strdup(m->mnt_opts); |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 35 | entries[count].mnt_freq = m->mnt_freq; |
| 36 | entries[count].mnt_passno = m->mnt_passno; |
| 37 | count++; |
| 38 | } |
| 39 | endmntent(mountTable); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 40 | if ((mountTable = setmntent(mtab_file, "w"))) { |
| 41 | int i; |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 42 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 43 | for (i = 0; i < count; i++) { |
| 44 | int result = (strcmp(entries[i].mnt_fsname, name) == 0 |
| 45 | || strcmp(entries[i].mnt_dir, name) == 0); |
| 46 | |
| 47 | if (result) |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 48 | continue; |
| 49 | else |
| 50 | addmntent(mountTable, &entries[i]); |
| 51 | } |
| 52 | endmntent(mountTable); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 53 | } else if (errno != EROFS) |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 54 | perror_msg("%s", mtab_file); |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 57 | void write_mtab(char *blockDevice, char *directory, |
| 58 | char *filesystemType, long flags, char *string_flags) |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 59 | { |
| 60 | FILE *mountTable = setmntent(mtab_file, "a+"); |
| 61 | struct mntent m; |
| 62 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 63 | if (mountTable == 0) { |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 64 | perror_msg("%s", mtab_file); |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 65 | return; |
| 66 | } |
| 67 | if (mountTable) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 68 | int length = strlen(directory); |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 69 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 70 | if (length > 1 && directory[length - 1] == '/') |
| 71 | directory[length - 1] = '\0'; |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 72 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 73 | if (filesystemType == 0) { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 74 | struct mntent *p = find_mount_point(blockDevice, "/proc/mounts"); |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 75 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 76 | if (p && p->mnt_type) |
| 77 | filesystemType = p->mnt_type; |
| 78 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 79 | m.mnt_fsname = blockDevice; |
| 80 | m.mnt_dir = directory; |
| 81 | m.mnt_type = filesystemType ? filesystemType : "default"; |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 82 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 83 | if (*string_flags) { |
| 84 | m.mnt_opts = string_flags; |
| 85 | } else { |
| 86 | if ((flags | MS_RDONLY) == flags) |
| 87 | m.mnt_opts = "ro"; |
| 88 | else |
| 89 | m.mnt_opts = "rw"; |
| 90 | } |
| 91 | |
| 92 | m.mnt_freq = 0; |
| 93 | m.mnt_passno = 0; |
| 94 | addmntent(mountTable, &m); |
| 95 | endmntent(mountTable); |
| 96 | } |
| 97 | } |