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>
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 #ifdef BB_FEATURE_REMOUNT
41 "\t-r:\tTry to remount devices as read-only if mount is busy\n"
45 struct _mtab_entry_t {
48 struct _mtab_entry_t *next;
51 static struct _mtab_entry_t *mtab_cache = NULL;
55 static int useMtab = TRUE;
56 static int umountAll = FALSE;
57 static int doRemount = FALSE;
58 extern const char mtab_file[]; /* Defined in utility.c */
60 #define MIN(x,y) (x > y ? x : y)
62 static int do_umount(const char *name, int useMtab)
65 char *blockDevice = mtab_getinfo(name, MTAB_GETDEVICE);
67 if (blockDevice && strcmp(blockDevice, name) == 0)
68 name = mtab_getinfo(blockDevice, MTAB_GETMOUNTPT);
70 status = umount(name);
72 #if defined BB_FEATURE_MOUNT_LOOP
73 if (blockDevice != NULL && !strncmp("/dev/loop", blockDevice, 9))
74 /* this was a loop device, delete it */
75 del_loop(blockDevice);
77 #if defined BB_FEATURE_REMOUNT
78 if (status != 0 && doRemount == TRUE && errno == EBUSY) {
79 status = mount(blockDevice, name, NULL,
80 MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
82 fprintf(stderr, "umount: %s busy - remounted read-only\n",
84 /* TODO: update mtab if BB_MTAB is defined */
86 fprintf(stderr, "umount: Cannot remount %s read-only\n",
101 static int umount_all(int useMtab)
107 for (mountpt = mtab_first(&iter); mountpt; mountpt = mtab_next(&iter)) {
108 status = do_umount(mountpt, useMtab);
110 /* Don't bother retrying the umount on busy devices */
111 if (errno == EBUSY) {
115 status = do_umount(mountpt, useMtab);
117 printf("Couldn't umount %s on %s: %s\n",
118 mountpt, mtab_getinfo(mountpt, MTAB_GETDEVICE),
126 extern int umount_main(int argc, char **argv)
132 /* Parse any options */
133 while (--argc > 0 && **(++argv) == '-') {
144 #ifdef BB_FEATURE_REMOUNT
155 if (umountAll == TRUE) {
156 exit(umount_all(useMtab));
158 if (do_umount(*argv, useMtab) == 0)
168 /* These functions are here because the getmntent functions do not appear
169 * to be re-entrant, which leads to all sorts of problems when we try to
170 * use them recursively - randolph
174 struct _mtab_entry_t *entry = NULL;
178 if (mtab_cache != NULL)
181 if ((fp = setmntent(mtab_file, "r")) == NULL) {
182 fprintf(stderr, "Cannot open %s\n", mtab_file);
185 while ((e = getmntent(fp))) {
186 entry = malloc(sizeof(struct _mtab_entry_t));
188 entry->device = strdup(e->mnt_fsname);
189 entry->mountpt = strdup(e->mnt_dir);
190 entry->next = mtab_cache;
196 char *mtab_getinfo(const char *match, const char which)
198 struct _mtab_entry_t *cur = mtab_cache;
201 if (strcmp(cur->mountpt, match) == 0 ||
202 strcmp(cur->device, match) == 0) {
203 if (which == MTAB_GETMOUNTPT) {
207 if (strcmp(cur->device, "/dev/root") == 0) {
208 struct fstab *fstabItem;
210 fstabItem = getfsfile("/");
211 if (fstabItem != NULL)
212 return fstabItem->fs_spec;
223 char *mtab_first(void **iter)
225 struct _mtab_entry_t *mtab_iter;
229 mtab_iter = mtab_cache;
230 *iter = (void *) mtab_iter;
231 return mtab_next(iter);
234 char *mtab_next(void **iter)
238 if (iter == NULL || *iter == NULL)
240 mp = ((struct _mtab_entry_t *) (*iter))->mountpt;
241 *iter = (void *) ((struct _mtab_entry_t *) (*iter))->next;
247 struct _mtab_entry_t *this, *next;