blob: 8565744f256926a94e42ea4d45a806fb35560519 [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 *
Eric Andersen8ec10a92001-01-27 09:33:39 +00006 * Copyright (C) 1999,2000,2001 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
Matt Kraaiadcbc122001-05-02 21:24:51 +000025#include <limits.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 Andersened3ef502001-01-27 08:24:39 +000029#include <string.h>
30#include <stdlib.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000031#include "busybox.h"
Eric Andersene1e23ee2000-06-19 18:38:51 +000032
33
Mark Whitley59ab0252001-01-23 22:30:04 +000034static const int MNT_FORCE = 1;
35static const int MS_MGC_VAL = 0xc0ed0000; /* Magic number indicatng "new" flags */
36static const int MS_REMOUNT = 32; /* Alter flags of a mounted FS. */
37static const int MS_RDONLY = 1; /* Mount read-only. */
Eric Andersen2cd439f2000-07-08 19:10:29 +000038
Eric Andersena57ba4d2000-07-08 19:20:49 +000039extern int mount (__const char *__special_file, __const char *__dir,
40 __const char *__fstype, unsigned long int __rwflag,
41 __const void *__data);
42extern int umount (__const char *__special_file);
43extern int umount2 (__const char *__special_file, int __flags);
Eric Andersen2cd439f2000-07-08 19:10:29 +000044
Erik Andersenfac10d72000-02-07 05:29:42 +000045struct _mtab_entry_t {
Erik Andersene49d5ec2000-02-08 19:58:47 +000046 char *device;
47 char *mountpt;
48 struct _mtab_entry_t *next;
Erik Andersenfac10d72000-02-07 05:29:42 +000049};
50
51static struct _mtab_entry_t *mtab_cache = NULL;
52
53
Eric Andersend0246fb1999-11-04 21:18:07 +000054
Erik Andersen6c5f2c62000-05-05 19:49:33 +000055#if defined BB_FEATURE_MOUNT_FORCE
56static int doForce = FALSE;
57#endif
Erik Andersence917322000-03-13 04:07:02 +000058#if defined BB_FEATURE_MOUNT_LOOP
59static int freeLoop = TRUE;
60#endif
Eric Andersenc4cef5a2001-04-01 16:01:11 +000061#if defined BB_FEATURE_MTAB_SUPPORT
Eric Andersend0246fb1999-11-04 21:18:07 +000062static int useMtab = TRUE;
Eric Andersen1ca20a72001-03-21 07:34:27 +000063#endif
Eric Andersend0246fb1999-11-04 21:18:07 +000064static int umountAll = FALSE;
Erik Andersenfac10d72000-02-07 05:29:42 +000065static int doRemount = FALSE;
Erik Andersene49d5ec2000-02-08 19:58:47 +000066extern const char mtab_file[]; /* Defined in utility.c */
Eric Andersend0246fb1999-11-04 21:18:07 +000067
Erik Andersenfac10d72000-02-07 05:29:42 +000068
Erik Andersen5b911dd2000-02-23 22:49:58 +000069
Erik Andersenfac10d72000-02-07 05:29:42 +000070/* These functions are here because the getmntent functions do not appear
71 * to be re-entrant, which leads to all sorts of problems when we try to
72 * use them recursively - randolph
Erik Andersen5b911dd2000-02-23 22:49:58 +000073 *
74 * TODO: Perhaps switch to using Glibc's getmntent_r
75 * -Erik
Erik Andersenfac10d72000-02-07 05:29:42 +000076 */
Eric Andersenc911a432001-05-15 17:42:16 +000077static void mtab_read(void)
Erik Andersenfac10d72000-02-07 05:29:42 +000078{
Erik Andersene49d5ec2000-02-08 19:58:47 +000079 struct _mtab_entry_t *entry = NULL;
80 struct mntent *e;
81 FILE *fp;
82
83 if (mtab_cache != NULL)
84 return;
85
86 if ((fp = setmntent(mtab_file, "r")) == NULL) {
Matt Kraaidd19c692001-01-31 19:00:21 +000087 error_msg("Cannot open %s", mtab_file);
Erik Andersene49d5ec2000-02-08 19:58:47 +000088 return;
89 }
90 while ((e = getmntent(fp))) {
Erik Andersen0d068a22000-03-21 22:32:57 +000091 entry = xmalloc(sizeof(struct _mtab_entry_t));
Erik Andersene49d5ec2000-02-08 19:58:47 +000092 entry->device = strdup(e->mnt_fsname);
93 entry->mountpt = strdup(e->mnt_dir);
94 entry->next = mtab_cache;
95 mtab_cache = entry;
96 }
97 endmntent(fp);
Erik Andersenfac10d72000-02-07 05:29:42 +000098}
99
Eric Andersenc911a432001-05-15 17:42:16 +0000100static char *mtab_getinfo(const char *match, const char which)
Erik Andersenfac10d72000-02-07 05:29:42 +0000101{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000102 struct _mtab_entry_t *cur = mtab_cache;
103
104 while (cur) {
105 if (strcmp(cur->mountpt, match) == 0 ||
106 strcmp(cur->device, match) == 0) {
107 if (which == MTAB_GETMOUNTPT) {
108 return cur->mountpt;
109 } else {
Eric Andersenc4cef5a2001-04-01 16:01:11 +0000110#if !defined BB_FEATURE_MTAB_SUPPORT
Erik Andersene49d5ec2000-02-08 19:58:47 +0000111 if (strcmp(cur->device, "/dev/root") == 0) {
Erik Andersenec5bd902000-03-22 07:12:05 +0000112 /* Adjusts device to be the real root device,
113 * or leaves device alone if it can't find it */
Eric Andersenc911a432001-05-15 17:42:16 +0000114 cur->device = find_real_root_device_name(cur->device);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000115 }
Erik Andersenfac10d72000-02-07 05:29:42 +0000116#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000117 return cur->device;
118 }
119 }
120 cur = cur->next;
Erik Andersenfac10d72000-02-07 05:29:42 +0000121 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000122 return NULL;
Erik Andersenfac10d72000-02-07 05:29:42 +0000123}
124
Eric Andersenc911a432001-05-15 17:42:16 +0000125static char *mtab_next(void **iter)
Erik Andersenfac10d72000-02-07 05:29:42 +0000126{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000127 char *mp;
128
129 if (iter == NULL || *iter == NULL)
130 return NULL;
131 mp = ((struct _mtab_entry_t *) (*iter))->mountpt;
132 *iter = (void *) ((struct _mtab_entry_t *) (*iter))->next;
133 return mp;
Erik Andersenfac10d72000-02-07 05:29:42 +0000134}
135
Eric Andersenc911a432001-05-15 17:42:16 +0000136static char *mtab_first(void **iter)
137{
138 struct _mtab_entry_t *mtab_iter;
139
140 if (!iter)
141 return NULL;
142 mtab_iter = mtab_cache;
143 *iter = (void *) mtab_iter;
144 return mtab_next(iter);
145}
146
Erik Andersen298854f2000-03-23 01:09:18 +0000147/* Don't bother to clean up, since exit() does that
148 * automagically, so we can save a few bytes */
Eric Andersenb040d4f2000-07-25 18:01:20 +0000149#ifdef BB_FEATURE_CLEAN_UP
Eric Andersenc911a432001-05-15 17:42:16 +0000150static void mtab_free(void)
Erik Andersenfac10d72000-02-07 05:29:42 +0000151{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000152 struct _mtab_entry_t *this, *next;
Erik Andersenfac10d72000-02-07 05:29:42 +0000153
Erik Andersene49d5ec2000-02-08 19:58:47 +0000154 this = mtab_cache;
155 while (this) {
156 next = this->next;
157 if (this->device)
158 free(this->device);
159 if (this->mountpt)
160 free(this->mountpt);
161 free(this);
162 this = next;
163 }
Erik Andersenfac10d72000-02-07 05:29:42 +0000164}
Erik Andersen298854f2000-03-23 01:09:18 +0000165#endif
Erik Andersene132f4b2000-02-09 04:16:43 +0000166
Eric Andersen1ca20a72001-03-21 07:34:27 +0000167static int do_umount(const char *name)
Erik Andersene132f4b2000-02-09 04:16:43 +0000168{
169 int status;
170 char *blockDevice = mtab_getinfo(name, MTAB_GETDEVICE);
171
172 if (blockDevice && strcmp(blockDevice, name) == 0)
173 name = mtab_getinfo(blockDevice, MTAB_GETMOUNTPT);
174
175 status = umount(name);
176
177#if defined BB_FEATURE_MOUNT_LOOP
Erik Andersence917322000-03-13 04:07:02 +0000178 if (freeLoop == TRUE && blockDevice != NULL && !strncmp("/dev/loop", blockDevice, 9))
Erik Andersene132f4b2000-02-09 04:16:43 +0000179 /* this was a loop device, delete it */
180 del_loop(blockDevice);
181#endif
Erik Andersen6c5f2c62000-05-05 19:49:33 +0000182#if defined BB_FEATURE_MOUNT_FORCE
183 if (status != 0 && doForce == TRUE) {
184 status = umount2(blockDevice, MNT_FORCE);
185 if (status != 0) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000186 error_msg_and_die("forced umount of %s failed!", blockDevice);
Erik Andersen6c5f2c62000-05-05 19:49:33 +0000187 }
188 }
189#endif
Erik Andersene132f4b2000-02-09 04:16:43 +0000190 if (status != 0 && doRemount == TRUE && errno == EBUSY) {
191 status = mount(blockDevice, name, NULL,
192 MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
193 if (status == 0) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000194 error_msg("%s busy - remounted read-only", blockDevice);
Erik Andersene132f4b2000-02-09 04:16:43 +0000195 } else {
Matt Kraaidd19c692001-01-31 19:00:21 +0000196 error_msg("Cannot remount %s read-only", blockDevice);
Erik Andersene132f4b2000-02-09 04:16:43 +0000197 }
198 }
Erik Andersene132f4b2000-02-09 04:16:43 +0000199 if (status == 0) {
Eric Andersenc4cef5a2001-04-01 16:01:11 +0000200#if defined BB_FEATURE_MTAB_SUPPORT
Erik Andersene132f4b2000-02-09 04:16:43 +0000201 if (useMtab == TRUE)
202 erase_mtab(name);
203#endif
204 return (TRUE);
205 }
206 return (FALSE);
207}
208
Eric Andersen1ca20a72001-03-21 07:34:27 +0000209static int umount_all(void)
Erik Andersene132f4b2000-02-09 04:16:43 +0000210{
211 int status = TRUE;
212 char *mountpt;
213 void *iter;
214
215 for (mountpt = mtab_first(&iter); mountpt; mountpt = mtab_next(&iter)) {
216 /* Never umount /proc on a umount -a */
217 if (strstr(mountpt, "proc")!= NULL)
218 continue;
Eric Andersen1ca20a72001-03-21 07:34:27 +0000219 if (!do_umount(mountpt)) {
Erik Andersene132f4b2000-02-09 04:16:43 +0000220 /* Don't bother retrying the umount on busy devices */
221 if (errno == EBUSY) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000222 perror_msg("%s", mountpt);
Matt Kraaifd4c58d2001-01-17 00:12:11 +0000223 status = FALSE;
Erik Andersene132f4b2000-02-09 04:16:43 +0000224 continue;
225 }
Eric Andersen1ca20a72001-03-21 07:34:27 +0000226 if (!do_umount(mountpt)) {
Erik Andersene132f4b2000-02-09 04:16:43 +0000227 printf("Couldn't umount %s on %s: %s\n",
228 mountpt, mtab_getinfo(mountpt, MTAB_GETDEVICE),
229 strerror(errno));
Matt Kraaifd4c58d2001-01-17 00:12:11 +0000230 status = FALSE;
Erik Andersene132f4b2000-02-09 04:16:43 +0000231 }
232 }
233 }
234 return (status);
235}
236
237extern int umount_main(int argc, char **argv)
238{
Matt Kraaiadcbc122001-05-02 21:24:51 +0000239 char path[PATH_MAX];
240
Erik Andersene132f4b2000-02-09 04:16:43 +0000241 if (argc < 2) {
Eric Andersen67991cf2001-02-14 21:23:06 +0000242 show_usage();
Erik Andersene132f4b2000-02-09 04:16:43 +0000243 }
Eric Andersenb040d4f2000-07-25 18:01:20 +0000244#ifdef BB_FEATURE_CLEAN_UP
245 atexit(mtab_free);
246#endif
Erik Andersene132f4b2000-02-09 04:16:43 +0000247
248 /* Parse any options */
249 while (--argc > 0 && **(++argv) == '-') {
250 while (*++(*argv))
251 switch (**argv) {
252 case 'a':
253 umountAll = TRUE;
254 break;
Erik Andersence917322000-03-13 04:07:02 +0000255#if defined BB_FEATURE_MOUNT_LOOP
Erik Andersen6c5f2c62000-05-05 19:49:33 +0000256 case 'l':
Erik Andersence917322000-03-13 04:07:02 +0000257 freeLoop = FALSE;
258 break;
259#endif
Eric Andersenc4cef5a2001-04-01 16:01:11 +0000260#ifdef BB_FEATURE_MTAB_SUPPORT
Erik Andersene132f4b2000-02-09 04:16:43 +0000261 case 'n':
262 useMtab = FALSE;
263 break;
264#endif
Erik Andersen6c5f2c62000-05-05 19:49:33 +0000265#ifdef BB_FEATURE_MOUNT_FORCE
266 case 'f':
267 doForce = TRUE;
268 break;
269#endif
Erik Andersene132f4b2000-02-09 04:16:43 +0000270 case 'r':
271 doRemount = TRUE;
272 break;
Erik Andersen983b51b2000-04-04 18:14:25 +0000273 case 'v':
274 break; /* ignore -v */
Erik Andersene132f4b2000-02-09 04:16:43 +0000275 default:
Eric Andersen67991cf2001-02-14 21:23:06 +0000276 show_usage();
Erik Andersene132f4b2000-02-09 04:16:43 +0000277 }
278 }
279
280 mtab_read();
281 if (umountAll == TRUE) {
Eric Andersen1ca20a72001-03-21 07:34:27 +0000282 if (umount_all() == TRUE)
Matt Kraai3e856ce2000-12-01 02:55:13 +0000283 return EXIT_SUCCESS;
284 else
285 return EXIT_FAILURE;
Erik Andersene132f4b2000-02-09 04:16:43 +0000286 }
Matt Kraaiadcbc122001-05-02 21:24:51 +0000287 if (realpath(*argv, path) == NULL)
288 perror_msg_and_die("%s", path);
289 if (do_umount(path) == TRUE)
Matt Kraai3e856ce2000-12-01 02:55:13 +0000290 return EXIT_SUCCESS;
Matt Kraaia9819b22000-12-22 01:48:07 +0000291 perror_msg_and_die("%s", *argv);
Erik Andersene132f4b2000-02-09 04:16:43 +0000292}
293