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