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;
70 extern const char mtab_file[]; /* Defined in utility.c */
74 /* These functions are here because the getmntent functions do not appear
75 * to be re-entrant, which leads to all sorts of problems when we try to
76 * use them recursively - randolph
78 * TODO: Perhaps switch to using Glibc's getmntent_r
81 static void mtab_read(void)
83 struct _mtab_entry_t *entry = NULL;
87 if (mtab_cache != NULL)
90 if ((fp = setmntent(mtab_file, "r")) == NULL) {
91 error_msg("Cannot open %s", mtab_file);
94 while ((e = getmntent(fp))) {
95 entry = xmalloc(sizeof(struct _mtab_entry_t));
96 entry->device = strdup(e->mnt_fsname);
97 entry->mountpt = strdup(e->mnt_dir);
98 entry->next = mtab_cache;
104 static char *mtab_getinfo(const char *match, const char which)
106 struct _mtab_entry_t *cur = mtab_cache;
109 if (strcmp(cur->mountpt, match) == 0 ||
110 strcmp(cur->device, match) == 0) {
111 if (which == MTAB_GETMOUNTPT) {
114 #if !defined CONFIG_FEATURE_MTAB_SUPPORT
115 if (strcmp(cur->device, "/dev/root") == 0) {
116 /* Adjusts device to be the real root device,
117 * or leaves device alone if it can't find it */
118 cur->device = find_real_root_device_name(cur->device);
129 static char *mtab_next(void **iter)
133 if (iter == NULL || *iter == NULL)
135 mp = ((struct _mtab_entry_t *) (*iter))->mountpt;
136 *iter = (void *) ((struct _mtab_entry_t *) (*iter))->next;
140 static char *mtab_first(void **iter)
142 struct _mtab_entry_t *mtab_iter;
146 mtab_iter = mtab_cache;
147 *iter = (void *) mtab_iter;
148 return mtab_next(iter);
151 /* Don't bother to clean up, since exit() does that
152 * automagically, so we can save a few bytes */
153 #ifdef CONFIG_FEATURE_CLEAN_UP
154 static void mtab_free(void)
156 struct _mtab_entry_t *this, *next;
171 static int do_umount(const char *name)
174 char *blockDevice = mtab_getinfo(name, MTAB_GETDEVICE);
176 if (blockDevice && strcmp(blockDevice, name) == 0)
177 name = mtab_getinfo(blockDevice, MTAB_GETMOUNTPT);
179 status = umount(name);
181 #if defined CONFIG_FEATURE_MOUNT_LOOP
182 if (freeLoop && blockDevice != NULL && !strncmp("/dev/loop", blockDevice, 9))
183 /* this was a loop device, delete it */
184 del_loop(blockDevice);
186 #if defined CONFIG_FEATURE_MOUNT_FORCE
187 if (status != 0 && doForce) {
188 status = umount2(blockDevice, MNT_FORCE);
190 error_msg_and_die("forced umount of %s failed!", blockDevice);
194 if (status != 0 && doRemount && errno == EBUSY) {
195 status = mount(blockDevice, name, NULL,
196 MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
198 error_msg("%s busy - remounted read-only", blockDevice);
200 error_msg("Cannot remount %s read-only", blockDevice);
204 #if defined CONFIG_FEATURE_MTAB_SUPPORT
213 static int umount_all(void)
219 for (mountpt = mtab_first(&iter); mountpt; mountpt = mtab_next(&iter)) {
220 /* Never umount /proc on a umount -a */
221 if (strstr(mountpt, "proc")!= NULL)
223 if (!do_umount(mountpt)) {
224 /* Don't bother retrying the umount on busy devices */
225 if (errno == EBUSY) {
226 perror_msg("%s", mountpt);
230 if (!do_umount(mountpt)) {
231 printf("Couldn't umount %s on %s: %s\n",
232 mountpt, mtab_getinfo(mountpt, MTAB_GETDEVICE),
241 extern int umount_main(int argc, char **argv)
248 #ifdef CONFIG_FEATURE_CLEAN_UP
252 /* Parse any options */
253 while (--argc > 0 && **(++argv) == '-') {
259 #if defined CONFIG_FEATURE_MOUNT_LOOP
264 #ifdef CONFIG_FEATURE_MTAB_SUPPORT
269 #ifdef CONFIG_FEATURE_MOUNT_FORCE
278 break; /* ignore -v */
291 if (realpath(*argv, path) == NULL)
292 perror_msg_and_die("%s", path);
295 perror_msg_and_die("%s", *argv);