6661db8789e9d1caa25496e4cfcf02ce8b13c421
[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 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 <fstab.h>
30 #include <errno.h>
31
32
33 static const char umount_usage[] =
34         "umount [flags] filesystem|directory\n\n"
35         "Flags:\n" "\t-a:\tUnmount all file systems"
36 #ifdef BB_MTAB
37         " in /etc/mtab\n\t-n:\tDon't erase /etc/mtab entries\n"
38 #else
39         "\n"
40 #endif
41 #ifdef BB_FEATURE_REMOUNT
42         "\t-r:\tTry to remount devices as read-only if mount is busy\n"
43 #endif
44 #if defined BB_FEATURE_MOUNT_LOOP
45         "\t-f:\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_LOOP
60 static int freeLoop = TRUE;
61 #endif
62 static int useMtab = TRUE;
63 static int umountAll = FALSE;
64 #if defined BB_FEATURE_REMOUNT
65 static int doRemount = FALSE;
66 #endif
67 extern const char mtab_file[];  /* Defined in utility.c */
68
69
70
71 /* These functions are here because the getmntent functions do not appear
72  * to be re-entrant, which leads to all sorts of problems when we try to
73  * use them recursively - randolph
74  *
75  * TODO: Perhaps switch to using Glibc's getmntent_r
76  *        -Erik
77  */
78 void mtab_read(void)
79 {
80         struct _mtab_entry_t *entry = NULL;
81         struct mntent *e;
82         FILE *fp;
83
84         if (mtab_cache != NULL)
85                 return;
86
87         if ((fp = setmntent(mtab_file, "r")) == NULL) {
88                 fprintf(stderr, "Cannot open %s\n", mtab_file);
89                 return;
90         }
91         while ((e = getmntent(fp))) {
92                 entry = xmalloc(sizeof(struct _mtab_entry_t));
93                 entry->device = strdup(e->mnt_fsname);
94                 entry->mountpt = strdup(e->mnt_dir);
95                 entry->next = mtab_cache;
96                 mtab_cache = entry;
97         }
98         endmntent(fp);
99 }
100
101 char *mtab_getinfo(const char *match, const char which)
102 {
103         struct _mtab_entry_t *cur = mtab_cache;
104
105         while (cur) {
106                 if (strcmp(cur->mountpt, match) == 0 ||
107                         strcmp(cur->device, match) == 0) {
108                         if (which == MTAB_GETMOUNTPT) {
109                                 return cur->mountpt;
110                         } else {
111 #if !defined BB_MTAB
112                                 if (strcmp(cur->device, "/dev/root") == 0) {
113                                         struct fstab *fstabItem;
114
115                                         fstabItem = getfsfile("/");
116                                         if (fstabItem != NULL)
117                                                 return fstabItem->fs_spec;
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 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
166 static int do_umount(const char *name, int useMtab)
167 {
168         int status;
169         char *blockDevice = mtab_getinfo(name, MTAB_GETDEVICE);
170
171         if (blockDevice && strcmp(blockDevice, name) == 0)
172                 name = mtab_getinfo(blockDevice, MTAB_GETMOUNTPT);
173
174         status = umount(name);
175
176 #if defined BB_FEATURE_MOUNT_LOOP
177         if (freeLoop == TRUE && blockDevice != NULL && !strncmp("/dev/loop", blockDevice, 9))
178                 /* this was a loop device, delete it */
179                 del_loop(blockDevice);
180 #endif
181 #if defined BB_FEATURE_REMOUNT
182         if (status != 0 && doRemount == TRUE && errno == EBUSY) {
183                 status = mount(blockDevice, name, NULL,
184                                            MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
185                 if (status == 0) {
186                         fprintf(stderr, "umount: %s busy - remounted read-only\n",
187                                         blockDevice);
188                         /* TODO: update mtab if BB_MTAB is defined */
189                 } else {
190                         fprintf(stderr, "umount: Cannot remount %s read-only\n",
191                                         blockDevice);
192                 }
193         }
194 #endif
195         if (status == 0) {
196 #if defined BB_MTAB
197                 if (useMtab == TRUE)
198                         erase_mtab(name);
199 #endif
200                 return (TRUE);
201         }
202         return (FALSE);
203 }
204
205 static int umount_all(int useMtab)
206 {
207         int status = TRUE;
208         char *mountpt;
209         void *iter;
210
211         for (mountpt = mtab_first(&iter); mountpt; mountpt = mtab_next(&iter)) {
212                 /* Never umount /proc on a umount -a */
213                 if (strstr(mountpt, "proc")!= NULL)
214                         continue;
215                 status = do_umount(mountpt, useMtab);
216                 if (status != 0) {
217                         /* Don't bother retrying the umount on busy devices */
218                         if (errno == EBUSY) {
219                                 perror(mountpt);
220                                 continue;
221                         }
222                         status = do_umount(mountpt, useMtab);
223                         if (status != 0) {
224                                 printf("Couldn't umount %s on %s: %s\n",
225                                            mountpt, mtab_getinfo(mountpt, MTAB_GETDEVICE),
226                                            strerror(errno));
227                         }
228                 }
229         }
230         return (status);
231 }
232
233 extern int umount_main(int argc, char **argv)
234 {
235         if (argc < 2) {
236                 usage(umount_usage);
237         }
238
239         /* Parse any options */
240         while (--argc > 0 && **(++argv) == '-') {
241                 while (*++(*argv))
242                         switch (**argv) {
243                         case 'a':
244                                 umountAll = TRUE;
245                                 break;
246 #if defined BB_FEATURE_MOUNT_LOOP
247                         case 'f':
248                                 freeLoop = FALSE;
249                                 break;
250 #endif
251 #ifdef BB_MTAB
252                         case 'n':
253                                 useMtab = FALSE;
254                                 break;
255 #endif
256 #ifdef BB_FEATURE_REMOUNT
257                         case 'r':
258                                 doRemount = TRUE;
259                                 break;
260 #endif
261                         default:
262                                 usage(umount_usage);
263                         }
264         }
265
266         mtab_read();
267         if (umountAll == TRUE) {
268                 exit(umount_all(useMtab));
269         }
270         if (do_umount(*argv, useMtab) == 0)
271                 exit(TRUE);
272         else {
273                 perror("umount");
274                 exit(FALSE);
275         }
276 }
277