c11f2e9d7e61772b3fe032c3d5991b1c6f0dd0c3
[platform/upstream/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, "rootfs") == 0) {
115                                         continue;
116                                 } else if (strcmp(cur->device, "/dev/root") == 0) {
117                                         /* Adjusts device to be the real root device,
118                                          * or leaves device alone if it can't find it */
119                                         cur->device = find_real_root_device_name(cur->device);
120                                 }
121 #endif
122                                 return cur->device;
123                         }
124                 }
125                 cur = cur->next;
126         }
127         return NULL;
128 }
129
130 static char *mtab_next(void **iter)
131 {
132         char *mp;
133
134         if (iter == NULL || *iter == NULL)
135                 return NULL;
136         mp = ((struct _mtab_entry_t *) (*iter))->mountpt;
137         *iter = (void *) ((struct _mtab_entry_t *) (*iter))->next;
138         return mp;
139 }
140
141 static char *mtab_first(void **iter)
142 {
143         struct _mtab_entry_t *mtab_iter;
144
145         if (!iter)
146                 return NULL;
147         mtab_iter = mtab_cache;
148         *iter = (void *) mtab_iter;
149         return mtab_next(iter);
150 }
151
152 /* Don't bother to clean up, since exit() does that 
153  * automagically, so we can save a few bytes */
154 #ifdef CONFIG_FEATURE_CLEAN_UP
155 static void mtab_free(void)
156 {
157         struct _mtab_entry_t *this, *next;
158
159         this = mtab_cache;
160         while (this) {
161                 next = this->next;
162                 free(this->device);
163                 free(this->mountpt);
164                 free(this);
165                 this = next;
166         }
167 }
168 #endif
169
170 static int do_umount(const char *name)
171 {
172         int status;
173         char *blockDevice = mtab_getinfo(name, MTAB_GETDEVICE);
174
175         if (blockDevice && strcmp(blockDevice, name) == 0)
176                 name = mtab_getinfo(blockDevice, MTAB_GETMOUNTPT);
177
178         status = umount(name);
179
180 #if defined CONFIG_FEATURE_MOUNT_LOOP
181         if (freeLoop && blockDevice != NULL && !strncmp("/dev/loop", blockDevice, 9))
182                 /* this was a loop device, delete it */
183                 del_loop(blockDevice);
184 #endif
185 #if defined CONFIG_FEATURE_MOUNT_FORCE
186         if (status != 0 && doForce) {
187                 status = umount2(blockDevice, MNT_FORCE);
188                 if (status != 0) {
189                         bb_error_msg_and_die("forced umount of %s failed!", blockDevice);
190                 }
191         }
192 #endif
193         if (status != 0 && doRemount && errno == EBUSY) {
194                 status = mount(blockDevice, name, NULL,
195                                            MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
196                 if (status == 0) {
197                         bb_error_msg("%s busy - remounted read-only", blockDevice);
198                 } else {
199                         bb_error_msg("Cannot remount %s read-only", blockDevice);
200                 }
201         }
202         if (status == 0) {
203 #if defined CONFIG_FEATURE_MTAB_SUPPORT
204                 if (useMtab)
205                         erase_mtab(name);
206 #endif
207                 return (TRUE);
208         }
209         return (FALSE);
210 }
211
212 static int umount_all(void)
213 {
214         int status = TRUE;
215         char *mountpt;
216         void *iter;
217
218         for (mountpt = mtab_first(&iter); mountpt; mountpt = mtab_next(&iter)) {
219                 /* Never umount /proc on a umount -a */
220                 if (strstr(mountpt, "proc")!= NULL)
221                         continue;
222                 if (!do_umount(mountpt)) {
223                         /* Don't bother retrying the umount on busy devices */
224                         if (errno == EBUSY) {
225                                 bb_perror_msg("%s", mountpt);
226                                 status = FALSE;
227                                 continue;
228                         }
229                         if (!do_umount(mountpt)) {
230                                 printf("Couldn't umount %s on %s: %s\n",
231                                            mountpt, mtab_getinfo(mountpt, MTAB_GETDEVICE),
232                                            strerror(errno));
233                                 status = FALSE;
234                         }
235                 }
236         }
237         return (status);
238 }
239
240 extern int umount_main(int argc, char **argv)
241 {
242         char path[PATH_MAX];
243
244         if (argc < 2) {
245                 bb_show_usage();
246         }
247 #ifdef CONFIG_FEATURE_CLEAN_UP
248         atexit(mtab_free);
249 #endif
250
251         /* Parse any options */
252         while (--argc > 0 && **(++argv) == '-') {
253                 while (*++(*argv))
254                         switch (**argv) {
255                         case 'a':
256                                 umountAll = TRUE;
257                                 break;
258 #if defined CONFIG_FEATURE_MOUNT_LOOP
259                         case 'l':
260                                 freeLoop = FALSE;
261                                 break;
262 #endif
263 #ifdef CONFIG_FEATURE_MTAB_SUPPORT
264                         case 'n':
265                                 useMtab = FALSE;
266                                 break;
267 #endif
268 #ifdef CONFIG_FEATURE_MOUNT_FORCE
269                         case 'f':
270                                 doForce = TRUE;
271                                 break;
272 #endif
273                         case 'r':
274                                 doRemount = TRUE;
275                                 break;
276                         case 'v':
277                                 break; /* ignore -v */
278                         default:
279                                 bb_show_usage();
280                         }
281         }
282
283         mtab_read();
284         if (umountAll) {
285                 if (umount_all())
286                         return EXIT_SUCCESS;
287                 else
288                         return EXIT_FAILURE;
289         }
290         if (realpath(*argv, path) == NULL)
291                 bb_perror_msg_and_die("%s", path);
292         if (do_umount(path))
293                 return EXIT_SUCCESS;
294         bb_perror_msg_and_die("%s", *argv);
295 }
296