34c979e0c12e47c090630d54fa638f7ca8fa25b7
[oweals/busybox.git] / util-linux / umount.c
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  *
8  * Licensed under GPL version 2, see file LICENSE in this tarball for details.
9  */
10
11 #include <mntent.h>
12 #include "libbb.h"
13
14 /* ignored: -v -d -t -i */
15 #define OPTION_STRING           "fldnra" "vdt:i"
16 #define OPT_FORCE               (1 << 0)
17 #define OPT_LAZY                (1 << 1)
18 #define OPT_FREELOOP            (1 << 2)
19 #define OPT_NO_MTAB             (1 << 3)
20 #define OPT_REMOUNT             (1 << 4)
21 #define OPT_ALL                 (ENABLE_FEATURE_UMOUNT_ALL ? (1 << 5) : 0)
22
23 // These constants from linux/fs.h must match OPT_FORCE and OPT_LAZY,
24 // otherwise "doForce" trick below won't work!
25 //#define MNT_FORCE  0x00000001 /* Attempt to forcibly umount */
26 //#define MNT_DETACH 0x00000002 /* Just detach from the tree */
27
28 int umount_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
29 int umount_main(int argc ATTRIBUTE_UNUSED, char **argv)
30 {
31         int doForce;
32         char *const path = xmalloc(PATH_MAX + 2); /* to save stack */
33         struct mntent me;
34         FILE *fp;
35         char *fstype = NULL;
36         int status = EXIT_SUCCESS;
37         unsigned opt;
38         struct mtab_list {
39                 char *dir;
40                 char *device;
41                 struct mtab_list *next;
42         } *mtl, *m;
43
44         opt = getopt32(argv, OPTION_STRING, &fstype);
45         //argc -= optind;
46         argv += optind;
47         doForce = MAX((opt & OPT_FORCE), (opt & OPT_LAZY));
48
49         /* Get a list of mount points from mtab.  We read them all in now mostly
50          * for umount -a (so we don't have to worry about the list changing while
51          * 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          * umounts the most recent entries first. */
54         m = mtl = NULL;
55
56         // If we're umounting all, then m points to the start of the list and
57         // the argument list should be empty (which will match all).
58         fp = setmntent(bb_path_mtab_file, "r");
59         if (!fp) {
60                 if (opt & OPT_ALL)
61                         bb_error_msg_and_die("cannot open %s", bb_path_mtab_file);
62         } else {
63                 while (getmntent_r(fp, &me, path, PATH_MAX)) {
64                         /* Match fstype if passed */
65                         if (fstype && match_fstype(&me, fstype))
66                                 continue;
67                         m = xmalloc(sizeof(struct mtab_list));
68                         m->next = mtl;
69                         m->device = xstrdup(me.mnt_fsname);
70                         m->dir = xstrdup(me.mnt_dir);
71                         mtl = m;
72                 }
73                 endmntent(fp);
74         }
75
76         // If we're not umounting all, we need at least one argument.
77         if (!(opt & OPT_ALL) && !fstype) {
78                 if (!argv[0])
79                         bb_show_usage();
80                 m = NULL;
81         }
82
83         // Loop through everything we're supposed to umount, and do so.
84         for (;;) {
85                 int curstat;
86                 char *zapit = *argv;
87
88                 // Do we already know what to umount this time through the loop?
89                 if (m)
90                         safe_strncpy(path, m->dir, PATH_MAX);
91                 // For umount -a, end of mtab means time to exit.
92                 else if (opt & OPT_ALL)
93                         break;
94                 // Use command line argument (and look it up in mtab list)
95                 else {
96                         if (!zapit)
97                                 break;
98                         argv++;
99                         realpath(zapit, path);
100                         for (m = mtl; m; m = m->next)
101                                 if (!strcmp(path, m->dir) || !strcmp(path, m->device))
102                                         break;
103                 }
104                 // If we couldn't find this sucker in /etc/mtab, punt by passing our
105                 // command line argument straight to the umount syscall.  Otherwise,
106                 // umount the directory even if we were given the block device.
107                 if (m) zapit = m->dir;
108
109                 // Let's ask the thing nicely to unmount.
110                 curstat = umount(zapit);
111
112                 // Force the unmount, if necessary.
113                 if (curstat && doForce)
114                         curstat = umount2(zapit, doForce);
115
116                 // If still can't umount, maybe remount read-only?
117                 if (curstat) {
118                         if ((opt & OPT_REMOUNT) && errno == EBUSY && m) {
119                                 // Note! Even if we succeed here, later we should not
120                                 // free loop device or erase mtab entry!
121                                 const char *msg = "%s busy - remounted read-only";
122                                 curstat = mount(m->device, zapit, NULL, MS_REMOUNT|MS_RDONLY, NULL);
123                                 if (curstat) {
124                                         msg = "cannot remount %s read-only";
125                                         status = EXIT_FAILURE;
126                                 }
127                                 bb_error_msg(msg, m->device);
128                         } else {
129                                 status = EXIT_FAILURE;
130                                 bb_perror_msg("cannot %sumount %s", (doForce ? "forcibly " : ""), zapit);
131                         }
132                 } else {
133                         // De-allocate the loop device.  This ioctl should be ignored on
134                         // any non-loop block devices.
135                         if (ENABLE_FEATURE_MOUNT_LOOP && (opt & OPT_FREELOOP) && m)
136                                 del_loop(m->device);
137                         if (ENABLE_FEATURE_MTAB_SUPPORT && !(opt & OPT_NO_MTAB) && m)
138                                 erase_mtab(m->dir);
139                 }
140
141                 // Find next matching mtab entry for -a or umount /dev
142                 // Note this means that "umount /dev/blah" will unmount all instances
143                 // of /dev/blah, not just the most recent.
144                 if (m) while ((m = m->next) != NULL)
145                         if ((opt & OPT_ALL) || !strcmp(path, m->device))
146                                 break;
147         }
148
149         // Free mtab list if necessary
150         if (ENABLE_FEATURE_CLEAN_UP) {
151                 while (mtl) {
152                         m = mtl->next;
153                         free(mtl->device);
154                         free(mtl->dir);
155                         free(mtl);
156                         mtl = m;
157                 }
158                 free(path);
159         }
160
161         return status;
162 }