blob: 863d52476b9d0ee768bfb2c668811c08d22bd852 [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 Andersencb81e642003-07-14 21:21:08 +00005 * Copyright (C) 1999-2003 by Erik Andersen <andersen@codepoet.org>
Eric Andersenf811e071999-10-09 00:25:00 +00006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
Matt Kraaiadcbc122001-05-02 21:24:51 +000023#include <limits.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000024#include <stdio.h>
Eric Andersenf811e071999-10-09 00:25:00 +000025#include <mntent.h>
Eric Andersenf811e071999-10-09 00:25:00 +000026#include <errno.h>
Eric Andersened3ef502001-01-27 08:24:39 +000027#include <string.h>
28#include <stdlib.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000029#include "busybox.h"
Eric Andersene1e23ee2000-06-19 18:38:51 +000030
Eric Andersen8a915882001-08-02 09:55:58 +000031/* Teach libc5 about realpath -- it includes it but the
32 * prototype is missing... */
33#if (__GLIBC__ <= 2) && (__GLIBC_MINOR__ < 1)
34extern char *realpath(const char *path, char *resolved_path);
35#endif
Eric Andersene1e23ee2000-06-19 18:38:51 +000036
Mark Whitley59ab0252001-01-23 22:30:04 +000037static const int MNT_FORCE = 1;
38static const int MS_MGC_VAL = 0xc0ed0000; /* Magic number indicatng "new" flags */
39static const int MS_REMOUNT = 32; /* Alter flags of a mounted FS. */
40static const int MS_RDONLY = 1; /* Mount read-only. */
Eric Andersen2cd439f2000-07-08 19:10:29 +000041
Eric Andersena57ba4d2000-07-08 19:20:49 +000042extern int mount (__const char *__special_file, __const char *__dir,
43 __const char *__fstype, unsigned long int __rwflag,
44 __const void *__data);
45extern int umount (__const char *__special_file);
46extern int umount2 (__const char *__special_file, int __flags);
Eric Andersen2cd439f2000-07-08 19:10:29 +000047
Erik Andersenfac10d72000-02-07 05:29:42 +000048struct _mtab_entry_t {
Erik Andersene49d5ec2000-02-08 19:58:47 +000049 char *device;
50 char *mountpt;
51 struct _mtab_entry_t *next;
Erik Andersenfac10d72000-02-07 05:29:42 +000052};
53
54static struct _mtab_entry_t *mtab_cache = NULL;
55
56
Eric Andersend0246fb1999-11-04 21:18:07 +000057
Eric Andersenbdfd0d72001-10-24 05:00:29 +000058#if defined CONFIG_FEATURE_MOUNT_FORCE
Erik Andersen6c5f2c62000-05-05 19:49:33 +000059static int doForce = FALSE;
60#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +000061#if defined CONFIG_FEATURE_MOUNT_LOOP
Erik Andersence917322000-03-13 04:07:02 +000062static int freeLoop = TRUE;
63#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +000064#if defined CONFIG_FEATURE_MTAB_SUPPORT
Eric Andersend0246fb1999-11-04 21:18:07 +000065static int useMtab = TRUE;
Eric Andersen1ca20a72001-03-21 07:34:27 +000066#endif
Eric Andersend0246fb1999-11-04 21:18:07 +000067static int umountAll = FALSE;
Erik Andersenfac10d72000-02-07 05:29:42 +000068static int doRemount = FALSE;
Eric Andersend0246fb1999-11-04 21:18:07 +000069
Erik Andersenfac10d72000-02-07 05:29:42 +000070
Erik Andersen5b911dd2000-02-23 22:49:58 +000071
Erik Andersenfac10d72000-02-07 05:29:42 +000072/* These functions are here because the getmntent functions do not appear
73 * to be re-entrant, which leads to all sorts of problems when we try to
74 * use them recursively - randolph
Erik Andersen5b911dd2000-02-23 22:49:58 +000075 *
76 * TODO: Perhaps switch to using Glibc's getmntent_r
77 * -Erik
Erik Andersenfac10d72000-02-07 05:29:42 +000078 */
Eric Andersenc911a432001-05-15 17:42:16 +000079static void mtab_read(void)
Erik Andersenfac10d72000-02-07 05:29:42 +000080{
Erik Andersene49d5ec2000-02-08 19:58:47 +000081 struct _mtab_entry_t *entry = NULL;
82 struct mntent *e;
83 FILE *fp;
84
85 if (mtab_cache != NULL)
86 return;
87
Manuel Novoa III cad53642003-03-19 09:13:01 +000088 if ((fp = setmntent(bb_path_mtab_file, "r")) == NULL) {
89 bb_error_msg("Cannot open %s", bb_path_mtab_file);
Erik Andersene49d5ec2000-02-08 19:58:47 +000090 return;
91 }
92 while ((e = getmntent(fp))) {
Erik Andersen0d068a22000-03-21 22:32:57 +000093 entry = xmalloc(sizeof(struct _mtab_entry_t));
Erik Andersene49d5ec2000-02-08 19:58:47 +000094 entry->device = strdup(e->mnt_fsname);
95 entry->mountpt = strdup(e->mnt_dir);
96 entry->next = mtab_cache;
97 mtab_cache = entry;
98 }
99 endmntent(fp);
Erik Andersenfac10d72000-02-07 05:29:42 +0000100}
101
Eric Andersenc911a432001-05-15 17:42:16 +0000102static char *mtab_getinfo(const char *match, const char which)
Erik Andersenfac10d72000-02-07 05:29:42 +0000103{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000104 struct _mtab_entry_t *cur = mtab_cache;
105
106 while (cur) {
107 if (strcmp(cur->mountpt, match) == 0 ||
108 strcmp(cur->device, match) == 0) {
109 if (which == MTAB_GETMOUNTPT) {
110 return cur->mountpt;
111 } else {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000112#if !defined CONFIG_FEATURE_MTAB_SUPPORT
Eric Andersen9d7f0f02003-06-20 09:36:49 +0000113 if (strcmp(cur->device, "rootfs") == 0) {
114 continue;
115 } else 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;
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000161 free(this->device);
162 free(this->mountpt);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000163 free(this);
164 this = next;
165 }
Erik Andersenfac10d72000-02-07 05:29:42 +0000166}
Erik Andersen298854f2000-03-23 01:09:18 +0000167#endif
Erik Andersene132f4b2000-02-09 04:16:43 +0000168
Eric Andersen1ca20a72001-03-21 07:34:27 +0000169static int do_umount(const char *name)
Erik Andersene132f4b2000-02-09 04:16:43 +0000170{
171 int status;
172 char *blockDevice = mtab_getinfo(name, MTAB_GETDEVICE);
173
174 if (blockDevice && strcmp(blockDevice, name) == 0)
175 name = mtab_getinfo(blockDevice, MTAB_GETMOUNTPT);
176
177 status = umount(name);
178
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000179#if defined CONFIG_FEATURE_MOUNT_LOOP
Matt Kraai1f0c4362001-12-20 23:13:26 +0000180 if (freeLoop && blockDevice != NULL && !strncmp("/dev/loop", blockDevice, 9))
Erik Andersene132f4b2000-02-09 04:16:43 +0000181 /* this was a loop device, delete it */
182 del_loop(blockDevice);
183#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000184#if defined CONFIG_FEATURE_MOUNT_FORCE
Matt Kraai1f0c4362001-12-20 23:13:26 +0000185 if (status != 0 && doForce) {
Erik Andersen6c5f2c62000-05-05 19:49:33 +0000186 status = umount2(blockDevice, MNT_FORCE);
187 if (status != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000188 bb_error_msg_and_die("forced umount of %s failed!", blockDevice);
Erik Andersen6c5f2c62000-05-05 19:49:33 +0000189 }
190 }
191#endif
Matt Kraai1f0c4362001-12-20 23:13:26 +0000192 if (status != 0 && doRemount && errno == EBUSY) {
Erik Andersene132f4b2000-02-09 04:16:43 +0000193 status = mount(blockDevice, name, NULL,
194 MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
195 if (status == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000196 bb_error_msg("%s busy - remounted read-only", blockDevice);
Erik Andersene132f4b2000-02-09 04:16:43 +0000197 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000198 bb_error_msg("Cannot remount %s read-only", blockDevice);
Erik Andersene132f4b2000-02-09 04:16:43 +0000199 }
200 }
Erik Andersene132f4b2000-02-09 04:16:43 +0000201 if (status == 0) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000202#if defined CONFIG_FEATURE_MTAB_SUPPORT
Matt Kraai1f0c4362001-12-20 23:13:26 +0000203 if (useMtab)
Erik Andersene132f4b2000-02-09 04:16:43 +0000204 erase_mtab(name);
205#endif
206 return (TRUE);
207 }
208 return (FALSE);
209}
210
Eric Andersen1ca20a72001-03-21 07:34:27 +0000211static int umount_all(void)
Erik Andersene132f4b2000-02-09 04:16:43 +0000212{
213 int status = TRUE;
214 char *mountpt;
215 void *iter;
216
217 for (mountpt = mtab_first(&iter); mountpt; mountpt = mtab_next(&iter)) {
218 /* Never umount /proc on a umount -a */
219 if (strstr(mountpt, "proc")!= NULL)
220 continue;
Eric Andersen1ca20a72001-03-21 07:34:27 +0000221 if (!do_umount(mountpt)) {
Erik Andersene132f4b2000-02-09 04:16:43 +0000222 /* Don't bother retrying the umount on busy devices */
223 if (errno == EBUSY) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000224 bb_perror_msg("%s", mountpt);
Matt Kraaifd4c58d2001-01-17 00:12:11 +0000225 status = FALSE;
Erik Andersene132f4b2000-02-09 04:16:43 +0000226 continue;
227 }
Eric Andersen1ca20a72001-03-21 07:34:27 +0000228 if (!do_umount(mountpt)) {
Erik Andersene132f4b2000-02-09 04:16:43 +0000229 printf("Couldn't umount %s on %s: %s\n",
230 mountpt, mtab_getinfo(mountpt, MTAB_GETDEVICE),
231 strerror(errno));
Matt Kraaifd4c58d2001-01-17 00:12:11 +0000232 status = FALSE;
Erik Andersene132f4b2000-02-09 04:16:43 +0000233 }
234 }
235 }
236 return (status);
237}
238
239extern int umount_main(int argc, char **argv)
240{
Matt Kraaiadcbc122001-05-02 21:24:51 +0000241 char path[PATH_MAX];
242
Erik Andersene132f4b2000-02-09 04:16:43 +0000243 if (argc < 2) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000244 bb_show_usage();
Erik Andersene132f4b2000-02-09 04:16:43 +0000245 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000246#ifdef CONFIG_FEATURE_CLEAN_UP
Eric Andersenb040d4f2000-07-25 18:01:20 +0000247 atexit(mtab_free);
248#endif
Erik Andersene132f4b2000-02-09 04:16:43 +0000249
250 /* Parse any options */
251 while (--argc > 0 && **(++argv) == '-') {
252 while (*++(*argv))
253 switch (**argv) {
254 case 'a':
255 umountAll = TRUE;
256 break;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000257#if defined CONFIG_FEATURE_MOUNT_LOOP
Erik Andersen6c5f2c62000-05-05 19:49:33 +0000258 case 'l':
Erik Andersence917322000-03-13 04:07:02 +0000259 freeLoop = FALSE;
260 break;
261#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000262#ifdef CONFIG_FEATURE_MTAB_SUPPORT
Erik Andersene132f4b2000-02-09 04:16:43 +0000263 case 'n':
264 useMtab = FALSE;
265 break;
266#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000267#ifdef CONFIG_FEATURE_MOUNT_FORCE
Erik Andersen6c5f2c62000-05-05 19:49:33 +0000268 case 'f':
269 doForce = TRUE;
270 break;
271#endif
Erik Andersene132f4b2000-02-09 04:16:43 +0000272 case 'r':
273 doRemount = TRUE;
274 break;
Erik Andersen983b51b2000-04-04 18:14:25 +0000275 case 'v':
276 break; /* ignore -v */
Erik Andersene132f4b2000-02-09 04:16:43 +0000277 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000278 bb_show_usage();
Erik Andersene132f4b2000-02-09 04:16:43 +0000279 }
280 }
281
282 mtab_read();
Matt Kraai1f0c4362001-12-20 23:13:26 +0000283 if (umountAll) {
284 if (umount_all())
Matt Kraai3e856ce2000-12-01 02:55:13 +0000285 return EXIT_SUCCESS;
286 else
287 return EXIT_FAILURE;
Erik Andersene132f4b2000-02-09 04:16:43 +0000288 }
Matt Kraaiadcbc122001-05-02 21:24:51 +0000289 if (realpath(*argv, path) == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000290 bb_perror_msg_and_die("%s", path);
Matt Kraai1f0c4362001-12-20 23:13:26 +0000291 if (do_umount(path))
Matt Kraai3e856ce2000-12-01 02:55:13 +0000292 return EXIT_SUCCESS;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000293 bb_perror_msg_and_die("%s", *argv);
Erik Andersene132f4b2000-02-09 04:16:43 +0000294}
295