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\n"
34 "Flags:\n" "\t-a:\tUnmount all file systems"
36 " in /etc/mtab\n\t-n:\tDon't erase /etc/mtab entries\n"
40 "\t-r:\tTry to remount devices as read-only if mount is busy\n"
41 #if defined BB_FEATURE_MOUNT_FORCE
42 "\t-f:\tForce filesystem umount (i.e. unreachable NFS server)\n"
44 #if defined BB_FEATURE_MOUNT_LOOP
45 "\t-l:\tDo not free loop device (if a loop device has been used)\n"
49 struct _mtab_entry_t {
52 struct _mtab_entry_t *next;
55 static struct _mtab_entry_t *mtab_cache = NULL;
59 #if defined BB_FEATURE_MOUNT_FORCE
60 static int doForce = FALSE;
62 #if defined BB_FEATURE_MOUNT_LOOP
63 static int freeLoop = TRUE;
65 static int useMtab = TRUE;
66 static int umountAll = FALSE;
67 static int doRemount = FALSE;
68 extern const char mtab_file[]; /* Defined in utility.c */
72 /* These functions are here because the getmntent functions do not appear
73 * to be re-entrant, which leads to all sorts of problems when we try to
74 * use them recursively - randolph
76 * TODO: Perhaps switch to using Glibc's getmntent_r
81 struct _mtab_entry_t *entry = NULL;
85 if (mtab_cache != NULL)
88 if ((fp = setmntent(mtab_file, "r")) == NULL) {
89 fprintf(stderr, "Cannot open %s\n", mtab_file);
92 while ((e = getmntent(fp))) {
93 entry = xmalloc(sizeof(struct _mtab_entry_t));
94 entry->device = strdup(e->mnt_fsname);
95 entry->mountpt = strdup(e->mnt_dir);
96 entry->next = mtab_cache;
102 char *mtab_getinfo(const char *match, const char which)
104 struct _mtab_entry_t *cur = mtab_cache;
107 if (strcmp(cur->mountpt, match) == 0 ||
108 strcmp(cur->device, match) == 0) {
109 if (which == MTAB_GETMOUNTPT) {
113 if (strcmp(cur->device, "/dev/root") == 0) {
114 /* Adjusts device to be the real root device,
115 * or leaves device alone if it can't find it */
116 find_real_root_device_name( cur->device);
117 return ( cur->device);
128 char *mtab_first(void **iter)
130 struct _mtab_entry_t *mtab_iter;
134 mtab_iter = mtab_cache;
135 *iter = (void *) mtab_iter;
136 return mtab_next(iter);
139 char *mtab_next(void **iter)
143 if (iter == NULL || *iter == NULL)
145 mp = ((struct _mtab_entry_t *) (*iter))->mountpt;
146 *iter = (void *) ((struct _mtab_entry_t *) (*iter))->next;
150 /* Don't bother to clean up, since exit() does that
151 * automagically, so we can save a few bytes */
155 struct _mtab_entry_t *this, *next;
170 static int do_umount(const char *name, int useMtab)
173 char *blockDevice = mtab_getinfo(name, MTAB_GETDEVICE);
175 if (blockDevice && strcmp(blockDevice, name) == 0)
176 name = mtab_getinfo(blockDevice, MTAB_GETMOUNTPT);
178 status = umount(name);
180 #if defined BB_FEATURE_MOUNT_LOOP
181 if (freeLoop == TRUE && blockDevice != NULL && !strncmp("/dev/loop", blockDevice, 9))
182 /* this was a loop device, delete it */
183 del_loop(blockDevice);
185 #if defined BB_FEATURE_MOUNT_FORCE
186 if (status != 0 && doForce == TRUE) {
187 status = umount2(blockDevice, MNT_FORCE);
189 fatalError("umount: forced umount of %s failed!\n", blockDevice);
193 if (status != 0 && doRemount == TRUE && errno == EBUSY) {
194 status = mount(blockDevice, name, NULL,
195 MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
197 fprintf(stderr, "umount: %s busy - remounted read-only\n",
200 fprintf(stderr, "umount: Cannot remount %s read-only\n",
214 static int umount_all(int useMtab)
220 for (mountpt = mtab_first(&iter); mountpt; mountpt = mtab_next(&iter)) {
221 /* Never umount /proc on a umount -a */
222 if (strstr(mountpt, "proc")!= NULL)
224 status = do_umount(mountpt, useMtab);
226 /* Don't bother retrying the umount on busy devices */
227 if (errno == EBUSY) {
231 status = do_umount(mountpt, useMtab);
233 printf("Couldn't umount %s on %s: %s\n",
234 mountpt, mtab_getinfo(mountpt, MTAB_GETDEVICE),
242 extern int umount_main(int argc, char **argv)
248 /* Parse any options */
249 while (--argc > 0 && **(++argv) == '-') {
255 #if defined BB_FEATURE_MOUNT_LOOP
265 #ifdef BB_FEATURE_MOUNT_FORCE
274 break; /* ignore -v */
281 if (umountAll == TRUE) {
282 exit(umount_all(useMtab));
284 if (do_umount(*argv, useMtab) == 0)