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