1 /* vi: set sw=4 ts=4: */
3 * Mini umount implementation for busybox
6 * Copyright (C) 1999,2000 by Lineo, inc.
7 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
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.
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.
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
32 #define MS_MGC_VAL 0xc0ed0000 /* Magic number indicatng "new" flags */
33 #define MS_REMOUNT 32 /* Alter flags of a mounted FS. */
34 #define MS_RDONLY 1 /* Mount read-only. */
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);
42 struct _mtab_entry_t {
45 struct _mtab_entry_t *next;
48 static struct _mtab_entry_t *mtab_cache = NULL;
52 #if defined BB_FEATURE_MOUNT_FORCE
53 static int doForce = FALSE;
55 #if defined BB_FEATURE_MOUNT_LOOP
56 static int freeLoop = TRUE;
58 static int useMtab = TRUE;
59 static int umountAll = FALSE;
60 static int doRemount = FALSE;
61 extern const char mtab_file[]; /* Defined in utility.c */
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
69 * TODO: Perhaps switch to using Glibc's getmntent_r
74 struct _mtab_entry_t *entry = NULL;
78 if (mtab_cache != NULL)
81 if ((fp = setmntent(mtab_file, "r")) == NULL) {
82 errorMsg("Cannot open %s\n", mtab_file);
85 while ((e = getmntent(fp))) {
86 entry = xmalloc(sizeof(struct _mtab_entry_t));
87 entry->device = strdup(e->mnt_fsname);
88 entry->mountpt = strdup(e->mnt_dir);
89 entry->next = mtab_cache;
95 char *mtab_getinfo(const char *match, const char which)
97 struct _mtab_entry_t *cur = mtab_cache;
100 if (strcmp(cur->mountpt, match) == 0 ||
101 strcmp(cur->device, match) == 0) {
102 if (which == MTAB_GETMOUNTPT) {
106 if (strcmp(cur->device, "/dev/root") == 0) {
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);
121 char *mtab_first(void **iter)
123 struct _mtab_entry_t *mtab_iter;
127 mtab_iter = mtab_cache;
128 *iter = (void *) mtab_iter;
129 return mtab_next(iter);
132 char *mtab_next(void **iter)
136 if (iter == NULL || *iter == NULL)
138 mp = ((struct _mtab_entry_t *) (*iter))->mountpt;
139 *iter = (void *) ((struct _mtab_entry_t *) (*iter))->next;
143 /* Don't bother to clean up, since exit() does that
144 * automagically, so we can save a few bytes */
145 #ifdef BB_FEATURE_CLEAN_UP
148 struct _mtab_entry_t *this, *next;
163 static int do_umount(const char *name, int useMtab)
166 char *blockDevice = mtab_getinfo(name, MTAB_GETDEVICE);
168 if (blockDevice && strcmp(blockDevice, name) == 0)
169 name = mtab_getinfo(blockDevice, MTAB_GETMOUNTPT);
171 status = umount(name);
173 #if defined BB_FEATURE_MOUNT_LOOP
174 if (freeLoop == TRUE && blockDevice != NULL && !strncmp("/dev/loop", blockDevice, 9))
175 /* this was a loop device, delete it */
176 del_loop(blockDevice);
178 #if defined BB_FEATURE_MOUNT_FORCE
179 if (status != 0 && doForce == TRUE) {
180 status = umount2(blockDevice, MNT_FORCE);
182 fatalError("forced umount of %s failed!\n", blockDevice);
186 if (status != 0 && doRemount == TRUE && errno == EBUSY) {
187 status = mount(blockDevice, name, NULL,
188 MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
190 errorMsg("%s busy - remounted read-only\n", blockDevice);
192 errorMsg("Cannot remount %s read-only\n", blockDevice);
205 static int umount_all(int useMtab)
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)
215 status = do_umount(mountpt, useMtab);
217 /* Don't bother retrying the umount on busy devices */
218 if (errno == EBUSY) {
222 status = do_umount(mountpt, useMtab);
224 printf("Couldn't umount %s on %s: %s\n",
225 mountpt, mtab_getinfo(mountpt, MTAB_GETDEVICE),
233 extern int umount_main(int argc, char **argv)
238 #ifdef BB_FEATURE_CLEAN_UP
242 /* Parse any options */
243 while (--argc > 0 && **(++argv) == '-') {
249 #if defined BB_FEATURE_MOUNT_LOOP
259 #ifdef BB_FEATURE_MOUNT_FORCE
268 break; /* ignore -v */
275 if (umountAll == TRUE) {
276 exit(umount_all(useMtab));
278 if (do_umount(*argv, useMtab) == 0)