Some formatting updates (ran the code through indent)
[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 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 #ifdef BB_FEATURE_REMOUNT
41         "\t-r:\tTry to remount devices as read-only if mount is busy\n"
42 #endif
43 ;
44
45 struct _mtab_entry_t {
46         char *device;
47         char *mountpt;
48         struct _mtab_entry_t *next;
49 };
50
51 static struct _mtab_entry_t *mtab_cache = NULL;
52
53
54
55 static int useMtab = TRUE;
56 static int umountAll = FALSE;
57 static int doRemount = FALSE;
58 extern const char mtab_file[];  /* Defined in utility.c */
59
60 #define MIN(x,y) (x > y ? x : y)
61
62 static int do_umount(const char *name, int useMtab)
63 {
64         int status;
65         char *blockDevice = mtab_getinfo(name, MTAB_GETDEVICE);
66
67         if (blockDevice && strcmp(blockDevice, name) == 0)
68                 name = mtab_getinfo(blockDevice, MTAB_GETMOUNTPT);
69
70         status = umount(name);
71
72 #if defined BB_FEATURE_MOUNT_LOOP
73         if (blockDevice != NULL && !strncmp("/dev/loop", blockDevice, 9))
74                 /* this was a loop device, delete it */
75                 del_loop(blockDevice);
76 #endif
77 #if defined BB_FEATURE_REMOUNT
78         if (status != 0 && doRemount == TRUE && errno == EBUSY) {
79                 status = mount(blockDevice, name, NULL,
80                                            MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
81                 if (status == 0) {
82                         fprintf(stderr, "umount: %s busy - remounted read-only\n",
83                                         blockDevice);
84                         /* TODO: update mtab if BB_MTAB is defined */
85                 } else {
86                         fprintf(stderr, "umount: Cannot remount %s read-only\n",
87                                         blockDevice);
88                 }
89         }
90 #endif
91         if (status == 0) {
92 #if defined BB_MTAB
93                 if (useMtab == TRUE)
94                         erase_mtab(name);
95 #endif
96                 return (TRUE);
97         }
98         return (FALSE);
99 }
100
101 static int umount_all(int useMtab)
102 {
103         int status = TRUE;
104         char *mountpt;
105         void *iter;
106
107         for (mountpt = mtab_first(&iter); mountpt; mountpt = mtab_next(&iter)) {
108                 status = do_umount(mountpt, useMtab);
109                 if (status != 0) {
110                         /* Don't bother retrying the umount on busy devices */
111                         if (errno == EBUSY) {
112                                 perror(mountpt);
113                                 continue;
114                         }
115                         status = do_umount(mountpt, useMtab);
116                         if (status != 0) {
117                                 printf("Couldn't umount %s on %s: %s\n",
118                                            mountpt, mtab_getinfo(mountpt, MTAB_GETDEVICE),
119                                            strerror(errno));
120                         }
121                 }
122         }
123         return (status);
124 }
125
126 extern int umount_main(int argc, char **argv)
127 {
128         if (argc < 2) {
129                 usage(umount_usage);
130         }
131
132         /* Parse any options */
133         while (--argc > 0 && **(++argv) == '-') {
134                 while (*++(*argv))
135                         switch (**argv) {
136                         case 'a':
137                                 umountAll = TRUE;
138                                 break;
139 #ifdef BB_MTAB
140                         case 'n':
141                                 useMtab = FALSE;
142                                 break;
143 #endif
144 #ifdef BB_FEATURE_REMOUNT
145                         case 'r':
146                                 doRemount = TRUE;
147                                 break;
148 #endif
149                         default:
150                                 usage(umount_usage);
151                         }
152         }
153
154         mtab_read();
155         if (umountAll == TRUE) {
156                 exit(umount_all(useMtab));
157         }
158         if (do_umount(*argv, useMtab) == 0)
159                 exit(TRUE);
160         else {
161                 perror("umount");
162                 exit(FALSE);
163         }
164 }
165
166
167
168 /* These functions are here because the getmntent functions do not appear
169  * to be re-entrant, which leads to all sorts of problems when we try to
170  * use them recursively - randolph
171  */
172 void mtab_read(void)
173 {
174         struct _mtab_entry_t *entry = NULL;
175         struct mntent *e;
176         FILE *fp;
177
178         if (mtab_cache != NULL)
179                 return;
180
181         if ((fp = setmntent(mtab_file, "r")) == NULL) {
182                 fprintf(stderr, "Cannot open %s\n", mtab_file);
183                 return;
184         }
185         while ((e = getmntent(fp))) {
186                 entry = malloc(sizeof(struct _mtab_entry_t));
187
188                 entry->device = strdup(e->mnt_fsname);
189                 entry->mountpt = strdup(e->mnt_dir);
190                 entry->next = mtab_cache;
191                 mtab_cache = entry;
192         }
193         endmntent(fp);
194 }
195
196 char *mtab_getinfo(const char *match, const char which)
197 {
198         struct _mtab_entry_t *cur = mtab_cache;
199
200         while (cur) {
201                 if (strcmp(cur->mountpt, match) == 0 ||
202                         strcmp(cur->device, match) == 0) {
203                         if (which == MTAB_GETMOUNTPT) {
204                                 return cur->mountpt;
205                         } else {
206 #if !defined BB_MTAB
207                                 if (strcmp(cur->device, "/dev/root") == 0) {
208                                         struct fstab *fstabItem;
209
210                                         fstabItem = getfsfile("/");
211                                         if (fstabItem != NULL)
212                                                 return fstabItem->fs_spec;
213                                 }
214 #endif
215                                 return cur->device;
216                         }
217                 }
218                 cur = cur->next;
219         }
220         return NULL;
221 }
222
223 char *mtab_first(void **iter)
224 {
225         struct _mtab_entry_t *mtab_iter;
226
227         if (!iter)
228                 return NULL;
229         mtab_iter = mtab_cache;
230         *iter = (void *) mtab_iter;
231         return mtab_next(iter);
232 }
233
234 char *mtab_next(void **iter)
235 {
236         char *mp;
237
238         if (iter == NULL || *iter == NULL)
239                 return NULL;
240         mp = ((struct _mtab_entry_t *) (*iter))->mountpt;
241         *iter = (void *) ((struct _mtab_entry_t *) (*iter))->next;
242         return mp;
243 }
244
245 void mtab_free(void)
246 {
247         struct _mtab_entry_t *this, *next;
248
249         this = mtab_cache;
250         while (this) {
251                 next = this->next;
252                 if (this->device)
253                         free(this->device);
254                 if (this->mountpt)
255                         free(this->mountpt);
256                 free(this);
257                 this = next;
258         }
259 }