cp: make "cp file /dev/node" special case; explained in comments
[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 <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_DONTFREELOOP        (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 int umount_main(int argc, char **argv);
25 int umount_main(int argc, char **argv)
26 {
27         int doForce;
28         char path[2*PATH_MAX];
29         struct mntent me;
30         FILE *fp;
31         char *fstype = 0;
32         int status = EXIT_SUCCESS;
33         unsigned opt;
34         struct mtab_list {
35                 char *dir;
36                 char *device;
37                 struct mtab_list *next;
38         } *mtl, *m;
39
40         /* Parse any options */
41
42         opt = getopt32(argv, OPTION_STRING, &fstype);
43
44         argc -= optind;
45         argv += optind;
46
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
55         m = mtl = 0;
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
60         fp = setmntent(bb_path_mtab_file, "r");
61         if (!fp) {
62                 if (opt & OPT_ALL)
63                         bb_error_msg_and_die("cannot open %s", bb_path_mtab_file);
64         } else {
65                 while (getmntent_r(fp, &me, path, sizeof(path))) {
66                         /* Match fstype if passed */
67                         if (fstype && match_fstype(&me, fstype))
68                                 continue;
69                         m = xmalloc(sizeof(struct mtab_list));
70                         m->next = mtl;
71                         m->device = xstrdup(me.mnt_fsname);
72                         m->dir = xstrdup(me.mnt_dir);
73                         mtl = m;
74                 }
75                 endmntent(fp);
76         }
77
78         /* If we're not umounting all, we need at least one argument. */
79         if (!(opt & OPT_ALL) && !fstype) {
80                 m = 0;
81                 if (!argc)
82                         bb_show_usage();
83         }
84
85         // Loop through everything we're supposed to umount, and do so.
86         for (;;) {
87                 int curstat;
88                 char *zapit = *argv;
89
90                 // Do we already know what to umount this time through the loop?
91                 if (m)
92                         safe_strncpy(path, m->dir, PATH_MAX);
93                 // For umount -a, end of mtab means time to exit.
94                 else if (opt & OPT_ALL)
95                         break;
96                 // Get next command line argument (and look it up in mtab list)
97                 else if (!argc--)
98                         break;
99                 else {
100                         argv++;
101                         realpath(zapit, path);
102                         for (m = mtl; m; m = m->next)
103                                 if (!strcmp(path, m->dir) || !strcmp(path, m->device))
104                                         break;
105                 }
106                 // If we couldn't find this sucker in /etc/mtab, punt by passing our
107                 // command line argument straight to the umount syscall.  Otherwise,
108                 // umount the directory even if we were given the block device.
109                 if (m) zapit = m->dir;
110
111                 // Let's ask the thing nicely to unmount.
112                 curstat = umount(zapit);
113
114                 // Force the unmount, if necessary.
115                 if (curstat && doForce) {
116                         curstat = umount2(zapit, doForce);
117                         if (curstat)
118                                 bb_error_msg("forced umount of %s failed!", zapit);
119                 }
120
121                 // 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) {
129                         status = EXIT_FAILURE;
130                         bb_perror_msg("cannot umount %s", zapit);
131                 } else {
132                         /* De-allocate the loop device.  This ioctl should be ignored on
133                          * any non-loop block devices. */
134                         if (ENABLE_FEATURE_MOUNT_LOOP && !(opt & OPT_DONTFREELOOP) && m)
135                                 del_loop(m->device);
136                         if (ENABLE_FEATURE_MTAB_SUPPORT && !(opt & OPT_NO_MTAB) && m)
137                                 erase_mtab(m->dir);
138                 }
139
140                 // Find next matching mtab entry for -a or umount /dev
141                 // Note this means that "umount /dev/blah" will unmount all instances
142                 // of /dev/blah, not just the most recent.
143                 while (m && (m = m->next))
144                         if ((opt & OPT_ALL) || !strcmp(path, m->device))
145                                 break;
146         }
147
148         // Free mtab list if necessary
149
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         }
159
160         return status;
161 }