blob: 1b250fd3a3ddbc7bd2267e1a8ea8d48afea57d11 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenf811e071999-10-09 00:25:00 +00002/*
3 * Mini umount implementation for busybox
4 *
Eric Andersenc4996011999-10-20 22:08:37 +00005 *
Erik Andersen61677fe2000-04-13 01:18:56 +00006 * Copyright (C) 1999,2000 by Lineo, inc.
Eric Andersenc4996011999-10-20 22:08:37 +00007 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
Eric Andersenf811e071999-10-09 00:25:00 +00008 *
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
Eric Andersencc8ed391999-10-05 16:24:54 +000025#include "internal.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000026#include <stdio.h>
Eric Andersenf811e071999-10-09 00:25:00 +000027#include <mntent.h>
Eric Andersenf811e071999-10-09 00:25:00 +000028#include <errno.h>
Eric Andersene1e23ee2000-06-19 18:38:51 +000029
30
Eric Andersen2cd439f2000-07-08 19:10:29 +000031#define MNT_FORCE 1
Eric Andersen2cd439f2000-07-08 19:10:29 +000032#define MS_MGC_VAL 0xc0ed0000 /* Magic number indicatng "new" flags */
Eric Andersen2cd439f2000-07-08 19:10:29 +000033#define MS_REMOUNT 32 /* Alter flags of a mounted FS. */
Eric Andersen2cd439f2000-07-08 19:10:29 +000034#define MS_RDONLY 1 /* Mount read-only. */
Eric Andersen2cd439f2000-07-08 19:10:29 +000035
Eric Andersena57ba4d2000-07-08 19:20:49 +000036extern int mount (__const char *__special_file, __const char *__dir,
37 __const char *__fstype, unsigned long int __rwflag,
38 __const void *__data);
39extern int umount (__const char *__special_file);
40extern int umount2 (__const char *__special_file, int __flags);
Eric Andersen2cd439f2000-07-08 19:10:29 +000041
Eric Andersencc8ed391999-10-05 16:24:54 +000042
Erik Andersene132f4b2000-02-09 04:16:43 +000043
Erik Andersene49d5ec2000-02-08 19:58:47 +000044static const char umount_usage[] =
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000045 "umount [flags] filesystem|directory\n"
46#ifndef BB_FEATURE_TRIVIAL_HELP
47 "Unmount file systems\n"
48 "\nFlags:\n" "\t-a:\tUnmount all file systems"
Eric Andersend0246fb1999-11-04 21:18:07 +000049#ifdef BB_MTAB
Erik Andersene49d5ec2000-02-08 19:58:47 +000050 " in /etc/mtab\n\t-n:\tDon't erase /etc/mtab entries\n"
Eric Andersend0246fb1999-11-04 21:18:07 +000051#else
Erik Andersene49d5ec2000-02-08 19:58:47 +000052 "\n"
Eric Andersend0246fb1999-11-04 21:18:07 +000053#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000054 "\t-r:\tTry to remount devices as read-only if mount is busy\n"
Erik Andersen6c5f2c62000-05-05 19:49:33 +000055#if defined BB_FEATURE_MOUNT_FORCE
56 "\t-f:\tForce filesystem umount (i.e. unreachable NFS server)\n"
57#endif
Erik Andersence917322000-03-13 04:07:02 +000058#if defined BB_FEATURE_MOUNT_LOOP
Erik Andersen6c5f2c62000-05-05 19:49:33 +000059 "\t-l:\tDo not free loop device (if a loop device has been used)\n"
Erik Andersence917322000-03-13 04:07:02 +000060#endif
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000061#endif
Eric Andersend0246fb1999-11-04 21:18:07 +000062;
63
Erik Andersenfac10d72000-02-07 05:29:42 +000064struct _mtab_entry_t {
Erik Andersene49d5ec2000-02-08 19:58:47 +000065 char *device;
66 char *mountpt;
67 struct _mtab_entry_t *next;
Erik Andersenfac10d72000-02-07 05:29:42 +000068};
69
70static struct _mtab_entry_t *mtab_cache = NULL;
71
72
Eric Andersend0246fb1999-11-04 21:18:07 +000073
Erik Andersen6c5f2c62000-05-05 19:49:33 +000074#if defined BB_FEATURE_MOUNT_FORCE
75static int doForce = FALSE;
76#endif
Erik Andersence917322000-03-13 04:07:02 +000077#if defined BB_FEATURE_MOUNT_LOOP
78static int freeLoop = TRUE;
79#endif
Eric Andersend0246fb1999-11-04 21:18:07 +000080static int useMtab = TRUE;
81static int umountAll = FALSE;
Erik Andersenfac10d72000-02-07 05:29:42 +000082static int doRemount = FALSE;
Erik Andersene49d5ec2000-02-08 19:58:47 +000083extern const char mtab_file[]; /* Defined in utility.c */
Eric Andersend0246fb1999-11-04 21:18:07 +000084
Erik Andersenfac10d72000-02-07 05:29:42 +000085
Erik Andersen5b911dd2000-02-23 22:49:58 +000086
Erik Andersenfac10d72000-02-07 05:29:42 +000087/* These functions are here because the getmntent functions do not appear
88 * to be re-entrant, which leads to all sorts of problems when we try to
89 * use them recursively - randolph
Erik Andersen5b911dd2000-02-23 22:49:58 +000090 *
91 * TODO: Perhaps switch to using Glibc's getmntent_r
92 * -Erik
Erik Andersenfac10d72000-02-07 05:29:42 +000093 */
94void mtab_read(void)
95{
Erik Andersene49d5ec2000-02-08 19:58:47 +000096 struct _mtab_entry_t *entry = NULL;
97 struct mntent *e;
98 FILE *fp;
99
100 if (mtab_cache != NULL)
101 return;
102
103 if ((fp = setmntent(mtab_file, "r")) == NULL) {
Matt Kraaid537a952000-07-14 01:51:25 +0000104 errorMsg("Cannot open %s\n", mtab_file);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000105 return;
106 }
107 while ((e = getmntent(fp))) {
Erik Andersen0d068a22000-03-21 22:32:57 +0000108 entry = xmalloc(sizeof(struct _mtab_entry_t));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000109 entry->device = strdup(e->mnt_fsname);
110 entry->mountpt = strdup(e->mnt_dir);
111 entry->next = mtab_cache;
112 mtab_cache = entry;
113 }
114 endmntent(fp);
Erik Andersenfac10d72000-02-07 05:29:42 +0000115}
116
117char *mtab_getinfo(const char *match, const char which)
118{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000119 struct _mtab_entry_t *cur = mtab_cache;
120
121 while (cur) {
122 if (strcmp(cur->mountpt, match) == 0 ||
123 strcmp(cur->device, match) == 0) {
124 if (which == MTAB_GETMOUNTPT) {
125 return cur->mountpt;
126 } else {
Erik Andersenfac10d72000-02-07 05:29:42 +0000127#if !defined BB_MTAB
Erik Andersene49d5ec2000-02-08 19:58:47 +0000128 if (strcmp(cur->device, "/dev/root") == 0) {
Erik Andersenec5bd902000-03-22 07:12:05 +0000129 /* Adjusts device to be the real root device,
130 * or leaves device alone if it can't find it */
131 find_real_root_device_name( cur->device);
132 return ( cur->device);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000133 }
Erik Andersenfac10d72000-02-07 05:29:42 +0000134#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000135 return cur->device;
136 }
137 }
138 cur = cur->next;
Erik Andersenfac10d72000-02-07 05:29:42 +0000139 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000140 return NULL;
Erik Andersenfac10d72000-02-07 05:29:42 +0000141}
142
143char *mtab_first(void **iter)
144{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000145 struct _mtab_entry_t *mtab_iter;
146
147 if (!iter)
148 return NULL;
149 mtab_iter = mtab_cache;
150 *iter = (void *) mtab_iter;
151 return mtab_next(iter);
Erik Andersenfac10d72000-02-07 05:29:42 +0000152}
153
154char *mtab_next(void **iter)
155{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000156 char *mp;
157
158 if (iter == NULL || *iter == NULL)
159 return NULL;
160 mp = ((struct _mtab_entry_t *) (*iter))->mountpt;
161 *iter = (void *) ((struct _mtab_entry_t *) (*iter))->next;
162 return mp;
Erik Andersenfac10d72000-02-07 05:29:42 +0000163}
164
Erik Andersen298854f2000-03-23 01:09:18 +0000165/* Don't bother to clean up, since exit() does that
166 * automagically, so we can save a few bytes */
167#if 0
Erik Andersenfac10d72000-02-07 05:29:42 +0000168void mtab_free(void)
169{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000170 struct _mtab_entry_t *this, *next;
Erik Andersenfac10d72000-02-07 05:29:42 +0000171
Erik Andersene49d5ec2000-02-08 19:58:47 +0000172 this = mtab_cache;
173 while (this) {
174 next = this->next;
175 if (this->device)
176 free(this->device);
177 if (this->mountpt)
178 free(this->mountpt);
179 free(this);
180 this = next;
181 }
Erik Andersenfac10d72000-02-07 05:29:42 +0000182}
Erik Andersen298854f2000-03-23 01:09:18 +0000183#endif
Erik Andersene132f4b2000-02-09 04:16:43 +0000184
185static int do_umount(const char *name, int useMtab)
186{
187 int status;
188 char *blockDevice = mtab_getinfo(name, MTAB_GETDEVICE);
189
190 if (blockDevice && strcmp(blockDevice, name) == 0)
191 name = mtab_getinfo(blockDevice, MTAB_GETMOUNTPT);
192
193 status = umount(name);
194
195#if defined BB_FEATURE_MOUNT_LOOP
Erik Andersence917322000-03-13 04:07:02 +0000196 if (freeLoop == TRUE && blockDevice != NULL && !strncmp("/dev/loop", blockDevice, 9))
Erik Andersene132f4b2000-02-09 04:16:43 +0000197 /* this was a loop device, delete it */
198 del_loop(blockDevice);
199#endif
Erik Andersen6c5f2c62000-05-05 19:49:33 +0000200#if defined BB_FEATURE_MOUNT_FORCE
201 if (status != 0 && doForce == TRUE) {
202 status = umount2(blockDevice, MNT_FORCE);
203 if (status != 0) {
Matt Kraaibe84cd42000-07-12 17:02:35 +0000204 fatalError("forced umount of %s failed!\n", blockDevice);
Erik Andersen6c5f2c62000-05-05 19:49:33 +0000205 }
206 }
207#endif
Erik Andersene132f4b2000-02-09 04:16:43 +0000208 if (status != 0 && doRemount == TRUE && errno == EBUSY) {
209 status = mount(blockDevice, name, NULL,
210 MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
211 if (status == 0) {
Matt Kraaid537a952000-07-14 01:51:25 +0000212 errorMsg("%s busy - remounted read-only\n", blockDevice);
Erik Andersene132f4b2000-02-09 04:16:43 +0000213 } else {
Matt Kraaid537a952000-07-14 01:51:25 +0000214 errorMsg("Cannot remount %s read-only\n", blockDevice);
Erik Andersene132f4b2000-02-09 04:16:43 +0000215 }
216 }
Erik Andersene132f4b2000-02-09 04:16:43 +0000217 if (status == 0) {
218#if defined BB_MTAB
219 if (useMtab == TRUE)
220 erase_mtab(name);
221#endif
222 return (TRUE);
223 }
224 return (FALSE);
225}
226
227static int umount_all(int useMtab)
228{
229 int status = TRUE;
230 char *mountpt;
231 void *iter;
232
233 for (mountpt = mtab_first(&iter); mountpt; mountpt = mtab_next(&iter)) {
234 /* Never umount /proc on a umount -a */
235 if (strstr(mountpt, "proc")!= NULL)
236 continue;
237 status = do_umount(mountpt, useMtab);
238 if (status != 0) {
239 /* Don't bother retrying the umount on busy devices */
240 if (errno == EBUSY) {
241 perror(mountpt);
242 continue;
243 }
244 status = do_umount(mountpt, useMtab);
245 if (status != 0) {
246 printf("Couldn't umount %s on %s: %s\n",
247 mountpt, mtab_getinfo(mountpt, MTAB_GETDEVICE),
248 strerror(errno));
249 }
250 }
251 }
252 return (status);
253}
254
255extern int umount_main(int argc, char **argv)
256{
257 if (argc < 2) {
258 usage(umount_usage);
259 }
260
261 /* Parse any options */
262 while (--argc > 0 && **(++argv) == '-') {
263 while (*++(*argv))
264 switch (**argv) {
265 case 'a':
266 umountAll = TRUE;
267 break;
Erik Andersence917322000-03-13 04:07:02 +0000268#if defined BB_FEATURE_MOUNT_LOOP
Erik Andersen6c5f2c62000-05-05 19:49:33 +0000269 case 'l':
Erik Andersence917322000-03-13 04:07:02 +0000270 freeLoop = FALSE;
271 break;
272#endif
Erik Andersene132f4b2000-02-09 04:16:43 +0000273#ifdef BB_MTAB
274 case 'n':
275 useMtab = FALSE;
276 break;
277#endif
Erik Andersen6c5f2c62000-05-05 19:49:33 +0000278#ifdef BB_FEATURE_MOUNT_FORCE
279 case 'f':
280 doForce = TRUE;
281 break;
282#endif
Erik Andersene132f4b2000-02-09 04:16:43 +0000283 case 'r':
284 doRemount = TRUE;
285 break;
Erik Andersen983b51b2000-04-04 18:14:25 +0000286 case 'v':
287 break; /* ignore -v */
Erik Andersene132f4b2000-02-09 04:16:43 +0000288 default:
289 usage(umount_usage);
290 }
291 }
292
293 mtab_read();
294 if (umountAll == TRUE) {
295 exit(umount_all(useMtab));
296 }
297 if (do_umount(*argv, useMtab) == 0)
298 exit(TRUE);
Eric Andersene1e23ee2000-06-19 18:38:51 +0000299 perror("umount");
300 return(FALSE);
Erik Andersene132f4b2000-02-09 04:16:43 +0000301}
302