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