Tito switched option parsing over to switch umount over to bb_getopt_ulflags().

This commit is contained in:
Rob Landley 2005-09-24 22:41:08 +00:00
parent 5423df9ebc
commit 16722554bf
2 changed files with 34 additions and 29 deletions

View File

@ -3089,8 +3089,9 @@
"\nFlags:\n" "\t-a\tUnmount all file systems" \ "\nFlags:\n" "\t-a\tUnmount all file systems" \
USAGE_MTAB(" in /etc/mtab\n\t-n\tDon't erase /etc/mtab entries") \ USAGE_MTAB(" in /etc/mtab\n\t-n\tDon't erase /etc/mtab entries") \
"\n\t-r\tTry to remount devices as read-only if mount is busy" \ "\n\t-r\tTry to remount devices as read-only if mount is busy" \
USAGE_MOUNT_FORCE("\n\t-f\tForce umount (i.e., unreachable NFS server)") \ "\n\t-l\tLazy umount (detach filesystem)" \
USAGE_MOUNT_LOOP("\n\t-l\tDo not free loop device (if a loop device has been used)") "\n\t-f\tForce umount (i.e., unreachable NFS server)" \
USAGE_MOUNT_LOOP("\n\t-D\tDo not free loop device (if a loop device has been used)")
#define umount_example_usage \ #define umount_example_usage \
"$ umount /dev/hdc1 \n" "$ umount /dev/hdc1 \n"

View File

@ -18,19 +18,27 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/mount.h> #include <sys/mount.h>
#include <getopt.h>
#include "busybox.h" #include "busybox.h"
#define OPTION_STRING "flaDnrv"
#define OPT_FORCE 1
#define OPT_LAZY 2
#define OPT_ALL 4
#define OPT_DONTFREELOOP 8
#define OPT_NO_MTAB 16
#define OPT_REMOUNT 32
/* -v is ignored */
extern int umount_main(int argc, char **argv) extern int umount_main(int argc, char **argv)
{ {
int doForce = 0; int doForce;
int freeLoop = ENABLE_FEATURE_MOUNT_LOOP;
int useMtab = ENABLE_FEATURE_MTAB_SUPPORT;
int umountAll = FALSE;
int doRemount = FALSE;
char path[2*PATH_MAX]; char path[2*PATH_MAX];
struct mntent me; struct mntent me;
FILE *fp; FILE *fp;
int status=EXIT_SUCCESS; int status=EXIT_SUCCESS;
unsigned long opt;
struct mtab_list { struct mtab_list {
char *dir; char *dir;
char *device; char *device;
@ -40,19 +48,14 @@ extern int umount_main(int argc, char **argv)
if(argc < 2) bb_show_usage(); if(argc < 2) bb_show_usage();
/* Parse any options */ /* Parse any options */
while (--argc > 0 && **(++argv) == '-') {
while (*++(*argv)) { opt = bb_getopt_ulflags (argc, argv, "flaDnrv");
if(**argv=='a') umountAll = TRUE;
else if(ENABLE_FEATURE_MOUNT_LOOP && **argv=='D') freeLoop = FALSE; argc -= optind;
else if(ENABLE_FEATURE_MTAB_SUPPORT && **argv=='n') useMtab = FALSE; argv += optind;
else if(**argv=='f') doForce = 1; // MNT_FORCE
else if(**argv=='l') doForce = 2; // MNT_DETACH
else if(**argv=='r') doRemount = TRUE;
else if(**argv=='v');
else bb_show_usage();
}
}
doForce = MAX((opt & OPT_FORCE), (opt & OPT_LAZY));
/* Get a list of mount points from mtab. We read them all in now mostly /* Get a list of mount points from mtab. We read them all in now mostly
* for umount -a (so we don't have to worry about the list changing while * for umount -a (so we don't have to worry about the list changing while
* we iterate over it, or about getting stuck in a loop on the same failing * we iterate over it, or about getting stuck in a loop on the same failing
@ -73,16 +76,16 @@ extern int umount_main(int argc, char **argv)
/* If we're umounting all, then m points to the start of the list and /* If we're umounting all, then m points to the start of the list and
* the argument list should be empty (which will match all). */ * the argument list should be empty (which will match all). */
if(!umountAll) m=0; if(!(opt & OPT_ALL)) m=0;
// Loop through everything we're supposed to umount, and do so. // Loop through everything we're supposed to umount, and do so.
for(;;) { for(;;) {
int curstat; int curstat;
// Do we alrady know what to umount this time through the loop? // Do we already know what to umount this time through the loop?
if(m) safe_strncpy(path,m->dir,PATH_MAX); if(m) safe_strncpy(path,m->dir,PATH_MAX);
// For umountAll, end of mtab means time to exit. // For umount -a, end of mtab means time to exit.
else if(umountAll) break; else if(opt & OPT_ALL) break;
// Get next command line argument (and look it up in mtab list) // Get next command line argument (and look it up in mtab list)
else if(!argc--) break; else if(!argc--) break;
else { else {
@ -104,27 +107,28 @@ extern int umount_main(int argc, char **argv)
} }
// If still can't umount, maybe remount read-only? // If still can't umount, maybe remount read-only?
if (curstat && doRemount && errno == EBUSY && m) { if (curstat && (opt & OPT_REMOUNT) && errno == EBUSY && m) {
curstat = mount(m->device, path, NULL, MS_REMOUNT|MS_RDONLY, NULL); curstat = mount(m->device, path, NULL, MS_REMOUNT|MS_RDONLY, NULL);
bb_error_msg(curstat ? "Cannot remount %s read-only" : bb_error_msg(curstat ? "Cannot remount %s read-only" :
"%s busy - remounted read-only", m->device); "%s busy - remounted read-only", m->device);
} }
/* De-allcate the loop device. This ioctl should be ignored on any /* De-allocate the loop device. This ioctl should be ignored on any
* non-loop block devices. */ * non-loop block devices. */
if(ENABLE_FEATURE_MOUNT_LOOP && freeLoop && m) if(ENABLE_FEATURE_MOUNT_LOOP && !(opt & OPT_DONTFREELOOP) && m)
del_loop(m->device); del_loop(m->device);
if(curstat) { if(curstat) {
/* Yes, the ENABLE is redundant here, but the optimizer for ARM /* Yes, the ENABLE is redundant here, but the optimizer for ARM
* can't do simple constant propogation in local variables... */ * can't do simple constant propagation in local variables... */
if(ENABLE_FEATURE_MTAB_SUPPORT && useMtab && m) erase_mtab(m->dir); if(ENABLE_FEATURE_MTAB_SUPPORT && !(opt & OPT_NO_MTAB) && m)
erase_mtab(m->dir);
status = EXIT_FAILURE; status = EXIT_FAILURE;
bb_perror_msg("Couldn't umount %s\n", path); bb_perror_msg("Couldn't umount %s\n", path);
} }
// Find next matching mtab entry for -a or umount /dev // Find next matching mtab entry for -a or umount /dev
while(m && (m = m->next)) while(m && (m = m->next))
if(umountAll || !strcmp(path,m->device)) if((opt & OPT_ALL) || !strcmp(path,m->device))
break; break;
} }