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