aboutsummaryrefslogtreecommitdiff
path: root/umount.c
diff options
context:
space:
mode:
Diffstat (limited to 'umount.c')
-rw-r--r--umount.c298
1 files changed, 0 insertions, 298 deletions
diff --git a/umount.c b/umount.c
deleted file mode 100644
index 74638d21c..000000000
--- a/umount.c
+++ /dev/null
@@ -1,298 +0,0 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Mini umount implementation for busybox
4 *
5 *
6 * Copyright (C) 1999,2000,2001 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 <limits.h>
26#include <stdio.h>
27#include <mntent.h>
28#include <errno.h>
29#include <string.h>
30#include <stdlib.h>
31#include "busybox.h"
32
33/* Teach libc5 about realpath -- it includes it but the
34 * prototype is missing... */
35#if (__GLIBC__ <= 2) && (__GLIBC_MINOR__ < 1)
36extern char *realpath(const char *path, char *resolved_path);
37#endif
38
39static const int MNT_FORCE = 1;
40static const int MS_MGC_VAL = 0xc0ed0000; /* Magic number indicatng "new" flags */
41static const int MS_REMOUNT = 32; /* Alter flags of a mounted FS. */
42static const int MS_RDONLY = 1; /* Mount read-only. */
43
44extern int mount (__const char *__special_file, __const char *__dir,
45 __const char *__fstype, unsigned long int __rwflag,
46 __const void *__data);
47extern int umount (__const char *__special_file);
48extern int umount2 (__const char *__special_file, int __flags);
49
50struct _mtab_entry_t {
51 char *device;
52 char *mountpt;
53 struct _mtab_entry_t *next;
54};
55
56static struct _mtab_entry_t *mtab_cache = NULL;
57
58
59
60#if defined BB_FEATURE_MOUNT_FORCE
61static int doForce = FALSE;
62#endif
63#if defined BB_FEATURE_MOUNT_LOOP
64static int freeLoop = TRUE;
65#endif
66#if defined BB_FEATURE_MTAB_SUPPORT
67static int useMtab = TRUE;
68#endif
69static int umountAll = FALSE;
70static int doRemount = FALSE;
71extern const char mtab_file[]; /* Defined in utility.c */
72
73
74
75/* These functions are here because the getmntent functions do not appear
76 * to be re-entrant, which leads to all sorts of problems when we try to
77 * use them recursively - randolph
78 *
79 * TODO: Perhaps switch to using Glibc's getmntent_r
80 * -Erik
81 */
82static void mtab_read(void)
83{
84 struct _mtab_entry_t *entry = NULL;
85 struct mntent *e;
86 FILE *fp;
87
88 if (mtab_cache != NULL)
89 return;
90
91 if ((fp = setmntent(mtab_file, "r")) == NULL) {
92 error_msg("Cannot open %s", mtab_file);
93 return;
94 }
95 while ((e = getmntent(fp))) {
96 entry = xmalloc(sizeof(struct _mtab_entry_t));
97 entry->device = strdup(e->mnt_fsname);
98 entry->mountpt = strdup(e->mnt_dir);
99 entry->next = mtab_cache;
100 mtab_cache = entry;
101 }
102 endmntent(fp);
103}
104
105static char *mtab_getinfo(const char *match, const char which)
106{
107 struct _mtab_entry_t *cur = mtab_cache;
108
109 while (cur) {
110 if (strcmp(cur->mountpt, match) == 0 ||
111 strcmp(cur->device, match) == 0) {
112 if (which == MTAB_GETMOUNTPT) {
113 return cur->mountpt;
114 } else {
115#if !defined BB_FEATURE_MTAB_SUPPORT
116 if (strcmp(cur->device, "/dev/root") == 0) {
117 /* Adjusts device to be the real root device,
118 * or leaves device alone if it can't find it */
119 cur->device = find_real_root_device_name(cur->device);
120 }
121#endif
122 return cur->device;
123 }
124 }
125 cur = cur->next;
126 }
127 return NULL;
128}
129
130static char *mtab_next(void **iter)
131{
132 char *mp;
133
134 if (iter == NULL || *iter == NULL)
135 return NULL;
136 mp = ((struct _mtab_entry_t *) (*iter))->mountpt;
137 *iter = (void *) ((struct _mtab_entry_t *) (*iter))->next;
138 return mp;
139}
140
141static char *mtab_first(void **iter)
142{
143 struct _mtab_entry_t *mtab_iter;
144
145 if (!iter)
146 return NULL;
147 mtab_iter = mtab_cache;
148 *iter = (void *) mtab_iter;
149 return mtab_next(iter);
150}
151
152/* Don't bother to clean up, since exit() does that
153 * automagically, so we can save a few bytes */
154#ifdef BB_FEATURE_CLEAN_UP
155static void mtab_free(void)
156{
157 struct _mtab_entry_t *this, *next;
158
159 this = mtab_cache;
160 while (this) {
161 next = this->next;
162 if (this->device)
163 free(this->device);
164 if (this->mountpt)
165 free(this->mountpt);
166 free(this);
167 this = next;
168 }
169}
170#endif
171
172static int do_umount(const char *name)
173{
174 int status;
175 char *blockDevice = mtab_getinfo(name, MTAB_GETDEVICE);
176
177 if (blockDevice && strcmp(blockDevice, name) == 0)
178 name = mtab_getinfo(blockDevice, MTAB_GETMOUNTPT);
179
180 status = umount(name);
181
182#if defined BB_FEATURE_MOUNT_LOOP
183 if (freeLoop == TRUE && blockDevice != NULL && !strncmp("/dev/loop", blockDevice, 9))
184 /* this was a loop device, delete it */
185 del_loop(blockDevice);
186#endif
187#if defined BB_FEATURE_MOUNT_FORCE
188 if (status != 0 && doForce == TRUE) {
189 status = umount2(blockDevice, MNT_FORCE);
190 if (status != 0) {
191 error_msg_and_die("forced umount of %s failed!", blockDevice);
192 }
193 }
194#endif
195 if (status != 0 && doRemount == TRUE && errno == EBUSY) {
196 status = mount(blockDevice, name, NULL,
197 MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
198 if (status == 0) {
199 error_msg("%s busy - remounted read-only", blockDevice);
200 } else {
201 error_msg("Cannot remount %s read-only", blockDevice);
202 }
203 }
204 if (status == 0) {
205#if defined BB_FEATURE_MTAB_SUPPORT
206 if (useMtab == TRUE)
207 erase_mtab(name);
208#endif
209 return (TRUE);
210 }
211 return (FALSE);
212}
213
214static int umount_all(void)
215{
216 int status = TRUE;
217 char *mountpt;
218 void *iter;
219
220 for (mountpt = mtab_first(&iter); mountpt; mountpt = mtab_next(&iter)) {
221 /* Never umount /proc on a umount -a */
222 if (strstr(mountpt, "proc")!= NULL)
223 continue;
224 if (!do_umount(mountpt)) {
225 /* Don't bother retrying the umount on busy devices */
226 if (errno == EBUSY) {
227 perror_msg("%s", mountpt);
228 status = FALSE;
229 continue;
230 }
231 if (!do_umount(mountpt)) {
232 printf("Couldn't umount %s on %s: %s\n",
233 mountpt, mtab_getinfo(mountpt, MTAB_GETDEVICE),
234 strerror(errno));
235 status = FALSE;
236 }
237 }
238 }
239 return (status);
240}
241
242extern int umount_main(int argc, char **argv)
243{
244 char path[PATH_MAX];
245
246 if (argc < 2) {
247 show_usage();
248 }
249#ifdef BB_FEATURE_CLEAN_UP
250 atexit(mtab_free);
251#endif
252
253 /* Parse any options */
254 while (--argc > 0 && **(++argv) == '-') {
255 while (*++(*argv))
256 switch (**argv) {
257 case 'a':
258 umountAll = TRUE;
259 break;
260#if defined BB_FEATURE_MOUNT_LOOP
261 case 'l':
262 freeLoop = FALSE;
263 break;
264#endif
265#ifdef BB_FEATURE_MTAB_SUPPORT
266 case 'n':
267 useMtab = FALSE;
268 break;
269#endif
270#ifdef BB_FEATURE_MOUNT_FORCE
271 case 'f':
272 doForce = TRUE;
273 break;
274#endif
275 case 'r':
276 doRemount = TRUE;
277 break;
278 case 'v':
279 break; /* ignore -v */
280 default:
281 show_usage();
282 }
283 }
284
285 mtab_read();
286 if (umountAll == TRUE) {
287 if (umount_all() == TRUE)
288 return EXIT_SUCCESS;
289 else
290 return EXIT_FAILURE;
291 }
292 if (realpath(*argv, path) == NULL)
293 perror_msg_and_die("%s", path);
294 if (do_umount(path) == TRUE)
295 return EXIT_SUCCESS;
296 perror_msg_and_die("%s", *argv);
297}
298