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