1b250fd3a3ddbc7bd2267e1a8ea8d48afea57d11
[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 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 "internal.h"
26 #include <stdio.h>
27 #include <mntent.h>
28 #include <errno.h>
29
30
31 #define MNT_FORCE               1
32 #define MS_MGC_VAL              0xc0ed0000 /* Magic number indicatng "new" flags */
33 #define MS_REMOUNT              32      /* Alter flags of a mounted FS.  */
34 #define MS_RDONLY               1       /* Mount read-only.  */
35
36 extern int mount (__const char *__special_file, __const char *__dir,
37                         __const char *__fstype, unsigned long int __rwflag,
38                         __const void *__data);
39 extern int umount (__const char *__special_file);
40 extern int umount2 (__const char *__special_file, int __flags);
41
42
43
44 static const char umount_usage[] =
45         "umount [flags] filesystem|directory\n"
46 #ifndef BB_FEATURE_TRIVIAL_HELP
47         "Unmount file systems\n"
48         "\nFlags:\n" "\t-a:\tUnmount all file systems"
49 #ifdef BB_MTAB
50         " in /etc/mtab\n\t-n:\tDon't erase /etc/mtab entries\n"
51 #else
52         "\n"
53 #endif
54         "\t-r:\tTry to remount devices as read-only if mount is busy\n"
55 #if defined BB_FEATURE_MOUNT_FORCE
56         "\t-f:\tForce filesystem umount (i.e. unreachable NFS server)\n"
57 #endif
58 #if defined BB_FEATURE_MOUNT_LOOP
59         "\t-l:\tDo not free loop device (if a loop device has been used)\n"
60 #endif
61 #endif
62 ;
63
64 struct _mtab_entry_t {
65         char *device;
66         char *mountpt;
67         struct _mtab_entry_t *next;
68 };
69
70 static struct _mtab_entry_t *mtab_cache = NULL;
71
72
73
74 #if defined BB_FEATURE_MOUNT_FORCE
75 static int doForce = FALSE;
76 #endif
77 #if defined BB_FEATURE_MOUNT_LOOP
78 static int freeLoop = TRUE;
79 #endif
80 static int useMtab = TRUE;
81 static int umountAll = FALSE;
82 static int doRemount = FALSE;
83 extern const char mtab_file[];  /* Defined in utility.c */
84
85
86
87 /* These functions are here because the getmntent functions do not appear
88  * to be re-entrant, which leads to all sorts of problems when we try to
89  * use them recursively - randolph
90  *
91  * TODO: Perhaps switch to using Glibc's getmntent_r
92  *        -Erik
93  */
94 void mtab_read(void)
95 {
96         struct _mtab_entry_t *entry = NULL;
97         struct mntent *e;
98         FILE *fp;
99
100         if (mtab_cache != NULL)
101                 return;
102
103         if ((fp = setmntent(mtab_file, "r")) == NULL) {
104                 errorMsg("Cannot open %s\n", mtab_file);
105                 return;
106         }
107         while ((e = getmntent(fp))) {
108                 entry = xmalloc(sizeof(struct _mtab_entry_t));
109                 entry->device = strdup(e->mnt_fsname);
110                 entry->mountpt = strdup(e->mnt_dir);
111                 entry->next = mtab_cache;
112                 mtab_cache = entry;
113         }
114         endmntent(fp);
115 }
116
117 char *mtab_getinfo(const char *match, const char which)
118 {
119         struct _mtab_entry_t *cur = mtab_cache;
120
121         while (cur) {
122                 if (strcmp(cur->mountpt, match) == 0 ||
123                         strcmp(cur->device, match) == 0) {
124                         if (which == MTAB_GETMOUNTPT) {
125                                 return cur->mountpt;
126                         } else {
127 #if !defined BB_MTAB
128                                 if (strcmp(cur->device, "/dev/root") == 0) {
129                                         /* Adjusts device to be the real root device,
130                                          * or leaves device alone if it can't find it */
131                                         find_real_root_device_name( cur->device);
132                                         return ( cur->device);
133                                 }
134 #endif
135                                 return cur->device;
136                         }
137                 }
138                 cur = cur->next;
139         }
140         return NULL;
141 }
142
143 char *mtab_first(void **iter)
144 {
145         struct _mtab_entry_t *mtab_iter;
146
147         if (!iter)
148                 return NULL;
149         mtab_iter = mtab_cache;
150         *iter = (void *) mtab_iter;
151         return mtab_next(iter);
152 }
153
154 char *mtab_next(void **iter)
155 {
156         char *mp;
157
158         if (iter == NULL || *iter == NULL)
159                 return NULL;
160         mp = ((struct _mtab_entry_t *) (*iter))->mountpt;
161         *iter = (void *) ((struct _mtab_entry_t *) (*iter))->next;
162         return mp;
163 }
164
165 /* Don't bother to clean up, since exit() does that 
166  * automagically, so we can save a few bytes */
167 #if 0
168 void mtab_free(void)
169 {
170         struct _mtab_entry_t *this, *next;
171
172         this = mtab_cache;
173         while (this) {
174                 next = this->next;
175                 if (this->device)
176                         free(this->device);
177                 if (this->mountpt)
178                         free(this->mountpt);
179                 free(this);
180                 this = next;
181         }
182 }
183 #endif
184
185 static int do_umount(const char *name, int useMtab)
186 {
187         int status;
188         char *blockDevice = mtab_getinfo(name, MTAB_GETDEVICE);
189
190         if (blockDevice && strcmp(blockDevice, name) == 0)
191                 name = mtab_getinfo(blockDevice, MTAB_GETMOUNTPT);
192
193         status = umount(name);
194
195 #if defined BB_FEATURE_MOUNT_LOOP
196         if (freeLoop == TRUE && blockDevice != NULL && !strncmp("/dev/loop", blockDevice, 9))
197                 /* this was a loop device, delete it */
198                 del_loop(blockDevice);
199 #endif
200 #if defined BB_FEATURE_MOUNT_FORCE
201         if (status != 0 && doForce == TRUE) {
202                 status = umount2(blockDevice, MNT_FORCE);
203                 if (status != 0) {
204                         fatalError("forced umount of %s failed!\n", blockDevice);
205                 }
206         }
207 #endif
208         if (status != 0 && doRemount == TRUE && errno == EBUSY) {
209                 status = mount(blockDevice, name, NULL,
210                                            MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
211                 if (status == 0) {
212                         errorMsg("%s busy - remounted read-only\n", blockDevice);
213                 } else {
214                         errorMsg("Cannot remount %s read-only\n", blockDevice);
215                 }
216         }
217         if (status == 0) {
218 #if defined BB_MTAB
219                 if (useMtab == TRUE)
220                         erase_mtab(name);
221 #endif
222                 return (TRUE);
223         }
224         return (FALSE);
225 }
226
227 static int umount_all(int useMtab)
228 {
229         int status = TRUE;
230         char *mountpt;
231         void *iter;
232
233         for (mountpt = mtab_first(&iter); mountpt; mountpt = mtab_next(&iter)) {
234                 /* Never umount /proc on a umount -a */
235                 if (strstr(mountpt, "proc")!= NULL)
236                         continue;
237                 status = do_umount(mountpt, useMtab);
238                 if (status != 0) {
239                         /* Don't bother retrying the umount on busy devices */
240                         if (errno == EBUSY) {
241                                 perror(mountpt);
242                                 continue;
243                         }
244                         status = do_umount(mountpt, useMtab);
245                         if (status != 0) {
246                                 printf("Couldn't umount %s on %s: %s\n",
247                                            mountpt, mtab_getinfo(mountpt, MTAB_GETDEVICE),
248                                            strerror(errno));
249                         }
250                 }
251         }
252         return (status);
253 }
254
255 extern int umount_main(int argc, char **argv)
256 {
257         if (argc < 2) {
258                 usage(umount_usage);
259         }
260
261         /* Parse any options */
262         while (--argc > 0 && **(++argv) == '-') {
263                 while (*++(*argv))
264                         switch (**argv) {
265                         case 'a':
266                                 umountAll = TRUE;
267                                 break;
268 #if defined BB_FEATURE_MOUNT_LOOP
269                         case 'l':
270                                 freeLoop = FALSE;
271                                 break;
272 #endif
273 #ifdef BB_MTAB
274                         case 'n':
275                                 useMtab = FALSE;
276                                 break;
277 #endif
278 #ifdef BB_FEATURE_MOUNT_FORCE
279                         case 'f':
280                                 doForce = TRUE;
281                                 break;
282 #endif
283                         case 'r':
284                                 doRemount = TRUE;
285                                 break;
286                         case 'v':
287                                 break; /* ignore -v */
288                         default:
289                                 usage(umount_usage);
290                         }
291         }
292
293         mtab_read();
294         if (umountAll == TRUE) {
295                 exit(umount_all(useMtab));
296         }
297         if (do_umount(*argv, useMtab) == 0)
298                 exit(TRUE);
299         perror("umount");
300         return(FALSE);
301 }
302