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