Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Mini umount implementation for busybox |
| 3 | * |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 1999 by Lineo, inc. |
| 6 | * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org> |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 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 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 24 | #include "internal.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 25 | #include <stdio.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 26 | #include <sys/mount.h> |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 27 | #include <mntent.h> |
| 28 | #include <fstab.h> |
| 29 | #include <errno.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 30 | |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 31 | static const char umount_usage[] = |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 32 | "Usage: umount [flags] filesystem|directory\n\n" |
| 33 | "Flags:\n" |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 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 |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 64 | |
| 65 | static int |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 66 | umount_all(int useMtab) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 67 | { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 68 | int status; |
| 69 | struct mntent *m; |
| 70 | FILE *mountTable; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 71 | |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 72 | if ((mountTable = setmntent (mtab_file, "r"))) { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 73 | while ((m = getmntent (mountTable)) != 0) { |
| 74 | char *blockDevice = m->mnt_fsname; |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 75 | #if ! defined BB_MTAB |
Eric Andersen | 1667fb4 | 1999-11-27 20:34:28 +0000 | [diff] [blame] | 76 | if (strcmp (blockDevice, "/dev/root") == 0) { |
| 77 | struct fstab* fstabItem; |
| 78 | fstabItem = getfsfile ("/"); |
| 79 | if (fstabItem != NULL) { |
| 80 | blockDevice = fstabItem->fs_spec; |
| 81 | } |
| 82 | } |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 83 | #endif |
Eric Andersen | cf8c9cf | 1999-11-05 00:31:46 +0000 | [diff] [blame] | 84 | /* Don't umount /proc when doing umount -a */ |
| 85 | if (strcmp (blockDevice, "proc") == 0) |
| 86 | continue; |
| 87 | |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 88 | status=do_umount (m->mnt_dir, useMtab); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 89 | if (status!=0) { |
| 90 | /* Don't bother retrying the umount on busy devices */ |
| 91 | if (errno==EBUSY) { |
| 92 | perror(m->mnt_dir); |
| 93 | continue; |
| 94 | } |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 95 | status=do_umount (blockDevice, useMtab); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 96 | if (status!=0) { |
| 97 | printf ("Couldn't umount %s on %s (type %s): %s\n", |
| 98 | blockDevice, m->mnt_dir, m->mnt_type, strerror(errno)); |
| 99 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 100 | } |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 101 | } |
| 102 | endmntent (mountTable); |
| 103 | } |
| 104 | return( TRUE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | extern int |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 108 | umount_main(int argc, char** argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 109 | { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 110 | if (argc < 2) { |
Eric Andersen | b0e9a70 | 1999-10-18 22:28:26 +0000 | [diff] [blame] | 111 | usage( umount_usage); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 112 | } |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 113 | |
| 114 | /* Parse any options */ |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 115 | while (--argc > 0 && **(++argv) == '-') { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 116 | while (*++(*argv)) switch (**argv) { |
| 117 | case 'a': |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 118 | umountAll = TRUE; |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 119 | break; |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 120 | #ifdef BB_MTAB |
| 121 | case 'n': |
| 122 | useMtab = FALSE; |
| 123 | break; |
| 124 | #endif |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 125 | default: |
Eric Andersen | b0e9a70 | 1999-10-18 22:28:26 +0000 | [diff] [blame] | 126 | usage( umount_usage); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 127 | } |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 128 | } |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 129 | |
| 130 | |
Eric Andersen | b6a44b8 | 1999-11-13 04:47:09 +0000 | [diff] [blame] | 131 | if(umountAll==TRUE) { |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 132 | exit(umount_all(useMtab)); |
| 133 | } |
| 134 | if ( do_umount(*argv,useMtab) == 0 ) |
| 135 | exit (TRUE); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 136 | else { |
| 137 | perror("umount"); |
Eric Andersen | 3c16382 | 1999-10-14 22:16:57 +0000 | [diff] [blame] | 138 | exit( FALSE); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 139 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 140 | } |
| 141 | |