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