blob: 74638d21c0899c4fa9c01be7ed8c7270a233786b [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
Eric Andersen8a915882001-08-02 09:55:58 +000033/* Teach libc5 about realpath -- it includes it but the
34 * prototype is missing... */
35#if (__GLIBC__ <= 2) && (__GLIBC_MINOR__ < 1)
36extern char *realpath(const char *path, char *resolved_path);
37#endif
Eric Andersene1e23ee2000-06-19 18:38:51 +000038
Mark Whitley59ab0252001-01-23 22:30:04 +000039static const int MNT_FORCE = 1;
40static const int MS_MGC_VAL = 0xc0ed0000; /* Magic number indicatng "new" flags */
41static const int MS_REMOUNT = 32; /* Alter flags of a mounted FS. */
42static const int MS_RDONLY = 1; /* Mount read-only. */
Eric Andersen2cd439f2000-07-08 19:10:29 +000043
Eric Andersena57ba4d2000-07-08 19:20:49 +000044extern int mount (__const char *__special_file, __const char *__dir,
45 __const char *__fstype, unsigned long int __rwflag,
46 __const void *__data);
47extern int umount (__const char *__special_file);
48extern int umount2 (__const char *__special_file, int __flags);
Eric Andersen2cd439f2000-07-08 19:10:29 +000049
Erik Andersenfac10d72000-02-07 05:29:42 +000050struct _mtab_entry_t {
Erik Andersene49d5ec2000-02-08 19:58:47 +000051 char *device;
52 char *mountpt;
53 struct _mtab_entry_t *next;
Erik Andersenfac10d72000-02-07 05:29:42 +000054};
55
56static struct _mtab_entry_t *mtab_cache = NULL;
57
58
Eric Andersend0246fb1999-11-04 21:18:07 +000059
Erik Andersen6c5f2c62000-05-05 19:49:33 +000060#if defined BB_FEATURE_MOUNT_FORCE
61static int doForce = FALSE;
62#endif
Erik Andersence917322000-03-13 04:07:02 +000063#if defined BB_FEATURE_MOUNT_LOOP
64static int freeLoop = TRUE;
65#endif
Eric Andersenc4cef5a2001-04-01 16:01:11 +000066#if defined BB_FEATURE_MTAB_SUPPORT
Eric Andersend0246fb1999-11-04 21:18:07 +000067static int useMtab = TRUE;
Eric Andersen1ca20a72001-03-21 07:34:27 +000068#endif
Eric Andersend0246fb1999-11-04 21:18:07 +000069static int umountAll = FALSE;
Erik Andersenfac10d72000-02-07 05:29:42 +000070static int doRemount = FALSE;
Erik Andersene49d5ec2000-02-08 19:58:47 +000071extern const char mtab_file[]; /* Defined in utility.c */
Eric Andersend0246fb1999-11-04 21:18:07 +000072
Erik Andersenfac10d72000-02-07 05:29:42 +000073
Erik Andersen5b911dd2000-02-23 22:49:58 +000074
Erik Andersenfac10d72000-02-07 05:29:42 +000075/* These functions are here because the getmntent functions do not appear
76 * to be re-entrant, which leads to all sorts of problems when we try to
77 * use them recursively - randolph
Erik Andersen5b911dd2000-02-23 22:49:58 +000078 *
79 * TODO: Perhaps switch to using Glibc's getmntent_r
80 * -Erik
Erik Andersenfac10d72000-02-07 05:29:42 +000081 */
Eric Andersenc911a432001-05-15 17:42:16 +000082static void mtab_read(void)
Erik Andersenfac10d72000-02-07 05:29:42 +000083{
Erik Andersene49d5ec2000-02-08 19:58:47 +000084 struct _mtab_entry_t *entry = NULL;
85 struct mntent *e;
86 FILE *fp;
87
88 if (mtab_cache != NULL)
89 return;
90
91 if ((fp = setmntent(mtab_file, "r")) == NULL) {
Matt Kraaidd19c692001-01-31 19:00:21 +000092 error_msg("Cannot open %s", mtab_file);
Erik Andersene49d5ec2000-02-08 19:58:47 +000093 return;
94 }
95 while ((e = getmntent(fp))) {
Erik Andersen0d068a22000-03-21 22:32:57 +000096 entry = xmalloc(sizeof(struct _mtab_entry_t));
Erik Andersene49d5ec2000-02-08 19:58:47 +000097 entry->device = strdup(e->mnt_fsname);
98 entry->mountpt = strdup(e->mnt_dir);
99 entry->next = mtab_cache;
100 mtab_cache = entry;
101 }
102 endmntent(fp);
Erik Andersenfac10d72000-02-07 05:29:42 +0000103}
104
Eric Andersenc911a432001-05-15 17:42:16 +0000105static char *mtab_getinfo(const char *match, const char which)
Erik Andersenfac10d72000-02-07 05:29:42 +0000106{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000107 struct _mtab_entry_t *cur = mtab_cache;
108
109 while (cur) {
110 if (strcmp(cur->mountpt, match) == 0 ||
111 strcmp(cur->device, match) == 0) {
112 if (which == MTAB_GETMOUNTPT) {
113 return cur->mountpt;
114 } else {
Eric Andersenc4cef5a2001-04-01 16:01:11 +0000115#if !defined BB_FEATURE_MTAB_SUPPORT
Erik Andersene49d5ec2000-02-08 19:58:47 +0000116 if (strcmp(cur->device, "/dev/root") == 0) {
Erik Andersenec5bd902000-03-22 07:12:05 +0000117 /* Adjusts device to be the real root device,
118 * or leaves device alone if it can't find it */
Eric Andersenc911a432001-05-15 17:42:16 +0000119 cur->device = find_real_root_device_name(cur->device);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000120 }
Erik Andersenfac10d72000-02-07 05:29:42 +0000121#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000122 return cur->device;
123 }
124 }
125 cur = cur->next;
Erik Andersenfac10d72000-02-07 05:29:42 +0000126 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000127 return NULL;
Erik Andersenfac10d72000-02-07 05:29:42 +0000128}
129
Eric Andersenc911a432001-05-15 17:42:16 +0000130static char *mtab_next(void **iter)
Erik Andersenfac10d72000-02-07 05:29:42 +0000131{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000132 char *mp;
133
134 if (iter == NULL || *iter == NULL)
135 return NULL;
136 mp = ((struct _mtab_entry_t *) (*iter))->mountpt;
137 *iter = (void *) ((struct _mtab_entry_t *) (*iter))->next;
138 return mp;
Erik Andersenfac10d72000-02-07 05:29:42 +0000139}
140
Eric Andersenc911a432001-05-15 17:42:16 +0000141static char *mtab_first(void **iter)
142{
143 struct _mtab_entry_t *mtab_iter;
144
145 if (!iter)
146 return NULL;
147 mtab_iter = mtab_cache;
148 *iter = (void *) mtab_iter;
149 return mtab_next(iter);
150}
151
Erik Andersen298854f2000-03-23 01:09:18 +0000152/* Don't bother to clean up, since exit() does that
153 * automagically, so we can save a few bytes */
Eric Andersenb040d4f2000-07-25 18:01:20 +0000154#ifdef BB_FEATURE_CLEAN_UP
Eric Andersenc911a432001-05-15 17:42:16 +0000155static void mtab_free(void)
Erik Andersenfac10d72000-02-07 05:29:42 +0000156{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000157 struct _mtab_entry_t *this, *next;
Erik Andersenfac10d72000-02-07 05:29:42 +0000158
Erik Andersene49d5ec2000-02-08 19:58:47 +0000159 this = mtab_cache;
160 while (this) {
161 next = this->next;
162 if (this->device)
163 free(this->device);
164 if (this->mountpt)
165 free(this->mountpt);
166 free(this);
167 this = next;
168 }
Erik Andersenfac10d72000-02-07 05:29:42 +0000169}
Erik Andersen298854f2000-03-23 01:09:18 +0000170#endif
Erik Andersene132f4b2000-02-09 04:16:43 +0000171
Eric Andersen1ca20a72001-03-21 07:34:27 +0000172static int do_umount(const char *name)
Erik Andersene132f4b2000-02-09 04:16:43 +0000173{
174 int status;
175 char *blockDevice = mtab_getinfo(name, MTAB_GETDEVICE);
176
177 if (blockDevice && strcmp(blockDevice, name) == 0)
178 name = mtab_getinfo(blockDevice, MTAB_GETMOUNTPT);
179
180 status = umount(name);
181
182#if defined BB_FEATURE_MOUNT_LOOP
Erik Andersence917322000-03-13 04:07:02 +0000183 if (freeLoop == TRUE && blockDevice != NULL && !strncmp("/dev/loop", blockDevice, 9))
Erik Andersene132f4b2000-02-09 04:16:43 +0000184 /* this was a loop device, delete it */
185 del_loop(blockDevice);
186#endif
Erik Andersen6c5f2c62000-05-05 19:49:33 +0000187#if defined BB_FEATURE_MOUNT_FORCE
188 if (status != 0 && doForce == TRUE) {
189 status = umount2(blockDevice, MNT_FORCE);
190 if (status != 0) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000191 error_msg_and_die("forced umount of %s failed!", blockDevice);
Erik Andersen6c5f2c62000-05-05 19:49:33 +0000192 }
193 }
194#endif
Erik Andersene132f4b2000-02-09 04:16:43 +0000195 if (status != 0 && doRemount == TRUE && errno == EBUSY) {
196 status = mount(blockDevice, name, NULL,
197 MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
198 if (status == 0) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000199 error_msg("%s busy - remounted read-only", blockDevice);
Erik Andersene132f4b2000-02-09 04:16:43 +0000200 } else {
Matt Kraaidd19c692001-01-31 19:00:21 +0000201 error_msg("Cannot remount %s read-only", blockDevice);
Erik Andersene132f4b2000-02-09 04:16:43 +0000202 }
203 }
Erik Andersene132f4b2000-02-09 04:16:43 +0000204 if (status == 0) {
Eric Andersenc4cef5a2001-04-01 16:01:11 +0000205#if defined BB_FEATURE_MTAB_SUPPORT
Erik Andersene132f4b2000-02-09 04:16:43 +0000206 if (useMtab == TRUE)
207 erase_mtab(name);
208#endif
209 return (TRUE);
210 }
211 return (FALSE);
212}
213
Eric Andersen1ca20a72001-03-21 07:34:27 +0000214static int umount_all(void)
Erik Andersene132f4b2000-02-09 04:16:43 +0000215{
216 int status = TRUE;
217 char *mountpt;
218 void *iter;
219
220 for (mountpt = mtab_first(&iter); mountpt; mountpt = mtab_next(&iter)) {
221 /* Never umount /proc on a umount -a */
222 if (strstr(mountpt, "proc")!= NULL)
223 continue;
Eric Andersen1ca20a72001-03-21 07:34:27 +0000224 if (!do_umount(mountpt)) {
Erik Andersene132f4b2000-02-09 04:16:43 +0000225 /* Don't bother retrying the umount on busy devices */
226 if (errno == EBUSY) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000227 perror_msg("%s", mountpt);
Matt Kraaifd4c58d2001-01-17 00:12:11 +0000228 status = FALSE;
Erik Andersene132f4b2000-02-09 04:16:43 +0000229 continue;
230 }
Eric Andersen1ca20a72001-03-21 07:34:27 +0000231 if (!do_umount(mountpt)) {
Erik Andersene132f4b2000-02-09 04:16:43 +0000232 printf("Couldn't umount %s on %s: %s\n",
233 mountpt, mtab_getinfo(mountpt, MTAB_GETDEVICE),
234 strerror(errno));
Matt Kraaifd4c58d2001-01-17 00:12:11 +0000235 status = FALSE;
Erik Andersene132f4b2000-02-09 04:16:43 +0000236 }
237 }
238 }
239 return (status);
240}
241
242extern int umount_main(int argc, char **argv)
243{
Matt Kraaiadcbc122001-05-02 21:24:51 +0000244 char path[PATH_MAX];
245
Erik Andersene132f4b2000-02-09 04:16:43 +0000246 if (argc < 2) {
Eric Andersen67991cf2001-02-14 21:23:06 +0000247 show_usage();
Erik Andersene132f4b2000-02-09 04:16:43 +0000248 }
Eric Andersenb040d4f2000-07-25 18:01:20 +0000249#ifdef BB_FEATURE_CLEAN_UP
250 atexit(mtab_free);
251#endif
Erik Andersene132f4b2000-02-09 04:16:43 +0000252
253 /* Parse any options */
254 while (--argc > 0 && **(++argv) == '-') {
255 while (*++(*argv))
256 switch (**argv) {
257 case 'a':
258 umountAll = TRUE;
259 break;
Erik Andersence917322000-03-13 04:07:02 +0000260#if defined BB_FEATURE_MOUNT_LOOP
Erik Andersen6c5f2c62000-05-05 19:49:33 +0000261 case 'l':
Erik Andersence917322000-03-13 04:07:02 +0000262 freeLoop = FALSE;
263 break;
264#endif
Eric Andersenc4cef5a2001-04-01 16:01:11 +0000265#ifdef BB_FEATURE_MTAB_SUPPORT
Erik Andersene132f4b2000-02-09 04:16:43 +0000266 case 'n':
267 useMtab = FALSE;
268 break;
269#endif
Erik Andersen6c5f2c62000-05-05 19:49:33 +0000270#ifdef BB_FEATURE_MOUNT_FORCE
271 case 'f':
272 doForce = TRUE;
273 break;
274#endif
Erik Andersene132f4b2000-02-09 04:16:43 +0000275 case 'r':
276 doRemount = TRUE;
277 break;
Erik Andersen983b51b2000-04-04 18:14:25 +0000278 case 'v':
279 break; /* ignore -v */
Erik Andersene132f4b2000-02-09 04:16:43 +0000280 default:
Eric Andersen67991cf2001-02-14 21:23:06 +0000281 show_usage();
Erik Andersene132f4b2000-02-09 04:16:43 +0000282 }
283 }
284
285 mtab_read();
286 if (umountAll == TRUE) {
Eric Andersen1ca20a72001-03-21 07:34:27 +0000287 if (umount_all() == TRUE)
Matt Kraai3e856ce2000-12-01 02:55:13 +0000288 return EXIT_SUCCESS;
289 else
290 return EXIT_FAILURE;
Erik Andersene132f4b2000-02-09 04:16:43 +0000291 }
Matt Kraaiadcbc122001-05-02 21:24:51 +0000292 if (realpath(*argv, path) == NULL)
293 perror_msg_and_die("%s", path);
294 if (do_umount(path) == TRUE)
Matt Kraai3e856ce2000-12-01 02:55:13 +0000295 return EXIT_SUCCESS;
Matt Kraaia9819b22000-12-22 01:48:07 +0000296 perror_msg_and_die("%s", *argv);
Erik Andersene132f4b2000-02-09 04:16:43 +0000297}
298