blob: 89c59f9ee81df3f38f1749e0ca908386c3fee5b6 [file] [log] [blame]
Eric Andersenf811e071999-10-09 00:25:00 +00001/*
2 * Mini umount implementation for busybox
3 *
Eric Andersenc4996011999-10-20 22:08:37 +00004 *
5 * Copyright (C) 1999 by Lineo, inc.
6 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
Eric Andersenf811e071999-10-09 00:25:00 +00007 *
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
Eric Andersencc8ed391999-10-05 16:24:54 +000024#include "internal.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000025#include <stdio.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000026#include <sys/mount.h>
Eric Andersenf811e071999-10-09 00:25:00 +000027#include <mntent.h>
28#include <fstab.h>
29#include <errno.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000030
Eric Andersene77ae3a1999-10-19 20:03:34 +000031static const char umount_usage[] =
Eric Andersend0246fb1999-11-04 21:18:07 +000032"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
43static int useMtab = TRUE;
44static int umountAll = FALSE;
45extern const char mtab_file[]; /* Defined in utility.c */
46
47#if ! defined BB_MTAB
48#define do_umount( blockDevice, useMtab) umount( blockDevice)
49#else
50static int
51do_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
Eric Andersencc8ed391999-10-05 16:24:54 +000064
65static int
Eric Andersend0246fb1999-11-04 21:18:07 +000066umount_all(int useMtab)
Eric Andersencc8ed391999-10-05 16:24:54 +000067{
Eric Andersenf811e071999-10-09 00:25:00 +000068 int status;
69 struct mntent *m;
70 FILE *mountTable;
Eric Andersencc8ed391999-10-05 16:24:54 +000071
Eric Andersend0246fb1999-11-04 21:18:07 +000072 if ((mountTable = setmntent (mtab_file, "r"))) {
Eric Andersenf811e071999-10-09 00:25:00 +000073 while ((m = getmntent (mountTable)) != 0) {
74 char *blockDevice = m->mnt_fsname;
Eric Andersend0246fb1999-11-04 21:18:07 +000075#if ! defined BB_MTAB
Eric Andersenf811e071999-10-09 00:25:00 +000076 if (strcmp (blockDevice, "/dev/root") == 0)
77 blockDevice = (getfsfile ("/"))->fs_spec;
Eric Andersend0246fb1999-11-04 21:18:07 +000078#endif
Eric Andersencf8c9cf1999-11-05 00:31:46 +000079 /* Don't umount /proc when doing umount -a */
80 if (strcmp (blockDevice, "proc") == 0)
81 continue;
82
Eric Andersend0246fb1999-11-04 21:18:07 +000083 status=do_umount (m->mnt_dir, useMtab);
Eric Andersenf811e071999-10-09 00:25:00 +000084 if (status!=0) {
85 /* Don't bother retrying the umount on busy devices */
86 if (errno==EBUSY) {
87 perror(m->mnt_dir);
88 continue;
89 }
Eric Andersend0246fb1999-11-04 21:18:07 +000090 status=do_umount (blockDevice, useMtab);
Eric Andersenf811e071999-10-09 00:25:00 +000091 if (status!=0) {
92 printf ("Couldn't umount %s on %s (type %s): %s\n",
93 blockDevice, m->mnt_dir, m->mnt_type, strerror(errno));
94 }
Eric Andersencc8ed391999-10-05 16:24:54 +000095 }
Eric Andersenf811e071999-10-09 00:25:00 +000096 }
97 endmntent (mountTable);
98 }
99 return( TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000100}
101
102extern int
Eric Andersend0246fb1999-11-04 21:18:07 +0000103umount_main(int argc, char** argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000104{
Eric Andersenf811e071999-10-09 00:25:00 +0000105
106 if (argc < 2) {
Eric Andersenb0e9a701999-10-18 22:28:26 +0000107 usage( umount_usage);
Eric Andersenf811e071999-10-09 00:25:00 +0000108 }
Eric Andersenf811e071999-10-09 00:25:00 +0000109
110 /* Parse any options */
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000111 while (argc-- > 0 && **(argv++) == '-') {
Eric Andersenf811e071999-10-09 00:25:00 +0000112 while (*++(*argv)) switch (**argv) {
113 case 'a':
Eric Andersend0246fb1999-11-04 21:18:07 +0000114 umountAll = TRUE;
Eric Andersenf811e071999-10-09 00:25:00 +0000115 break;
Eric Andersend0246fb1999-11-04 21:18:07 +0000116#ifdef BB_MTAB
117 case 'n':
118 useMtab = FALSE;
119 break;
120#endif
Eric Andersenf811e071999-10-09 00:25:00 +0000121 default:
Eric Andersenb0e9a701999-10-18 22:28:26 +0000122 usage( umount_usage);
Eric Andersencc8ed391999-10-05 16:24:54 +0000123 }
Eric Andersenf811e071999-10-09 00:25:00 +0000124 }
Eric Andersend0246fb1999-11-04 21:18:07 +0000125
126
127 if(umountAll) {
128 exit(umount_all(useMtab));
129 }
130 if ( do_umount(*argv,useMtab) == 0 )
131 exit (TRUE);
Eric Andersenf811e071999-10-09 00:25:00 +0000132 else {
133 perror("umount");
Eric Andersen3c163821999-10-14 22:16:57 +0000134 exit( FALSE);
Eric Andersenf811e071999-10-09 00:25:00 +0000135 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000136}
137