blob: 59dcb6651b9589e929dd9336934a601d7dc6b068 [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) {
104 fprintf(stderr, "Cannot open %s\n", mtab_file);
105 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) {
212 fprintf(stderr, "umount: %s busy - remounted read-only\n",
213 blockDevice);
Erik Andersene132f4b2000-02-09 04:16:43 +0000214 } else {
215 fprintf(stderr, "umount: Cannot remount %s read-only\n",
216 blockDevice);
217 }
218 }
Erik Andersene132f4b2000-02-09 04:16:43 +0000219 if (status == 0) {
220#if defined BB_MTAB
221 if (useMtab == TRUE)
222 erase_mtab(name);
223#endif
224 return (TRUE);
225 }
226 return (FALSE);
227}
228
229static int umount_all(int useMtab)
230{
231 int status = TRUE;
232 char *mountpt;
233 void *iter;
234
235 for (mountpt = mtab_first(&iter); mountpt; mountpt = mtab_next(&iter)) {
236 /* Never umount /proc on a umount -a */
237 if (strstr(mountpt, "proc")!= NULL)
238 continue;
239 status = do_umount(mountpt, useMtab);
240 if (status != 0) {
241 /* Don't bother retrying the umount on busy devices */
242 if (errno == EBUSY) {
243 perror(mountpt);
244 continue;
245 }
246 status = do_umount(mountpt, useMtab);
247 if (status != 0) {
248 printf("Couldn't umount %s on %s: %s\n",
249 mountpt, mtab_getinfo(mountpt, MTAB_GETDEVICE),
250 strerror(errno));
251 }
252 }
253 }
254 return (status);
255}
256
257extern int umount_main(int argc, char **argv)
258{
259 if (argc < 2) {
260 usage(umount_usage);
261 }
262
263 /* Parse any options */
264 while (--argc > 0 && **(++argv) == '-') {
265 while (*++(*argv))
266 switch (**argv) {
267 case 'a':
268 umountAll = TRUE;
269 break;
Erik Andersence917322000-03-13 04:07:02 +0000270#if defined BB_FEATURE_MOUNT_LOOP
Erik Andersen6c5f2c62000-05-05 19:49:33 +0000271 case 'l':
Erik Andersence917322000-03-13 04:07:02 +0000272 freeLoop = FALSE;
273 break;
274#endif
Erik Andersene132f4b2000-02-09 04:16:43 +0000275#ifdef BB_MTAB
276 case 'n':
277 useMtab = FALSE;
278 break;
279#endif
Erik Andersen6c5f2c62000-05-05 19:49:33 +0000280#ifdef BB_FEATURE_MOUNT_FORCE
281 case 'f':
282 doForce = TRUE;
283 break;
284#endif
Erik Andersene132f4b2000-02-09 04:16:43 +0000285 case 'r':
286 doRemount = TRUE;
287 break;
Erik Andersen983b51b2000-04-04 18:14:25 +0000288 case 'v':
289 break; /* ignore -v */
Erik Andersene132f4b2000-02-09 04:16:43 +0000290 default:
291 usage(umount_usage);
292 }
293 }
294
295 mtab_read();
296 if (umountAll == TRUE) {
297 exit(umount_all(useMtab));
298 }
299 if (do_umount(*argv, useMtab) == 0)
300 exit(TRUE);
Eric Andersene1e23ee2000-06-19 18:38:51 +0000301 perror("umount");
302 return(FALSE);
Erik Andersene132f4b2000-02-09 04:16:43 +0000303}
304