]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - sys-utils/eject.c
docs: Fix dead references to kernel documentation
[thirdparty/util-linux.git] / sys-utils / eject.c
index 5ca1e7538a46a05cd1a8b3ffed09c4c0ae746fd6..fa85243b0f3af2d12dd9f6c769df9c0a9a2cf506 100644 (file)
@@ -52,6 +52,7 @@
 #include "xalloc.h"
 #include "pathnames.h"
 #include "sysfs.h"
+#include "monotonic.h"
 
 /*
  * sg_io_hdr_t driver_status -- see kernel include/scsi/scsi.h
  */
 #define TRAY_WAS_ALREADY_OPEN_USECS  200000    /* about 0.2 seconds */
 
-/* eject(1) is able to eject only 'removable' devices (attribute in /sys)
- * _or_ devices connected by hotplug subsystem.
- */
-static const char * const hotplug_subsystems[] = {
-       "usb",
-       "ieee1394",
-       "pcmcia",
-       "mmc",
-       "ccw"
-};
-
 struct eject_control {
        struct libmnt_table *mtab;
        char *device;                   /* device or mount point to be ejected */
@@ -106,6 +96,9 @@ struct eject_control {
                x_option:1,
                a_arg:1,
                i_arg:1;
+
+       unsigned int force_exclusive;   /* use O_EXCL */
+
        long int c_arg;                 /* changer slot number */
        long int x_arg;                 /* cd speed */
 };
@@ -137,13 +130,16 @@ static inline void info(const char *fmt, ...)
        va_end(va);
 }
 
-static void __attribute__ ((__noreturn__)) usage(FILE * out)
+static void __attribute__((__noreturn__)) usage(void)
 {
+       FILE *out = stdout;
        fputs(USAGE_HEADER, out);
-
        fprintf(out,
                _(" %s [options] [<device>|<mountpoint>]\n"), program_invocation_short_name);
 
+       fputs(USAGE_SEPARATOR, out);
+       fputs(_("Eject removable media.\n"), out);
+
        fputs(USAGE_OPTIONS, out);
        fputs(_(" -a, --auto <on|off>         turn auto-eject feature on or off\n"
                " -c, --changerslot <slot>    switch discs on a CD-ROM changer\n"
@@ -166,13 +162,12 @@ static void __attribute__ ((__noreturn__)) usage(FILE * out)
                out);
 
        fputs(USAGE_SEPARATOR, out);
-       fputs(USAGE_HELP, out);
-       fputs(USAGE_VERSION, out);
+       printf(USAGE_HELP_OPTIONS(29));
 
        fputs(_("\nBy default tries -r, -s, -f, and -q in order until success.\n"), out);
-       fprintf(out, USAGE_MAN_TAIL("eject(1)"));
+       printf(USAGE_MAN_TAIL("eject(1)"));
 
-       exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
+       exit(EXIT_SUCCESS);
 }
 
 
@@ -201,7 +196,7 @@ static void parse_args(struct eject_control *ctl, int argc, char **argv)
                {"traytoggle",  no_argument,       NULL, 'T'},
                {"verbose",     no_argument,       NULL, 'v'},
                {"version",     no_argument,       NULL, 'V'},
-               {0, 0, 0, 0}
+               {NULL, 0, NULL, 0}
        };
        int c;
 
