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 | * |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 6 | * Copyright (C) 1999,2000 by Lineo, inc. |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 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 | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 27 | #include <mntent.h> |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 28 | #include <errno.h> |
Eric Andersen | e1e23ee | 2000-06-19 18:38:51 +0000 | [diff] [blame] | 29 | #include <linux/unistd.h> |
| 30 | |
| 31 | |
Eric Andersen | bd4a75f | 2000-06-19 20:03:48 +0000 | [diff] [blame] | 32 | //#include <sys/mount.h> |
Eric Andersen | e1e23ee | 2000-06-19 18:38:51 +0000 | [diff] [blame] | 33 | /* Include our own version of sys/mount.h, since libc5 doesn't |
| 34 | * know about umount2 */ |
| 35 | static _syscall1(int, umount, const char *, special_file); |
| 36 | static _syscall2(int, umount2, const char *, special_file, int, flags); |
| 37 | static _syscall5(int, mount, const char *, special_file, const char *, dir, |
| 38 | const char *, fstype, unsigned long int, rwflag, const void *, data); |
| 39 | #define MNT_FORCE 1 |
| 40 | #define MS_MGC_VAL 0xc0ed0000 /* Magic flag number to indicate "new" flags */ |
| 41 | #define MS_REMOUNT 32 /* Alter flags of a mounted FS. */ |
| 42 | #define MS_RDONLY 1 /* Mount read-only. */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 43 | |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 44 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 45 | static const char umount_usage[] = |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 46 | "umount [flags] filesystem|directory\n" |
| 47 | #ifndef BB_FEATURE_TRIVIAL_HELP |
| 48 | "Unmount file systems\n" |
| 49 | "\nFlags:\n" "\t-a:\tUnmount all file systems" |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 50 | #ifdef BB_MTAB |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 51 | " 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] | 52 | #else |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 53 | "\n" |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 54 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 55 | "\t-r:\tTry to remount devices as read-only if mount is busy\n" |
Erik Andersen | 6c5f2c6 | 2000-05-05 19:49:33 +0000 | [diff] [blame] | 56 | #if defined BB_FEATURE_MOUNT_FORCE |
| 57 | "\t-f:\tForce filesystem umount (i.e. unreachable NFS server)\n" |
| 58 | #endif |
Erik Andersen | ce91732 | 2000-03-13 04:07:02 +0000 | [diff] [blame] | 59 | #if defined BB_FEATURE_MOUNT_LOOP |
Erik Andersen | 6c5f2c6 | 2000-05-05 19:49:33 +0000 | [diff] [blame] | 60 | "\t-l:\tDo not free loop device (if a loop device has been used)\n" |
Erik Andersen | ce91732 | 2000-03-13 04:07:02 +0000 | [diff] [blame] | 61 | #endif |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 62 | #endif |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 63 | ; |
| 64 | |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 65 | struct _mtab_entry_t { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 66 | char *device; |
| 67 | char *mountpt; |
| 68 | struct _mtab_entry_t *next; |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | static struct _mtab_entry_t *mtab_cache = NULL; |
| 72 | |
| 73 | |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 74 | |
Erik Andersen | 6c5f2c6 | 2000-05-05 19:49:33 +0000 | [diff] [blame] | 75 | #if defined BB_FEATURE_MOUNT_FORCE |
| 76 | static int doForce = FALSE; |
| 77 | #endif |
Erik Andersen | ce91732 | 2000-03-13 04:07:02 +0000 | [diff] [blame] | 78 | #if defined BB_FEATURE_MOUNT_LOOP |
| 79 | static int freeLoop = TRUE; |
| 80 | #endif |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 81 | static int useMtab = TRUE; |
| 82 | static int umountAll = FALSE; |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 83 | static int doRemount = FALSE; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 84 | extern const char mtab_file[]; /* Defined in utility.c */ |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 85 | |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 86 | |
Erik Andersen | 5b911dd | 2000-02-23 22:49:58 +0000 | [diff] [blame] | 87 | |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 88 | /* These functions are here because the getmntent functions do not appear |
| 89 | * to be re-entrant, which leads to all sorts of problems when we try to |
| 90 | * use them recursively - randolph |
Erik Andersen | 5b911dd | 2000-02-23 22:49:58 +0000 | [diff] [blame] | 91 | * |
| 92 | * TODO: Perhaps switch to using Glibc's getmntent_r |
| 93 | * -Erik |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 94 | */ |
| 95 | void mtab_read(void) |
| 96 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 97 | struct _mtab_entry_t *entry = NULL; |
| 98 | struct mntent *e; |
| 99 | FILE *fp; |
| 100 | |
| 101 | if (mtab_cache != NULL) |
| 102 | return; |
| 103 | |
| 104 | if ((fp = setmntent(mtab_file, "r")) == NULL) { |
| 105 | fprintf(stderr, "Cannot open %s\n", mtab_file); |
| 106 | return; |
| 107 | } |
| 108 | while ((e = getmntent(fp))) { |
Erik Andersen | 0d068a2 | 2000-03-21 22:32:57 +0000 | [diff] [blame] | 109 | entry = xmalloc(sizeof(struct _mtab_entry_t)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 110 | entry->device = strdup(e->mnt_fsname); |
| 111 | entry->mountpt = strdup(e->mnt_dir); |
| 112 | entry->next = mtab_cache; |
| 113 | mtab_cache = entry; |
| 114 | } |
| 115 | endmntent(fp); |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | char *mtab_getinfo(const char *match, const char which) |
| 119 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 120 | struct _mtab_entry_t *cur = mtab_cache; |
| 121 | |
| 122 | while (cur) { |
| 123 | if (strcmp(cur->mountpt, match) == 0 || |
| 124 | strcmp(cur->device, match) == 0) { |
| 125 | if (which == MTAB_GETMOUNTPT) { |
| 126 | return cur->mountpt; |
| 127 | } else { |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 128 | #if !defined BB_MTAB |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 129 | if (strcmp(cur->device, "/dev/root") == 0) { |
Erik Andersen | ec5bd90 | 2000-03-22 07:12:05 +0000 | [diff] [blame] | 130 | /* Adjusts device to be the real root device, |
| 131 | * or leaves device alone if it can't find it */ |
| 132 | find_real_root_device_name( cur->device); |
| 133 | return ( cur->device); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 134 | } |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 135 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 136 | return cur->device; |
| 137 | } |
| 138 | } |
| 139 | cur = cur->next; |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 140 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 141 | return NULL; |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | char *mtab_first(void **iter) |
| 145 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 146 | struct _mtab_entry_t *mtab_iter; |
| 147 | |
| 148 | if (!iter) |
| 149 | return NULL; |
| 150 | mtab_iter = mtab_cache; |
| 151 | *iter = (void *) mtab_iter; |
| 152 | return mtab_next(iter); |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | char *mtab_next(void **iter) |
| 156 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 157 | char *mp; |
| 158 | |
| 159 | if (iter == NULL || *iter == NULL) |
| 160 | return NULL; |
| 161 | mp = ((struct _mtab_entry_t *) (*iter))->mountpt; |
| 162 | *iter = (void *) ((struct _mtab_entry_t *) (*iter))->next; |
| 163 | return mp; |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 166 | /* Don't bother to clean up, since exit() does that |
| 167 | * automagically, so we can save a few bytes */ |
| 168 | #if 0 |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 169 | void mtab_free(void) |
| 170 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 171 | struct _mtab_entry_t *this, *next; |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 172 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 173 | this = mtab_cache; |
| 174 | while (this) { |
| 175 | next = this->next; |
| 176 | if (this->device) |
| 177 | free(this->device); |
| 178 | if (this->mountpt) |
| 179 | free(this->mountpt); |
| 180 | free(this); |
| 181 | this = next; |
| 182 | } |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 183 | } |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 184 | #endif |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 185 | |
| 186 | static int do_umount(const char *name, int useMtab) |
| 187 | { |
| 188 | int status; |
| 189 | char *blockDevice = mtab_getinfo(name, MTAB_GETDEVICE); |
| 190 | |
| 191 | if (blockDevice && strcmp(blockDevice, name) == 0) |
| 192 | name = mtab_getinfo(blockDevice, MTAB_GETMOUNTPT); |
| 193 | |
| 194 | status = umount(name); |
| 195 | |
| 196 | #if defined BB_FEATURE_MOUNT_LOOP |
Erik Andersen | ce91732 | 2000-03-13 04:07:02 +0000 | [diff] [blame] | 197 | if (freeLoop == TRUE && blockDevice != NULL && !strncmp("/dev/loop", blockDevice, 9)) |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 198 | /* this was a loop device, delete it */ |
| 199 | del_loop(blockDevice); |
| 200 | #endif |
Erik Andersen | 6c5f2c6 | 2000-05-05 19:49:33 +0000 | [diff] [blame] | 201 | #if defined BB_FEATURE_MOUNT_FORCE |
| 202 | if (status != 0 && doForce == TRUE) { |
| 203 | status = umount2(blockDevice, MNT_FORCE); |
| 204 | if (status != 0) { |
| 205 | fatalError("umount: forced umount of %s failed!\n", blockDevice); |
| 206 | } |
| 207 | } |
| 208 | #endif |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 209 | if (status != 0 && doRemount == TRUE && errno == EBUSY) { |
| 210 | status = mount(blockDevice, name, NULL, |
| 211 | MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL); |
| 212 | if (status == 0) { |
| 213 | fprintf(stderr, "umount: %s busy - remounted read-only\n", |
| 214 | blockDevice); |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 215 | } else { |
| 216 | fprintf(stderr, "umount: Cannot remount %s read-only\n", |
| 217 | blockDevice); |
| 218 | } |
| 219 | } |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 220 | if (status == 0) { |
| 221 | #if defined BB_MTAB |
| 222 | if (useMtab == TRUE) |
| 223 | erase_mtab(name); |
| 224 | #endif |
| 225 | return (TRUE); |
| 226 | } |
| 227 | return (FALSE); |
| 228 | } |
| 229 | |
| 230 | static int umount_all(int useMtab) |
| 231 | { |
| 232 | int status = TRUE; |
| 233 | char *mountpt; |
| 234 | void *iter; |
| 235 | |
| 236 | for (mountpt = mtab_first(&iter); mountpt; mountpt = mtab_next(&iter)) { |
| 237 | /* Never umount /proc on a umount -a */ |
| 238 | if (strstr(mountpt, "proc")!= NULL) |
| 239 | continue; |
| 240 | status = do_umount(mountpt, useMtab); |
| 241 | if (status != 0) { |
| 242 | /* Don't bother retrying the umount on busy devices */ |
| 243 | if (errno == EBUSY) { |
| 244 | perror(mountpt); |
| 245 | continue; |
| 246 | } |
| 247 | status = do_umount(mountpt, useMtab); |
| 248 | if (status != 0) { |
| 249 | printf("Couldn't umount %s on %s: %s\n", |
| 250 | mountpt, mtab_getinfo(mountpt, MTAB_GETDEVICE), |
| 251 | strerror(errno)); |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | return (status); |
| 256 | } |
| 257 | |
| 258 | extern int umount_main(int argc, char **argv) |
| 259 | { |
| 260 | if (argc < 2) { |
| 261 | usage(umount_usage); |
| 262 | } |
| 263 | |
| 264 | /* Parse any options */ |
| 265 | while (--argc > 0 && **(++argv) == '-') { |
| 266 | while (*++(*argv)) |
| 267 | switch (**argv) { |
| 268 | case 'a': |
| 269 | umountAll = TRUE; |
| 270 | break; |
Erik Andersen | ce91732 | 2000-03-13 04:07:02 +0000 | [diff] [blame] | 271 | #if defined BB_FEATURE_MOUNT_LOOP |
Erik Andersen | 6c5f2c6 | 2000-05-05 19:49:33 +0000 | [diff] [blame] | 272 | case 'l': |
Erik Andersen | ce91732 | 2000-03-13 04:07:02 +0000 | [diff] [blame] | 273 | freeLoop = FALSE; |
| 274 | break; |
| 275 | #endif |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 276 | #ifdef BB_MTAB |
| 277 | case 'n': |
| 278 | useMtab = FALSE; |
| 279 | break; |
| 280 | #endif |
Erik Andersen | 6c5f2c6 | 2000-05-05 19:49:33 +0000 | [diff] [blame] | 281 | #ifdef BB_FEATURE_MOUNT_FORCE |
| 282 | case 'f': |
| 283 | doForce = TRUE; |
| 284 | break; |
| 285 | #endif |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 286 | case 'r': |
| 287 | doRemount = TRUE; |
| 288 | break; |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 289 | case 'v': |
| 290 | break; /* ignore -v */ |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 291 | default: |
| 292 | usage(umount_usage); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | mtab_read(); |
| 297 | if (umountAll == TRUE) { |
| 298 | exit(umount_all(useMtab)); |
| 299 | } |
| 300 | if (do_umount(*argv, useMtab) == 0) |
| 301 | exit(TRUE); |
Eric Andersen | e1e23ee | 2000-06-19 18:38:51 +0000 | [diff] [blame] | 302 | perror("umount"); |
| 303 | return(FALSE); |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 304 | } |
| 305 | |