1 /* vi: set sw=4 ts=4: */
3 * Mini umount implementation for busybox
5 * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen
6 * Copyright (C) 1999,2000,2001 by Erik Andersen <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
32 /* Teach libc5 about realpath -- it includes it but the
33 * prototype is missing... */
34 #if (__GLIBC__ <= 2) && (__GLIBC_MINOR__ < 1)
35 extern char *realpath(const char *path, char *resolved_path);
38 static const int MNT_FORCE = 1;
39 static const int MS_MGC_VAL = 0xc0ed0000; /* Magic number indicatng "new" flags */
40 static const int MS_REMOUNT = 32; /* Alter flags of a mounted FS. */
41 static const int MS_RDONLY = 1; /* Mount read-only. */
43 extern int mount (__const char *__special_file, __const char *__dir,
44 __const char *__fstype, unsigned long int __rwflag,
45 __const void *__data);
46 extern int umount (__const char *__special_file);
47 extern int umount2 (__const char *__special_file, int __flags);
49 struct _mtab_entry_t {
52 struct _mtab_entry_t *next;
55 static struct _mtab_entry_t *mtab_cache = NULL;
59 #if defined CONFIG_FEATURE_MOUNT_FORCE
60 static int doForce = FALSE;
62 #if defined CONFIG_FEATURE_MOUNT_LOOP
63 static int freeLoop = TRUE;
65 #if defined CONFIG_FEATURE_MTAB_SUPPORT
66 static int useMtab = TRUE;
68 static int umountAll = FALSE;
69 static int doRemount = FALSE;
73 /* These functions are here because the getmntent functions do not appear
74 * to be re-entrant, which leads to all sorts of problems when we try to
75 * use them recursively - randolph
77 * TODO: Perhaps switch to using Glibc's getmntent_r
80 static void mtab_read(void)
82 struct _mtab_entry_t *entry = NULL;
86 if (mtab_cache != NULL)
89 if ((fp = setmntent(bb_path_mtab_file, "r")) == NULL) {
90 bb_error_msg("Cannot open %s", bb_path_mtab_file);
93 while ((e = getmntent(fp))) {
94 entry = xmalloc(sizeof(struct _mtab_entry_t));
95 entry->device = strdup(e->mnt_fsname);
96 entry->mountpt = strdup(e->mnt_dir);
97 entry->next = mtab_cache;
103 static char *mtab_getinfo(const char *match, const char which)
105 struct _mtab_entry_t *cur = mtab_cache;
108 if (strcmp(cur->mountpt, match) == 0 ||
109 strcmp(cur->device, match) == 0) {
110 if (which == MTAB_GETMOUNTPT) {
113 #if !defined CONFIG_FEATURE_MTAB_SUPPORT
114 if (strcmp(cur->device, "rootfs") == 0) {
116 } else if (strcmp(cur->device, "/dev/root") == 0) {
117 /* Adjusts device to be the real root device,
118 * or leaves device alone if it can't find it */
119 cur->device = find_real_root_device_name(cur->device);
130 static char *mtab_next(void **iter)
134 if (iter == NULL || *iter == NULL)
136 mp = ((struct _mtab_entry_t *) (*iter))->mountpt;
137 *iter = (void *) ((struct _mtab_entry_t *) (*iter))->next;
141 static char *mtab_first(void **iter)
143 struct _mtab_entry_t *mtab_iter;
147 mtab_iter = mtab_cache;
148 *iter = (void *) mtab_iter;
149 return mtab_next(iter);
152 /* Don't bother to clean up, since exit() does that
153 * automagically, so we can save a few bytes */
154 #ifdef CONFIG_FEATURE_CLEAN_UP
155 static void mtab_free(void)
157 struct _mtab_entry_t *this, *next;
170 static int do_umount(const char *name)
173 char *blockDevice = mtab_getinfo(name, MTAB_GETDEVICE);
175 if (blockDevice && strcmp(blockDevice, name) == 0)
176 name = mtab_getinfo(blockDevice, MTAB_GETMOUNTPT);
178 status = umount(name);
180 #if defined CONFIG_FEATURE_MOUNT_LOOP
181 if (freeLoop && blockDevice != NULL && !strncmp("/dev/loop", blockDevice, 9))
182 /* this was a loop device, delete it */
183 del_loop(blockDevice);
185 #if defined CONFIG_FEATURE_MOUNT_FORCE
186 if (status != 0 && doForce) {
187 status = umount2(blockDevice, MNT_FORCE);
189 bb_error_msg_and_die("forced umount of %s failed!", blockDevice);
193 if (status != 0 && doRemount && errno == EBUSY) {
194 status = mount(blockDevice, name, NULL,
195 MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
197 bb_error_msg("%s busy - remounted read-only", blockDevice);
199 bb_error_msg("Cannot remount %s read-only", blockDevice);
203 #if defined CONFIG_FEATURE_MTAB_SUPPORT
212 static int umount_all(void)
218 for (mountpt = mtab_first(&iter); mountpt; mountpt = mtab_next(&iter)) {
219 /* Never umount /proc on a umount -a */
220 if (strstr(mountpt, "proc")!= NULL)
222 if (!do_umount(mountpt)) {
223 /* Don't bother retrying the umount on busy devices */
224 if (errno == EBUSY) {
225 bb_perror_msg("%s", mountpt);
229 if (!do_umount(mountpt)) {
230 printf("Couldn't umount %s on %s: %s\n",
231 mountpt, mtab_getinfo(mountpt, MTAB_GETDEVICE),
240 extern int umount_main(int argc, char **argv)
247 #ifdef CONFIG_FEATURE_CLEAN_UP
251 /* Parse any options */
252 while (--argc > 0 && **(++argv) == '-') {
258 #if defined CONFIG_FEATURE_MOUNT_LOOP
263 #ifdef CONFIG_FEATURE_MTAB_SUPPORT
268 #ifdef CONFIG_FEATURE_MOUNT_FORCE
277 break; /* ignore -v */
290 if (realpath(*argv, path) == NULL)
291 bb_perror_msg_and_die("%s", path);
294 bb_perror_msg_and_die("%s", *argv);