blob: b58b1a08c5bb19038682a94304da41883d2c91f1 [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 *
6 * Copyright (C) 1999 by Lineo, inc.
7 * 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 Andersencc8ed391999-10-05 16:24:54 +000027#include <sys/mount.h>
Eric Andersenf811e071999-10-09 00:25:00 +000028#include <mntent.h>
29#include <fstab.h>
30#include <errno.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000031
Erik Andersene132f4b2000-02-09 04:16:43 +000032
Erik Andersene49d5ec2000-02-08 19:58:47 +000033static const char umount_usage[] =
34 "umount [flags] filesystem|directory\n\n"
35 "Flags:\n" "\t-a:\tUnmount all file systems"
Eric Andersend0246fb1999-11-04 21:18:07 +000036#ifdef BB_MTAB
Erik Andersene49d5ec2000-02-08 19:58:47 +000037 " in /etc/mtab\n\t-n:\tDon't erase /etc/mtab entries\n"
Eric Andersend0246fb1999-11-04 21:18:07 +000038#else
Erik Andersene49d5ec2000-02-08 19:58:47 +000039 "\n"
Eric Andersend0246fb1999-11-04 21:18:07 +000040#endif
Erik Andersenfac10d72000-02-07 05:29:42 +000041#ifdef BB_FEATURE_REMOUNT
Erik Andersene49d5ec2000-02-08 19:58:47 +000042 "\t-r:\tTry to remount devices as read-only if mount is busy\n"
Erik Andersenfac10d72000-02-07 05:29:42 +000043#endif
Eric Andersend0246fb1999-11-04 21:18:07 +000044;
45
Erik Andersenfac10d72000-02-07 05:29:42 +000046struct _mtab_entry_t {
Erik Andersene49d5ec2000-02-08 19:58:47 +000047 char *device;
48 char *mountpt;
49 struct _mtab_entry_t *next;
Erik Andersenfac10d72000-02-07 05:29:42 +000050};
51
52static struct _mtab_entry_t *mtab_cache = NULL;
53
54
Eric Andersend0246fb1999-11-04 21:18:07 +000055
56static int useMtab = TRUE;
57static int umountAll = FALSE;
Erik Andersenfac10d72000-02-07 05:29:42 +000058static int doRemount = FALSE;
Erik Andersene49d5ec2000-02-08 19:58:47 +000059extern const char mtab_file[]; /* Defined in utility.c */
Eric Andersend0246fb1999-11-04 21:18:07 +000060
Erik Andersenfac10d72000-02-07 05:29:42 +000061
62/* These functions are here because the getmntent functions do not appear
63 * to be re-entrant, which leads to all sorts of problems when we try to
64 * use them recursively - randolph
65 */
66void mtab_read(void)
67{
Erik Andersene49d5ec2000-02-08 19:58:47 +000068 struct _mtab_entry_t *entry = NULL;
69 struct mntent *e;
70 FILE *fp;
71
72 if (mtab_cache != NULL)
73 return;
74
75 if ((fp = setmntent(mtab_file, "r")) == NULL) {
76 fprintf(stderr, "Cannot open %s\n", mtab_file);
77 return;
78 }
79 while ((e = getmntent(fp))) {
80 entry = malloc(sizeof(struct _mtab_entry_t));
81
82 entry->device = strdup(e->mnt_fsname);
83 entry->mountpt = strdup(e->mnt_dir);
84 entry->next = mtab_cache;
85 mtab_cache = entry;
86 }
87 endmntent(fp);
Erik Andersenfac10d72000-02-07 05:29:42 +000088}
89
90char *mtab_getinfo(const char *match, const char which)
91{
Erik Andersene49d5ec2000-02-08 19:58:47 +000092 struct _mtab_entry_t *cur = mtab_cache;
93
94 while (cur) {
95 if (strcmp(cur->mountpt, match) == 0 ||
96 strcmp(cur->device, match) == 0) {
97 if (which == MTAB_GETMOUNTPT) {
98 return cur->mountpt;
99 } else {
Erik Andersenfac10d72000-02-07 05:29:42 +0000100#if !defined BB_MTAB
Erik Andersene49d5ec2000-02-08 19:58:47 +0000101 if (strcmp(cur->device, "/dev/root") == 0) {
102 struct fstab *fstabItem;
103
104 fstabItem = getfsfile("/");
105 if (fstabItem != NULL)
106 return fstabItem->fs_spec;
107 }
Erik Andersenfac10d72000-02-07 05:29:42 +0000108#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000109 return cur->device;
110 }
111 }
112 cur = cur->next;
Erik Andersenfac10d72000-02-07 05:29:42 +0000113 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000114 return NULL;
Erik Andersenfac10d72000-02-07 05:29:42 +0000115}
116
117char *mtab_first(void **iter)
118{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000119 struct _mtab_entry_t *mtab_iter;
120
121 if (!iter)
122 return NULL;
123 mtab_iter = mtab_cache;
124 *iter = (void *) mtab_iter;
125 return mtab_next(iter);
Erik Andersenfac10d72000-02-07 05:29:42 +0000126}
127
128char *mtab_next(void **iter)
129{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000130 char *mp;
131
132 if (iter == NULL || *iter == NULL)
133 return NULL;
134 mp = ((struct _mtab_entry_t *) (*iter))->mountpt;
135 *iter = (void *) ((struct _mtab_entry_t *) (*iter))->next;
136 return mp;
Erik Andersenfac10d72000-02-07 05:29:42 +0000137}
138
139void mtab_free(void)
140{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000141 struct _mtab_entry_t *this, *next;
Erik Andersenfac10d72000-02-07 05:29:42 +0000142
Erik Andersene49d5ec2000-02-08 19:58:47 +0000143 this = mtab_cache;
144 while (this) {
145 next = this->next;
146 if (this->device)
147 free(this->device);
148 if (this->mountpt)
149 free(this->mountpt);
150 free(this);
151 this = next;
152 }
Erik Andersenfac10d72000-02-07 05:29:42 +0000153}
Erik Andersene132f4b2000-02-09 04:16:43 +0000154
155static int do_umount(const char *name, int useMtab)
156{
157 int status;
158 char *blockDevice = mtab_getinfo(name, MTAB_GETDEVICE);
159
160 if (blockDevice && strcmp(blockDevice, name) == 0)
161 name = mtab_getinfo(blockDevice, MTAB_GETMOUNTPT);
162
163 status = umount(name);
164
165#if defined BB_FEATURE_MOUNT_LOOP
166 if (blockDevice != NULL && !strncmp("/dev/loop", blockDevice, 9))
167 /* this was a loop device, delete it */
168 del_loop(blockDevice);
169#endif
170#if defined BB_FEATURE_REMOUNT
171 if (status != 0 && doRemount == TRUE && errno == EBUSY) {
172 status = mount(blockDevice, name, NULL,
173 MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
174 if (status == 0) {
175 fprintf(stderr, "umount: %s busy - remounted read-only\n",
176 blockDevice);
177 /* TODO: update mtab if BB_MTAB is defined */
178 } else {
179 fprintf(stderr, "umount: Cannot remount %s read-only\n",
180 blockDevice);
181 }
182 }
183#endif
184 if (status == 0) {
185#if defined BB_MTAB
186 if (useMtab == TRUE)
187 erase_mtab(name);
188#endif
189 return (TRUE);
190 }
191 return (FALSE);
192}
193
194static int umount_all(int useMtab)
195{
196 int status = TRUE;
197 char *mountpt;
198 void *iter;
199
200 for (mountpt = mtab_first(&iter); mountpt; mountpt = mtab_next(&iter)) {
201 /* Never umount /proc on a umount -a */
202 if (strstr(mountpt, "proc")!= NULL)
203 continue;
204 status = do_umount(mountpt, useMtab);
205 if (status != 0) {
206 /* Don't bother retrying the umount on busy devices */
207 if (errno == EBUSY) {
208 perror(mountpt);
209 continue;
210 }
211 status = do_umount(mountpt, useMtab);
212 if (status != 0) {
213 printf("Couldn't umount %s on %s: %s\n",
214 mountpt, mtab_getinfo(mountpt, MTAB_GETDEVICE),
215 strerror(errno));
216 }
217 }
218 }
219 return (status);
220}
221
222extern int umount_main(int argc, char **argv)
223{
224 if (argc < 2) {
225 usage(umount_usage);
226 }
227
228 /* Parse any options */
229 while (--argc > 0 && **(++argv) == '-') {
230 while (*++(*argv))
231 switch (**argv) {
232 case 'a':
233 umountAll = TRUE;
234 break;
235#ifdef BB_MTAB
236 case 'n':
237 useMtab = FALSE;
238 break;
239#endif
240#ifdef BB_FEATURE_REMOUNT
241 case 'r':
242 doRemount = TRUE;
243 break;
244#endif
245 default:
246 usage(umount_usage);
247 }
248 }
249
250 mtab_read();
251 if (umountAll == TRUE) {
252 exit(umount_all(useMtab));
253 }
254 if (do_umount(*argv, useMtab) == 0)
255 exit(TRUE);
256 else {
257 perror("umount");
258 exit(FALSE);
259 }
260}
261