Changed names of functions in utility.c and all affected files, to make
[oweals/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 "busybox.h"
26 #include <stdio.h>
27 #include <mntent.h>
28 #include <errno.h>
29
30
31 #define MNT_FORCE               1
32 #define MS_MGC_VAL              0xc0ed0000 /* Magic number indicatng "new" flags */
33 #define MS_REMOUNT              32      /* Alter flags of a mounted FS.  */
34 #define MS_RDONLY               1       /* Mount read-only.  */
35
36 extern int mount (__const char *__special_file, __const char *__dir,
37                         __const char *__fstype, unsigned long int __rwflag,
38                         __const void *__data);
39 extern int umount (__const char *__special_file);
40 extern int umount2 (__const char *__special_file, int __flags);
41
42 struct _mtab_entry_t {
43         char *device;
44         char *mountpt;
45         struct _mtab_entry_t *next;
46 };
47
48 static struct _mtab_entry_t *mtab_cache = NULL;
49
50
51
52 #if defined BB_FEATURE_MOUNT_FORCE
53 static int doForce = FALSE;
54 #endif
55 #if defined BB_FEATURE_MOUNT_LOOP
56 static int freeLoop = TRUE;
57 #endif
58 static int useMtab = TRUE;
59 static int umountAll = FALSE;
60 static int doRemount = FALSE;
61 extern const char mtab_file[];  /* Defined in utility.c */
62
63
64
65 /* These functions are here because the getmntent functions do not appear
66  * to be re-entrant, which leads to all sorts of problems when we try to
67  * use them recursively - randolph
68  *
69  * TODO: Perhaps switch to using Glibc's getmntent_r
70  *        -Erik
71  */
72 void mtab_read(void)
73 {
74         struct _mtab_entry_t *entry = NULL;
75         struct mntent *e;
76         FILE *fp;
77
78         if (mtab_cache != NULL)
79                 return;
80
81         if ((fp = setmntent(mtab_file, "r")) == NULL) {
82                 error_msg("Cannot open %s\n", mtab_file);
83                 return;
84         }
85         while ((e = getmntent(fp))) {
86                 entry = xmalloc(sizeof(struct _mtab_entry_t));
87                 entry->device = strdup(e->mnt_fsname);
88                 entry->mountpt = strdup(e->mnt_dir);
89                 entry->next = mtab_cache;
90                 mtab_cache = entry;
91         }
92         endmntent(fp);
93 }
94
95 char *mtab_getinfo(const char *match, const char which)
96 {
97         struct _mtab_entry_t *cur = mtab_cache;
98
99         while (cur) {
100                 if (strcmp(cur->mountpt, match) == 0 ||
101                         strcmp(cur->device, match) == 0) {
102                         if (which == MTAB_GETMOUNTPT) {
103                                 return cur->mountpt;
104                         } else {
105 #if !defined BB_MTAB
106                                 if (strcmp(cur->device, "/dev/root") == 0) {
107                                         /* Adjusts device to be the real root device,
108                                          * or leaves device alone if it can't find it */
109                                         find_real_root_device_name( cur->device);
110                                         return ( cur->device);
111                                 }
112 #endif
113                                 return cur->device;
114                         }
115                 }
116                 cur = cur->next;
117         }
118         return NULL;
119 }
120
121 char *mtab_first(void **iter)
122 {
123         struct _mtab_entry_t *mtab_iter;
124
125         if (!iter)
126                 return NULL;
127         mtab_iter = mtab_cache;
128         *iter = (void *) mtab_iter;
129         return mtab_next(iter);
130 }
131
132 char *mtab_next(void **iter)
133 {
134         char *mp;
135
136         if (iter == NULL || *iter == NULL)
137                 return NULL;
138         mp = ((struct _mtab_entry_t *) (*iter))->mountpt;
139         *iter = (void *) ((struct _mtab_entry_t *) (*iter))->next;
140         return mp;
141 }
142
143 /* Don't bother to clean up, since exit() does that 
144  * automagically, so we can save a few bytes */
145 #ifdef BB_FEATURE_CLEAN_UP
146 void mtab_free(void)
147 {
148         struct _mtab_entry_t *this, *next;
149
150         this = mtab_cache;
151         while (this) {
152                 next = this->next;
153                 if (this->device)
154                         free(this->device);
155                 if (this->mountpt)
156                         free(this->mountpt);
157                 free(this);
158                 this = next;
159         }
160 }
161 #endif
162
163 static int do_umount(const char *name, int useMtab)
164 {
165         int status;
166         char *blockDevice = mtab_getinfo(name, MTAB_GETDEVICE);
167
168         if (blockDevice && strcmp(blockDevice, name) == 0)
169                 name = mtab_getinfo(blockDevice, MTAB_GETMOUNTPT);
170
171         status = umount(name);
172
173 #if defined BB_FEATURE_MOUNT_LOOP
174         if (freeLoop == TRUE && blockDevice != NULL && !strncmp("/dev/loop", blockDevice, 9))
175                 /* this was a loop device, delete it */
176                 del_loop(blockDevice);
177 #endif
178 #if defined BB_FEATURE_MOUNT_FORCE
179         if (status != 0 && doForce == TRUE) {
180                 status = umount2(blockDevice, MNT_FORCE);
181                 if (status != 0) {
182                         error_msg_and_die("forced umount of %s failed!\n", blockDevice);
183                 }
184         }
185 #endif
186         if (status != 0 && doRemount == TRUE && errno == EBUSY) {
187                 status = mount(blockDevice, name, NULL,
188                                            MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
189                 if (status == 0) {
190                         error_msg("%s busy - remounted read-only\n", blockDevice);
191                 } else {
192                         error_msg("Cannot remount %s read-only\n", blockDevice);
193                 }
194         }
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 #ifdef BB_FEATURE_CLEAN_UP
239         atexit(mtab_free);
240 #endif
241
242         /* Parse any options */
243         while (--argc > 0 && **(++argv) == '-') {
244                 while (*++(*argv))
245                         switch (**argv) {
246                         case 'a':
247                                 umountAll = TRUE;
248                                 break;
249 #if defined BB_FEATURE_MOUNT_LOOP
250                         case 'l':
251                                 freeLoop = FALSE;
252                                 break;
253 #endif
254 #ifdef BB_MTAB
255                         case 'n':
256                                 useMtab = FALSE;
257                                 break;
258 #endif
259 #ifdef BB_FEATURE_MOUNT_FORCE
260                         case 'f':
261                                 doForce = TRUE;
262                                 break;
263 #endif
264                         case 'r':
265                                 doRemount = TRUE;
266                                 break;
267                         case 'v':
268                                 break; /* ignore -v */
269                         default:
270                                 usage(umount_usage);
271                         }
272         }
273
274         mtab_read();
275         if (umountAll == TRUE) {
276                 if (umount_all(useMtab) == TRUE)
277                         return EXIT_SUCCESS;
278                 else
279                         return EXIT_FAILURE;
280         }
281         if (do_umount(*argv, useMtab) == TRUE)
282                 return EXIT_SUCCESS;
283         perror("umount");
284         return EXIT_FAILURE;
285 }
286