blob: 99db3084cf1d15ef7394e8ff28b14da2d1a72c27 [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 Andersenbdfd0d72001-10-24 05:00:29 +00005 * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen
6 * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org>
Eric Andersenf811e071999-10-09 00:25:00 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
Matt Kraaiadcbc122001-05-02 21:24:51 +000024#include <limits.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000025#include <stdio.h>
Eric Andersenf811e071999-10-09 00:25:00 +000026#include <mntent.h>
Eric Andersenf811e071999-10-09 00:25:00 +000027#include <errno.h>
Eric Andersened3ef502001-01-27 08:24:39 +000028#include <string.h>
29#include <stdlib.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000030#include "busybox.h"
Eric Andersene1e23ee2000-06-19 18:38:51 +000031
Eric Andersen8a915882001-08-02 09:55:58 +000032/* Teach libc5 about realpath -- it includes it but the
33 * prototype is missing... */
34#if (__GLIBC__ <= 2) && (__GLIBC_MINOR__ < 1)
35extern char *realpath(const char *path, char *resolved_path);
36#endif
Eric Andersene1e23ee2000-06-19 18:38:51 +000037
Mark Whitley59ab0252001-01-23 22:30:04 +000038static const int MNT_FORCE = 1;
39static const int MS_MGC_VAL = 0xc0ed0000; /* Magic number indicatng "new" flags */
40static const int MS_REMOUNT = 32; /* Alter flags of a mounted FS. */
41static const int MS_RDONLY = 1; /* Mount read-only. */
Eric Andersen2cd439f2000-07-08 19:10:29 +000042
Eric Andersena57ba4d2000-07-08 19:20:49 +000043extern int mount (__const char *__special_file, __const char *__dir,
44 __const char *__fstype, unsigned long int __rwflag,
45 __const void *__data);
46extern int umount (__const char *__special_file);
47extern int umount2 (__const char *__special_file, int __flags);
Eric Andersen2cd439f2000-07-08 19:10:29 +000048
Erik Andersenfac10d72000-02-07 05:29:42 +000049struct _mtab_entry_t {
Erik Andersene49d5ec2000-02-08 19:58:47 +000050 char *device;
51 char *mountpt;
52 struct _mtab_entry_t *next;
Erik Andersenfac10d72000-02-07 05:29:42 +000053};
54
55static struct _mtab_entry_t *mtab_cache = NULL;
56
57
Eric Andersend0246fb1999-11-04 21:18:07 +000058
Eric Andersenbdfd0d72001-10-24 05:00:29 +000059#if defined CONFIG_FEATURE_MOUNT_FORCE
Erik Andersen6c5f2c62000-05-05 19:49:33 +000060static int doForce = FALSE;
61#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +000062#if defined CONFIG_FEATURE_MOUNT_LOOP
Erik Andersence917322000-03-13 04:07:02 +000063static int freeLoop = TRUE;
64#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +000065#if defined CONFIG_FEATURE_MTAB_SUPPORT
Eric Andersend0246fb1999-11-04 21:18:07 +000066static int useMtab = TRUE;
Eric Andersen1ca20a72001-03-21 07:34:27 +000067#endif
Eric Andersend0246fb1999-11-04 21:18:07 +000068static int umountAll = FALSE;
Erik Andersenfac10d72000-02-07 05:29:42 +000069static int doRemount = FALSE;
Erik Andersene49d5ec2000-02-08 19:58:47 +000070extern const char mtab_file[]; /* Defined in utility.c */
Eric Andersend0246fb1999-11-04 21:18:07 +000071
Erik Andersenfac10d72000-02-07 05:29:42 +000072
Erik Andersen5b911dd2000-02-23 22:49:58 +000073
Erik Andersenfac10d72000-02-07 05:29:42 +000074/* These functions are here because the getmntent functions do not appear
75 * to be re-entrant, which leads to all sorts of problems when we try to
76 * use them recursively - randolph
Erik Andersen5b911dd2000-02-23 22:49:58 +000077 *
78 * TODO: Perhaps switch to using Glibc's getmntent_r
79 * -Erik
Erik Andersenfac10d72000-02-07 05:29:42 +000080 */
Eric Andersenc911a432001-05-15 17:42:16 +000081static void mtab_read(void)
Erik Andersenfac10d72000-02-07 05:29:42 +000082{
Erik Andersene49d5ec2000-02-08 19:58:47 +000083 struct _mtab_entry_t *entry = NULL;
84 struct mntent *e;
85 FILE *fp;
86
87 if (mtab_cache != NULL)
88 return;
89
90 if ((fp = setmntent(mtab_file, "r")) == NULL) {
Matt Kraaidd19c692001-01-31 19:00:21 +000091 error_msg("Cannot open %s", mtab_file);
Erik Andersene49d5ec2000-02-08 19:58:47 +000092 return;
93 }
94 while ((e = getmntent(fp))) {
Erik Andersen0d068a22000-03-21 22:32:57 +000095 entry = xmalloc(sizeof(struct _mtab_entry_t));
Erik Andersene49d5ec2000-02-08 19:58:47 +000096 entry->device = strdup(e->mnt_fsname);
97 entry->mountpt = strdup(e->mnt_dir);
98 entry->next = mtab_cache;
99 mtab_cache = entry;
100 }
101 endmntent(fp);
Erik Andersenfac10d72000-02-07 05:29:42 +0000102}
103
Eric Andersenc911a432001-05-15 17:42:16 +0000104static char *mtab_getinfo(const char *match, const char which)
Erik Andersenfac10d72000-02-07 05:29:42 +0000105{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000106 struct _mtab_entry_t *cur = mtab_cache;
107
108 while (cur) {
109 if (strcmp(cur->mountpt, match) == 0 ||
110 strcmp(cur->device, match) == 0) {
111 if (which == MTAB_GETMOUNTPT) {
112 return cur->mountpt;
113 } else {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000114#if !defined CONFIG_FEATURE_MTAB_SUPPORT
Erik Andersene49d5ec2000-02-08 19:58:47 +0000115 if (strcmp(cur->device, "/dev/root") == 0) {
Erik Andersenec5bd902000-03-22 07:12:05 +0000116 /* Adjusts device to be the real root device,
117 * or leaves device alone if it can't find it */
Eric Andersenc911a432001-05-15 17:42:16 +0000118 cur->device = find_real_root_device_name(cur->device);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000119 }
Erik Andersenfac10d72000-02-07 05:29:42 +0000120#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000121 return cur->device;
122 }
123 }
124 cur = cur->next;
Erik Andersenfac10d72000-02-07 05:29:42 +0000125 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000126 return NULL;
Erik Andersenfac10d72000-02-07 05:29:42 +0000127}
128
Eric Andersenc911a432001-05-15 17:42:16 +0000129static char *mtab_next(void **iter)
Erik Andersenfac10d72000-02-07 05:29:42 +0000130{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000131 char *mp;
132
133 if (iter == NULL || *iter == NULL)
134 return NULL;
135 mp = ((struct _mtab_entry_t *) (*iter))->mountpt;
136 *iter = (void *) ((struct _mtab_entry_t *) (*iter))->next;
137 return mp;
Erik Andersenfac10d72000-02-07 05:29:42 +0000138}
139
Eric Andersenc911a432001-05-15 17:42:16 +0000140static char *mtab_first(void **iter)
141{
142 struct _mtab_entry_t *mtab_iter;
143
144 if (!iter)
145 return NULL;
146 mtab_iter = mtab_cache;
147 *iter = (void *) mtab_iter;
148 return mtab_next(iter);
149}
150
Erik Andersen298854f2000-03-23 01:09:18 +0000151/* Don't bother to clean up, since exit() does that
152 * automagically, so we can save a few bytes */
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000153#ifdef CONFIG_FEATURE_CLEAN_UP
Eric Andersenc911a432001-05-15 17:42:16 +0000154static void mtab_free(void)
Erik Andersenfac10d72000-02-07 05:29:42 +0000155{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000156 struct _mtab_entry_t *this, *next;
Erik Andersenfac10d72000-02-07 05:29:42 +0000157
Erik Andersene49d5ec2000-02-08 19:58:47 +0000158 this = mtab_cache;
159 while (this) {
160 next = this->next;
161 if (this->device)
162 free(this->device);
163 if (this->mountpt)
164 free(this->mountpt);
165 free(this);
166 this = next;
167 }
Erik Andersenfac10d72000-02-07 05:29:42 +0000168}
Erik Andersen298854f2000-03-23 01:09:18 +0000169#endif
Erik Andersene132f4b2000-02-09 04:16:43 +0000170
Eric Andersen1ca20a72001-03-21 07:34:27 +0000171static int do_umount(const char *name)
Erik Andersene132f4b2000-02-09 04:16:43 +0000172{
173 int status;
174 char *blockDevice = mtab_getinfo(name, MTAB_GETDEVICE);
175
176 if (blockDevice && strcmp(blockDevice, name) == 0)
177 name = mtab_getinfo(blockDevice, MTAB_GETMOUNTPT);
178
179 status = umount(name);
180
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000181#if defined CONFIG_FEATURE_MOUNT_LOOP
Erik Andersence917322000-03-13 04:07:02 +0000182 if (freeLoop == TRUE && blockDevice != NULL && !strncmp("/dev/loop", blockDevice, 9))
Erik Andersene132f4b2000-02-09 04:16:43 +0000183 /* this was a loop device, delete it */
184 del_loop(blockDevice);
185#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000186#if defined CONFIG_FEATURE_MOUNT_FORCE
Erik Andersen6c5f2c62000-05-05 19:49:33 +0000187 if (status != 0 && doForce == TRUE) {
188 status = umount2(blockDevice, MNT_FORCE);
189 if (status != 0) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000190 error_msg_and_die("forced umount of %s failed!", blockDevice);
Erik Andersen6c5f2c62000-05-05 19:49:33 +0000191 }
192 }
193#endif
Erik Andersene132f4b2000-02-09 04:16:43 +0000194 if (status != 0 && doRemount == TRUE && errno == EBUSY) {
195 status = mount(blockDevice, name, NULL,
196 MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
197 if (status == 0) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000198 error_msg("%s busy - remounted read-only", blockDevice);
Erik Andersene132f4b2000-02-09 04:16:43 +0000199 } else {
Matt Kraaidd19c692001-01-31 19:00:21 +0000200 error_msg("Cannot remount %s read-only", blockDevice);
Erik Andersene132f4b2000-02-09 04:16:43 +0000201 }
202 }
Erik Andersene132f4b2000-02-09 04:16:43 +0000203 if (status == 0) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000204#if defined CONFIG_FEATURE_MTAB_SUPPORT
Erik Andersene132f4b2000-02-09 04:16:43 +0000205 if (useMtab == TRUE)
206 erase_mtab(name);
207#endif
208 return (TRUE);
209 }
210 return (FALSE);
211}
212
Eric Andersen1ca20a72001-03-21 07:34:27 +0000213static int umount_all(void)
Erik Andersene132f4b2000-02-09 04:16:43 +0000214{
215 int status = TRUE;
216 char *mountpt;
217 void *iter;
218
219 for (mountpt = mtab_first(&iter); mountpt; mountpt = mtab_next(&iter)) {
220 /* Never umount /proc on a umount -a */
221 if (strstr(mountpt, "proc")!= NULL)
222 continue;
Eric Andersen1ca20a72001-03-21 07:34:27 +0000223 if (!do_umount(mountpt)) {
Erik Andersene132f4b2000-02-09 04:16:43 +0000224 /* Don't bother retrying the umount on busy devices */
225 if (errno == EBUSY) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000226 perror_msg("%s", mountpt);
Matt Kraaifd4c58d2001-01-17 00:12:11 +0000227 status = FALSE;
Erik Andersene132f4b2000-02-09 04:16:43 +0000228 continue;
229 }
Eric Andersen1ca20a72001-03-21 07:34:27 +0000230 if (!do_umount(mountpt)) {
Erik Andersene132f4b2000-02-09 04:16:43 +0000231 printf("Couldn't umount %s on %s: %s\n",
232 mountpt, mtab_getinfo(mountpt, MTAB_GETDEVICE),
233 strerror(errno));
Matt Kraaifd4c58d2001-01-17 00:12:11 +0000234 status = FALSE;
Erik Andersene132f4b2000-02-09 04:16:43 +0000235 }
236 }
237 }
238 return (status);
239}
240
241extern int umount_main(int argc, char **argv)
242{
Matt Kraaiadcbc122001-05-02 21:24:51 +0000243 char path[PATH_MAX];
244
Erik Andersene132f4b2000-02-09 04:16:43 +0000245 if (argc < 2) {
Eric Andersen67991cf2001-02-14 21:23:06 +0000246 show_usage();
Erik Andersene132f4b2000-02-09 04:16:43 +0000247 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000248#ifdef CONFIG_FEATURE_CLEAN_UP
Eric Andersenb040d4f2000-07-25 18:01:20 +0000249 atexit(mtab_free);
250#endif
Erik Andersene132f4b2000-02-09 04:16:43 +0000251
252 /* Parse any options */
253 while (--argc > 0 && **(++argv) == '-') {
254 while (*++(*argv))
255 switch (**argv) {
256 case 'a':
257 umountAll = TRUE;
258 break;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000259#if defined CONFIG_FEATURE_MOUNT_LOOP
Erik Andersen6c5f2c62000-05-05 19:49:33 +0000260 case 'l':
Erik Andersence917322000-03-13 04:07:02 +0000261 freeLoop = FALSE;
262 break;
263#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000264#ifdef CONFIG_FEATURE_MTAB_SUPPORT
Erik Andersene132f4b2000-02-09 04:16:43 +0000265 case 'n':
266 useMtab = FALSE;
267 break;
268#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000269#ifdef CONFIG_FEATURE_MOUNT_FORCE
Erik Andersen6c5f2c62000-05-05 19:49:33 +0000270 case 'f':
271 doForce = TRUE;
272 break;
273#endif
Erik Andersene132f4b2000-02-09 04:16:43 +0000274 case 'r':
275 doRemount = TRUE;
276 break;
Erik Andersen983b51b2000-04-04 18:14:25 +0000277 case 'v':
278 break; /* ignore -v */
Erik Andersene132f4b2000-02-09 04:16:43 +0000279 default:
Eric Andersen67991cf2001-02-14 21:23:06 +0000280 show_usage();
Erik Andersene132f4b2000-02-09 04:16:43 +0000281 }
282 }
283
284 mtab_read();
285 if (umountAll == TRUE) {
Eric Andersen1ca20a72001-03-21 07:34:27 +0000286 if (umount_all() == TRUE)
Matt Kraai3e856ce2000-12-01 02:55:13 +0000287 return EXIT_SUCCESS;
288 else
289 return EXIT_FAILURE;
Erik Andersene132f4b2000-02-09 04:16:43 +0000290 }
Matt Kraaiadcbc122001-05-02 21:24:51 +0000291 if (realpath(*argv, path) == NULL)
292 perror_msg_and_die("%s", path);
293 if (do_umount(path) == TRUE)
Matt Kraai3e856ce2000-12-01 02:55:13 +0000294 return EXIT_SUCCESS;
Matt Kraaia9819b22000-12-22 01:48:07 +0000295 perror_msg_and_die("%s", *argv);
Erik Andersene132f4b2000-02-09 04:16:43 +0000296}
297