1d3e89301bc360861925b1e9404fcb140e62117a
[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,2000 by Lineo, inc. and Erik Andersen
6  * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  */
23
24 #include <limits.h>
25 #include <stdio.h>
26 #include <mntent.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include "busybox.h"
31
32 /* Teach libc5 about realpath -- it includes it but the 
33  * prototype is missing... */
34 #if (__GLIBC__ <= 2) && (__GLIBC_MINOR__ < 1)
35 extern char *realpath(const char *path, char *resolved_path);
36 #endif
37
38 static const int MNT_FORCE = 1;
39 static const int MS_MGC_VAL = 0xc0ed0000; /* Magic number indicatng "new" flags */
40 static const int MS_REMOUNT = 32;       /* Alter flags of a mounted FS.  */
41 static const int MS_RDONLY = 1; /* Mount read-only.  */
42
43 extern int mount (__const char *__special_file, __const char *__dir,
44                         __const char *__fstype, unsigned long int __rwflag,
45                         __const void *__data);
46 extern int umount (__const char *__special_file);
47 extern int umount2 (__const char *__special_file, int __flags);
48
49 struct _mtab_entry_t {
50         char *device;
51         char *mountpt;
52         struct _mtab_entry_t *next;
53 };
54
55 static struct _mtab_entry_t *mtab_cache = NULL;
56
57
58
59 #if defined CONFIG_FEATURE_MOUNT_FORCE
60 static int doForce = FALSE;
61 #endif
62 #if defined CONFIG_FEATURE_MOUNT_LOOP
63 static int freeLoop = TRUE;
64 #endif
65 #if defined CONFIG_FEATURE_MTAB_SUPPORT
66 static int useMtab = TRUE;
67 #endif
68 static int umountAll = FALSE;
69 static int doRemount = FALSE;
70
71
72
73 /* These functions are here because the getmntent functions do not appear
74  * to be re-entrant, which leads to all sorts of problems when we try to
75  * use them recursively - randolph
76  *
77  * TODO: Perhaps switch to using Glibc's getmntent_r
78  *        -Erik
79  */
80 static void mtab_read(void)
81 {
82         struct _mtab_entry_t *entry = NULL;
83         struct mntent *e;
84         FILE *fp;
85
86         if (mtab_cache != NULL)
87                 return;
88
89         if ((fp = setmntent(bb_path_mtab_file, "r")) == NULL) {
90                 bb_error_msg("Cannot open %s", bb_path_mtab_file);
91                 return;
92         }
93         while ((e = getmntent(fp))) {
94                 entry = xmalloc(sizeof(struct _mtab_entry_t));
95                 entry->device = strdup(e->mnt_fsname);
96                 entry->mountpt = strdup(e->mnt_dir);
97                 entry->next = mtab_cache;
98                 mtab_cache = entry;
99         }
100         endmntent(fp);
101 }
102
103 static char *mtab_getinfo(const char *match, const char which)
104 {
105         struct _mtab_entry_t *cur = mtab_cache;
106
107         while (cur) {
108                 if (strcmp(cur->mountpt, match) == 0 ||
109                         strcmp(cur->device, match) == 0) {
110                         if (which == MTAB_GETMOUNTPT) {
111                                 return cur->mountpt;
112                         } else {
113 #if !defined CONFIG_FEATURE_MTAB_SUPPORT
114                                 if (strcmp(cur->device, "/dev/root") == 0) {
115                                         /* Adjusts device to be the real root device,
116                                          * or leaves device alone if it can't find it */
117                                         cur->device = find_real_root_device_name(cur->device);
118                                 }
119 #endif
120                                 return cur->device;
121                         }
122                 }
123                 cur = cur->next;
124         }
125         return NULL;
126 }
127
128 static char *mtab_next(void **iter)
129 {
130         char *mp;
131
132         if (iter == NULL || *iter == NULL)
133                 return NULL;
134         mp = ((struct _mtab_entry_t *) (*iter))->mountpt;
135         *iter = (void *) ((struct _mtab_entry_t *) (*iter))->next;
136         return mp;
137 }
138
139 static char *mtab_first(void **iter)
140 {
141         struct _mtab_entry_t *mtab_iter;
142
143         if (!iter)
144                 return NULL;
145         mtab_iter = mtab_cache;
146         *iter = (void *) mtab_iter;
147         return mtab_next(iter);
148 }
149
150 /* Don't bother to clean up, since exit() does that 
151  * automagically, so we can save a few bytes */
152 #ifdef CONFIG_FEATURE_CLEAN_UP
153 static void mtab_free(void)
154 {
155         struct _mtab_entry_t *this, *next;
156
157         this = mtab_cache;
158         while (this) {
159                 next = this->next;
160                 free(this->device);
161                 free(this->mountpt);
162                 free(this);
163                 this = next;
164         }
165 }
166 #endif
167
168 static int do_umount(const char *name)
169 {
170         int status;
171         char *blockDevice = mtab_getinfo(name, MTAB_GETDEVICE);
172
173         if (blockDevice && strcmp(blockDevice, name) == 0)
174                 name = mtab_getinfo(blockDevice, MTAB_GETMOUNTPT);
175
176         status = umount(name);
177
178 #if defined CONFIG_FEATURE_MOUNT_LOOP
179         if (freeLoop && blockDevice != NULL && !strncmp("/dev/loop", blockDevice, 9))
180                 /* this was a loop device, delete it */
181                 del_loop(blockDevice);
182 #endif
183 #if defined CONFIG_FEATURE_MOUNT_FORCE
184         if (status != 0 && doForce) {
185                 status = umount2(blockDevice, MNT_FORCE);
186                 if (status != 0) {
187                         bb_error_msg_and_die("forced umount of %s failed!", blockDevice);
188                 }
189         }
190 #endif
191         if (status != 0 && doRemount && errno == EBUSY) {
192                 status = mount(blockDevice, name, NULL,
193                                            MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
194                 if (status == 0) {
195                         bb_error_msg("%s busy - remounted read-only", blockDevice);
196                 } else {
197                         bb_error_msg("Cannot remount %s read-only", blockDevice);
198                 }
199         }
200         if (status == 0) {
201 #if defined CONFIG_FEATURE_MTAB_SUPPORT
202                 if (useMtab)
203                         erase_mtab(name);
204 #endif
205                 return (TRUE);
206         }
207         return (FALSE);
208 }
209
210 static int umount_all(void)
211 {
212         int status = TRUE;
213         char *mountpt;
214         void *iter;
215
216         for (mountpt = mtab_first(&iter); mountpt; mountpt = mtab_next(&iter)) {
217                 /* Never umount /proc on a umount -a */
218                 if (strstr(mountpt, "proc")!= NULL)
219                         continue;
220                 if (!do_umount(mountpt)) {
221                         /* Don't bother retrying the umount on busy devices */
222                         if (errno == EBUSY) {
223                                 bb_perror_msg("%s", mountpt);
224                                 status = FALSE;
225                                 continue;
226                         }
227                         if (!do_umount(mountpt)) {
228                                 printf("Couldn't umount %s on %s: %s\n",
229                                            mountpt, mtab_getinfo(mountpt, MTAB_GETDEVICE),
230                                            strerror(errno));
231                                 status = FALSE;
232                         }
233                 }
234         }
235         return (status);
236 }
237
238 extern int umount_main(int argc, char **argv)
239 {
240         char path[PATH_MAX];
241
242         if (argc < 2) {
243                 bb_show_usage();
244         }
245 #ifdef CONFIG_FEATURE_CLEAN_UP
246         atexit(mtab_free);
247 #endif
248
249         /* Parse any options */
250         while (--argc > 0 && **(++argv) == '-') {
251                 while (*++(*argv))
252                         switch (**argv) {
253                         case 'a':
254                                 umountAll = TRUE;
255                                 break;
256 #if defined CONFIG_FEATURE_MOUNT_LOOP
257                         case 'l':
258                                 freeLoop = FALSE;
259                                 break;
260 #endif
261 #ifdef CONFIG_FEATURE_MTAB_SUPPORT
262                         case 'n':
263                                 useMtab = FALSE;
264                                 break;
265 #endif
266 #ifdef CONFIG_FEATURE_MOUNT_FORCE
267                         case 'f':
268                                 doForce = TRUE;
269                                 break;
270 #endif
271                         case 'r':
272                                 doRemount = TRUE;
273                                 break;
274                         case 'v':
275                                 break; /* ignore -v */
276                         default:
277                                 bb_show_usage();
278                         }
279         }
280
281         mtab_read();
282         if (umountAll) {
283                 if (umount_all())
284                         return EXIT_SUCCESS;
285                 else
286                         return EXIT_FAILURE;
287         }
288         if (realpath(*argv, path) == NULL)
289                 bb_perror_msg_and_die("%s", path);
290         if (do_umount(path))
291                 return EXIT_SUCCESS;
292         bb_perror_msg_and_die("%s", *argv);
293 }
294