Changeset 2725 in MondoRescue for branches/2.2.9/mindi-busybox/util-linux/umount.c
- Timestamp:
- Feb 25, 2011, 9:26:54 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2.2.9/mindi-busybox/util-linux/umount.c
r1765 r2725 6 6 * Copyright (C) 2005 by Rob Landley <rob@landley.net> 7 7 * 8 * Licensed under GPL version 2, see file LICENSE in this tarball for details.8 * Licensed under GPLv2, see file LICENSE in this source tree. 9 9 */ 10 11 10 #include <mntent.h> 12 #include < getopt.h>11 #include <sys/mount.h> 13 12 #include "libbb.h" 14 13 15 #define OPTION_STRING "flDnravdt:" 16 #define OPT_FORCE 1 17 #define OPT_LAZY 2 18 #define OPT_DONTFREELOOP 4 19 #define OPT_NO_MTAB 8 20 #define OPT_REMOUNT 16 21 #define OPT_ALL (ENABLE_FEATURE_UMOUNT_ALL ? 32 : 0) 14 #if defined(__dietlibc__) 15 // TODO: This does not belong here. 16 /* 16.12.2006, Sampo Kellomaki (sampo@iki.fi) 17 * dietlibc-0.30 does not have implementation of getmntent_r() */ 18 static struct mntent *getmntent_r(FILE* stream, struct mntent* result, 19 char* buffer UNUSED_PARAM, int bufsize UNUSED_PARAM) 20 { 21 struct mntent* ment = getmntent(stream); 22 return memcpy(result, ment, sizeof(*ment)); 23 } 24 #endif 22 25 23 int umount_main(int argc, char **argv); 24 int umount_main(int argc, char **argv) 26 /* ignored: -v -d -t -i */ 27 #define OPTION_STRING "fldnra" "vdt:i" 28 #define OPT_FORCE (1 << 0) // Same as MNT_FORCE 29 #define OPT_LAZY (1 << 1) // Same as MNT_DETACH 30 #define OPT_FREELOOP (1 << 2) 31 #define OPT_NO_MTAB (1 << 3) 32 #define OPT_REMOUNT (1 << 4) 33 #define OPT_ALL (ENABLE_FEATURE_UMOUNT_ALL ? (1 << 5) : 0) 34 35 int umount_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 36 int umount_main(int argc UNUSED_PARAM, char **argv) 25 37 { 26 38 int doForce; 27 char path[2*PATH_MAX];28 39 struct mntent me; 29 40 FILE *fp; 30 char *fstype = 0;41 char *fstype = NULL; 31 42 int status = EXIT_SUCCESS; 32 43 unsigned opt; … … 37 48 } *mtl, *m; 38 49 39 /* Parse any options */40 41 50 opt = getopt32(argv, OPTION_STRING, &fstype); 42 43 argc -= optind; 51 //argc -= optind; 44 52 argv += optind; 45 53 54 // MNT_FORCE and MNT_DETACH (from linux/fs.h) must match 55 // OPT_FORCE and OPT_LAZY, otherwise this trick won't work: 46 56 doForce = MAX((opt & OPT_FORCE), (opt & OPT_LAZY)); 47 57 … … 51 61 * entry. Notice that this also naturally reverses the list so that -a 52 62 * umounts the most recent entries first. */ 63 m = mtl = NULL; 53 64 54 m = mtl = 0; 55 56 /* If we're umounting all, then m points to the start of the list and 57 * the argument list should be empty (which will match all). */ 58 65 // If we're umounting all, then m points to the start of the list and 66 // the argument list should be empty (which will match all). 59 67 fp = setmntent(bb_path_mtab_file, "r"); 60 68 if (!fp) { 61 69 if (opt & OPT_ALL) 62 bb_error_msg_and_die("can not open %s", bb_path_mtab_file);70 bb_error_msg_and_die("can't open '%s'", bb_path_mtab_file); 63 71 } else { 64 while (getmntent_r(fp, &me, path, sizeof(path))) {72 while (getmntent_r(fp, &me, bb_common_bufsiz1, sizeof(bb_common_bufsiz1))) { 65 73 /* Match fstype if passed */ 66 if ( fstype &&match_fstype(&me, fstype))74 if (!match_fstype(&me, fstype)) 67 75 continue; 68 m = x malloc(sizeof(struct mtab_list));76 m = xzalloc(sizeof(*m)); 69 77 m->next = mtl; 70 78 m->device = xstrdup(me.mnt_fsname); … … 75 83 } 76 84 77 / * If we're not umounting all, we need at least one argument. */85 // If we're not umounting all, we need at least one argument. 78 86 if (!(opt & OPT_ALL) && !fstype) { 79 m = 0; 80 if (!argc) bb_show_usage(); 87 if (!argv[0]) 88 bb_show_usage(); 89 m = NULL; 81 90 } 82 91 … … 85 94 int curstat; 86 95 char *zapit = *argv; 96 char *path; 87 97 88 98 // Do we already know what to umount this time through the loop? 89 if (m) safe_strncpy(path, m->dir, PATH_MAX); 99 if (m) 100 path = xstrdup(m->dir); 90 101 // For umount -a, end of mtab means time to exit. 91 else if (opt & OPT_ALL) break;92 // Get next command line argument (and look it up in mtab list)93 else if (!argc--) break;102 else if (opt & OPT_ALL) 103 break; 104 // Use command line argument (and look it up in mtab list) 94 105 else { 106 if (!zapit) 107 break; 95 108 argv++; 96 realpath(zapit, path); 97 for (m = mtl; m; m = m->next) 98 if (!strcmp(path, m->dir) || !strcmp(path, m->device)) 99 break; 109 path = xmalloc_realpath(zapit); 110 if (path) { 111 for (m = mtl; m; m = m->next) 112 if (strcmp(path, m->dir) == 0 || strcmp(path, m->device) == 0) 113 break; 114 } 100 115 } 101 116 // If we couldn't find this sucker in /etc/mtab, punt by passing our … … 108 123 109 124 // Force the unmount, if necessary. 110 if (curstat && doForce) {125 if (curstat && doForce) 111 126 curstat = umount2(zapit, doForce); 112 if (curstat)113 bb_error_msg("forced umount of %s failed!", zapit);114 }115 127 116 128 // If still can't umount, maybe remount read-only? 117 if (curstat && (opt & OPT_REMOUNT) && errno == EBUSY && m) {118 curstat = mount(m->device, zapit, NULL, MS_REMOUNT|MS_RDONLY, NULL);119 bb_error_msg(curstat ? "cannot remount %s read-only" :120 "%s busy - remounted read-only", m->device);121 }122 123 129 if (curstat) { 124 status = EXIT_FAILURE; 125 bb_perror_msg("cannot umount %s", zapit); 130 if ((opt & OPT_REMOUNT) && errno == EBUSY && m) { 131 // Note! Even if we succeed here, later we should not 132 // free loop device or erase mtab entry! 133 const char *msg = "%s busy - remounted read-only"; 134 curstat = mount(m->device, zapit, NULL, MS_REMOUNT|MS_RDONLY, NULL); 135 if (curstat) { 136 msg = "can't remount %s read-only"; 137 status = EXIT_FAILURE; 138 } 139 bb_error_msg(msg, m->device); 140 } else { 141 status = EXIT_FAILURE; 142 bb_perror_msg("can't %sumount %s", (doForce ? "forcibly " : ""), zapit); 143 } 126 144 } else { 127 / *De-allocate the loop device. This ioctl should be ignored on128 * any non-loop block devices. */129 if (ENABLE_FEATURE_MOUNT_LOOP && !(opt & OPT_DONTFREELOOP) && m)145 // De-allocate the loop device. This ioctl should be ignored on 146 // any non-loop block devices. 147 if (ENABLE_FEATURE_MOUNT_LOOP && (opt & OPT_FREELOOP) && m) 130 148 del_loop(m->device); 131 149 if (ENABLE_FEATURE_MTAB_SUPPORT && !(opt & OPT_NO_MTAB) && m) … … 136 154 // Note this means that "umount /dev/blah" will unmount all instances 137 155 // of /dev/blah, not just the most recent. 138 while (m && (m = m->next)) 139 if ((opt & OPT_ALL) || !strcmp(path, m->device)) 140 break; 156 if (m) { 157 while ((m = m->next) != NULL) 158 // NB: if m is non-NULL, path is non-NULL as well 159 if ((opt & OPT_ALL) || strcmp(path, m->device) == 0) 160 break; 161 } 162 free(path); 141 163 } 142 164 143 165 // Free mtab list if necessary 144 145 166 if (ENABLE_FEATURE_CLEAN_UP) { 146 167 while (mtl) {
Note:
See TracChangeset
for help on using the changeset viewer.