source: MondoRescue/branches/2.2.9/mindi-busybox/util-linux/umount.c@ 2725

Last change on this file since 2725 was 2725, checked in by Bruno Cornec, 13 years ago
  • Update mindi-busybox to 1.18.3 to avoid problems with the tar command which is now failing on recent versions with busybox 1.7.3
File size: 5.1 KB
RevLine 
[821]1/* vi: set sw=4 ts=4: */
2/*
3 * Mini umount implementation for busybox
4 *
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6 * Copyright (C) 2005 by Rob Landley <rob@landley.net>
7 *
[2725]8 * Licensed under GPLv2, see file LICENSE in this source tree.
[821]9 */
10#include <mntent.h>
[2725]11#include <sys/mount.h>
[1765]12#include "libbb.h"
[821]13
[2725]14#if defined(__dietlibc__)
15// TODO: This does not belong here.
16/* 16.12.2006, Sampo Kellomaki (sampo@iki.fi)
17 * dietlibc-0.30 does not have implementation of getmntent_r() */
18static struct mntent *getmntent_r(FILE* stream, struct mntent* result,
19 char* buffer UNUSED_PARAM, int bufsize UNUSED_PARAM)
20{
21 struct mntent* ment = getmntent(stream);
22 return memcpy(result, ment, sizeof(*ment));
23}
24#endif
[821]25
[2725]26/* ignored: -v -d -t -i */
27#define OPTION_STRING "fldnra" "vdt:i"
28#define OPT_FORCE (1 << 0) // Same as MNT_FORCE
29#define OPT_LAZY (1 << 1) // Same as MNT_DETACH
30#define OPT_FREELOOP (1 << 2)
31#define OPT_NO_MTAB (1 << 3)
32#define OPT_REMOUNT (1 << 4)
33#define OPT_ALL (ENABLE_FEATURE_UMOUNT_ALL ? (1 << 5) : 0)
34
35int umount_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
36int umount_main(int argc UNUSED_PARAM, char **argv)
[821]37{
38 int doForce;
39 struct mntent me;
40 FILE *fp;
[2725]41 char *fstype = NULL;
[821]42 int status = EXIT_SUCCESS;
[1765]43 unsigned opt;
[821]44 struct mtab_list {
45 char *dir;
46 char *device;
47 struct mtab_list *next;
48 } *mtl, *m;
49
[1765]50 opt = getopt32(argv, OPTION_STRING, &fstype);
[2725]51 //argc -= optind;
[821]52 argv += optind;
53
[2725]54 // MNT_FORCE and MNT_DETACH (from linux/fs.h) must match
55 // OPT_FORCE and OPT_LAZY, otherwise this trick won't work:
[821]56 doForce = MAX((opt & OPT_FORCE), (opt & OPT_LAZY));
57
58 /* Get a list of mount points from mtab. We read them all in now mostly
59 * for umount -a (so we don't have to worry about the list changing while
60 * we iterate over it, or about getting stuck in a loop on the same failing
61 * entry. Notice that this also naturally reverses the list so that -a
62 * umounts the most recent entries first. */
[2725]63 m = mtl = NULL;
[821]64
[2725]65 // If we're umounting all, then m points to the start of the list and
66 // the argument list should be empty (which will match all).
[1765]67 fp = setmntent(bb_path_mtab_file, "r");
68 if (!fp) {
[821]69 if (opt & OPT_ALL)
[2725]70 bb_error_msg_and_die("can't open '%s'", bb_path_mtab_file);
[1765]71 } else {
[2725]72 while (getmntent_r(fp, &me, bb_common_bufsiz1, sizeof(bb_common_bufsiz1))) {
[1765]73 /* Match fstype if passed */
[2725]74 if (!match_fstype(&me, fstype))
[1765]75 continue;
[2725]76 m = xzalloc(sizeof(*m));
[1765]77 m->next = mtl;
78 m->device = xstrdup(me.mnt_fsname);
79 m->dir = xstrdup(me.mnt_dir);
80 mtl = m;
81 }
82 endmntent(fp);
[821]83 }
84
[2725]85 // If we're not umounting all, we need at least one argument.
[1765]86 if (!(opt & OPT_ALL) && !fstype) {
[2725]87 if (!argv[0])
88 bb_show_usage();
89 m = NULL;
[821]90 }
[1765]91
[821]92 // Loop through everything we're supposed to umount, and do so.
93 for (;;) {
94 int curstat;
[902]95 char *zapit = *argv;
[2725]96 char *path;
[821]97
98 // Do we already know what to umount this time through the loop?
[2725]99 if (m)
100 path = xstrdup(m->dir);
[821]101 // For umount -a, end of mtab means time to exit.
[2725]102 else if (opt & OPT_ALL)
103 break;
104 // Use command line argument (and look it up in mtab list)
[821]105 else {
[2725]106 if (!zapit)
107 break;
[902]108 argv++;
[2725]109 path = xmalloc_realpath(zapit);
110 if (path) {
111 for (m = mtl; m; m = m->next)
112 if (strcmp(path, m->dir) == 0 || strcmp(path, m->device) == 0)
113 break;
114 }
[821]115 }
[902]116 // If we couldn't find this sucker in /etc/mtab, punt by passing our
117 // command line argument straight to the umount syscall. Otherwise,
118 // umount the directory even if we were given the block device.
119 if (m) zapit = m->dir;
[821]120
121 // Let's ask the thing nicely to unmount.
[902]122 curstat = umount(zapit);
[821]123
124 // Force the unmount, if necessary.
[2725]125 if (curstat && doForce)
[902]126 curstat = umount2(zapit, doForce);
[821]127
128 // If still can't umount, maybe remount read-only?
129 if (curstat) {
[2725]130 if ((opt & OPT_REMOUNT) && errno == EBUSY && m) {
131 // Note! Even if we succeed here, later we should not
132 // free loop device or erase mtab entry!
133 const char *msg = "%s busy - remounted read-only";
134 curstat = mount(m->device, zapit, NULL, MS_REMOUNT|MS_RDONLY, NULL);
135 if (curstat) {
136 msg = "can't remount %s read-only";
137 status = EXIT_FAILURE;
138 }
139 bb_error_msg(msg, m->device);
140 } else {
141 status = EXIT_FAILURE;
142 bb_perror_msg("can't %sumount %s", (doForce ? "forcibly " : ""), zapit);
143 }
[902]144 } else {
[2725]145 // De-allocate the loop device. This ioctl should be ignored on
146 // any non-loop block devices.
147 if (ENABLE_FEATURE_MOUNT_LOOP && (opt & OPT_FREELOOP) && m)
[902]148 del_loop(m->device);
149 if (ENABLE_FEATURE_MTAB_SUPPORT && !(opt & OPT_NO_MTAB) && m)
[821]150 erase_mtab(m->dir);
151 }
[902]152
[821]153 // Find next matching mtab entry for -a or umount /dev
[902]154 // Note this means that "umount /dev/blah" will unmount all instances
155 // of /dev/blah, not just the most recent.
[2725]156 if (m) {
157 while ((m = m->next) != NULL)
158 // NB: if m is non-NULL, path is non-NULL as well
159 if ((opt & OPT_ALL) || strcmp(path, m->device) == 0)
160 break;
161 }
162 free(path);
[821]163 }
164
165 // Free mtab list if necessary
166 if (ENABLE_FEATURE_CLEAN_UP) {
167 while (mtl) {
168 m = mtl->next;
169 free(mtl->device);
170 free(mtl->dir);
171 free(mtl);
[1765]172 mtl = m;
[821]173 }
174 }
175
176 return status;
177}
Note: See TracBrowser for help on using the repository browser.