2000-02-09 01:28:47 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
1999-10-09 05:55:00 +05:30
|
|
|
/*
|
|
|
|
* Mini umount implementation for busybox
|
|
|
|
*
|
2003-07-15 02:51:08 +05:30
|
|
|
* Copyright (C) 1999-2003 by Erik Andersen <andersen@codepoet.org>
|
1999-10-09 05:55:00 +05:30
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2001-05-03 02:54:51 +05:30
|
|
|
#include <limits.h>
|
1999-10-05 21:54:54 +05:30
|
|
|
#include <stdio.h>
|
1999-10-09 05:55:00 +05:30
|
|
|
#include <mntent.h>
|
|
|
|
#include <errno.h>
|
2001-01-27 13:54:39 +05:30
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2001-02-20 11:44:08 +05:30
|
|
|
#include "busybox.h"
|
2000-06-20 00:08:51 +05:30
|
|
|
|
2001-08-02 15:25:58 +05:30
|
|
|
/* Teach libc5 about realpath -- it includes it but the
|
|
|
|
* prototype is missing... */
|
|
|
|
#if (__GLIBC__ <= 2) && (__GLIBC_MINOR__ < 1)
|
|
|
|
extern char *realpath(const char *path, char *resolved_path);
|
|
|
|
#endif
|
2000-06-20 00:08:51 +05:30
|
|
|
|
2001-01-24 04:00:04 +05:30
|
|
|
static const int MNT_FORCE = 1;
|
|
|
|
static const int MS_MGC_VAL = 0xc0ed0000; /* Magic number indicatng "new" flags */
|
|
|
|
static const int MS_REMOUNT = 32; /* Alter flags of a mounted FS. */
|
|
|
|
static const int MS_RDONLY = 1; /* Mount read-only. */
|
2000-07-09 00:40:29 +05:30
|
|
|
|
2000-07-09 00:50:49 +05:30
|
|
|
extern int mount (__const char *__special_file, __const char *__dir,
|
|
|
|
__const char *__fstype, unsigned long int __rwflag,
|
|
|
|
__const void *__data);
|
|
|
|
extern int umount (__const char *__special_file);
|
|
|
|
extern int umount2 (__const char *__special_file, int __flags);
|
2000-07-09 00:40:29 +05:30
|
|
|
|
2000-02-07 10:59:42 +05:30
|
|
|
struct _mtab_entry_t {
|
2000-02-09 01:28:47 +05:30
|
|
|
char *device;
|
|
|
|
char *mountpt;
|
|
|
|
struct _mtab_entry_t *next;
|
2000-02-07 10:59:42 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
static struct _mtab_entry_t *mtab_cache = NULL;
|
|
|
|
|
|
|
|
|
1999-11-05 02:48:07 +05:30
|
|
|
|
2001-10-24 10:30:29 +05:30
|
|
|
#if defined CONFIG_FEATURE_MOUNT_FORCE
|
2000-05-06 01:19:33 +05:30
|
|
|
static int doForce = FALSE;
|
|
|
|
#endif
|
2001-10-24 10:30:29 +05:30
|
|
|
#if defined CONFIG_FEATURE_MOUNT_LOOP
|
2000-03-13 09:37:02 +05:30
|
|
|
static int freeLoop = TRUE;
|
|
|
|
#endif
|
2001-10-24 10:30:29 +05:30
|
|
|
#if defined CONFIG_FEATURE_MTAB_SUPPORT
|
1999-11-05 02:48:07 +05:30
|
|
|
static int useMtab = TRUE;
|
2001-03-21 13:04:27 +05:30
|
|
|
#endif
|
1999-11-05 02:48:07 +05:30
|
|
|
static int umountAll = FALSE;
|
2000-02-07 10:59:42 +05:30
|
|
|
static int doRemount = FALSE;
|
1999-11-05 02:48:07 +05:30
|
|
|
|
2000-02-09 09:46:43 +05:30
|
|
|
|
2000-02-24 04:19:58 +05:30
|
|
|
|
2000-02-09 09:46:43 +05:30
|
|
|
/* These functions are here because the getmntent functions do not appear
|
|
|
|
* to be re-entrant, which leads to all sorts of problems when we try to
|
|
|
|
* use them recursively - randolph
|
2000-02-24 04:19:58 +05:30
|
|
|
*
|
|
|
|
* TODO: Perhaps switch to using Glibc's getmntent_r
|
|
|
|
* -Erik
|
2000-02-09 09:46:43 +05:30
|
|
|
*/
|
2001-05-15 23:12:16 +05:30
|
|
|
static void mtab_read(void)
|
2000-02-09 09:46:43 +05:30
|
|
|
{
|
|
|
|
struct _mtab_entry_t *entry = NULL;
|
|
|
|
struct mntent *e;
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
if (mtab_cache != NULL)
|
|
|
|
return;
|
|
|
|
|
2003-03-19 14:43:01 +05:30
|
|
|
if ((fp = setmntent(bb_path_mtab_file, "r")) == NULL) {
|
|
|
|
bb_error_msg("Cannot open %s", bb_path_mtab_file);
|
2000-02-09 09:46:43 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
while ((e = getmntent(fp))) {
|
2000-03-22 04:02:57 +05:30
|
|
|
entry = xmalloc(sizeof(struct _mtab_entry_t));
|
2000-02-09 09:46:43 +05:30
|
|
|
entry->device = strdup(e->mnt_fsname);
|
|
|
|
entry->mountpt = strdup(e->mnt_dir);
|
|
|
|
entry->next = mtab_cache;
|
|
|
|
mtab_cache = entry;
|
|
|
|
}
|
|
|
|
endmntent(fp);
|
|
|
|
}
|
|
|
|
|
2001-05-15 23:12:16 +05:30
|
|
|
static char *mtab_getinfo(const char *match, const char which)
|
2000-02-09 09:46:43 +05:30
|
|
|
{
|
|
|
|
struct _mtab_entry_t *cur = mtab_cache;
|
|
|
|
|
|
|
|
while (cur) {
|
|
|
|
if (strcmp(cur->mountpt, match) == 0 ||
|
|
|
|
strcmp(cur->device, match) == 0) {
|
|
|
|
if (which == MTAB_GETMOUNTPT) {
|
|
|
|
return cur->mountpt;
|
|
|
|
} else {
|
2001-10-24 10:30:29 +05:30
|
|
|
#if !defined CONFIG_FEATURE_MTAB_SUPPORT
|
2003-06-20 15:06:49 +05:30
|
|
|
if (strcmp(cur->device, "rootfs") == 0) {
|
|
|
|
continue;
|
|
|
|
} else if (strcmp(cur->device, "/dev/root") == 0) {
|
2000-03-22 12:42:05 +05:30
|
|
|
/* Adjusts device to be the real root device,
|
|
|
|
* or leaves device alone if it can't find it */
|
2001-05-15 23:12:16 +05:30
|
|
|
cur->device = find_real_root_device_name(cur->device);
|
2000-02-09 09:46:43 +05:30
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return cur->device;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cur = cur->next;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-05-15 23:12:16 +05:30
|
|
|
static char *mtab_next(void **iter)
|
2000-02-09 09:46:43 +05:30
|
|
|
{
|
|
|
|
char *mp;
|
|
|
|
|
|
|
|
if (iter == NULL || *iter == NULL)
|
|
|
|
return NULL;
|
|
|
|
mp = ((struct _mtab_entry_t *) (*iter))->mountpt;
|
|
|
|
*iter = (void *) ((struct _mtab_entry_t *) (*iter))->next;
|
|
|
|
return mp;
|
|
|
|
}
|
|
|
|
|
2001-05-15 23:12:16 +05:30
|
|
|
static char *mtab_first(void **iter)
|
|
|
|
{
|
|
|
|
struct _mtab_entry_t *mtab_iter;
|
|
|
|
|
|
|
|
if (!iter)
|
|
|
|
return NULL;
|
|
|
|
mtab_iter = mtab_cache;
|
|
|
|
*iter = (void *) mtab_iter;
|
|
|
|
return mtab_next(iter);
|
|
|
|
}
|
|
|
|
|
2000-03-23 06:39:18 +05:30
|
|
|
/* Don't bother to clean up, since exit() does that
|
|
|
|
* automagically, so we can save a few bytes */
|
2001-10-24 10:30:29 +05:30
|
|
|
#ifdef CONFIG_FEATURE_CLEAN_UP
|
2001-05-15 23:12:16 +05:30
|
|
|
static void mtab_free(void)
|
2000-02-09 09:46:43 +05:30
|
|
|
{
|
|
|
|
struct _mtab_entry_t *this, *next;
|
|
|
|
|
|
|
|
this = mtab_cache;
|
|
|
|
while (this) {
|
|
|
|
next = this->next;
|
2002-11-28 16:57:31 +05:30
|
|
|
free(this->device);
|
|
|
|
free(this->mountpt);
|
2000-02-09 09:46:43 +05:30
|
|
|
free(this);
|
|
|
|
this = next;
|
|
|
|
}
|
|
|
|
}
|
2000-03-23 06:39:18 +05:30
|
|
|
#endif
|
2000-01-27 01:36:48 +05:30
|
|
|
|
2001-03-21 13:04:27 +05:30
|
|
|
static int do_umount(const char *name)
|
1999-11-05 02:48:07 +05:30
|
|
|
{
|
2000-02-09 01:28:47 +05:30
|
|
|
int status;
|
|
|
|
char *blockDevice = mtab_getinfo(name, MTAB_GETDEVICE);
|
2000-02-07 10:59:42 +05:30
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
if (blockDevice && strcmp(blockDevice, name) == 0)
|
|
|
|
name = mtab_getinfo(blockDevice, MTAB_GETMOUNTPT);
|
2000-01-13 12:08:14 +05:30
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
status = umount(name);
|
1999-11-05 02:48:07 +05:30
|
|
|
|
2001-10-24 10:30:29 +05:30
|
|
|
#if defined CONFIG_FEATURE_MOUNT_LOOP
|
2001-12-21 04:43:26 +05:30
|
|
|
if (freeLoop && blockDevice != NULL && !strncmp("/dev/loop", blockDevice, 9))
|
2000-02-09 01:28:47 +05:30
|
|
|
/* this was a loop device, delete it */
|
|
|
|
del_loop(blockDevice);
|
2000-05-06 01:19:33 +05:30
|
|
|
#endif
|
2001-10-24 10:30:29 +05:30
|
|
|
#if defined CONFIG_FEATURE_MOUNT_FORCE
|
2001-12-21 04:43:26 +05:30
|
|
|
if (status != 0 && doForce) {
|
2000-05-06 01:19:33 +05:30
|
|
|
status = umount2(blockDevice, MNT_FORCE);
|
|
|
|
if (status != 0) {
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_error_msg_and_die("forced umount of %s failed!", blockDevice);
|
2000-05-06 01:19:33 +05:30
|
|
|
}
|
|
|
|
}
|
2000-01-13 12:08:14 +05:30
|
|
|
#endif
|
2001-12-21 04:43:26 +05:30
|
|
|
if (status != 0 && doRemount && errno == EBUSY) {
|
2000-02-09 01:28:47 +05:30
|
|
|
status = mount(blockDevice, name, NULL,
|
|
|
|
MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
|
|
|
|
if (status == 0) {
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_error_msg("%s busy - remounted read-only", blockDevice);
|
2000-02-09 01:28:47 +05:30
|
|
|
} else {
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_error_msg("Cannot remount %s read-only", blockDevice);
|
2000-02-09 01:28:47 +05:30
|
|
|
}
|
2000-02-07 10:59:42 +05:30
|
|
|
}
|
2000-02-09 01:28:47 +05:30
|
|
|
if (status == 0) {
|
2001-10-24 10:30:29 +05:30
|
|
|
#if defined CONFIG_FEATURE_MTAB_SUPPORT
|
2001-12-21 04:43:26 +05:30
|
|
|
if (useMtab)
|
2000-02-09 01:28:47 +05:30
|
|
|
erase_mtab(name);
|
1999-11-05 02:48:07 +05:30
|
|
|
#endif
|
2000-02-09 01:28:47 +05:30
|
|
|
return (TRUE);
|
|
|
|
}
|
|
|
|
return (FALSE);
|
2000-01-13 12:08:14 +05:30
|
|
|
}
|
1999-10-05 21:54:54 +05:30
|
|
|
|
2001-03-21 13:04:27 +05:30
|
|
|
static int umount_all(void)
|
1999-10-05 21:54:54 +05:30
|
|
|
{
|
2000-02-07 10:59:42 +05:30
|
|
|
int status = TRUE;
|
|
|
|
char *mountpt;
|
|
|
|
void *iter;
|
|
|
|
|
|
|
|
for (mountpt = mtab_first(&iter); mountpt; mountpt = mtab_next(&iter)) {
|
2000-02-09 09:46:43 +05:30
|
|
|
/* Never umount /proc on a umount -a */
|
|
|
|
if (strstr(mountpt, "proc")!= NULL)
|
|
|
|
continue;
|
2001-03-21 13:04:27 +05:30
|
|
|
if (!do_umount(mountpt)) {
|
2000-02-09 01:28:47 +05:30
|
|
|
/* Don't bother retrying the umount on busy devices */
|
|
|
|
if (errno == EBUSY) {
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_perror_msg("%s", mountpt);
|
2001-01-17 05:42:11 +05:30
|
|
|
status = FALSE;
|
2000-02-09 01:28:47 +05:30
|
|
|
continue;
|
|
|
|
}
|
2001-03-21 13:04:27 +05:30
|
|
|
if (!do_umount(mountpt)) {
|
2000-02-09 01:28:47 +05:30
|
|
|
printf("Couldn't umount %s on %s: %s\n",
|
|
|
|
mountpt, mtab_getinfo(mountpt, MTAB_GETDEVICE),
|
|
|
|
strerror(errno));
|
2001-01-17 05:42:11 +05:30
|
|
|
status = FALSE;
|
2000-02-09 01:28:47 +05:30
|
|
|
}
|
1999-10-05 21:54:54 +05:30
|
|
|
}
|
2000-02-09 01:28:47 +05:30
|
|
|
}
|
|
|
|
return (status);
|
1999-10-05 21:54:54 +05:30
|
|
|
}
|
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
extern int umount_main(int argc, char **argv)
|
1999-10-05 21:54:54 +05:30
|
|
|
{
|
2001-05-03 02:54:51 +05:30
|
|
|
char path[PATH_MAX];
|
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
if (argc < 2) {
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_show_usage();
|
2000-02-09 01:28:47 +05:30
|
|
|
}
|
2001-10-24 10:30:29 +05:30
|
|
|
#ifdef CONFIG_FEATURE_CLEAN_UP
|
2000-07-25 23:31:20 +05:30
|
|
|
atexit(mtab_free);
|
|
|
|
#endif
|
2000-02-09 01:28:47 +05:30
|
|
|
|
|
|
|
/* Parse any options */
|
|
|
|
while (--argc > 0 && **(++argv) == '-') {
|
|
|
|
while (*++(*argv))
|
|
|
|
switch (**argv) {
|
|
|
|
case 'a':
|
|
|
|
umountAll = TRUE;
|
|
|
|
break;
|
2001-10-24 10:30:29 +05:30
|
|
|
#if defined CONFIG_FEATURE_MOUNT_LOOP
|
2000-05-06 01:19:33 +05:30
|
|
|
case 'l':
|
2000-03-13 09:37:02 +05:30
|
|
|
freeLoop = FALSE;
|
|
|
|
break;
|
|
|
|
#endif
|
2001-10-24 10:30:29 +05:30
|
|
|
#ifdef CONFIG_FEATURE_MTAB_SUPPORT
|
2000-02-09 01:28:47 +05:30
|
|
|
case 'n':
|
|
|
|
useMtab = FALSE;
|
|
|
|
break;
|
2000-05-06 01:19:33 +05:30
|
|
|
#endif
|
2001-10-24 10:30:29 +05:30
|
|
|
#ifdef CONFIG_FEATURE_MOUNT_FORCE
|
2000-05-06 01:19:33 +05:30
|
|
|
case 'f':
|
|
|
|
doForce = TRUE;
|
|
|
|
break;
|
2000-02-07 10:59:42 +05:30
|
|
|
#endif
|
2000-02-09 01:28:47 +05:30
|
|
|
case 'r':
|
|
|
|
doRemount = TRUE;
|
|
|
|
break;
|
2000-04-04 23:44:25 +05:30
|
|
|
case 'v':
|
|
|
|
break; /* ignore -v */
|
2000-02-09 01:28:47 +05:30
|
|
|
default:
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_show_usage();
|
2000-02-09 01:28:47 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mtab_read();
|
2001-12-21 04:43:26 +05:30
|
|
|
if (umountAll) {
|
|
|
|
if (umount_all())
|
2000-12-01 08:25:13 +05:30
|
|
|
return EXIT_SUCCESS;
|
|
|
|
else
|
|
|
|
return EXIT_FAILURE;
|
2000-02-09 01:28:47 +05:30
|
|
|
}
|
2001-05-03 02:54:51 +05:30
|
|
|
if (realpath(*argv, path) == NULL)
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_perror_msg_and_die("%s", path);
|
2001-12-21 04:43:26 +05:30
|
|
|
if (do_umount(path))
|
2000-12-01 08:25:13 +05:30
|
|
|
return EXIT_SUCCESS;
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_perror_msg_and_die("%s", *argv);
|
1999-10-05 21:54:54 +05:30
|
|
|
}
|
1999-10-09 05:55:00 +05:30
|
|
|
|