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
29 #include <linux/unistd.h>
32 //#include <sys/mount.h>
33 /* Include our own version of sys/mount.h, since libc5 doesn't
34 * know about umount2 */
35 static _syscall1(int, umount, const char *, special_file);
36 static _syscall2(int, umount2, const char *, special_file, int, flags);
37 static _syscall5(int, mount, const char *, special_file, const char *, dir,
38 const char *, fstype, unsigned long int, rwflag, const void *, data);
40 #define MS_MGC_VAL 0xc0ed0000 /* Magic flag number to indicate "new" flags */
41 #define MS_REMOUNT 32 /* Alter flags of a mounted FS. */
42 #define MS_RDONLY 1 /* Mount read-only. */
45 static const char umount_usage[] =
46 "umount [flags] filesystem|directory\n"
47 #ifndef BB_FEATURE_TRIVIAL_HELP
48 "Unmount file systems\n"
49 "\nFlags:\n" "\t-a:\tUnmount all file systems"
51 " in /etc/mtab\n\t-n:\tDon't erase /etc/mtab entries\n"
55 "\t-r:\tTry to remount devices as read-only if mount is busy\n"
56 #if defined BB_FEATURE_MOUNT_FORCE
57 "\t-f:\tForce filesystem umount (i.e. unreachable NFS server)\n"
59 #if defined BB_FEATURE_MOUNT_LOOP
60 "\t-l:\tDo not free loop device (if a loop device has been used)\n"
65 struct _mtab_entry_t {
68 struct _mtab_entry_t *next;
71 static struct _mtab_entry_t *mtab_cache = NULL;
75 #if defined BB_FEATURE_MOUNT_FORCE
76 static int doForce = FALSE;
78 #if defined BB_FEATURE_MOUNT_LOOP
79 static int freeLoop = TRUE;
81 static int useMtab = TRUE;
82 static int umountAll = FALSE;
83 static int doRemount = FALSE;
84 extern const char mtab_file[]; /* Defined in utility.c */
88 /* These functions are here because the getmntent functions do not appear
89 * to be re-entrant, which leads to all sorts of problems when we try to
90 * use them recursively - randolph
92 * TODO: Perhaps switch to using Glibc's getmntent_r
97 struct _mtab_entry_t *entry = NULL;
101 if (mtab_cache != NULL)
104 if ((fp = setmntent(mtab_file, "r")) == NULL) {
105 fprintf(stderr, "Cannot open %s\n", mtab_file);
108 while ((e = getmntent(fp))) {
109 entry = xmalloc(sizeof(struct _mtab_entry_t));
110 entry->device = strdup(e->mnt_fsname);
111 entry->mountpt = strdup(e->mnt_dir);
112 entry->next = mtab_cache;
118 char *mtab_getinfo(const char *match, const char which)
120 struct _mtab_entry_t *cur = mtab_cache;
123 if (strcmp(cur->mountpt, match) == 0 ||
124 strcmp(cur->device, match) == 0) {
125 if (which == MTAB_GETMOUNTPT) {
129 if (strcmp(cur->device, "/dev/root") == 0) {
130 /* Adjusts device to be the real root device,
131 * or leaves device alone if it can't find it */
132 find_real_root_device_name( cur->device);
133 return ( cur->device);
144 char *mtab_first(void **iter)
146 struct _mtab_entry_t *mtab_iter;
150 mtab_iter = mtab_cache;
151 *iter = (void *) mtab_iter;
152 return mtab_next(iter);
155 char *mtab_next(void **iter)
159 if (iter == NULL || *iter == NULL)
161 mp = ((struct _mtab_entry_t *) (*iter))->mountpt;
162 *iter = (void *) ((struct _mtab_entry_t *) (*iter))->next;
166 /* Don't bother to clean up, since exit() does that
167 * automagically, so we can save a few bytes */
171 struct _mtab_entry_t *this, *next;
186 static int do_umount(const char *name, int useMtab)
189 char *blockDevice = mtab_getinfo(name, MTAB_GETDEVICE);
191 if (blockDevice && strcmp(blockDevice, name) == 0)
192 name = mtab_getinfo(blockDevice, MTAB_GETMOUNTPT);
194 status = umount(name);
196 #if defined BB_FEATURE_MOUNT_LOOP
197 if (freeLoop == TRUE && blockDevice != NULL && !strncmp("/dev/loop", blockDevice, 9))
198 /* this was a loop device, delete it */
199 del_loop(blockDevice);
201 #if defined BB_FEATURE_MOUNT_FORCE
202 if (status != 0 && doForce == TRUE) {
203 status = umount2(blockDevice, MNT_FORCE);
205 fatalError("umount: forced umount of %s failed!\n", blockDevice);
209 if (status != 0 && doRemount == TRUE && errno == EBUSY) {
210 status = mount(blockDevice, name, NULL,
211 MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
213 fprintf(stderr, "umount: %s busy - remounted read-only\n",
216 fprintf(stderr, "umount: Cannot remount %s read-only\n",
230 static int umount_all(int useMtab)
236 for (mountpt = mtab_first(&iter); mountpt; mountpt = mtab_next(&iter)) {
237 /* Never umount /proc on a umount -a */
238 if (strstr(mountpt, "proc")!= NULL)
240 status = do_umount(mountpt, useMtab);
242 /* Don't bother retrying the umount on busy devices */
243 if (errno == EBUSY) {
247 status = do_umount(mountpt, useMtab);
249 printf("Couldn't umount %s on %s: %s\n",
250 mountpt, mtab_getinfo(mountpt, MTAB_GETDEVICE),
258 extern int umount_main(int argc, char **argv)
264 /* Parse any options */
265 while (--argc > 0 && **(++argv) == '-') {
271 #if defined BB_FEATURE_MOUNT_LOOP
281 #ifdef BB_FEATURE_MOUNT_FORCE
290 break; /* ignore -v */
297 if (umountAll == TRUE) {
298 exit(umount_all(useMtab));
300 if (do_umount(*argv, useMtab) == 0)