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