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