2 * Mini umount implementation for busybox
5 * Copyright (C) 1999 by Lineo, inc.
6 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <sys/mount.h>
31 static const char umount_usage[] =
32 "umount [flags] filesystem|directory\n\n"
34 "\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)
63 do_umount(const char* name, int useMtab)
66 char *blockDevice = mtab_getinfo(name, MTAB_GETDEVICE);
68 if (blockDevice && strcmp(blockDevice, name) == 0)
69 name = mtab_getinfo(blockDevice, MTAB_GETMOUNTPT);
71 status = umount(name);
73 #if defined BB_FEATURE_MOUNT_LOOP
74 if (blockDevice != NULL && !strncmp("/dev/loop", blockDevice, 9))
75 /* this was a loop device, delete it */
76 del_loop(blockDevice);
78 #if defined BB_FEATURE_REMOUNT
79 if ( status != 0 && doRemount == TRUE && errno == EBUSY ) {
80 status = mount(blockDevice, name, NULL,
81 MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
83 fprintf(stderr, "umount: %s busy - remounted read-only\n",
85 /* TODO: update mtab if BB_MTAB is defined */
87 fprintf(stderr, "umount: Cannot remount %s read-only\n",
103 umount_all(int useMtab)
109 for (mountpt = mtab_first(&iter); mountpt; mountpt = mtab_next(&iter)) {
110 status=do_umount (mountpt, useMtab);
112 /* Don't bother retrying the umount on busy devices */
113 if (errno == EBUSY) {
117 status = do_umount (mountpt, useMtab);
119 printf ("Couldn't umount %s on %s: %s\n",
120 mountpt, mtab_getinfo(mountpt, MTAB_GETDEVICE), strerror(errno));
128 umount_main(int argc, char** argv)
131 usage( umount_usage);
134 /* Parse any options */
135 while (--argc > 0 && **(++argv) == '-') {
136 while (*++(*argv)) switch (**argv) {
145 #ifdef BB_FEATURE_REMOUNT
151 usage( umount_usage);
156 if (umountAll==TRUE) {
157 exit(umount_all(useMtab));
159 if ( do_umount(*argv,useMtab) == 0 )
169 /* These functions are here because the getmntent functions do not appear
170 * to be re-entrant, which leads to all sorts of problems when we try to
171 * use them recursively - randolph
175 struct _mtab_entry_t *entry = NULL;
179 if (mtab_cache != NULL) return;
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));
187 entry->device = strdup(e->mnt_fsname);
188 entry->mountpt = strdup(e->mnt_dir);
189 entry->next = mtab_cache;
195 char *mtab_getinfo(const char *match, const char which)
197 struct _mtab_entry_t *cur = mtab_cache;
199 if (strcmp(cur->mountpt, match) == 0 ||
200 strcmp(cur->device, match) == 0) {
201 if (which == MTAB_GETMOUNTPT) {
205 if (strcmp(cur->device, "/dev/root") == 0) {
206 struct fstab* fstabItem;
207 fstabItem = getfsfile ("/");
208 if (fstabItem != NULL) return fstabItem->fs_spec;
219 char *mtab_first(void **iter)
221 struct _mtab_entry_t *mtab_iter;
222 if (!iter) return NULL;
223 mtab_iter = mtab_cache;
224 *iter = (void *)mtab_iter;
225 return mtab_next(iter);
228 char *mtab_next(void **iter)
231 if (iter == NULL || *iter == NULL) return NULL;
232 mp = ((struct _mtab_entry_t *)(*iter))->mountpt;
233 *iter = (void *)((struct _mtab_entry_t *)(*iter))->next;
239 struct _mtab_entry_t *this, *next;
244 if (this->device) free(this->device);
245 if (this->mountpt) free(this->mountpt);