b65caf76e21eb049c9e59c463bbabc4320ca1c1a
[oweals/busybox.git] / umount.c
1 /*
2  * Mini umount implementation for busybox
3  *
4  *
5  * Copyright (C) 1999 by Lineo, inc.
6  * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
7  *
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
24 #include "internal.h"
25 #include <stdio.h>
26 #include <sys/mount.h>
27 #include <mntent.h>
28 #include <fstab.h>
29 #include <errno.h>
30
31 static const char umount_usage[] = 
32 "umount [flags] filesystem|directory\n\n"
33 "Flags:\n"
34 "\t-a:\tUnmount all file systems"
35 #ifdef BB_MTAB
36 " in /etc/mtab\n\t-n:\tDon't erase /etc/mtab entries\n"
37 #else
38 "\n"
39 #endif
40 #ifdef BB_FEATURE_REMOUNT
41 "\t-r:\tTry to remount devices as read-only if mount is busy\n"
42 #endif
43 ;
44
45 struct _mtab_entry_t {
46   char *device;
47   char *mountpt;
48   struct _mtab_entry_t *next;
49 };
50
51 static struct _mtab_entry_t *mtab_cache = NULL;
52
53
54
55 static int useMtab = TRUE;
56 static int umountAll = FALSE;
57 static int doRemount = FALSE;
58 extern const char mtab_file[]; /* Defined in utility.c */
59
60 #define MIN(x,y) (x > y ? x : y)
61
62 static int 
63 do_umount(const char* name, int useMtab)
64 {
65     int status;
66     char *blockDevice = mtab_getinfo(name, MTAB_GETDEVICE);
67
68     if (blockDevice && strcmp(blockDevice, name) == 0)
69         name = mtab_getinfo(blockDevice, MTAB_GETMOUNTPT);
70
71     status = umount(name);
72
73 #if defined BB_FEATURE_MOUNT_LOOP
74     if (blockDevice != NULL && !strncmp("/dev/loop", blockDevice, 9))
75         /* this was a loop device, delete it */
76         del_loop(blockDevice);
77 #endif
78 #if defined BB_FEATURE_REMOUNT
79     if ( status != 0 && doRemount == TRUE && errno == EBUSY ) {
80         status = mount(blockDevice, name, NULL, 
81                        MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
82         if (status == 0) {
83             fprintf(stderr, "umount: %s busy - remounted read-only\n", 
84                     blockDevice);
85             /* TODO: update mtab if BB_MTAB is defined */
86         } else {
87             fprintf(stderr, "umount: Cannot remount %s read-only\n",
88                     blockDevice);
89         }
90     }
91 #endif
92     if ( status == 0 ) {
93 #if defined BB_MTAB
94         if ( useMtab==TRUE )
95             erase_mtab(name);
96 #endif
97         return( TRUE);
98     }
99     return(FALSE);
100 }
101
102 static int
103 umount_all(int useMtab)
104 {
105         int status = TRUE;
106         char *mountpt;
107         void *iter;
108
109         for (mountpt = mtab_first(&iter); mountpt; mountpt = mtab_next(&iter)) {
110             status=do_umount (mountpt, useMtab);
111             if (status != 0) {
112                 /* Don't bother retrying the umount on busy devices */
113                 if (errno == EBUSY) {
114                    perror(mountpt);
115                    continue;
116                 }
117                 status = do_umount (mountpt, useMtab);
118                 if (status != 0) {
119                     printf ("Couldn't umount %s on %s: %s\n", 
120                         mountpt, mtab_getinfo(mountpt, MTAB_GETDEVICE), strerror(errno));
121                 }
122             }
123         }
124         return (status);
125 }
126
127 extern int
128 umount_main(int argc, char** argv)
129 {
130     if (argc < 2) {
131         usage( umount_usage); 
132     }
133
134     /* Parse any options */
135     while (--argc > 0 && **(++argv) == '-') {
136         while (*++(*argv)) switch (**argv) {
137             case 'a':
138                 umountAll = TRUE;
139                 break;
140 #ifdef BB_MTAB
141             case 'n':
142                 useMtab = FALSE;
143                 break;
144 #endif
145 #ifdef BB_FEATURE_REMOUNT
146             case 'r':
147                 doRemount = TRUE;
148                 break;
149 #endif
150             default:
151                 usage( umount_usage);
152         }
153     }
154
155     mtab_read();
156     if (umountAll==TRUE) {
157         exit(umount_all(useMtab));
158     }
159     if ( do_umount(*argv,useMtab) == 0 )
160         exit (TRUE);
161     else {
162         perror("umount");
163         exit(FALSE);
164     }
165 }
166
167
168
169 /* These functions are here because the getmntent functions do not appear
170  * to be re-entrant, which leads to all sorts of problems when we try to
171  * use them recursively - randolph
172  */
173 void mtab_read(void)
174 {
175     struct _mtab_entry_t *entry = NULL;
176     struct mntent *e;
177     FILE *fp;
178   
179     if (mtab_cache != NULL) return;
180  
181     if ((fp = setmntent(mtab_file, "r")) == NULL) {
182         fprintf(stderr, "Cannot open %s\n", mtab_file);
183         return;
184     }
185     while ((e = getmntent(fp))) {
186         entry = malloc(sizeof(struct _mtab_entry_t));
187         entry->device = strdup(e->mnt_fsname);
188         entry->mountpt = strdup(e->mnt_dir);
189         entry->next = mtab_cache;
190         mtab_cache = entry;
191     }
192     endmntent(fp);
193 }
194
195 char *mtab_getinfo(const char *match, const char which)
196 {
197     struct _mtab_entry_t *cur = mtab_cache;
198     while (cur) {
199         if (strcmp(cur->mountpt, match) == 0 ||
200             strcmp(cur->device, match) == 0) {
201             if (which == MTAB_GETMOUNTPT) {
202                 return cur->mountpt;
203             } else {
204 #if !defined BB_MTAB
205                 if (strcmp(cur->device, "/dev/root") == 0) {
206                     struct fstab* fstabItem;
207                     fstabItem = getfsfile ("/");
208                     if (fstabItem != NULL) return fstabItem->fs_spec;
209                 }
210 #endif
211                 return cur->device;  
212             }
213         }
214         cur = cur->next;
215     }
216     return NULL;
217 }
218
219 char *mtab_first(void **iter)
220 {
221     struct _mtab_entry_t *mtab_iter;
222     if (!iter) return NULL;
223     mtab_iter = mtab_cache;
224     *iter = (void *)mtab_iter;
225     return mtab_next(iter);
226 }
227
228 char *mtab_next(void **iter)
229 {
230     char *mp;
231     if (iter == NULL || *iter == NULL) return NULL;
232     mp = ((struct _mtab_entry_t *)(*iter))->mountpt;
233     *iter = (void *)((struct _mtab_entry_t *)(*iter))->next;
234     return mp;
235 }
236
237 void mtab_free(void)
238 {
239     struct _mtab_entry_t *this, *next;
240
241     this = mtab_cache;
242     while (this) {
243       next = this->next;
244       if (this->device) free(this->device);
245       if (this->mountpt) free(this->mountpt);
246       free(this);
247       this = next;
248     }
249 }
250