1 /* vi: set sw=4 ts=4: */
3 * Mini umount implementation for busybox
6 * Copyright (C) 1999,2000,2001 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
33 static const int MNT_FORCE = 1;
34 static const int MS_MGC_VAL = 0xc0ed0000; /* Magic number indicatng "new" flags */
35 static const int MS_REMOUNT = 32; /* Alter flags of a mounted FS. */
36 static const int MS_RDONLY = 1; /* Mount read-only. */
38 extern int mount (__const char *__special_file, __const char *__dir,
39 __const char *__fstype, unsigned long int __rwflag,
40 __const void *__data);
41 extern int umount (__const char *__special_file);
42 extern int umount2 (__const char *__special_file, int __flags);
44 struct _mtab_entry_t {
47 struct _mtab_entry_t *next;
50 static struct _mtab_entry_t *mtab_cache = NULL;
54 #if defined BB_FEATURE_MOUNT_FORCE
55 static int doForce = FALSE;
57 #if defined BB_FEATURE_MOUNT_LOOP
58 static int freeLoop = TRUE;
60 static int useMtab = TRUE;
61 static int umountAll = FALSE;
62 static int doRemount = FALSE;
63 extern const char mtab_file[]; /* Defined in utility.c */
67 /* These functions are here because the getmntent functions do not appear
68 * to be re-entrant, which leads to all sorts of problems when we try to
69 * use them recursively - randolph
71 * TODO: Perhaps switch to using Glibc's getmntent_r
76 struct _mtab_entry_t *entry = NULL;
80 if (mtab_cache != NULL)
83 if ((fp = setmntent(mtab_file, "r")) == NULL) {
84 error_msg("Cannot open %s", mtab_file);
87 while ((e = getmntent(fp))) {
88 entry = xmalloc(sizeof(struct _mtab_entry_t));
89 entry->device = strdup(e->mnt_fsname);
90 entry->mountpt = strdup(e->mnt_dir);
91 entry->next = mtab_cache;
97 char *mtab_getinfo(const char *match, const char which)
99 struct _mtab_entry_t *cur = mtab_cache;
102 if (strcmp(cur->mountpt, match) == 0 ||
103 strcmp(cur->device, match) == 0) {
104 if (which == MTAB_GETMOUNTPT) {
108 if (strcmp(cur->device, "/dev/root") == 0) {
109 /* Adjusts device to be the real root device,
110 * or leaves device alone if it can't find it */
111 find_real_root_device_name( cur->device);
112 return ( cur->device);
123 char *mtab_first(void **iter)
125 struct _mtab_entry_t *mtab_iter;
129 mtab_iter = mtab_cache;
130 *iter = (void *) mtab_iter;
131 return mtab_next(iter);
134 char *mtab_next(void **iter)
138 if (iter == NULL || *iter == NULL)
140 mp = ((struct _mtab_entry_t *) (*iter))->mountpt;
141 *iter = (void *) ((struct _mtab_entry_t *) (*iter))->next;
145 /* Don't bother to clean up, since exit() does that
146 * automagically, so we can save a few bytes */
147 #ifdef BB_FEATURE_CLEAN_UP
150 struct _mtab_entry_t *this, *next;
165 static int do_umount(const char *name, int useMtab)
168 char *blockDevice = mtab_getinfo(name, MTAB_GETDEVICE);
170 if (blockDevice && strcmp(blockDevice, name) == 0)
171 name = mtab_getinfo(blockDevice, MTAB_GETMOUNTPT);
173 status = umount(name);
175 #if defined BB_FEATURE_MOUNT_LOOP
176 if (freeLoop == TRUE && blockDevice != NULL && !strncmp("/dev/loop", blockDevice, 9))
177 /* this was a loop device, delete it */
178 del_loop(blockDevice);
180 #if defined BB_FEATURE_MOUNT_FORCE
181 if (status != 0 && doForce == TRUE) {
182 status = umount2(blockDevice, MNT_FORCE);
184 error_msg_and_die("forced umount of %s failed!", blockDevice);
188 if (status != 0 && doRemount == TRUE && errno == EBUSY) {
189 status = mount(blockDevice, name, NULL,
190 MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
192 error_msg("%s busy - remounted read-only", blockDevice);
194 error_msg("Cannot remount %s read-only", blockDevice);
207 static int umount_all(int useMtab)
213 for (mountpt = mtab_first(&iter); mountpt; mountpt = mtab_next(&iter)) {
214 /* Never umount /proc on a umount -a */
215 if (strstr(mountpt, "proc")!= NULL)
217 if (!do_umount(mountpt, useMtab)) {
218 /* Don't bother retrying the umount on busy devices */
219 if (errno == EBUSY) {
220 perror_msg("%s", mountpt);
224 if (!do_umount(mountpt, useMtab)) {
225 printf("Couldn't umount %s on %s: %s\n",
226 mountpt, mtab_getinfo(mountpt, MTAB_GETDEVICE),
235 extern int umount_main(int argc, char **argv)
240 #ifdef BB_FEATURE_CLEAN_UP
244 /* Parse any options */
245 while (--argc > 0 && **(++argv) == '-') {
251 #if defined BB_FEATURE_MOUNT_LOOP
261 #ifdef BB_FEATURE_MOUNT_FORCE
270 break; /* ignore -v */
277 if (umountAll == TRUE) {
278 if (umount_all(useMtab) == TRUE)
283 if (do_umount(*argv, useMtab) == TRUE)
285 perror_msg_and_die("%s", *argv);