blob: 95f7dfb3cbeab4bb9e829d3ea75b343e253ccd1a [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 Andersenbed30e91999-10-18 19:02:32 +000032"Usage: umount filesystem\n"
33" or: umount directory\n"
34" or: umount -a"
35"to unmount all mounted file systems.\n";
Eric Andersencc8ed391999-10-05 16:24:54 +000036
37static int
Eric Andersenf811e071999-10-09 00:25:00 +000038umount_all()
Eric Andersencc8ed391999-10-05 16:24:54 +000039{
Eric Andersenf811e071999-10-09 00:25:00 +000040 int status;
41 struct mntent *m;
42 FILE *mountTable;
Eric Andersencc8ed391999-10-05 16:24:54 +000043
Eric Andersenf811e071999-10-09 00:25:00 +000044 if ((mountTable = setmntent ("/proc/mounts", "r"))) {
45 while ((m = getmntent (mountTable)) != 0) {
46 char *blockDevice = m->mnt_fsname;
47 if (strcmp (blockDevice, "/dev/root") == 0)
48 blockDevice = (getfsfile ("/"))->fs_spec;
49 status=umount (m->mnt_dir);
50 if (status!=0) {
51 /* Don't bother retrying the umount on busy devices */
52 if (errno==EBUSY) {
53 perror(m->mnt_dir);
54 continue;
55 }
56 printf ("Trying to umount %s failed: %s\n",
57 m->mnt_dir, strerror(errno));
58 printf ("Instead trying to umount %s\n", blockDevice);
59 status=umount (blockDevice);
60 if (status!=0) {
61 printf ("Couldn't umount %s on %s (type %s): %s\n",
62 blockDevice, m->mnt_dir, m->mnt_type, strerror(errno));
63 }
Eric Andersencc8ed391999-10-05 16:24:54 +000064 }
Eric Andersenf811e071999-10-09 00:25:00 +000065 }
66 endmntent (mountTable);
67 }
68 return( TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +000069}
70
71extern int
Eric Andersenf811e071999-10-09 00:25:00 +000072umount_main(int argc, char * * argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000073{
Eric Andersenf811e071999-10-09 00:25:00 +000074
75 if (argc < 2) {
Eric Andersenb0e9a701999-10-18 22:28:26 +000076 usage( umount_usage);
Eric Andersenf811e071999-10-09 00:25:00 +000077 }
78 argc--;
79 argv++;
80
81 /* Parse any options */
82 while (**argv == '-') {
83 while (*++(*argv)) switch (**argv) {
84 case 'a':
Eric Andersen3c163821999-10-14 22:16:57 +000085 exit ( umount_all() );
Eric Andersenf811e071999-10-09 00:25:00 +000086 break;
87 default:
Eric Andersenb0e9a701999-10-18 22:28:26 +000088 usage( umount_usage);
Eric Andersencc8ed391999-10-05 16:24:54 +000089 }
Eric Andersenf811e071999-10-09 00:25:00 +000090 }
91 if ( umount(*argv) == 0 )
Eric Andersen3c163821999-10-14 22:16:57 +000092 exit (TRUE);
Eric Andersenf811e071999-10-09 00:25:00 +000093 else {
94 perror("umount");
Eric Andersen3c163821999-10-14 22:16:57 +000095 exit( FALSE);
Eric Andersenf811e071999-10-09 00:25:00 +000096 }
Eric Andersencc8ed391999-10-05 16:24:54 +000097}
98