More stuff.
[oweals/busybox.git] / util-linux / 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 "Usage: umount [flags] filesystem|directory\n"
33 "Optional 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 #if ! defined BB_MTAB
48 #define do_umount( blockDevice, useMtab) umount( blockDevice)
49 #else
50 static int 
51 do_umount(const char* name, int useMtab)
52 {
53     int status = umount(name);
54
55     if ( status == 0 ) {
56         if ( useMtab==TRUE )
57             erase_mtab(name);
58         return 0;
59     }
60     else 
61         return( status);
62 }
63 #endif
64
65 static int
66 umount_all(int useMtab)
67 {
68         int status;
69         struct mntent *m;
70         FILE *mountTable;
71
72         if ((mountTable = setmntent (mtab_file, "r"))) {
73             while ((m = getmntent (mountTable)) != 0) {
74                 char *blockDevice = m->mnt_fsname;
75 #if ! defined BB_MTAB
76                 if (strcmp (blockDevice, "/dev/root") == 0)
77                     blockDevice = (getfsfile ("/"))->fs_spec;
78 #endif
79                 status=do_umount (m->mnt_dir, useMtab);
80                 if (status!=0) {
81                     /* Don't bother retrying the umount on busy devices */
82                     if (errno==EBUSY) {
83                         perror(m->mnt_dir); 
84                         continue;
85                     }
86                     printf ("Trying to umount %s failed: %s\n", 
87                             m->mnt_dir, strerror(errno)); 
88                     printf ("Instead trying to umount %s\n", blockDevice); 
89                     status=do_umount (blockDevice, useMtab);
90                     if (status!=0) {
91                         printf ("Couldn't umount %s on %s (type %s): %s\n", 
92                                 blockDevice, m->mnt_dir, m->mnt_type, strerror(errno));
93                     }
94                 }
95             }
96             endmntent (mountTable);
97         }
98         return( TRUE);
99 }
100
101 extern int
102 umount_main(int argc, char** argv)
103 {
104
105     if (argc < 2) {
106         usage( umount_usage); 
107     }
108
109     /* Parse any options */
110     while (argc-- > 0 && **(++argv) == '-') {
111         while (*++(*argv)) switch (**argv) {
112             case 'a':
113                 umountAll = TRUE;
114                 break;
115 #ifdef BB_MTAB
116             case 'n':
117                 useMtab = FALSE;
118                 break;
119 #endif
120             default:
121                 usage( umount_usage);
122         }
123     }
124
125
126     if(umountAll) {
127         exit(umount_all(useMtab));
128     }
129     if ( do_umount(*argv,useMtab) == 0 )
130         exit (TRUE);
131     else {
132         perror("umount");
133         exit( FALSE);
134     }
135 }
136