1 /* vi: set sw=4 ts=4: */
3 * Mini umount implementation for busybox
6 * Copyright (C) 1999 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>
33 static const char umount_usage[] =
34 "umount [flags] filesystem|directory\n\n"
35 "Flags:\n" "\t-a:\tUnmount all file systems"
37 " in /etc/mtab\n\t-n:\tDon't erase /etc/mtab entries\n"
41 #ifdef BB_FEATURE_REMOUNT
42 "\t-r:\tTry to remount devices as read-only if mount is busy\n"
44 #if defined BB_FEATURE_MOUNT_LOOP
45 "\t-f:\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_LOOP
60 static int freeLoop = TRUE;
62 static int useMtab = TRUE;
63 static int umountAll = FALSE;
64 #if defined BB_FEATURE_REMOUNT
65 static int doRemount = FALSE;
67 extern const char mtab_file[]; /* Defined in utility.c */
71 /* These functions are here because the getmntent functions do not appear
72 * to be re-entrant, which leads to all sorts of problems when we try to
73 * use them recursively - randolph
75 * TODO: Perhaps switch to using Glibc's getmntent_r
80 struct _mtab_entry_t *entry = NULL;
84 if (mtab_cache != NULL)
87 if ((fp = setmntent(mtab_file, "r")) == NULL) {
88 fprintf(stderr, "Cannot open %s\n", mtab_file);
91 while ((e = getmntent(fp))) {
92 entry = malloc(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 struct fstab *fstabItem;
116 fstabItem = getfsfile("/");
117 if (fstabItem != NULL)
118 return fstabItem->fs_spec;
129 char *mtab_first(void **iter)
131 struct _mtab_entry_t *mtab_iter;
135 mtab_iter = mtab_cache;
136 *iter = (void *) mtab_iter;
137 return mtab_next(iter);
140 char *mtab_next(void **iter)
144 if (iter == NULL || *iter == NULL)
146 mp = ((struct _mtab_entry_t *) (*iter))->mountpt;
147 *iter = (void *) ((struct _mtab_entry_t *) (*iter))->next;
153 struct _mtab_entry_t *this, *next;
167 static int do_umount(const char *name, int useMtab)
170 char *blockDevice = mtab_getinfo(name, MTAB_GETDEVICE);
172 if (blockDevice && strcmp(blockDevice, name) == 0)
173 name = mtab_getinfo(blockDevice, MTAB_GETMOUNTPT);
175 status = umount(name);
177 #if defined BB_FEATURE_MOUNT_LOOP
178 if (freeLoop == TRUE && blockDevice != NULL && !strncmp("/dev/loop", blockDevice, 9))
179 /* this was a loop device, delete it */
180 del_loop(blockDevice);
182 #if defined BB_FEATURE_REMOUNT
183 if (status != 0 && doRemount == TRUE && errno == EBUSY) {
184 status = mount(blockDevice, name, NULL,
185 MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
187 fprintf(stderr, "umount: %s busy - remounted read-only\n",
189 /* TODO: update mtab if BB_MTAB is defined */
191 fprintf(stderr, "umount: Cannot remount %s read-only\n",
206 static int umount_all(int useMtab)
212 for (mountpt = mtab_first(&iter); mountpt; mountpt = mtab_next(&iter)) {
213 /* Never umount /proc on a umount -a */
214 if (strstr(mountpt, "proc")!= NULL)
216 status = do_umount(mountpt, useMtab);
218 /* Don't bother retrying the umount on busy devices */
219 if (errno == EBUSY) {
223 status = do_umount(mountpt, useMtab);
225 printf("Couldn't umount %s on %s: %s\n",
226 mountpt, mtab_getinfo(mountpt, MTAB_GETDEVICE),
234 extern int umount_main(int argc, char **argv)
240 /* Parse any options */
241 while (--argc > 0 && **(++argv) == '-') {
247 #if defined BB_FEATURE_MOUNT_LOOP
257 #ifdef BB_FEATURE_REMOUNT
268 if (umountAll == TRUE) {
269 exit(umount_all(useMtab));
271 if (do_umount(*argv, useMtab) == 0)