Some bug fixes I forgot to check-in the other day.
[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 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 = malloc(sizeof(struct _mtab_entry_t));
93
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                                         struct fstab *fstabItem;
115
116                                         fstabItem = getfsfile("/");
117                                         if (fstabItem != NULL)
118                                                 return fstabItem->fs_spec;
119                                 }
120 #endif
121                                 return cur->device;
122                         }
123                 }
124                 cur = cur->next;
125         }
126         return NULL;
127 }
128
129 char *mtab_first(void **iter)
130 {
131         struct _mtab_entry_t *mtab_iter;
132
133         if (!iter)
134                 return NULL;
135         mtab_iter = mtab_cache;
136         *iter = (void *) mtab_iter;
137         return mtab_next(iter);
138 }
139
140 char *mtab_next(void **iter)
141 {
142         char *mp;
143
144         if (iter == NULL || *iter == NULL)
145                 return NULL;
146         mp = ((struct _mtab_entry_t *) (*iter))->mountpt;
147         *iter = (void *) ((struct _mtab_entry_t *) (*iter))->next;
148         return mp;
149 }
150
151 void mtab_free(void)
152 {
153         struct _mtab_entry_t *this, *next;
154
155         this = mtab_cache;
156         while (this) {
157                 next = this->next;
158                 if (this->device)
159                         free(this->device);
160                 if (this->mountpt)
161                         free(this->mountpt);
162                 free(this);
163                 this = next;
164         }
165 }
166
167 static int do_umount(const char *name, int useMtab)
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_REMOUNT
183         if (status != 0 && doRemount == TRUE && errno == EBUSY) {
184                 status = mount(blockDevice, name, NULL,
185                                            MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
186                 if (status == 0) {
187                         fprintf(stderr, "umount: %s busy - remounted read-only\n",
188                                         blockDevice);
189                         /* TODO: update mtab if BB_MTAB is defined */
190                 } else {
191                         fprintf(stderr, "umount: Cannot remount %s read-only\n",
192                                         blockDevice);
193                 }
194         }
195 #endif
196         if (status == 0) {
197 #if defined BB_MTAB
198                 if (useMtab == TRUE)
199                         erase_mtab(name);
200 #endif
201                 return (TRUE);
202         }
203         return (FALSE);
204 }
205
206 static int umount_all(int useMtab)
207 {
208         int status = TRUE;
209         char *mountpt;
210         void *iter;
211
212         for (mountpt = mtab_first(&iter); mountpt; mountpt = mtab_next(&iter)) {
213                 /* Never umount /proc on a umount -a */
214                 if (strstr(mountpt, "proc")!= NULL)
215                         continue;
216                 status = do_umount(mountpt, useMtab);
217                 if (status != 0) {
218                         /* Don't bother retrying the umount on busy devices */
219                         if (errno == EBUSY) {
220                                 perror(mountpt);
221                                 continue;
222                         }
223                         status = do_umount(mountpt, useMtab);
224                         if (status != 0) {
225                                 printf("Couldn't umount %s on %s: %s\n",
226                                            mountpt, mtab_getinfo(mountpt, MTAB_GETDEVICE),
227                                            strerror(errno));
228                         }
229                 }
230         }
231         return (status);
232 }
233
234 extern int umount_main(int argc, char **argv)
235 {
236         if (argc < 2) {
237                 usage(umount_usage);
238         }
239
240         /* Parse any options */
241         while (--argc > 0 && **(++argv) == '-') {
242                 while (*++(*argv))
243                         switch (**argv) {
244                         case 'a':
245                                 umountAll = TRUE;
246                                 break;
247 #if defined BB_FEATURE_MOUNT_LOOP
248                         case 'f':
249                                 freeLoop = FALSE;
250                                 break;
251 #endif
252 #ifdef BB_MTAB
253                         case 'n':
254                                 useMtab = FALSE;
255                                 break;
256 #endif
257 #ifdef BB_FEATURE_REMOUNT
258                         case 'r':
259                                 doRemount = TRUE;
260                                 break;
261 #endif
262                         default:
263                                 usage(umount_usage);
264                         }
265         }
266
267         mtab_read();
268         if (umountAll == TRUE) {
269                 exit(umount_all(useMtab));
270         }
271         if (do_umount(*argv, useMtab) == 0)
272                 exit(TRUE);
273         else {
274                 perror("umount");
275                 exit(FALSE);
276         }
277 }
278