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 /* Teach libc5 about realpath -- it includes it but the
34 * prototype is missing... */
35 #if (__GLIBC__ <= 2) && (__GLIBC_MINOR__ < 1)
36 extern char *realpath(const char *path, char *resolved_path);
39 static const int MNT_FORCE = 1;
40 static const int MS_MGC_VAL = 0xc0ed0000; /* Magic number indicatng "new" flags */
41 static const int MS_REMOUNT = 32; /* Alter flags of a mounted FS. */
42 static const int MS_RDONLY = 1; /* Mount read-only. */
44 extern int mount (__const char *__special_file, __const char *__dir,
45 __const char *__fstype, unsigned long int __rwflag,
46 __const void *__data);
47 extern int umount (__const char *__special_file);
48 extern int umount2 (__const char *__special_file, int __flags);
50 struct _mtab_entry_t {
53 struct _mtab_entry_t *next;
56 static struct _mtab_entry_t *mtab_cache = NULL;
60 #if defined BB_FEATURE_MOUNT_FORCE
61 static int doForce = FALSE;
63 #if defined BB_FEATURE_MOUNT_LOOP
64 static int freeLoop = TRUE;
66 #if defined BB_FEATURE_MTAB_SUPPORT
67 static int useMtab = TRUE;
69 static int umountAll = FALSE;
70 static int doRemount = FALSE;
71 extern const char mtab_file[]; /* Defined in utility.c */
75 /* These functions are here because the getmntent functions do not appear
76 * to be re-entrant, which leads to all sorts of problems when we try to
77 * use them recursively - randolph
79 * TODO: Perhaps switch to using Glibc's getmntent_r
82 static void mtab_read(void)
84 struct _mtab_entry_t *entry = NULL;
88 if (mtab_cache != NULL)
91 if ((fp = setmntent(mtab_file, "r")) == NULL) {
92 error_msg("Cannot open %s", mtab_file);
95 while ((e = getmntent(fp))) {
96 entry = xmalloc(sizeof(struct _mtab_entry_t));
97 entry->device = strdup(e->mnt_fsname);
98 entry->mountpt = strdup(e->mnt_dir);
99 entry->next = mtab_cache;
105 static char *mtab_getinfo(const char *match, const char which)
107 struct _mtab_entry_t *cur = mtab_cache;
110 if (strcmp(cur->mountpt, match) == 0 ||
111 strcmp(cur->device, match) == 0) {
112 if (which == MTAB_GETMOUNTPT) {
115 #if !defined BB_FEATURE_MTAB_SUPPORT
116 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 BB_FEATURE_CLEAN_UP
155 static void mtab_free(void)
157 struct _mtab_entry_t *this, *next;
172 static int do_umount(const char *name)
175 char *blockDevice = mtab_getinfo(name, MTAB_GETDEVICE);
177 if (blockDevice && strcmp(blockDevice, name) == 0)
178 name = mtab_getinfo(blockDevice, MTAB_GETMOUNTPT);
180 status = umount(name);
182 #if defined BB_FEATURE_MOUNT_LOOP
183 if (freeLoop == TRUE && blockDevice != NULL && !strncmp("/dev/loop", blockDevice, 9))
184 /* this was a loop device, delete it */
185 del_loop(blockDevice);
187 #if defined BB_FEATURE_MOUNT_FORCE
188 if (status != 0 && doForce == TRUE) {
189 status = umount2(blockDevice, MNT_FORCE);
191 error_msg_and_die("forced umount of %s failed!", blockDevice);
195 if (status != 0 && doRemount == TRUE && errno == EBUSY) {
196 status = mount(blockDevice, name, NULL,
197 MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
199 error_msg("%s busy - remounted read-only", blockDevice);
201 error_msg("Cannot remount %s read-only", blockDevice);
205 #if defined BB_FEATURE_MTAB_SUPPORT
214 static int umount_all(void)
220 for (mountpt = mtab_first(&iter); mountpt; mountpt = mtab_next(&iter)) {
221 /* Never umount /proc on a umount -a */
222 if (strstr(mountpt, "proc")!= NULL)
224 if (!do_umount(mountpt)) {
225 /* Don't bother retrying the umount on busy devices */
226 if (errno == EBUSY) {
227 perror_msg("%s", mountpt);
231 if (!do_umount(mountpt)) {
232 printf("Couldn't umount %s on %s: %s\n",
233 mountpt, mtab_getinfo(mountpt, MTAB_GETDEVICE),
242 extern int umount_main(int argc, char **argv)
249 #ifdef BB_FEATURE_CLEAN_UP
253 /* Parse any options */
254 while (--argc > 0 && **(++argv) == '-') {
260 #if defined BB_FEATURE_MOUNT_LOOP
265 #ifdef BB_FEATURE_MTAB_SUPPORT
270 #ifdef BB_FEATURE_MOUNT_FORCE
279 break; /* ignore -v */
286 if (umountAll == TRUE) {
287 if (umount_all() == TRUE)
292 if (realpath(*argv, path) == NULL)
293 perror_msg_and_die("%s", path);
294 if (do_umount(path) == TRUE)
296 perror_msg_and_die("%s", *argv);