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"
46 struct _mtab_entry_t {
49 struct _mtab_entry_t *next;
52 static struct _mtab_entry_t *mtab_cache = NULL;
56 static int useMtab = TRUE;
57 static int umountAll = FALSE;
58 static int doRemount = FALSE;
59 extern const char mtab_file[]; /* Defined in utility.c */
62 /* These functions are here because the getmntent functions do not appear
63 * to be re-entrant, which leads to all sorts of problems when we try to
64 * use them recursively - randolph
68 struct _mtab_entry_t *entry = NULL;
72 if (mtab_cache != NULL)
75 if ((fp = setmntent(mtab_file, "r")) == NULL) {
76 fprintf(stderr, "Cannot open %s\n", mtab_file);
79 while ((e = getmntent(fp))) {
80 entry = malloc(sizeof(struct _mtab_entry_t));
82 entry->device = strdup(e->mnt_fsname);
83 entry->mountpt = strdup(e->mnt_dir);
84 entry->next = mtab_cache;
90 char *mtab_getinfo(const char *match, const char which)
92 struct _mtab_entry_t *cur = mtab_cache;
95 if (strcmp(cur->mountpt, match) == 0 ||
96 strcmp(cur->device, match) == 0) {
97 if (which == MTAB_GETMOUNTPT) {
101 if (strcmp(cur->device, "/dev/root") == 0) {
102 struct fstab *fstabItem;
104 fstabItem = getfsfile("/");
105 if (fstabItem != NULL)
106 return fstabItem->fs_spec;
117 char *mtab_first(void **iter)
119 struct _mtab_entry_t *mtab_iter;
123 mtab_iter = mtab_cache;
124 *iter = (void *) mtab_iter;
125 return mtab_next(iter);
128 char *mtab_next(void **iter)
132 if (iter == NULL || *iter == NULL)
134 mp = ((struct _mtab_entry_t *) (*iter))->mountpt;
135 *iter = (void *) ((struct _mtab_entry_t *) (*iter))->next;
141 struct _mtab_entry_t *this, *next;
155 static int do_umount(const char *name, int useMtab)
158 char *blockDevice = mtab_getinfo(name, MTAB_GETDEVICE);
160 if (blockDevice && strcmp(blockDevice, name) == 0)
161 name = mtab_getinfo(blockDevice, MTAB_GETMOUNTPT);
163 status = umount(name);
165 #if defined BB_FEATURE_MOUNT_LOOP
166 if (blockDevice != NULL && !strncmp("/dev/loop", blockDevice, 9))
167 /* this was a loop device, delete it */
168 del_loop(blockDevice);
170 #if defined BB_FEATURE_REMOUNT
171 if (status != 0 && doRemount == TRUE && errno == EBUSY) {
172 status = mount(blockDevice, name, NULL,
173 MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
175 fprintf(stderr, "umount: %s busy - remounted read-only\n",
177 /* TODO: update mtab if BB_MTAB is defined */
179 fprintf(stderr, "umount: Cannot remount %s read-only\n",
194 static int umount_all(int useMtab)
200 for (mountpt = mtab_first(&iter); mountpt; mountpt = mtab_next(&iter)) {
201 /* Never umount /proc on a umount -a */
202 if (strstr(mountpt, "proc")!= NULL)
204 status = do_umount(mountpt, useMtab);
206 /* Don't bother retrying the umount on busy devices */
207 if (errno == EBUSY) {
211 status = do_umount(mountpt, useMtab);
213 printf("Couldn't umount %s on %s: %s\n",
214 mountpt, mtab_getinfo(mountpt, MTAB_GETDEVICE),
222 extern int umount_main(int argc, char **argv)
228 /* Parse any options */
229 while (--argc > 0 && **(++argv) == '-') {
240 #ifdef BB_FEATURE_REMOUNT
251 if (umountAll == TRUE) {
252 exit(umount_all(useMtab));
254 if (do_umount(*argv, useMtab) == 0)