Enable more tools in config.maemo
[busybox4maemo] / 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 <getopt.h>
13 #include "libbb.h"
14
15 /* ignored: -v -d -t -i */
16 #define OPTION_STRING           "fldnra" "vdt:i"
17 #define OPT_FORCE               (1 << 0)
18 #define OPT_LAZY                (1 << 1)
19 #define OPT_FREELOOP            (1 << 2)
20 #define OPT_NO_MTAB             (1 << 3)
21 #define OPT_REMOUNT             (1 << 4)
22 #define OPT_ALL                 (ENABLE_FEATURE_UMOUNT_ALL ? (1 << 5) : 0)
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
29 int umount_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
30 int umount_main(int argc ATTRIBUTE_UNUSED, char **argv)
31 {
32         int doForce;
33         char *const path = xmalloc(PATH_MAX + 2); /* to save stack */
34         struct mntent me;
35         FILE *fp;
36         char *fstype = NULL;
37         int status = EXIT_SUCCESS;
38         unsigned opt;
39         struct mtab_list {
40                 char *dir;
41                 char *device;
42                 struct mtab_list *next;
43         } *mtl, *m;
44
45         opt = getopt32(argv, OPTION_STRING, &fstype);
46         //argc -= optind;
47         argv += optind;
48         doForce = MAX((opt & OPT_FORCE), (opt & OPT_LAZY));
49
50         /* Get a list of mount points from mtab.  We read them all in now mostly
51          * for umount -a (so we don't have to worry about the list changing while
52          * we iterate over it, or about getting stuck in a loop on the same failing
53          * entry.  Notice that this also naturally reverses the list so that -a
54          * umounts the most recent entries first. */
55         m = mtl = NULL;
56
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         fp = setmntent(bb_path_mtab_file, "r");
60         if (!fp) {
61                 if (opt & OPT_ALL)
62                         bb_error_msg_and_die("cannot open %s", bb_path_mtab_file);
63         } else {
64                 while (getmntent_r(fp, &me, path, PATH_MAX)) {
65                         /* Match fstype if passed */
66                         if (fstype && match_fstype(&me, fstype))
67                                 continue;
68                         m = xmalloc(sizeof(struct mtab_list));
69                         m->next = mtl;
70                         m->device = xstrdup(me.mnt_fsname);
71                         m->dir = xstrdup(me.mnt_dir);
72                         mtl = m;
73                 }
74                 endmntent(fp);
75         }
76
77         // If we're not umounting all, we need at least one argument.
78         if (!(opt & OPT_ALL) && !fstype) {
79                 if (!argv[0])
80                         bb_show_usage();
81                 m = NULL;
82         }
83
84         // Loop through everything we're supposed to umount, and do so.
85         for (;;) {
86                 int curstat;
87                 char *zapit = *argv;
88
89                 // Do we already know what to umount this time through the loop?
90                 if (m)
91                         safe_strncpy(path, m->dir, PATH_MAX);
92                 // For umount -a, end of mtab means time to exit.
93                 else if (opt & OPT_ALL)
94                         break;
95                 // Use command line argument (and look it up in mtab list)
96                 else {
97                         if (!zapit)
98                                 break;
99                         argv++;
100                         realpath(zapit, path);
101                         for (m = mtl; m; m = m->next)
102                                 if (!strcmp(path, m->dir) || !strcmp(path, m->device))
103                                         break;
104                 }
105                 // If we couldn't find this sucker in /etc/mtab, punt by passing our
106                 // command line argument straight to the umount syscall.  Otherwise,
107                 // umount the directory even if we were given the block device.
108                 if (m) zapit = m->dir;
109
110                 // Let's ask the thing nicely to unmount.
111                 curstat = umount(zapit);
112
113                 // Force the unmount, if necessary.
114                 if (curstat && doForce)
115                         curstat = umount2(zapit, doForce);
116
117                 // If still can't umount, maybe remount read-only?
118                 if (curstat) {
119                         if ((opt & OPT_REMOUNT) && errno == EBUSY && m) {
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                         }
133                 } else {
134                         // De-allocate the loop device.  This ioctl should be ignored on
135                         // any non-loop block devices.
136                         if (ENABLE_FEATURE_MOUNT_LOOP && (opt & OPT_FREELOOP) && m)
137                                 del_loop(m->device);
138                         if (ENABLE_FEATURE_MTAB_SUPPORT && !(opt & OPT_NO_MTAB) && m)
139                                 erase_mtab(m->dir);
140                 }
141
142                 // Find next matching mtab entry for -a or umount /dev
143                 // Note this means that "umount /dev/blah" will unmount all instances
144                 // of /dev/blah, not just the most recent.
145                 if (m) while ((m = m->next) != NULL)
146                         if ((opt & OPT_ALL) || !strcmp(path, m->device))
147                                 break;
148         }
149
150         // Free mtab list if necessary
151         if (ENABLE_FEATURE_CLEAN_UP) {
152                 while (mtl) {
153                         m = mtl->next;
154                         free(mtl->device);
155                         free(mtl->dir);
156                         free(mtl);
157                         mtl = m;
158                 }
159                 free(path);
160         }
161
162         return status;
163 }