18a5afe8014a9e71ffb7ec364f38de56636ab97f
[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 <sys/mount.h>
28 #include <mntent.h>
29 #include <errno.h>
30
31
32 static const char umount_usage[] =
33         "umount [flags] filesystem|directory\n\n"
34         "Flags:\n" "\t-a:\tUnmount all file systems"
35 #ifdef BB_MTAB
36         " in /etc/mtab\n\t-n:\tDon't erase /etc/mtab entries\n"
37 #else
38         "\n"
39 #endif
40         "\t-r:\tTry to remount devices as read-only if mount is busy\n"
41 #if defined BB_FEATURE_MOUNT_FORCE
42         "\t-f:\tForce filesystem umount (i.e. unreachable NFS server)\n"
43 #endif
44 #if defined BB_FEATURE_MOUNT_LOOP
45         "\t-l:\tDo not free loop device (if a loop device has been used)\n"
46 #endif
47 ;
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 BB_FEATURE_MOUNT_FORCE
60 static int doForce = FALSE;
61 #endif
62 #if defined BB_FEATURE_MOUNT_LOOP
63 static int freeLoop = TRUE;
64 #endif
65 static int useMtab = TRUE;
66 static int umountAll = FALSE;
67 static int doRemount = FALSE;
68 extern const char mtab_file[];  /* Defined in utility.c */
69
70
71
72 /* These functions are here because the getmntent functions do not appear
73  * to be re-entrant, which leads to all sorts of problems when we try to
74  * use them recursively - randolph
75  *
76  * TODO: Perhaps switch to using Glibc's getmntent_r
77  *        -Erik
78  */
79 void mtab_read(void)
80 {
81         struct _mtab_entry_t *entry = NULL;
82         struct mntent *e;
83         FILE *fp;
84
85         if (mtab_cache != NULL)
86                 return;
87
88         if ((fp = setmntent(mtab_file, "r")) == NULL) {
89                 fprintf(stderr, "Cannot open %s\n", mtab_file);
90                 return;
91         }
92         while ((e = getmntent(fp))) {
93                 entry = xmalloc(sizeof(struct _mtab_entry_t));
94                 entry->device = strdup(e->mnt_fsname);
95                 entry->mountpt = strdup(e->mnt_dir);
96                 entry->next = mtab_cache;
97                 mtab_cache = entry;
98         }
99         endmntent(fp);
100 }
101
102 char *mtab_getinfo(const char *match, const char which)
103 {
104         struct _mtab_entry_t *cur = mtab_cache;
105
106         while (cur) {
107                 if (strcmp(cur->mountpt, match) == 0 ||
108                         strcmp(cur->device, match) == 0) {
109                         if (which == MTAB_GETMOUNTPT) {
110                                 return cur->mountpt;
111                         } else {
112 #if !defined BB_MTAB
113                                 if (strcmp(cur->device, "/dev/root") == 0) {
114                                         /* Adjusts device to be the real root device,
115                                          * or leaves device alone if it can't find it */
116                                         find_real_root_device_name( cur->device);
117                                         return ( cur->device);
118                                 }
119 #endif
120                                 return cur->device;
121                         }
122                 }
123                 cur = cur->next;
124         }
125         return NULL;
126 }
127
128 char *mtab_first(void **iter)
129 {
130         struct _mtab_entry_t *mtab_iter;
131
132         if (!iter)
133                 return NULL;
134         mtab_iter = mtab_cache;
135         *iter = (void *) mtab_iter;
136         return mtab_next(iter);
137 }
138
139 char *mtab_next(void **iter)
140 {
141         char *mp;
142
143         if (iter == NULL || *iter == NULL)
144                 return NULL;
145         mp = ((struct _mtab_entry_t *) (*iter))->mountpt;
146         *iter = (void *) ((struct _mtab_entry_t *) (*iter))->next;
147         return mp;
148 }
149
150 /* Don't bother to clean up, since exit() does that 
151  * automagically, so we can save a few bytes */
152 #if 0
153 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                 if (this->device)
161                         free(this->device);
162                 if (this->mountpt)
163                         free(this->mountpt);
164                 free(this);
165                 this = next;
166         }
167 }
168 #endif
169
170 static int do_umount(const char *name, int useMtab)
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 BB_FEATURE_MOUNT_LOOP
181         if (freeLoop == TRUE && blockDevice != NULL && !strncmp("/dev/loop", blockDevice, 9))
182                 /* this was a loop device, delete it */
183                 del_loop(blockDevice);
184 #endif
185 #if defined BB_FEATURE_MOUNT_FORCE
186         if (status != 0 && doForce == TRUE) {
187                 status = umount2(blockDevice, MNT_FORCE);
188                 if (status != 0) {
189                         fatalError("umount: forced umount of %s failed!\n", blockDevice);
190                 }
191         }
192 #endif
193         if (status != 0 && doRemount == TRUE && errno == EBUSY) {
194                 status = mount(blockDevice, name, NULL,
195                                            MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
196                 if (status == 0) {
197                         fprintf(stderr, "umount: %s busy - remounted read-only\n",
198                                         blockDevice);
199                 } else {
200                         fprintf(stderr, "umount: Cannot remount %s read-only\n",
201                                         blockDevice);
202                 }
203         }
204         if (status == 0) {
205 #if defined BB_MTAB
206                 if (useMtab == TRUE)
207                         erase_mtab(name);
208 #endif
209                 return (TRUE);
210         }
211         return (FALSE);
212 }
213
214 static int umount_all(int useMtab)
215 {
216         int status = TRUE;
217         char *mountpt;
218         void *iter;
219
220         for (mountpt = mtab_first(&iter); mountpt; mountpt = mtab_next(&iter)) {
221                 /* Never umount /proc on a umount -a */
222                 if (strstr(mountpt, "proc")!= NULL)
223                         continue;
224                 status = do_umount(mountpt, useMtab);
225                 if (status != 0) {
226                         /* Don't bother retrying the umount on busy devices */
227                         if (errno == EBUSY) {
228                                 perror(mountpt);
229                                 continue;
230                         }
231                         status = do_umount(mountpt, useMtab);
232                         if (status != 0) {
233                                 printf("Couldn't umount %s on %s: %s\n",
234                                            mountpt, mtab_getinfo(mountpt, MTAB_GETDEVICE),
235                                            strerror(errno));
236                         }
237                 }
238         }
239         return (status);
240 }
241
242 extern int umount_main(int argc, char **argv)
243 {
244         if (argc < 2) {
245                 usage(umount_usage);
246         }
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_MTAB
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                                 usage(umount_usage);
277                         }
278         }
279
280         mtab_read();
281         if (umountAll == TRUE) {
282                 exit(umount_all(useMtab));
283         }
284         if (do_umount(*argv, useMtab) == 0)
285                 exit(TRUE);
286         else {
287                 perror("umount");
288                 exit(FALSE);
289         }
290 }
291