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