blob: 61f7f902803bc53c66a9e1e57872cbe160f2a1de [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#include <linux/unistd.h>
30
31
Eric Andersenbd4a75f2000-06-19 20:03:48 +000032//#include <sys/mount.h>
Eric Andersene1e23ee2000-06-19 18:38:51 +000033/* Include our own version of sys/mount.h, since libc5 doesn't
34 * know about umount2 */
35static _syscall1(int, umount, const char *, special_file);
36static _syscall2(int, umount2, const char *, special_file, int, flags);
37static _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. */
Eric Andersencc8ed391999-10-05 16:24:54 +000043
Erik Andersene132f4b2000-02-09 04:16:43 +000044
Erik Andersene49d5ec2000-02-08 19:58:47 +000045static const char umount_usage[] =
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000046 "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"
Eric Andersend0246fb1999-11-04 21:18:07 +000050#ifdef BB_MTAB
Erik Andersene49d5ec2000-02-08 19:58:47 +000051 " in /etc/mtab\n\t-n:\tDon't erase /etc/mtab entries\n"
Eric Andersend0246fb1999-11-04 21:18:07 +000052#else
Erik Andersene49d5ec2000-02-08 19:58:47 +000053 "\n"
Eric Andersend0246fb1999-11-04 21:18:07 +000054#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000055 "\t-r:\tTry to remount devices as read-only if mount is busy\n"
Erik Andersen6c5f2c62000-05-05 19:49:33 +000056#if defined BB_FEATURE_MOUNT_FORCE
57 "\t-f:\tForce filesystem umount (i.e. unreachable NFS server)\n"
58#endif
Erik Andersence917322000-03-13 04:07:02 +000059#if defined BB_FEATURE_MOUNT_LOOP
Erik Andersen6c5f2c62000-05-05 19:49:33 +000060 "\t-l:\tDo not free loop device (if a loop device has been used)\n"
Erik Andersence917322000-03-13 04:07:02 +000061#endif
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000062#endif
Eric Andersend0246fb1999-11-04 21:18:07 +000063;
64
Erik Andersenfac10d72000-02-07 05:29:42 +000065struct _mtab_entry_t {
Erik Andersene49d5ec2000-02-08 19:58:47 +000066 char *device;
67 char *mountpt;
68 struct _mtab_entry_t *next;
Erik Andersenfac10d72000-02-07 05:29:42 +000069};
70
71static struct _mtab_entry_t *mtab_cache = NULL;
72
73
Eric Andersend0246fb1999-11-04 21:18:07 +000074
Erik Andersen6c5f2c62000-05-05 19:49:33 +000075#if defined BB_FEATURE_MOUNT_FORCE
76static int doForce = FALSE;
77#endif
Erik Andersence917322000-03-13 04:07:02 +000078#if defined BB_FEATURE_MOUNT_LOOP
79static int freeLoop = TRUE;
80#endif
Eric Andersend0246fb1999-11-04 21:18:07 +000081static int useMtab = TRUE;
82static int umountAll = FALSE;
Erik Andersenfac10d72000-02-07 05:29:42 +000083static int doRemount = FALSE;
Erik Andersene49d5ec2000-02-08 19:58:47 +000084extern const char mtab_file[]; /* Defined in utility.c */
Eric Andersend0246fb1999-11-04 21:18:07 +000085
Erik Andersenfac10d72000-02-07 05:29:42 +000086
Erik Andersen5b911dd2000-02-23 22:49:58 +000087
Erik Andersenfac10d72000-02-07 05:29:42 +000088/* 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
Erik Andersen5b911dd2000-02-23 22:49:58 +000091 *
92 * TODO: Perhaps switch to using Glibc's getmntent_r
93 * -Erik
Erik Andersenfac10d72000-02-07 05:29:42 +000094 */
95void mtab_read(void)
96{
Erik Andersene49d5ec2000-02-08 19:58:47 +000097 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))) {
Erik Andersen0d068a22000-03-21 22:32:57 +0000109 entry = xmalloc(sizeof(struct _mtab_entry_t));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000110 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);
Erik Andersenfac10d72000-02-07 05:29:42 +0000116}
117
118char *mtab_getinfo(const char *match, const char which)
119{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000120 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 {
Erik Andersenfac10d72000-02-07 05:29:42 +0000128#if !defined BB_MTAB
Erik Andersene49d5ec2000-02-08 19:58:47 +0000129 if (strcmp(cur->device, "/dev/root") == 0) {
Erik Andersenec5bd902000-03-22 07:12:05 +0000130 /* 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);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000134 }
Erik Andersenfac10d72000-02-07 05:29:42 +0000135#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000136 return cur->device;
137 }
138 }
139 cur = cur->next;
Erik Andersenfac10d72000-02-07 05:29:42 +0000140 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000141 return NULL;
Erik Andersenfac10d72000-02-07 05:29:42 +0000142}
143
144char *mtab_first(void **iter)
145{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000146 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);
Erik Andersenfac10d72000-02-07 05:29:42 +0000153}
154
155char *mtab_next(void **iter)
156{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000157 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;
Erik Andersenfac10d72000-02-07 05:29:42 +0000164}
165
Erik Andersen298854f2000-03-23 01:09:18 +0000166/* Don't bother to clean up, since exit() does that
167 * automagically, so we can save a few bytes */
168#if 0
Erik Andersenfac10d72000-02-07 05:29:42 +0000169void mtab_free(void)
170{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000171 struct _mtab_entry_t *this, *next;
Erik Andersenfac10d72000-02-07 05:29:42 +0000172
Erik Andersene49d5ec2000-02-08 19:58:47 +0000173 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 }
Erik Andersenfac10d72000-02-07 05:29:42 +0000183}
Erik Andersen298854f2000-03-23 01:09:18 +0000184#endif
Erik Andersene132f4b2000-02-09 04:16:43 +0000185
186static 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
Erik Andersence917322000-03-13 04:07:02 +0000197 if (freeLoop == TRUE && blockDevice != NULL && !strncmp("/dev/loop", blockDevice, 9))
Erik Andersene132f4b2000-02-09 04:16:43 +0000198 /* this was a loop device, delete it */
199 del_loop(blockDevice);
200#endif
Erik Andersen6c5f2c62000-05-05 19:49:33 +0000201#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
Erik Andersene132f4b2000-02-09 04:16:43 +0000209 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);
Erik Andersene132f4b2000-02-09 04:16:43 +0000215 } else {
216 fprintf(stderr, "umount: Cannot remount %s read-only\n",
217 blockDevice);
218 }
219 }
Erik Andersene132f4b2000-02-09 04:16:43 +0000220 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
230static 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
258extern 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;
Erik Andersence917322000-03-13 04:07:02 +0000271#if defined BB_FEATURE_MOUNT_LOOP
Erik Andersen6c5f2c62000-05-05 19:49:33 +0000272 case 'l':
Erik Andersence917322000-03-13 04:07:02 +0000273 freeLoop = FALSE;
274 break;
275#endif
Erik Andersene132f4b2000-02-09 04:16:43 +0000276#ifdef BB_MTAB
277 case 'n':
278 useMtab = FALSE;
279 break;
280#endif
Erik Andersen6c5f2c62000-05-05 19:49:33 +0000281#ifdef BB_FEATURE_MOUNT_FORCE
282 case 'f':
283 doForce = TRUE;
284 break;
285#endif
Erik Andersene132f4b2000-02-09 04:16:43 +0000286 case 'r':
287 doRemount = TRUE;
288 break;
Erik Andersen983b51b2000-04-04 18:14:25 +0000289 case 'v':
290 break; /* ignore -v */
Erik Andersene132f4b2000-02-09 04:16:43 +0000291 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);
Eric Andersene1e23ee2000-06-19 18:38:51 +0000302 perror("umount");
303 return(FALSE);
Erik Andersene132f4b2000-02-09 04:16:43 +0000304}
305