Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini umount implementation for busybox |
| 4 | * |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 5 | * |
| 6 | * Copyright (C) 1999 by Lineo, inc. |
| 7 | * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org> |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 | * |
| 23 | */ |
| 24 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 25 | #include "internal.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 26 | #include <stdio.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 27 | #include <sys/mount.h> |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 28 | #include <mntent.h> |
| 29 | #include <fstab.h> |
| 30 | #include <errno.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 31 | |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 32 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 33 | static const char umount_usage[] = |
| 34 | "umount [flags] filesystem|directory\n\n" |
| 35 | "Flags:\n" "\t-a:\tUnmount all file systems" |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 36 | #ifdef BB_MTAB |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 37 | " in /etc/mtab\n\t-n:\tDon't erase /etc/mtab entries\n" |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 38 | #else |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 39 | "\n" |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 40 | #endif |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 41 | #ifdef BB_FEATURE_REMOUNT |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 42 | "\t-r:\tTry to remount devices as read-only if mount is busy\n" |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 43 | #endif |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 44 | ; |
| 45 | |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 46 | struct _mtab_entry_t { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 47 | char *device; |
| 48 | char *mountpt; |
| 49 | struct _mtab_entry_t *next; |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | static struct _mtab_entry_t *mtab_cache = NULL; |
| 53 | |
| 54 | |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 55 | |
| 56 | static int useMtab = TRUE; |
| 57 | static int umountAll = FALSE; |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 58 | static int doRemount = FALSE; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 59 | extern const char mtab_file[]; /* Defined in utility.c */ |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 60 | |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 61 | |
Erik Andersen | 5b911dd | 2000-02-23 22:49:58 +0000 | [diff] [blame] | 62 | |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 63 | /* These functions are here because the getmntent functions do not appear |
| 64 | * to be re-entrant, which leads to all sorts of problems when we try to |
| 65 | * use them recursively - randolph |
Erik Andersen | 5b911dd | 2000-02-23 22:49:58 +0000 | [diff] [blame] | 66 | * |
| 67 | * TODO: Perhaps switch to using Glibc's getmntent_r |
| 68 | * -Erik |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 69 | */ |
| 70 | void mtab_read(void) |
| 71 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 72 | struct _mtab_entry_t *entry = NULL; |
| 73 | struct mntent *e; |
| 74 | FILE *fp; |
| 75 | |
| 76 | if (mtab_cache != NULL) |
| 77 | return; |
| 78 | |
| 79 | if ((fp = setmntent(mtab_file, "r")) == NULL) { |
| 80 | fprintf(stderr, "Cannot open %s\n", mtab_file); |
| 81 | return; |
| 82 | } |
| 83 | while ((e = getmntent(fp))) { |
| 84 | entry = malloc(sizeof(struct _mtab_entry_t)); |
| 85 | |
| 86 | entry->device = strdup(e->mnt_fsname); |
| 87 | entry->mountpt = strdup(e->mnt_dir); |
| 88 | entry->next = mtab_cache; |
| 89 | mtab_cache = entry; |
| 90 | } |
| 91 | endmntent(fp); |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | char *mtab_getinfo(const char *match, const char which) |
| 95 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 96 | struct _mtab_entry_t *cur = mtab_cache; |
| 97 | |
| 98 | while (cur) { |
| 99 | if (strcmp(cur->mountpt, match) == 0 || |
| 100 | strcmp(cur->device, match) == 0) { |
| 101 | if (which == MTAB_GETMOUNTPT) { |
| 102 | return cur->mountpt; |
| 103 | } else { |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 104 | #if !defined BB_MTAB |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 105 | if (strcmp(cur->device, "/dev/root") == 0) { |
| 106 | struct fstab *fstabItem; |
| 107 | |
| 108 | fstabItem = getfsfile("/"); |
| 109 | if (fstabItem != NULL) |
| 110 | return fstabItem->fs_spec; |
| 111 | } |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 112 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 113 | return cur->device; |
| 114 | } |
| 115 | } |
| 116 | cur = cur->next; |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 117 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 118 | return NULL; |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | char *mtab_first(void **iter) |
| 122 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 123 | struct _mtab_entry_t *mtab_iter; |
| 124 | |
| 125 | if (!iter) |
| 126 | return NULL; |
| 127 | mtab_iter = mtab_cache; |
| 128 | *iter = (void *) mtab_iter; |
| 129 | return mtab_next(iter); |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | char *mtab_next(void **iter) |
| 133 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 134 | char *mp; |
| 135 | |
| 136 | if (iter == NULL || *iter == NULL) |
| 137 | return NULL; |
| 138 | mp = ((struct _mtab_entry_t *) (*iter))->mountpt; |
| 139 | *iter = (void *) ((struct _mtab_entry_t *) (*iter))->next; |
| 140 | return mp; |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void mtab_free(void) |
| 144 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 145 | struct _mtab_entry_t *this, *next; |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 146 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 147 | this = mtab_cache; |
| 148 | while (this) { |
| 149 | next = this->next; |
| 150 | if (this->device) |
| 151 | free(this->device); |
| 152 | if (this->mountpt) |
| 153 | free(this->mountpt); |
| 154 | free(this); |
| 155 | this = next; |
| 156 | } |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 157 | } |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 158 | |
| 159 | static int do_umount(const char *name, int useMtab) |
| 160 | { |
| 161 | int status; |
| 162 | char *blockDevice = mtab_getinfo(name, MTAB_GETDEVICE); |
| 163 | |
| 164 | if (blockDevice && strcmp(blockDevice, name) == 0) |
| 165 | name = mtab_getinfo(blockDevice, MTAB_GETMOUNTPT); |
| 166 | |
| 167 | status = umount(name); |
| 168 | |
| 169 | #if defined BB_FEATURE_MOUNT_LOOP |
| 170 | if (blockDevice != NULL && !strncmp("/dev/loop", blockDevice, 9)) |
| 171 | /* this was a loop device, delete it */ |
| 172 | del_loop(blockDevice); |
| 173 | #endif |
| 174 | #if defined BB_FEATURE_REMOUNT |
| 175 | if (status != 0 && doRemount == TRUE && errno == EBUSY) { |
| 176 | status = mount(blockDevice, name, NULL, |
| 177 | MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL); |
| 178 | if (status == 0) { |
| 179 | fprintf(stderr, "umount: %s busy - remounted read-only\n", |
| 180 | blockDevice); |
| 181 | /* TODO: update mtab if BB_MTAB is defined */ |
| 182 | } else { |
| 183 | fprintf(stderr, "umount: Cannot remount %s read-only\n", |
| 184 | blockDevice); |
| 185 | } |
| 186 | } |
| 187 | #endif |
| 188 | if (status == 0) { |
| 189 | #if defined BB_MTAB |
| 190 | if (useMtab == TRUE) |
| 191 | erase_mtab(name); |
| 192 | #endif |
| 193 | return (TRUE); |
| 194 | } |
| 195 | return (FALSE); |
| 196 | } |
| 197 | |
| 198 | static int umount_all(int useMtab) |
| 199 | { |
| 200 | int status = TRUE; |
| 201 | char *mountpt; |
| 202 | void *iter; |
| 203 | |
| 204 | for (mountpt = mtab_first(&iter); mountpt; mountpt = mtab_next(&iter)) { |
| 205 | /* Never umount /proc on a umount -a */ |
| 206 | if (strstr(mountpt, "proc")!= NULL) |
| 207 | continue; |
| 208 | status = do_umount(mountpt, useMtab); |
| 209 | if (status != 0) { |
| 210 | /* Don't bother retrying the umount on busy devices */ |
| 211 | if (errno == EBUSY) { |
| 212 | perror(mountpt); |
| 213 | continue; |
| 214 | } |
| 215 | status = do_umount(mountpt, useMtab); |
| 216 | if (status != 0) { |
| 217 | printf("Couldn't umount %s on %s: %s\n", |
| 218 | mountpt, mtab_getinfo(mountpt, MTAB_GETDEVICE), |
| 219 | strerror(errno)); |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | return (status); |
| 224 | } |
| 225 | |
| 226 | extern int umount_main(int argc, char **argv) |
| 227 | { |
| 228 | if (argc < 2) { |
| 229 | usage(umount_usage); |
| 230 | } |
| 231 | |
| 232 | /* Parse any options */ |
| 233 | while (--argc > 0 && **(++argv) == '-') { |
| 234 | while (*++(*argv)) |
| 235 | switch (**argv) { |
| 236 | case 'a': |
| 237 | umountAll = TRUE; |
| 238 | break; |
| 239 | #ifdef BB_MTAB |
| 240 | case 'n': |
| 241 | useMtab = FALSE; |
| 242 | break; |
| 243 | #endif |
| 244 | #ifdef BB_FEATURE_REMOUNT |
| 245 | case 'r': |
| 246 | doRemount = TRUE; |
| 247 | break; |
| 248 | #endif |
| 249 | default: |
| 250 | usage(umount_usage); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | mtab_read(); |
| 255 | if (umountAll == TRUE) { |
| 256 | exit(umount_all(useMtab)); |
| 257 | } |
| 258 | if (do_umount(*argv, useMtab) == 0) |
| 259 | exit(TRUE); |
| 260 | else { |
| 261 | perror("umount"); |
| 262 | exit(FALSE); |
| 263 | } |
| 264 | } |
| 265 | |