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
34 static const int MNT_FORCE = 1;
35 static const int MS_MGC_VAL = 0xc0ed0000; /* Magic number indicatng "new" flags */
36 static const int MS_REMOUNT = 32; /* Alter flags of a mounted FS. */
37 static const int MS_RDONLY = 1; /* Mount read-only. */
39 extern int mount (__const char *__special_file, __const char *__dir,
40 __const char *__fstype, unsigned long int __rwflag,
41 __const void *__data);
42 extern int umount (__const char *__special_file);
43 extern int umount2 (__const char *__special_file, int __flags);
45 struct _mtab_entry_t {
48 struct _mtab_entry_t *next;
51 static struct _mtab_entry_t *mtab_cache = NULL;
55 #if defined BB_FEATURE_MOUNT_FORCE
56 static int doForce = FALSE;
58 #if defined BB_FEATURE_MOUNT_LOOP
59 static int freeLoop = TRUE;
61 #if defined BB_FEATURE_MTAB_SUPPORT
62 static int useMtab = TRUE;
64 static int umountAll = FALSE;
65 static int doRemount = FALSE;
66 extern const char mtab_file[]; /* Defined in utility.c */
70 /* These functions are here because the getmntent functions do not appear
71 * to be re-entrant, which leads to all sorts of problems when we try to
72 * use them recursively - randolph
74 * TODO: Perhaps switch to using Glibc's getmntent_r
79 struct _mtab_entry_t *entry = NULL;
83 if (mtab_cache != NULL)
86 if ((fp = setmntent(mtab_file, "r")) == NULL) {
87 error_msg("Cannot open %s", mtab_file);
90 while ((e = getmntent(fp))) {
91 entry = xmalloc(sizeof(struct _mtab_entry_t));
92 entry->device = strdup(e->mnt_fsname);
93 entry->mountpt = strdup(e->mnt_dir);
94 entry->next = mtab_cache;
100 char *mtab_getinfo(const char *match, const char which)
102 struct _mtab_entry_t *cur = mtab_cache;
105 if (strcmp(cur->mountpt, match) == 0 ||
106 strcmp(cur->device, match) == 0) {
107 if (which == MTAB_GETMOUNTPT) {
110 #if !defined BB_FEATURE_MTAB_SUPPORT
111 if (strcmp(cur->device, "/dev/root") == 0) {
112 /* Adjusts device to be the real root device,
113 * or leaves device alone if it can't find it */
114 find_real_root_device_name( cur->device);
115 return ( cur->device);
126 char *mtab_first(void **iter)
128 struct _mtab_entry_t *mtab_iter;
132 mtab_iter = mtab_cache;
133 *iter = (void *) mtab_iter;
134 return mtab_next(iter);
137 char *mtab_next(void **iter)
141 if (iter == NULL || *iter == NULL)
143 mp = ((struct _mtab_entry_t *) (*iter))->mountpt;
144 *iter = (void *) ((struct _mtab_entry_t *) (*iter))->next;
148 /* Don't bother to clean up, since exit() does that
149 * automagically, so we can save a few bytes */
150 #ifdef BB_FEATURE_CLEAN_UP
153 struct _mtab_entry_t *this, *next;
168 static int do_umount(const char *name)
171 char *blockDevice = mtab_getinfo(name, MTAB_GETDEVICE);
173 if (blockDevice && strcmp(blockDevice, name) == 0)
174 name = mtab_getinfo(blockDevice, MTAB_GETMOUNTPT);
176 status = umount(name);
178 #if defined BB_FEATURE_MOUNT_LOOP
179 if (freeLoop == TRUE && blockDevice != NULL && !strncmp("/dev/loop", blockDevice, 9))
180 /* this was a loop device, delete it */
181 del_loop(blockDevice);
183 #if defined BB_FEATURE_MOUNT_FORCE
184 if (status != 0 && doForce == TRUE) {
185 status = umount2(blockDevice, MNT_FORCE);
187 error_msg_and_die("forced umount of %s failed!", blockDevice);
191 if (status != 0 && doRemount == TRUE && errno == EBUSY) {
192 status = mount(blockDevice, name, NULL,
193 MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
195 error_msg("%s busy - remounted read-only", blockDevice);
197 error_msg("Cannot remount %s read-only", blockDevice);
201 #if defined BB_FEATURE_MTAB_SUPPORT
210 static int umount_all(void)
216 for (mountpt = mtab_first(&iter); mountpt; mountpt = mtab_next(&iter)) {
217 /* Never umount /proc on a umount -a */
218 if (strstr(mountpt, "proc")!= NULL)
220 if (!do_umount(mountpt)) {
221 /* Don't bother retrying the umount on busy devices */
222 if (errno == EBUSY) {
223 perror_msg("%s", mountpt);
227 if (!do_umount(mountpt)) {
228 printf("Couldn't umount %s on %s: %s\n",
229 mountpt, mtab_getinfo(mountpt, MTAB_GETDEVICE),
238 extern int umount_main(int argc, char **argv)
245 #ifdef BB_FEATURE_CLEAN_UP
249 /* Parse any options */
250 while (--argc > 0 && **(++argv) == '-') {
256 #if defined BB_FEATURE_MOUNT_LOOP
261 #ifdef BB_FEATURE_MTAB_SUPPORT
266 #ifdef BB_FEATURE_MOUNT_FORCE
275 break; /* ignore -v */
282 if (umountAll == TRUE) {
283 if (umount_all() == TRUE)
288 if (realpath(*argv, path) == NULL)
289 perror_msg_and_die("%s", path);
290 if (do_umount(path) == TRUE)
292 perror_msg_and_die("%s", *argv);