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 = xmalloc(sizeof(struct _mtab_entry_t));
93 entry->device = strdup(e->mnt_fsname);
94 entry->mountpt = strdup(e->mnt_dir);
95 entry->next = mtab_cache;
101 char *mtab_getinfo(const char *match, const char which)
103 struct _mtab_entry_t *cur = mtab_cache;
106 if (strcmp(cur->mountpt, match) == 0 ||
107 strcmp(cur->device, match) == 0) {
108 if (which == MTAB_GETMOUNTPT) {
112 if (strcmp(cur->device, "/dev/root") == 0) {
113 struct fstab *fstabItem;
115 fstabItem = getfsfile("/");
116 if (fstabItem != NULL)
117 return fstabItem->fs_spec;
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;
152 struct _mtab_entry_t *this, *next;
166 static int do_umount(const char *name, int useMtab)
169 char *blockDevice = mtab_getinfo(name, MTAB_GETDEVICE);
171 if (blockDevice && strcmp(blockDevice, name) == 0)
172 name = mtab_getinfo(blockDevice, MTAB_GETMOUNTPT);
174 status = umount(name);
176 #if defined BB_FEATURE_MOUNT_LOOP
177 if (freeLoop == TRUE && blockDevice != NULL && !strncmp("/dev/loop", blockDevice, 9))
178 /* this was a loop device, delete it */
179 del_loop(blockDevice);
181 #if defined BB_FEATURE_REMOUNT
182 if (status != 0 && doRemount == TRUE && errno == EBUSY) {
183 status = mount(blockDevice, name, NULL,
184 MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
186 fprintf(stderr, "umount: %s busy - remounted read-only\n",
188 /* TODO: update mtab if BB_MTAB is defined */
190 fprintf(stderr, "umount: Cannot remount %s read-only\n",
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)
239 /* Parse any options */
240 while (--argc > 0 && **(++argv) == '-') {
246 #if defined BB_FEATURE_MOUNT_LOOP
256 #ifdef BB_FEATURE_REMOUNT
267 if (umountAll == TRUE) {
268 exit(umount_all(useMtab));
270 if (do_umount(*argv, useMtab) == 0)