@@ -210,12 +205,8 @@ static void parse_args(struct eject_control *ctl, int argc, char **argv)
                switch (c) {
                case 'a':
                        ctl->a_option = 1;
-                       if (!strcmp(optarg, "0") || !strcmp(optarg, "off"))
-                               ctl->a_arg = 0;
-                       else if (!strcmp(optarg, "1") || !strcmp(optarg, "on"))
-                               ctl->a_arg = 1;
-                       else
-                               errx(EXIT_FAILURE, _("invalid argument to --auto/-a option"));
+                       ctl->a_arg = parse_switch(optarg, _("argument error"),
+                                               "on", "off",  "1", "0",  NULL);
                        break;
                case 'c':
                        ctl->c_option = 1;
@@ -234,17 +225,10 @@ static void parse_args(struct eject_control *ctl, int argc, char **argv)
                case 'F':
                        ctl->F_option = 1;
                        break;
-               case 'h':
-                       usage(stdout);
-                       break;
                case 'i':
                        ctl->i_option = 1;
-                       if (!strcmp(optarg, "0") || !strcmp(optarg, "off"))
-                               ctl->i_arg = 0;
-                       else if (!strcmp(optarg, "1") || !strcmp(optarg, "on"))
-                               ctl->i_arg = 1;
-                       else
-                               errx(EXIT_FAILURE, _("invalid argument to --manualeject/-i option"));
+                       ctl->i_arg = parse_switch(optarg, _("argument error"),
+                                               "on", "off",  "1", "0",  NULL);
                        break;
                case 'm':
                        ctl->m_option = 1;
@@ -279,13 +263,13 @@ static void parse_args(struct eject_control *ctl, int argc, char **argv)
                case 'v':
                        ctl->v_option = 1;
                        break;
+
+               case 'h':
+                       usage();
                case 'V':
-                       printf(UTIL_LINUX_VERSION);
-                       exit(EXIT_SUCCESS);
-                       break;
+                       print_version(EXIT_SUCCESS);
                default:
-               case '?':
-                       usage(stderr);
+                       errtryhelp(EXIT_FAILURE);
                        break;
                }
        }
@@ -314,13 +298,12 @@ static char *find_device(const char *name)
 
        if ((*name == '.' || *name == '/') && access(name, F_OK) == 0)
                return xstrdup(name);
-       else {
-               char buf[PATH_MAX];
 
-               snprintf(buf, sizeof(buf), "/dev/%s", name);
-               if (access(buf, F_OK) == 0)
-                       return xstrdup(buf);
-       }
+       char buf[PATH_MAX];
+
+       snprintf(buf, sizeof(buf), "/dev/%s", name);
+       if (access(buf, F_OK) == 0)
+               return xstrdup(buf);
 
        return NULL;
 }
