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