aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/usage.h2
-rw-r--r--util-linux/umount.c73
2 files changed, 38 insertions, 37 deletions
diff --git a/include/usage.h b/include/usage.h
index df15dbb46..061cb5568 100644
--- a/include/usage.h
+++ b/include/usage.h
@@ -3996,7 +3996,7 @@ USE_FEATURE_RUN_PARTS_FANCY("\n -l Prints names of all matching files even when
3996 "\n -l Lazy umount (detach filesystem)" \ 3996 "\n -l Lazy umount (detach filesystem)" \
3997 "\n -f Force umount (i.e., unreachable NFS server)" \ 3997 "\n -f Force umount (i.e., unreachable NFS server)" \
3998 USE_FEATURE_MOUNT_LOOP( \ 3998 USE_FEATURE_MOUNT_LOOP( \
3999 "\n -D Do not free loop device (if a loop device has been used)") 3999 "\n -d Free loop device if it has been used")
4000#define umount_example_usage \ 4000#define umount_example_usage \
4001 "$ umount /dev/hdc1\n" 4001 "$ umount /dev/hdc1\n"
4002 4002
diff --git a/util-linux/umount.c b/util-linux/umount.c
index 2a25c3f52..3f7c0abbd 100644
--- a/util-linux/umount.c
+++ b/util-linux/umount.c
@@ -13,14 +13,19 @@
13#include "libbb.h" 13#include "libbb.h"
14 14
15/* ignored: -v -d -t -i */ 15/* ignored: -v -d -t -i */
16#define OPTION_STRING "flDnra" "vdt:i" 16#define OPTION_STRING "fldnra" "vdt:i"
17#define OPT_FORCE (1 << 0) 17#define OPT_FORCE (1 << 0)
18#define OPT_LAZY (1 << 1) 18#define OPT_LAZY (1 << 1)
19#define OPT_DONTFREELOOP (1 << 2) 19#define OPT_FREELOOP (1 << 2)
20#define OPT_NO_MTAB (1 << 3) 20#define OPT_NO_MTAB (1 << 3)
21#define OPT_REMOUNT (1 << 4) 21#define OPT_REMOUNT (1 << 4)
22#define OPT_ALL (ENABLE_FEATURE_UMOUNT_ALL ? (1 << 5) : 0) 22#define OPT_ALL (ENABLE_FEATURE_UMOUNT_ALL ? (1 << 5) : 0)
23 23
24// These constants from linux/fs.h must match OPT_FORCE and OPT_LAZY,
25// otherwise "doForce" trick below won't work!
26//#define MNT_FORCE 0x00000001 /* Attempt to forcibly umount */
27//#define MNT_DETACH 0x00000002 /* Just detach from the tree */
28
24int umount_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 29int umount_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
25int umount_main(int argc, char **argv) 30int umount_main(int argc, char **argv)
26{ 31{
@@ -28,7 +33,7 @@ int umount_main(int argc, char **argv)
28 char *const path = xmalloc(PATH_MAX + 2); /* to save stack */ 33 char *const path = xmalloc(PATH_MAX + 2); /* to save stack */
29 struct mntent me; 34 struct mntent me;
30 FILE *fp; 35 FILE *fp;
31 char *fstype = 0; 36 char *fstype = NULL;
32 int status = EXIT_SUCCESS; 37 int status = EXIT_SUCCESS;
33 unsigned opt; 38 unsigned opt;
34 struct mtab_list { 39 struct mtab_list {
@@ -37,13 +42,9 @@ int umount_main(int argc, char **argv)
37 struct mtab_list *next; 42 struct mtab_list *next;
38 } *mtl, *m; 43 } *mtl, *m;
39 44
40 /* Parse any options */
41
42 opt = getopt32(argv, OPTION_STRING, &fstype); 45 opt = getopt32(argv, OPTION_STRING, &fstype);
43 46 //argc -= optind;
44 argc -= optind;
45 argv += optind; 47 argv += optind;
46
47 doForce = MAX((opt & OPT_FORCE), (opt & OPT_LAZY)); 48 doForce = MAX((opt & OPT_FORCE), (opt & OPT_LAZY));
48 49
49 /* Get a list of mount points from mtab. We read them all in now mostly 50 /* Get a list of mount points from mtab. We read them all in now mostly
@@ -51,12 +52,10 @@ int umount_main(int argc, char **argv)
51 * we iterate over it, or about getting stuck in a loop on the same failing 52 * we iterate over it, or about getting stuck in a loop on the same failing
52 * entry. Notice that this also naturally reverses the list so that -a 53 * entry. Notice that this also naturally reverses the list so that -a
53 * umounts the most recent entries first. */ 54 * umounts the most recent entries first. */
55 m = mtl = NULL;
54 56
55 m = mtl = 0; 57 // If we're umounting all, then m points to the start of the list and
56 58 // the argument list should be empty (which will match all).
57 /* If we're umounting all, then m points to the start of the list and
58 * the argument list should be empty (which will match all). */
59
60 fp = setmntent(bb_path_mtab_file, "r"); 59 fp = setmntent(bb_path_mtab_file, "r");
61 if (!fp) { 60 if (!fp) {
62 if (opt & OPT_ALL) 61 if (opt & OPT_ALL)
@@ -75,11 +74,11 @@ int umount_main(int argc, char **argv)
75 endmntent(fp); 74 endmntent(fp);
76 } 75 }
77 76
78 /* If we're not umounting all, we need at least one argument. */ 77 // If we're not umounting all, we need at least one argument.
79 if (!(opt & OPT_ALL) && !fstype) { 78 if (!(opt & OPT_ALL) && !fstype) {
80 m = 0; 79 if (!argv[0])
81 if (!argc)
82 bb_show_usage(); 80 bb_show_usage();
81 m = NULL;
83 } 82 }
84 83
85 // Loop through everything we're supposed to umount, and do so. 84 // Loop through everything we're supposed to umount, and do so.
@@ -93,10 +92,10 @@ int umount_main(int argc, char **argv)
93 // For umount -a, end of mtab means time to exit. 92 // For umount -a, end of mtab means time to exit.
94 else if (opt & OPT_ALL) 93 else if (opt & OPT_ALL)
95 break; 94 break;
96 // Get next command line argument (and look it up in mtab list) 95 // Use command line argument (and look it up in mtab list)
97 else if (!argc--)
98 break;
99 else { 96 else {
97 if (!zapit)
98 break;
100 argv++; 99 argv++;
101 realpath(zapit, path); 100 realpath(zapit, path);
102 for (m = mtl; m; m = m->next) 101 for (m = mtl; m; m = m->next)
@@ -112,26 +111,29 @@ int umount_main(int argc, char **argv)
112 curstat = umount(zapit); 111 curstat = umount(zapit);
113 112
114 // Force the unmount, if necessary. 113 // Force the unmount, if necessary.
115 if (curstat && doForce) { 114 if (curstat && doForce)
116 curstat = umount2(zapit, doForce); 115 curstat = umount2(zapit, doForce);
117 if (curstat)
118 bb_error_msg("forced umount of %s failed!", zapit);
119 }
120 116
121 // If still can't umount, maybe remount read-only? 117 // If still can't umount, maybe remount read-only?
122 if (curstat && (opt & OPT_REMOUNT) && errno == EBUSY && m) {
123 curstat = mount(m->device, zapit, NULL, MS_REMOUNT|MS_RDONLY, NULL);
124 bb_error_msg(curstat ? "cannot remount %s read-only" :
125 "%s busy - remounted read-only", m->device);
126 }
127
128 if (curstat) { 118 if (curstat) {
129 status = EXIT_FAILURE; 119 if ((opt & OPT_REMOUNT) && errno == EBUSY && m) {
130 bb_perror_msg("cannot umount %s", zapit); 120 // Note! Even if we succeed here, later we should not
121 // free loop device or erase mtab entry!
122 const char *msg = "%s busy - remounted read-only";
123 curstat = mount(m->device, zapit, NULL, MS_REMOUNT|MS_RDONLY, NULL);
124 if (curstat) {
125 msg = "cannot remount %s read-only";
126 status = EXIT_FAILURE;
127 }
128 bb_error_msg(msg, m->device);
129 } else {
130 status = EXIT_FAILURE;
131 bb_perror_msg("cannot %sumount %s", (doForce ? "forcibly " : ""), zapit);
132 }
131 } else { 133 } else {
132 /* De-allocate the loop device. This ioctl should be ignored on 134 // De-allocate the loop device. This ioctl should be ignored on
133 * any non-loop block devices. */ 135 // any non-loop block devices.
134 if (ENABLE_FEATURE_MOUNT_LOOP && !(opt & OPT_DONTFREELOOP) && m) 136 if (ENABLE_FEATURE_MOUNT_LOOP && (opt & OPT_FREELOOP) && m)
135 del_loop(m->device); 137 del_loop(m->device);
136 if (ENABLE_FEATURE_MTAB_SUPPORT && !(opt & OPT_NO_MTAB) && m) 138 if (ENABLE_FEATURE_MTAB_SUPPORT && !(opt & OPT_NO_MTAB) && m)
137 erase_mtab(m->dir); 139 erase_mtab(m->dir);
@@ -140,13 +142,12 @@ int umount_main(int argc, char **argv)
140 // Find next matching mtab entry for -a or umount /dev 142 // Find next matching mtab entry for -a or umount /dev
141 // Note this means that "umount /dev/blah" will unmount all instances 143 // Note this means that "umount /dev/blah" will unmount all instances
142 // of /dev/blah, not just the most recent. 144 // of /dev/blah, not just the most recent.
143 while (m && (m = m->next)) 145 if (m) while ((m = m->next) != NULL)
144 if ((opt & OPT_ALL) || !strcmp(path, m->device)) 146 if ((opt & OPT_ALL) || !strcmp(path, m->device))
145 break; 147 break;
146 } 148 }
147 149
148 // Free mtab list if necessary 150 // Free mtab list if necessary
149
150 if (ENABLE_FEATURE_CLEAN_UP) { 151 if (ENABLE_FEATURE_CLEAN_UP) {
151 while (mtl) { 152 while (mtl) {
152 m = mtl->next; 153 m = mtl->next;