@@ -343,20 +326,23 @@ static void auto_eject(const struct eject_control *ctl)
 }
 
 /*
- * Stops CDROM from opening on manual eject pressing the button.
+ * Stops CDROM from opening on manual eject button press.
  * This can be useful when you carry your laptop
  * in your bag while it's on and no CD inserted in it's drive.
- * Implemented as found in Documentation/ioctl/cdrom.txt
- *
- * TODO: Maybe we should check this also:
- * EDRIVE_CANT_DO_THIS   Door lock function not supported.
- * EBUSY                 Attempt to unlock when multiple users
- *                       have the drive open and not CAP_SYS_ADMIN
+ * Implemented as found in Documentation/userspace-api/ioctl/cdrom.rst
  */
 static void manual_eject(const struct eject_control *ctl)
 {
-       if (ioctl(ctl->fd, CDROM_LOCKDOOR, ctl->i_arg) < 0)
-               err(EXIT_FAILURE, _("CD-ROM lock door command failed"));
+       if (ioctl(ctl->fd, CDROM_LOCKDOOR, ctl->i_arg) < 0) {
+               switch (errno) {
+               case EDRIVE_CANT_DO_THIS:
+                       errx(EXIT_FAILURE, _("CD-ROM door lock is not supported"));
+               case EBUSY:
+                       errx(EXIT_FAILURE, _("other users have the drive open and not CAP_SYS_ADMIN"));
+               default:
+                       err(EXIT_FAILURE, _("CD-ROM lock door command failed"));
+               }
+       }
 
        if (ctl->i_arg)
                info(_("CD-Drive may NOT be ejected with device button"));
@@ -433,9 +419,6 @@ static int eject_cdrom(int fd)
  */
 static void toggle_tray(int fd)
 {
-       struct timeval time_start, time_stop;
-       int time_elapsed;
-
 #ifdef CDROM_DRIVE_STATUS
        /* First ask the CDROM for info, otherwise fall back to manual.  */
        switch (ioctl(fd, CDROM_DRIVE_STATUS)) {
@@ -455,15 +438,17 @@ static void toggle_tray(int fd)
                warnx(_("CD-ROM drive is not ready"));
                return;
        default:
-               abort();
+               err(EXIT_FAILURE, _("CD-ROM status command failed"));
        }
-#endif
+#else
+       struct timeval time_start, time_stop;
+       int time_elapsed;
 
-       /* Try to open the CDROM tray and measure the time therefor
+       /* Try to open the CDROM tray and measure the time therefore
         * needed.  In my experience the function needs less than 0.05
         * seconds if the tray was already open, and at least 1.5 seconds
         * if it was closed.  */
-       gettimeofday(&time_start, NULL);
+       gettime_monotonic(&time_start);
 
        /* Send the CDROMEJECT command to the device. */
        if (!eject_cdrom(fd))
@@ -471,7 +456,7 @@ static void toggle_tray(int fd)
 
        /* Get the second timestamp, to measure the time needed to open
         * the tray.  */
-       gettimeofday(&time_stop, NULL);
+       gettime_monotonic(&time_stop);
 
        time_elapsed = (time_stop.tv_sec * 1000000 + time_stop.tv_usec) -
                (time_start.tv_sec * 1000000 + time_start.tv_usec);
@@ -481,6 +466,7 @@ static void toggle_tray(int fd)
         * closed before. This would mean that we are done.  */
        if (time_elapsed < TRAY_WAS_ALREADY_OPEN_USECS)
                close_tray(fd);
+#endif
 }
 
 /*
@@ -488,10 +474,10 @@ static void toggle_tray(int fd)
  * Thanks to Roland Krivanek (krivanek@fmph.uniba.sk)
  * http://dmpc.dbp.fmph.uniba.sk/~krivanek/cdrom_speed/
  */
-static void select_speed(int fd, int speed)
+static void select_speed(const struct eject_control *ctl)
 {
 #ifdef CDROM_SELECT_SPEED
-       if (ioctl(fd, CDROM_SELECT_SPEED, speed) != 0)
+       if (ioctl(ctl->fd, CDROM_SELECT_SPEED, ctl->x_arg) != 0)
                err(EXIT_FAILURE, _("CD-ROM select speed command failed"));
 #else
        warnx(_("CD-ROM select speed command not supported by this kernel"));
@@ -560,28 +546,24 @@ static int read_speed(const char *devname)
 /*
  * List Speed of CD-ROM drive.
  */
-static void list_speeds(const struct eject_control *ctl)
+static void list_speeds(struct eject_control *ctl)
 {
-#ifdef CDROM_SELECT_SPEED
-       int max_speed, curr_speed = 0, prev_speed;
+       int max_speed, curr_speed = 0;
 
-       select_speed(ctl->fd, 0);
+       select_speed(ctl);
        max_speed = read_speed(ctl->device);
 
        while (curr_speed < max_speed) {
-               prev_speed = curr_speed;
-               select_speed(ctl->fd, prev_speed + 1);
+               ctl->x_arg = curr_speed + 1;
+               select_speed(ctl);
                curr_speed = read_speed(ctl->device);
-               if (curr_speed > prev_speed)
+               if (ctl->x_arg < curr_speed)
                        printf("%d ", curr_speed);
                else
-                       curr_speed = prev_speed + 1;
+                       curr_speed = ctl->x_arg + 1;
        }
 
        printf("\n");
-#else
-       warnx(_("CD-ROM select speed command not supported by this kernel"));
-#endif
 }
 
 /*
@@ -685,7 +667,7 @@ static void umount_one(const struct eject_control *ctl, const char *name)
                else
                        execl("/bin/umount", "/bin/umount", name, NULL);
 
-               errx(EXIT_FAILURE, _("unable to exec /bin/umount of `%s'"), name);
+               errexec("/bin/umount");
 
        case -1:
                warn( _("unable to fork"));
@@ -704,15 +686,16 @@ static void umount_one(const struct eject_control *ctl, const char *name)
 }
 
 /* Open a device file. */
-static int open_device(const char *name)
+static void open_device(struct eject_control *ctl)
 {
-       int fd = open(name, O_RDWR|O_NONBLOCK);
-
-       if (fd < 0)
-               fd = open(name, O_RDONLY|O_NONBLOCK);
-       if (fd == -1)
-               err(EXIT_FAILURE, _("cannot open %s"), name);
-       return fd;
+       int extra = ctl->F_option == 0 &&               /* never use O_EXCL on --force */
+                   ctl->force_exclusive ? O_EXCL : 0;
+
+       ctl->fd = open(ctl->device, O_RDWR | O_NONBLOCK | extra);
+       if (ctl->fd < 0)
+               ctl->fd = open(ctl->device, O_RDONLY | O_NONBLOCK | extra);
+       if (ctl->fd == -1)
+               err(EXIT_FAILURE, _("cannot open %s"), ctl->device);
 }
 
 /*
@@ -777,20 +760,25 @@ static char *get_disk_devname(const char *device)
        return st.st_rdev == diskno ? NULL : find_device(diskname);
 }
 
+/* umount all partitions if -M not specified, otherwise returns
+ * number of the mounted partitions only.
+ */
 static int umount_partitions(struct eject_control *ctl)
 {
-       struct sysfs_cxt cxt = UL_SYSFSCXT_EMPTY;
+       struct path_cxt *pc = NULL;
        dev_t devno;
        DIR *dir = NULL;
        struct dirent *d;
        int count = 0;
 
-       devno = sysfs_devname_to_devno(ctl->device, NULL);
-       if (sysfs_init(&cxt, devno, NULL) != 0)
+       devno = sysfs_devname_to_devno(ctl->device);
+       if (devno)
+               pc = ul_new_sysfs_path(devno, NULL, NULL);
+       if (!pc)
                return 0;
 
        /* open /sys/block/<wholedisk> */
-       if (!(dir = sysfs_opendir(&cxt, NULL)))
+       if (!(dir = ul_path_opendir(pc, NULL)))
                goto done;
 
        /* scan for partition subdirs */
@@ -798,7 +786,7 @@ static int umount_partitions(struct eject_control *ctl)
                if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
                        continue;
 
-               if (sysfs_is_partition_dirent(dir, d, ctl->device)) {
+               if (sysfs_blkdev_is_partition_dirent(dir, d, ctl->device)) {
                        char *mnt = NULL;
                        char *dev = find_device(d->d_name);
 
@@ -816,126 +804,32 @@ static int umount_partitions(struct eject_control *ctl)
 done:
        if (dir)
                closedir(dir);
-       sysfs_deinit(&cxt);
+       ul_unref_path(pc);
 
        return count;
 }
 
-static int is_hotpluggable_subsystem(const char *name)
-{
-       size_t i;
-
-       for (i = 0; i < ARRAY_SIZE(hotplug_subsystems); i++)
-               if (strcmp(name, hotplug_subsystems[i]) == 0)
-                       return 1;
-
-       return 0;
-}
-
-#define SUBSYSTEM_LINKNAME     "/subsystem"
-
-/*
- * For example:
- *
- * chain: /sys/dev/block/../../devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.2/ \
- *                           1-1.2:1.0/host65/target65:0:0/65:0:0:0/block/sdb
- *
- * The function check if <chain>/subsystem symlink exists, if yes then returns
- * basename of the readlink result, and remove the last subdirectory from the
- * <chain> path.
- */
-static char *get_subsystem(char *chain, char *buf, size_t bufsz)
-{
-       size_t len;
-       char *p;
-
-       if (!chain || !*chain)
-               return NULL;
-
-       len = strlen(chain);
-       if (len + sizeof(SUBSYSTEM_LINKNAME) > PATH_MAX)
-               return NULL;
-
-       do {
-               ssize_t sz;
-
-               /* append "/subsystem" to the path */
-               memcpy(chain + len, SUBSYSTEM_LINKNAME, sizeof(SUBSYSTEM_LINKNAME));
-
-               /* try if subsystem symlink exists */
-               sz = readlink(chain, buf, bufsz - 1);
-
-               /* remove last subsystem from chain */
-               chain[len] = '\0';
-               p = strrchr(chain, '/');
-               if (p) {
-                       *p = '\0';
-                       len = p - chain;
-               }
-
-               if (sz > 0) {
-                       /* we found symlink to subsystem, return basename */
-                       buf[sz] = '\0';
-                       return basename(buf);
-               }
-
-       } while (p);
-
-       return NULL;
-}
-
 static int is_hotpluggable(const struct eject_control *ctl)
 {
-       struct sysfs_cxt cxt = UL_SYSFSCXT_EMPTY;
-       char devchain[PATH_MAX];
-       char subbuf[PATH_MAX];
+       struct path_cxt *pc = NULL;
        dev_t devno;
        int rc = 0;
-       ssize_t sz;
-       char *sub;
 
-       devno = sysfs_devname_to_devno(ctl->device, NULL);
-       if (sysfs_init(&cxt, devno, NULL) != 0)
+       devno = sysfs_devname_to_devno(ctl->device);
+       if (devno)
+               pc = ul_new_sysfs_path(devno, NULL, NULL);
+       if (!pc)
                return 0;
 
-       /* check /sys/dev/block/<maj>:<min>/removable attribute */
-       if (sysfs_read_int(&cxt, "removable", &rc) == 0 && rc == 1) {
-               verbose(ctl, _("%s: is removable device"), ctl->device);
-               goto done;
-       }
-
-       /* read /sys/dev/block/<maj>:<min> symlink */
-       sz = sysfs_readlink(&cxt, NULL, devchain, sizeof(devchain));
-       if (sz <= 0 || sz + sizeof(_PATH_SYS_DEVBLOCK "/") > sizeof(devchain))
-               goto done;
-
-       devchain[sz++] = '\0';
-
-       /* create absolute patch from the link */
-       memmove(devchain + sizeof(_PATH_SYS_DEVBLOCK "/") - 1, devchain, sz);
-       memcpy(devchain, _PATH_SYS_DEVBLOCK "/",
-              sizeof(_PATH_SYS_DEVBLOCK "/") - 1);
-
-       while ((sub = get_subsystem(devchain, subbuf, sizeof(subbuf)))) {
-               rc = is_hotpluggable_subsystem(sub);
-               if (rc) {
-                       verbose(ctl, _("%s: connected by hotplug subsystem: %s"),
-                               ctl->device, sub);
-                       break;
-               }
-       }
-
-done:
-       sysfs_deinit(&cxt);
+       rc = sysfs_blkdev_is_hotpluggable(pc);
+       ul_unref_path(pc);
        return rc;
 }
 
 
 /* handle -x option */
-static void set_device_speed(const struct eject_control *ctl)
+static void set_device_speed(struct eject_control *ctl)
 {
-       int fd;
-
        if (!ctl->x_option)
                return;
 
@@ -944,8 +838,8 @@ static void set_device_speed(const struct eject_control *ctl)
        else
                verbose(ctl, _("setting CD-ROM speed to %ldX"), ctl->x_arg);
 
-       fd = open_device(ctl->device);
-       select_speed(fd, ctl->x_arg);
+       open_device(ctl);
+       select_speed(ctl);
        exit(EXIT_SUCCESS);
 }
 
@@ -956,12 +850,12 @@ int main(int argc, char **argv)
        char *disk = NULL;
        char *mountpoint = NULL;
        int worked = 0;    /* set to 1 when successfully ejected */
-       struct eject_control ctl = { 0 };
+       struct eject_control ctl = { NULL };
 
        setlocale(LC_ALL,"");
        bindtextdomain(PACKAGE, LOCALEDIR);
        textdomain(PACKAGE);
-       atexit(close_stdout);
+       close_stdout_atexit();
 
        /* parse the command line arguments */
        parse_args(&ctl, argc, argv);
@@ -993,7 +887,7 @@ int main(int argc, char **argv)
        }
 
        if (!ctl.device)
-               errx(EXIT_FAILURE, _("%s: unable to find device"), ctl.device);
+               errx(EXIT_FAILURE, _("unable to find device"));
 
        verbose(&ctl, _("device name is `%s'"), ctl.device);
 
@@ -1031,7 +925,7 @@ int main(int argc, char **argv)
 
        /* handle -i option */
        if (ctl.i_option) {
-               ctl.fd = open_device(ctl.device);
+               open_device(&ctl);
                manual_eject(&ctl);
                return EXIT_SUCCESS;
        }
@@ -1042,7 +936,7 @@ int main(int argc, char **argv)
                        verbose(&ctl, _("%s: enabling auto-eject mode"), ctl.device);
                else
                        verbose(&ctl, _("%s: disabling auto-eject mode"), ctl.device);
-               ctl.fd = open_device(ctl.device);
+               open_device(&ctl);
                auto_eject(&ctl);
                return EXIT_SUCCESS;
        }
@@ -1050,7 +944,7 @@ int main(int argc, char **argv)
        /* handle -t option */
        if (ctl.t_option) {
                verbose(&ctl, _("%s: closing tray"), ctl.device);
-               ctl.fd = open_device(ctl.device);
+               open_device(&ctl);
                close_tray(ctl.fd);
                set_device_speed(&ctl);
                return EXIT_SUCCESS;
@@ -1059,7 +953,7 @@ int main(int argc, char **argv)
        /* handle -T option */
        if (ctl.T_option) {
                verbose(&ctl, _("%s: toggling tray"), ctl.device);
-               ctl.fd = open_device(ctl.device);
+               open_device(&ctl);
                toggle_tray(ctl.fd);
                set_device_speed(&ctl);
                return EXIT_SUCCESS;
@@ -1068,7 +962,7 @@ int main(int argc, char **argv)
        /* handle -X option */
        if (ctl.X_option) {
                verbose(&ctl, _("%s: listing CD-ROM speed"), ctl.device);
-               ctl.fd = open_device(ctl.device);
+               open_device(&ctl);
                list_speeds(&ctl);
                return EXIT_SUCCESS;
        }
@@ -1084,7 +978,7 @@ int main(int argc, char **argv)
         * partition is mounted.
         */
        if (!ctl.m_option) {
-               int ct = umount_partitions(&ctl);
+               int ct = umount_partitions(&ctl); /* umount all, or count mounted on -M */
 
                if (ct == 0 && mountpoint)
                        umount_one(&ctl, mountpoint); /* probably whole-device */
@@ -1095,12 +989,17 @@ int main(int argc, char **argv)
                        else if (ct)
                                errx(EXIT_FAILURE, _("error: %s: device in use"), ctl.device);
                }
+               /* Now, we assume the device is no more used, use O_EXCL to be
+                * resistant against our bugs and possible races (someone else
+                * remounted the device).
+                */
+               ctl.force_exclusive = 1;
        }
 
        /* handle -c option */
        if (ctl.c_option) {
                verbose(&ctl, _("%s: selecting CD-ROM disc #%ld"), ctl.device, ctl.c_arg);
-               ctl.fd = open_device(ctl.device);
+               open_device(&ctl);
                changer_select(&ctl);
                set_device_speed(&ctl);
                return EXIT_SUCCESS;
@@ -1111,7 +1010,7 @@ int main(int argc, char **argv)
                ctl.r_option = ctl.s_option = ctl.f_option = ctl.q_option = 1;
 
        /* open device */
-       ctl.fd = open_device(ctl.device);
+       open_device(&ctl);
 
        /* try various methods of ejecting until it works */
        if (ctl.r_option) {