From 566cd92c9feb9b1dda29d799477a97b106078ea7 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Wed, 13 Jun 2012 13:46:24 +0200 Subject: eject: add --no-partitions-unmount Signed-off-by: Karel Zak --- sys-utils/eject.c | 64 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 41 insertions(+), 23 deletions(-) (limited to 'sys-utils/eject.c') diff --git a/sys-utils/eject.c b/sys-utils/eject.c index 4f3fb831f..240e27e74 100644 --- a/sys-utils/eject.c +++ b/sys-utils/eject.c @@ -90,6 +90,7 @@ static int v_option; static int x_option; static int p_option; static int m_option; +static int M_option; static int i_option; static int a_arg; static int i_arg; @@ -134,23 +135,24 @@ static void __attribute__ ((__noreturn__)) usage(FILE * out) _(" %s [options] [|]\n"), program_invocation_short_name); fputs(USAGE_OPTIONS, out); - fputs(_(" -a, --auto turn auto-eject feature on or off\n" - " -c, --changerslot switch discs on a CD-ROM changer\n" - " -d, --default display default device\n" - " -f, --floppy eject floppy\n" - " -F, --force don't care about device type\n" - " -i, --manualeject toggle manual eject protection on/off\n" - " -m, --no-unmount do not unmount device even if it is mounted\n" - " -n, --noop don't eject, just show device found\n" - " -p, --proc use /proc/mounts instead of /etc/mtab\n" - " -q, --tape eject tape\n" - " -r, --cdrom eject CD-ROM\n" - " -s, --scsi eject SCSI device\n" - " -t, --trayclose close tray\n" - " -T, --traytoggle toggle tray\n" - " -v, --verbose enable verbose output\n" - " -x, --cdspeed set CD-ROM max speed\n" - " -X, --listspeed list CD-ROM available speeds\n"), + fputs(_(" -a, --auto turn auto-eject feature on or off\n" + " -c, --changerslot switch discs on a CD-ROM changer\n" + " -d, --default display default device\n" + " -f, --floppy eject floppy\n" + " -F, --force don't care about device type\n" + " -i, --manualeject toggle manual eject protection on/off\n" + " -m, --no-unmount do not unmount device even if it is mounted\n" + " -M, --no-partitions-unmount do not unmount another partitions\n" + " -n, --noop don't eject, just show device found\n" + " -p, --proc use /proc/mounts instead of /etc/mtab\n" + " -q, --tape eject tape\n" + " -r, --cdrom eject CD-ROM\n" + " -s, --scsi eject SCSI device\n" + " -t, --trayclose close tray\n" + " -T, --traytoggle toggle tray\n" + " -v, --verbose enable verbose output\n" + " -x, --cdspeed set CD-ROM max speed\n" + " -X, --listspeed list CD-ROM available speeds\n"), out); fputs(USAGE_SEPARATOR, out); @@ -181,6 +183,7 @@ static void parse_args(int argc, char **argv, char **device) {"manualeject", required_argument, NULL, 'i'}, {"noop", no_argument, NULL, 'n'}, {"no-unmount", no_argument, NULL, 'm'}, + {"no-partitions-unmount", no_argument, NULL, 'M' }, {"proc", no_argument, NULL, 'p'}, {"scsi", no_argument, NULL, 's'}, {"tape", no_argument, NULL, 'q'}, @@ -193,7 +196,7 @@ static void parse_args(int argc, char **argv, char **device) int c; while ((c = getopt_long(argc, argv, - "a:c:i:x:dfFhnqrstTXvVpm", long_opts, NULL)) != -1) { + "a:c:i:x:dfFhnqrstTXvVpmM", long_opts, NULL)) != -1) { switch (c) { case 'a': a_option = 1; @@ -236,6 +239,9 @@ static void parse_args(int argc, char **argv, char **device) case 'm': m_option = 1; break; + case 'M': + M_option = 1; + break; case 'n': n_option = 1; break; @@ -743,16 +749,17 @@ static char *get_disk_devname(const char *device) return st.st_rdev == diskno ? NULL : find_device(diskname); } -static void umount_partitions(const char *disk) +static int umount_partitions(const char *disk, int checkonly) { struct sysfs_cxt cxt = UL_SYSFSCXT_EMPTY; dev_t devno; DIR *dir = NULL; struct dirent *d; + int count = 0; devno = sysfs_devname_to_devno(disk, NULL); if (sysfs_init(&cxt, devno, NULL) != 0) - return; + return 0; /* open /sys/block/ */ if (!(dir = sysfs_opendir(&cxt, NULL))) @@ -768,8 +775,12 @@ static void umount_partitions(const char *disk) char *dev = find_device(d->d_name); if (dev && device_get_mountpoint(&dev, &mnt) == 0) { - verbose(_("%s: mounted at %s"), dev, mnt); - umount_one(mnt); + verbose(_("%s: mounted on %s"), dev, mnt); + if (checkonly) + warnx(_("warning: %s: mounted on %s"), dev, mnt); + else + umount_one(mnt); + count++; } free(dev); } @@ -779,6 +790,8 @@ done: if (dir) closedir(dir); sysfs_deinit(&cxt); + + return count; } static int is_hotpluggable_subsystem(const char *name) @@ -1035,9 +1048,14 @@ int main(int argc, char **argv) /* if it is a multipartition device, unmount any other partitions on the device */ if (m_option != 1) { + int rc; + if (mountpoint) umount_one(mountpoint); /* usually whole-disk */ - umount_partitions(device); + + rc = umount_partitions(device, M_option); + if (M_option && rc) + errx(EXIT_FAILURE, _("error: %s: device in use"), device); } /* handle -c option */ -- cgit v1.2.3-55-g7522