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
27 #include <sys/mount.h>
32 static const char umount_usage[] =
33 "umount [flags] filesystem|directory\n"
34 #ifndef BB_FEATURE_TRIVIAL_HELP
35 "Unmount file systems\n"
36 "\nFlags:\n" "\t-a:\tUnmount all file systems"
38 " in /etc/mtab\n\t-n:\tDon't erase /etc/mtab entries\n"
42 "\t-r:\tTry to remount devices as read-only if mount is busy\n"
43 #if defined BB_FEATURE_MOUNT_FORCE
44 "\t-f:\tForce filesystem umount (i.e. unreachable NFS server)\n"
46 #if defined BB_FEATURE_MOUNT_LOOP
47 "\t-l:\tDo not free loop device (if a loop device has been used)\n"
52 struct _mtab_entry_t {
55 struct _mtab_entry_t *next;
58 static struct _mtab_entry_t *mtab_cache = NULL;
62 #if defined BB_FEATURE_MOUNT_FORCE
63 static int doForce = FALSE;
65 #if defined BB_FEATURE_MOUNT_LOOP
66 static int freeLoop = TRUE;
68 static int useMtab = TRUE;
69 static int umountAll = FALSE;
70 static int doRemount = FALSE;
71 extern const char mtab_file[]; /* Defined in utility.c */
75 /* These functions are here because the getmntent functions do not appear
76 * to be re-entrant, which leads to all sorts of problems when we try to
77 * use them recursively - randolph
79 * TODO: Perhaps switch to using Glibc's getmntent_r
84 struct _mtab_entry_t *entry = NULL;
88 if (mtab_cache != NULL)
91 if ((fp = setmntent(mtab_file, "r")) == NULL) {
92 fprintf(stderr, "Cannot open %s\n", mtab_file);
95 while ((e = getmntent(fp))) {
96 entry = xmalloc(sizeof(struct _mtab_entry_t));
97 entry->device = strdup(e->mnt_fsname);
98 entry->mountpt = strdup(e->mnt_dir);
99 entry->next = mtab_cache;
105 char *mtab_getinfo(const char *match, const char which)
107 struct _mtab_entry_t *cur = mtab_cache;
110 if (strcmp(cur->mountpt, match) == 0 ||
111 strcmp(cur->device, match) == 0) {
112 if (which == MTAB_GETMOUNTPT) {
116 if (strcmp(cur->device, "/dev/root") == 0) {
117 /* Adjusts device to be the real root device,
118 * or leaves device alone if it can't find it */
119 find_real_root_device_name( cur->device);
120 return ( cur->device);
131 char *mtab_first(void **iter)
133 struct _mtab_entry_t *mtab_iter;
137 mtab_iter = mtab_cache;
138 *iter = (void *) mtab_iter;
139 return mtab_next(iter);
142 char *mtab_next(void **iter)
146 if (iter == NULL || *iter == NULL)
148 mp = ((struct _mtab_entry_t *) (*iter))->mountpt;
149 *iter = (void *) ((struct _mtab_entry_t *) (*iter))->next;
153 /* Don't bother to clean up, since exit() does that
154 * automagically, so we can save a few bytes */
158 struct _mtab_entry_t *this, *next;
173 static int do_umount(const char *name, int useMtab)
176 char *blockDevice = mtab_getinfo(name, MTAB_GETDEVICE);
178 if (blockDevice && strcmp(blockDevice, name) == 0)
179 name = mtab_getinfo(blockDevice, MTAB_GETMOUNTPT);
181 status = umount(name);
183 #if defined BB_FEATURE_MOUNT_LOOP
184 if (freeLoop == TRUE && blockDevice != NULL && !strncmp("/dev/loop", blockDevice, 9))
185 /* this was a loop device, delete it */
186 del_loop(blockDevice);
188 #if defined BB_FEATURE_MOUNT_FORCE
189 if (status != 0 && doForce == TRUE) {
190 status = umount2(blockDevice, MNT_FORCE);
192 fatalError("umount: forced umount of %s failed!\n", blockDevice);
196 if (status != 0 && doRemount == TRUE && errno == EBUSY) {
197 status = mount(blockDevice, name, NULL,
198 MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
200 fprintf(stderr, "umount: %s busy - remounted read-only\n",
203 fprintf(stderr, "umount: Cannot remount %s read-only\n",
217 static int umount_all(int useMtab)
223 for (mountpt = mtab_first(&iter); mountpt; mountpt = mtab_next(&iter)) {
224 /* Never umount /proc on a umount -a */
225 if (strstr(mountpt, "proc")!= NULL)
227 status = do_umount(mountpt, useMtab);
229 /* Don't bother retrying the umount on busy devices */
230 if (errno == EBUSY) {
234 status = do_umount(mountpt, useMtab);
236 printf("Couldn't umount %s on %s: %s\n",
237 mountpt, mtab_getinfo(mountpt, MTAB_GETDEVICE),
245 extern int umount_main(int argc, char **argv)
251 /* Parse any options */
252 while (--argc > 0 && **(++argv) == '-') {
258 #if defined BB_FEATURE_MOUNT_LOOP
268 #ifdef BB_FEATURE_MOUNT_FORCE
277 break; /* ignore -v */
284 if (umountAll == TRUE) {
285 exit(umount_all(useMtab));
287 if (do_umount(*argv, useMtab) == 0)