copyFile could call chmod on a symlink, changing the perms
[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 ;
41
42
43 static int useMtab = TRUE;
44 static int umountAll = FALSE;
45 extern const char mtab_file[]; /* Defined in utility.c */
46
47 #define MIN(x,y) (x > y ? x : y)
48
49 static int 
50 do_umount(const char* name, int useMtab)
51 {
52     int status;
53     struct mntent *m;
54     FILE *mountTable;
55     const char *blockDevice = NULL;
56
57     if ((mountTable = setmntent (mtab_file, "r"))) {
58         while ((m = getmntent (mountTable)) != 0) {
59             if (strncmp(m->mnt_dir, name,
60                         MIN(strlen(m->mnt_dir),strlen(name))) == 0)
61                 blockDevice = m->mnt_fsname;
62             else if (strcmp(m->mnt_fsname, name) == 0) {
63                 blockDevice = name;
64                 name = m->mnt_dir;
65             }
66         }
67     }
68
69     status = umount(name);
70
71 #if defined BB_FEATURE_MOUNT_LOOP
72     if (blockDevice != NULL && !strncmp("/dev/loop", blockDevice, 9))
73         /* this was a loop device, delete it */
74         del_loop(blockDevice);
75 #endif
76 #if defined BB_MTAB
77     if ( status == 0 ) {
78         if ( useMtab==TRUE )
79             erase_mtab(name);
80         return 0;
81     }
82     else
83 #endif
84         return(status);
85 }
86
87 static int
88 umount_all(int useMtab)
89 {
90         int status;
91         struct mntent *m;
92         FILE *mountTable;
93
94         if ((mountTable = setmntent (mtab_file, "r"))) {
95             while ((m = getmntent (mountTable)) != 0) {
96                 char *blockDevice = m->mnt_fsname;
97 #if ! defined BB_MTAB
98                 if (strcmp (blockDevice, "/dev/root") == 0) {
99                     struct fstab* fstabItem;
100                     fstabItem = getfsfile ("/");
101                     if (fstabItem != NULL) {
102                         blockDevice = fstabItem->fs_spec;
103                     }
104                 }
105 #endif
106                 /* Don't umount /proc when doing umount -a */
107                 if (strcmp (blockDevice, "proc") == 0)
108                     continue;
109
110                 status=do_umount (m->mnt_dir, useMtab);
111                 if (status!=0) {
112                     /* Don't bother retrying the umount on busy devices */
113                     if (errno==EBUSY) {
114                         perror(m->mnt_dir); 
115                         continue;
116                     }
117                     status=do_umount (blockDevice, useMtab);
118                     if (status!=0) {
119                         printf ("Couldn't umount %s on %s (type %s): %s\n", 
120                                 blockDevice, m->mnt_dir, m->mnt_type, strerror(errno));
121                     }
122                 }
123             }
124             endmntent (mountTable);
125         }
126         return( TRUE);
127 }
128
129 extern int
130 umount_main(int argc, char** argv)
131 {
132     if (argc < 2) {
133         usage( umount_usage); 
134     }
135
136     /* Parse any options */
137     while (--argc > 0 && **(++argv) == '-') {
138         while (*++(*argv)) switch (**argv) {
139             case 'a':
140                 umountAll = TRUE;
141                 break;
142 #ifdef BB_MTAB
143             case 'n':
144                 useMtab = FALSE;
145                 break;
146 #endif
147             default:
148                 usage( umount_usage);
149         }
150     }
151
152
153     if(umountAll==TRUE) {
154         exit(umount_all(useMtab));
155     }
156     if ( do_umount(*argv,useMtab) == 0 )
157         exit (TRUE);
158     else {
159         perror("umount");
160         exit(FALSE);
161     }
162 }
163