2 * Mini umount implementation for busybox
5 * Copyright (C) 1999 by Lineo, inc.
6 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
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.
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.
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
26 #include <sys/mount.h>
31 #if defined BB_FEATURE_MOUNT_LOOP
33 #include <sys/ioctl.h>
34 #include <linux/loop.h>
36 static int del_loop(const char *device);
39 static const char umount_usage[] =
40 "Usage: umount [flags] filesystem|directory\n\n"
42 "\t-a:\tUnmount all file systems"
44 " in /etc/mtab\n\t-n:\tDon't erase /etc/mtab entries\n"
51 static int useMtab = TRUE;
52 static int umountAll = FALSE;
53 extern const char mtab_file[]; /* Defined in utility.c */
56 do_umount(const char* name, int useMtab)
60 #if defined BB_FEATURE_MOUNT_LOOP
61 /* check to see if this is a loop device */
64 const char *oldname = NULL;
67 if (stat(name, &fst)) {
68 fprintf(stderr, "umount: %s: %s\n", name, strerror(errno));
71 for (i = 0 ; i <= 7 ; i++) {
73 sprintf(dev, "/dev/loop%d", i);
76 if (lst.st_dev == fst.st_dev) {
84 status = umount(name);
86 #if defined BB_FEATURE_MOUNT_LOOP
87 if (!strncmp("/dev/loop", name, 9)) { /* this was a loop device, delete it */
105 umount_all(int useMtab)
111 if ((mountTable = setmntent (mtab_file, "r"))) {
112 while ((m = getmntent (mountTable)) != 0) {
113 char *blockDevice = m->mnt_fsname;
114 #if ! defined BB_MTAB
115 if (strcmp (blockDevice, "/dev/root") == 0) {
116 struct fstab* fstabItem;
117 fstabItem = getfsfile ("/");
118 if (fstabItem != NULL) {
119 blockDevice = fstabItem->fs_spec;
123 /* Don't umount /proc when doing umount -a */
124 if (strcmp (blockDevice, "proc") == 0)
127 status=do_umount (m->mnt_dir, useMtab);
129 /* Don't bother retrying the umount on busy devices */
134 status=do_umount (blockDevice, useMtab);
136 printf ("Couldn't umount %s on %s (type %s): %s\n",
137 blockDevice, m->mnt_dir, m->mnt_type, strerror(errno));
141 endmntent (mountTable);
147 umount_main(int argc, char** argv)
150 usage( umount_usage);
153 /* Parse any options */
154 while (--argc > 0 && **(++argv) == '-') {
155 while (*++(*argv)) switch (**argv) {
165 usage( umount_usage);
170 if(umountAll==TRUE) {
171 exit(umount_all(useMtab));
173 if ( do_umount(*argv,useMtab) == 0 )
181 #if defined BB_FEATURE_MOUNT_LOOP
182 static int del_loop(const char *device)
186 if ((fd = open(device, O_RDONLY)) < 0) {
190 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
191 perror("ioctl: LOOP_CLR_FD");