Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 2 | #include "internal.h" |
| 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 | 0ecb54a | 1999-12-05 23:24:55 +0000 | [diff] [blame] | 24 | #if ! defined BB_FEATURE_USE_PROCFS |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 25 | ) { |
| 26 | #else |
| 27 | /* Bummer. fall back on trying the /proc filesystem */ |
| 28 | && (mountTable = setmntent("/proc/mounts", "r")) == 0) { |
Eric Andersen | 0ecb54a | 1999-12-05 23:24:55 +0000 | [diff] [blame] | 29 | #endif |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 30 | perror(mtab_file); |
| 31 | return; |
| 32 | } |
| 33 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 34 | while ((m = getmntent(mountTable)) != 0) { |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 35 | entries[count].mnt_fsname = strdup(m->mnt_fsname); |
| 36 | entries[count].mnt_dir = strdup(m->mnt_dir); |
| 37 | entries[count].mnt_type = strdup(m->mnt_type); |
| 38 | entries[count].mnt_opts = strdup(m->mnt_opts); |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 39 | entries[count].mnt_freq = m->mnt_freq; |
| 40 | entries[count].mnt_passno = m->mnt_passno; |
| 41 | count++; |
| 42 | } |
| 43 | endmntent(mountTable); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 44 | if ((mountTable = setmntent(mtab_file, "w"))) { |
| 45 | int i; |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 46 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 47 | for (i = 0; i < count; i++) { |
| 48 | int result = (strcmp(entries[i].mnt_fsname, name) == 0 |
| 49 | || strcmp(entries[i].mnt_dir, name) == 0); |
| 50 | |
| 51 | if (result) |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 52 | continue; |
| 53 | else |
| 54 | addmntent(mountTable, &entries[i]); |
| 55 | } |
| 56 | endmntent(mountTable); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 57 | } else if (errno != EROFS) |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 58 | perror(mtab_file); |
| 59 | } |
| 60 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 61 | void write_mtab(char *blockDevice, char *directory, |
| 62 | char *filesystemType, long flags, char *string_flags) |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 63 | { |
| 64 | FILE *mountTable = setmntent(mtab_file, "a+"); |
| 65 | struct mntent m; |
| 66 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 67 | if (mountTable == 0) { |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 68 | perror(mtab_file); |
| 69 | return; |
| 70 | } |
| 71 | if (mountTable) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 72 | int length = strlen(directory); |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 73 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 74 | if (length > 1 && directory[length - 1] == '/') |
| 75 | directory[length - 1] = '\0'; |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 76 | |
Eric Andersen | 0ecb54a | 1999-12-05 23:24:55 +0000 | [diff] [blame] | 77 | #ifdef BB_FEATURE_USE_PROCFS |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 78 | if (filesystemType == 0) { |
| 79 | struct mntent *p = findMountPoint(blockDevice, "/proc/mounts"); |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 80 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 81 | if (p && p->mnt_type) |
| 82 | filesystemType = p->mnt_type; |
| 83 | } |
Eric Andersen | 0ecb54a | 1999-12-05 23:24:55 +0000 | [diff] [blame] | 84 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 85 | m.mnt_fsname = blockDevice; |
| 86 | m.mnt_dir = directory; |
| 87 | m.mnt_type = filesystemType ? filesystemType : "default"; |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 88 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 89 | if (*string_flags) { |
| 90 | m.mnt_opts = string_flags; |
| 91 | } else { |
| 92 | if ((flags | MS_RDONLY) == flags) |
| 93 | m.mnt_opts = "ro"; |
| 94 | else |
| 95 | m.mnt_opts = "rw"; |
| 96 | } |
| 97 | |
| 98 | m.mnt_freq = 0; |
| 99 | m.mnt_passno = 0; |
| 100 | addmntent(mountTable, &m); |
| 101 | endmntent(mountTable); |
| 102 | } |
| 103 | } |