2 * Copyright (C) 1994-2005 Jeff Tranter (tranter@pobox.com)
3 * Copyright (C) 2012 Karel Zak <kzak@redhat.com>
4 * Copyright (C) Michal Luscon <mluscon@redhat.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 #include <sys/types.h>
35 #include <sys/ioctl.h>
38 #include <linux/cdrom.h>
40 #include <sys/mount.h>
41 #include <scsi/scsi.h>
43 #include <scsi/scsi_ioctl.h>
52 #include "pathnames.h"
55 #define EJECT_DEFAULT_DEVICE "/dev/cdrom"
58 /* Used by the toggle_tray() function. If ejecting the tray takes this
59 * time or less, the tray was probably already ejected, so we close it
62 #define TRAY_WAS_ALREADY_OPEN_USECS 200000 /* about 0.2 seconds */
64 /* eject(1) is able to eject only 'removable' devices (attribute in /sys)
65 * _or_ devices connected by hotplug subsystem.
67 static const char *hotplug_subsystems
[] = {
75 /* Global Variables */
76 static int a_option
; /* command flags and arguments */
95 static long int c_arg
;
96 static long int x_arg
;
98 struct libmnt_table
*mtab
;
99 struct libmnt_cache
*cache
;
101 static void vinfo(const char *fmt
, va_list va
)
103 fprintf(stdout
, "%s: ", program_invocation_short_name
);
108 static inline void verbose(const char *fmt
, ...)
120 static inline void info(const char *fmt
, ...)
128 static void __attribute__ ((__noreturn__
)) usage(FILE * out
)
130 fputs(USAGE_HEADER
, out
);
133 _(" %s [options] [<device>|<mountpoint>]\n"), program_invocation_short_name
);
135 fputs(USAGE_OPTIONS
, out
);
136 fputs(_(" -a, --auto <on|off> turn auto-eject feature on or off\n"
137 " -c, --changerslot <slot> switch discs on a CD-ROM changer\n"
138 " -d, --default display default device\n"
139 " -f, --floppy eject floppy\n"
140 " -F, --force don't care about device type\n"
141 " -i, --manualeject <on|off> toggle manual eject protection on/off\n"
142 " -m, --no-unmount do not unmount device even if it is mounted\n"
143 " -n, --noop don't eject, just show device found\n"
144 " -p, --proc use /proc/mounts instead of /etc/mtab\n"
145 " -q, --tape eject tape\n"
146 " -r, --cdrom eject CD-ROM\n"
147 " -s, --scsi eject SCSI device\n"
148 " -t, --trayclose close tray\n"
149 " -T, --traytoggle toggle tray\n"
150 " -v, --verbose enable verbose output\n"
151 " -x, --cdspeed <speed> set CD-ROM max speed\n"
152 " -X, --listspeed list CD-ROM available speeds\n"),
155 fputs(USAGE_SEPARATOR
, out
);
156 fputs(USAGE_HELP
, out
);
157 fputs(USAGE_VERSION
, out
);
159 fputs(_("\nBy default tries -r, -s, -f, and -q in order until success.\n"), out
);
160 fprintf(out
, USAGE_MAN_TAIL("eject(1)"));
162 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
166 /* Handle command line options. */
167 static void parse_args(int argc
, char **argv
, char **device
)
169 static const struct option long_opts
[] =
171 {"auto", required_argument
, NULL
, 'a'},
172 {"cdrom", no_argument
, NULL
, 'r'},
173 {"cdspeed", required_argument
, NULL
, 'x'},
174 {"changerslot", required_argument
, NULL
, 'c'},
175 {"default", no_argument
, NULL
, 'd'},
176 {"floppy", no_argument
, NULL
, 'f'},
177 {"force", no_argument
, NULL
, 'F'},
178 {"help", no_argument
, NULL
, 'h'},
179 {"listspeed", no_argument
, NULL
, 'X'},
180 {"manualeject", required_argument
, NULL
, 'i'},
181 {"noop", no_argument
, NULL
, 'n'},
182 {"no-unmount", no_argument
, NULL
, 'm'},
183 {"proc", no_argument
, NULL
, 'p'},
184 {"scsi", no_argument
, NULL
, 's'},
185 {"tape", no_argument
, NULL
, 'q'},
186 {"trayclose", no_argument
, NULL
, 't'},
187 {"traytoggle", no_argument
, NULL
, 'T'},
188 {"verbose", no_argument
, NULL
, 'v'},
189 {"version", no_argument
, NULL
, 'V'},
194 while ((c
= getopt_long(argc
, argv
,
195 "a:c:i:x:dfFhnqrstTXvVpm", long_opts
, NULL
)) != -1) {
199 if (!strcmp(optarg
, "0") || !strcmp(optarg
, "off"))
201 else if (!strcmp(optarg
, "1") || !strcmp(optarg
, "on"))
204 errx(EXIT_FAILURE
, _("invalid argument to --auto/-a option"));
208 c_arg
= strtoul_or_err(optarg
, _("invalid argument to --changerslot/-c option"));
212 x_arg
= strtoul_or_err(optarg
, _("invalid argument to --cdspeed/-x option"));
228 if (!strcmp(optarg
, "0") || !strcmp(optarg
, "off"))
230 else if (!strcmp(optarg
, "1") || !strcmp(optarg
, "on"))
233 errx(EXIT_FAILURE
, _("invalid argument to --manualeject/-i option"));
266 printf(UTIL_LINUX_VERSION
);
276 /* check for a single additional argument */
277 if ((argc
- optind
) > 1)
278 errx(EXIT_FAILURE
, _("too many arguments"));
280 if ((argc
- optind
) == 1)
281 *device
= xstrdup(argv
[optind
]);
285 * Given name, such as foo, see if any of the following exist:
287 * foo (if foo starts with '.' or '/')
290 * If found, return the full path. If not found, return 0.
291 * Returns pointer to dynamically allocated string.
293 static char *find_device(const char *name
)
298 if ((*name
== '.' || *name
== '/') && access(name
, F_OK
) == 0)
299 return xstrdup(name
);
303 snprintf(buf
, sizeof(buf
), "/dev/%s", name
);
304 if (access(buf
, F_OK
) == 0)
311 /* Set or clear auto-eject mode. */
312 static void auto_eject(int fd
, int on
)
316 #if defined(CDROM_SET_OPTIONS) && defined(CDROM_CLEAR_OPTIONS)
318 status
= ioctl(fd
, CDROM_SET_OPTIONS
, CDO_AUTO_EJECT
);
320 status
= ioctl(fd
, CDROM_CLEAR_OPTIONS
, CDO_AUTO_EJECT
);
325 err(EXIT_FAILURE
,_("CD-ROM auto-eject command failed"));
329 * Stops CDROM from opening on manual eject pressing the button.
330 * This can be useful when you carry your laptop
331 * in your bag while it's on and no CD inserted in it's drive.
332 * Implemented as found in Documentation/ioctl/cdrom.txt
334 * TODO: Maybe we should check this also:
335 * EDRIVE_CANT_DO_THIS Door lock function not supported.
336 * EBUSY Attempt to unlock when multiple users
337 * have the drive open and not CAP_SYS_ADMIN
339 static void manual_eject(int fd
, int on
)
341 if (ioctl(fd
, CDROM_LOCKDOOR
, on
) < 0)
342 err(EXIT_FAILURE
, _("CD-ROM lock door command failed"));
345 info(_("CD-Drive may NOT be ejected with device button"));
347 info(_("CD-Drive may be ejected with device button"));
351 * Changer select. CDROM_SELECT_DISC is preferred, older kernels used
354 static void changer_select(int fd
, int slot
)
356 #ifdef CDROM_SELECT_DISC
357 if (ioctl(fd
, CDROM_SELECT_DISC
, slot
) < 0)
358 err(EXIT_FAILURE
, _("CD-ROM select disc command failed"));
360 #elif defined CDROMLOADFROMSLOT
361 if (ioctl(fd
, CDROMLOADFROMSLOT
, slot
) != 0)
362 err(EXIT_FAILURE
, _("CD-ROM load from slot command failed"));
364 warnx(_("IDE/ATAPI CD-ROM changer not supported by this kernel\n") );
369 * Close tray. Not supported by older kernels.
371 static void close_tray(int fd
)
375 #if defined(CDROMCLOSETRAY) || defined(CDIOCCLOSE)
376 #if defined(CDROMCLOSETRAY)
377 status
= ioctl(fd
, CDROMCLOSETRAY
);
378 #elif defined(CDIOCCLOSE)
379 status
= ioctl(fd
, CDIOCCLOSE
);
382 err(EXIT_FAILURE
, _("CD-ROM tray close command failed"));
384 warnx(_("CD-ROM tray close command not supported by this kernel\n"));
391 * Written by Benjamin Schwenk <benjaminschwenk@yahoo.de> and
392 * Sybren Stuvel <sybren@thirdtower.com>
394 * Not supported by older kernels because it might use
398 static void toggle_tray(int fd
)
400 struct timeval time_start
, time_stop
;
403 #ifdef CDROMCLOSETRAY
404 /* Try to open the CDROM tray and measure the time therefor
405 * needed. In my experience the function needs less than 0.05
406 * seconds if the tray was already open, and at least 1.5 seconds
407 * if it was closed. */
408 gettimeofday(&time_start
, NULL
);
410 /* Send the CDROMEJECT command to the device. */
411 if (ioctl(fd
, CDROMEJECT
, 0) < 0)
412 err(EXIT_FAILURE
, _("CD-ROM eject command failed"));
414 /* Get the second timestamp, to measure the time needed to open
416 gettimeofday(&time_stop
, NULL
);
418 time_elapsed
= (time_stop
.tv_sec
* 1000000 + time_stop
.tv_usec
) -
419 (time_start
.tv_sec
* 1000000 + time_start
.tv_usec
);
421 /* If the tray "opened" too fast, we can be nearly sure, that it
422 * was already open. In this case, close it now. Else the tray was
423 * closed before. This would mean that we are done. */
424 if (time_elapsed
< TRAY_WAS_ALREADY_OPEN_USECS
)
427 warnx(_("CD-ROM tray toggle command not supported by this kernel"));
433 * Select Speed of CD-ROM drive.
434 * Thanks to Roland Krivanek (krivanek@fmph.uniba.sk)
435 * http://dmpc.dbp.fmph.uniba.sk/~krivanek/cdrom_speed/
437 static void select_speed(int fd
, int speed
)
439 #ifdef CDROM_SELECT_SPEED
440 if (ioctl(fd
, CDROM_SELECT_SPEED
, speed
) != 0)
441 err(EXIT_FAILURE
, _("CD-ROM select speed command failed"));
443 warnx(_("CD-ROM select speed command not supported by this kernel"));
448 * Read Speed of CD-ROM drive. From Linux 2.6.13, the current speed
449 * is correctly reported
451 static int read_speed(const char *devname
)
453 int drive_number
= -1;
457 f
= fopen(_PATH_PROC_CDROMINFO
, "r");
459 err(EXIT_FAILURE
, _("%s: open failed"), _PATH_PROC_CDROMINFO
);
461 name
= rindex(devname
, '/') + 1;
467 if (!fgets(line
, sizeof(line
), f
))
470 /* find drive number in line "drive name" */
471 if (drive_number
== -1) {
472 if (strncmp(line
, "drive name:", 11) == 0) {
473 str
= strtok(&line
[11], "\t ");
475 while (strncmp(name
, str
, strlen(name
)) != 0) {
477 str
= strtok(NULL
, "\t ");
480 _("%s: failed to finding CD-ROM name"),
481 _PATH_PROC_CDROMINFO
);
484 /* find line "drive speed" and read the correct speed */
486 if (strncmp(line
, "drive speed:", 12) == 0) {
490 str
= strtok(&line
[12], "\t ");
491 for (i
= 1; i
< drive_number
; i
++)
492 str
= strtok(NULL
, "\t ");
496 _("%s: failed to read speed"),
497 _PATH_PROC_CDROMINFO
);
503 errx(EXIT_FAILURE
, _("failed to read speed"));
507 * List Speed of CD-ROM drive.
509 static void list_speeds(const char *name
, int fd
)
511 #ifdef CDROM_SELECT_SPEED
512 int max_speed
, curr_speed
= 0, prev_speed
;
515 max_speed
= read_speed(name
);
517 while (curr_speed
< max_speed
) {
518 prev_speed
= curr_speed
;
519 select_speed(fd
, prev_speed
+ 1);
520 curr_speed
= read_speed(name
);
521 if (curr_speed
> prev_speed
)
522 printf("%d ", curr_speed
);
524 curr_speed
= prev_speed
+ 1;
529 warnx(_("CD-ROM select speed command not supported by this kernel"));
534 * Eject using CDROMEJECT ioctl. Return 1 if successful, 0 otherwise.
536 static int eject_cdrom(int fd
)
538 #if defined(CDROMEJECT)
539 return ioctl(fd
, CDROMEJECT
) == 0;
540 #elif defined(CDIOCEJECT)
541 return ioctl(fd
, CDIOCEJECT
) == 0;
543 warnx(_("CD-ROM eject unsupported"));
548 * Eject using SCSI SG_IO commands. Return 1 if successful, 0 otherwise.
550 static int eject_scsi(int fd
)
554 unsigned char allowRmBlk
[6] = {ALLOW_MEDIUM_REMOVAL
, 0, 0, 0, 0, 0};
555 unsigned char startStop1Blk
[6] = {START_STOP
, 0, 0, 0, 1, 0};
556 unsigned char startStop2Blk
[6] = {START_STOP
, 0, 0, 0, 2, 0};
557 unsigned char inqBuff
[2];
558 unsigned char sense_buffer
[32];
560 if ((ioctl(fd
, SG_GET_VERSION_NUM
, &k
) < 0) || (k
< 30000)) {
561 verbose(_("not an sg device, or old sg driver"));
565 memset(&io_hdr
, 0, sizeof(sg_io_hdr_t
));
566 io_hdr
.interface_id
= 'S';
568 io_hdr
.mx_sb_len
= sizeof(sense_buffer
);
569 io_hdr
.dxfer_direction
= SG_DXFER_NONE
;
570 io_hdr
.dxfer_len
= 0;
571 io_hdr
.dxferp
= inqBuff
;
572 io_hdr
.sbp
= sense_buffer
;
573 io_hdr
.timeout
= 10000;
575 io_hdr
.cmdp
= allowRmBlk
;
576 status
= ioctl(fd
, SG_IO
, (void *)&io_hdr
);
580 io_hdr
.cmdp
= startStop1Blk
;
581 status
= ioctl(fd
, SG_IO
, (void *)&io_hdr
);
585 io_hdr
.cmdp
= startStop2Blk
;
586 status
= ioctl(fd
, SG_IO
, (void *)&io_hdr
);
590 /* force kernel to reread partition table when new disc inserted */
591 status
= ioctl(fd
, BLKRRPART
);
596 * Eject using FDEJECT ioctl. Return 1 if successful, 0 otherwise.
598 static int eject_floppy(int fd
)
600 return ioctl(fd
, FDEJECT
) >= 0;
605 * Rewind and eject using tape ioctl. Return 1 if successful, 0 otherwise.
607 static int eject_tape(int fd
)
609 struct mtop op
= { .mt_op
= MTOFFL
, .mt_count
= 0 };
611 return ioctl(fd
, MTIOCTOP
, &op
) >= 0;
615 /* umount a device. */
616 static void umount_one(const char *name
)
623 verbose(_("%s: unmounting"), name
);
627 if (setgid(getgid()) < 0)
628 err(EXIT_FAILURE
, _("cannot set group id"));
630 if (setuid(getuid()) < 0)
631 err(EXIT_FAILURE
, _("eject: cannot set user id"));
634 execl("/bin/umount", "/bin/umount", name
, "-n", NULL
);
636 execl("/bin/umount", "/bin/umount", name
, NULL
);
638 errx(EXIT_FAILURE
, _("unable to exec /bin/umount of `%s'"), name
);
641 warn( _("unable to fork"));
644 default: /* parent */
646 if (WIFEXITED(status
) == 0)
648 _("unmount of `%s' did not exit normally"), name
);
650 if (WEXITSTATUS(status
) != 0)
651 errx(EXIT_FAILURE
, _("unmount of `%s' failed\n"), name
);
656 /* Open a device file. */
657 static int open_device(const char *name
)
659 int fd
= open(name
, O_RDWR
|O_NONBLOCK
);
662 fd
= open(name
, O_RDONLY
|O_NONBLOCK
);
664 errx(EXIT_FAILURE
, _("%s: open failed"), name
);
669 * See if device has been mounted by looking in mount table. If so, set
670 * device name and mount point name, and return 1, otherwise return 0.
672 static int device_get_mountpoint(char **devname
, char **mnt
)
674 struct libmnt_fs
*fs
;
680 mtab
= mnt_new_table();
682 err(EXIT_FAILURE
, _("failed to initialize libmount table"));
684 cache
= mnt_new_cache();
685 mnt_table_set_cache(mtab
, cache
);
688 rc
= mnt_table_parse_file(mtab
, _PATH_PROC_MOUNTINFO
);
690 rc
= mnt_table_parse_mtab(mtab
, NULL
);
692 err(EXIT_FAILURE
, _("failed to parse mount table"));
695 fs
= mnt_table_find_source(mtab
, *devname
, MNT_ITER_BACKWARD
);
697 /* maybe 'devname' is mountpoint rather than a real device */
698 fs
= mnt_table_find_target(mtab
, *devname
, MNT_ITER_BACKWARD
);
701 *devname
= xstrdup(mnt_fs_get_source(fs
));
706 *mnt
= xstrdup(mnt_fs_get_target(fs
));
707 /* We'll call umount(), so remove the filesystem from the table
708 * to avoid duplicate results in the next device_get_mountpoint()
712 return *mnt
? 0 : -1;
715 static char *get_disk_devname(const char *device
)
721 if (stat(device
, &st
) != 0)
724 /* get whole-disk devno */
725 if (sysfs_devno_to_wholedisk(st
.st_rdev
, diskname
,
726 sizeof(diskname
), &diskno
) != 0)
729 return st
.st_rdev
== diskno
? NULL
: find_device(diskname
);
732 static void umount_partitions(const char *disk
)
734 struct sysfs_cxt cxt
= UL_SYSFSCXT_EMPTY
;
739 devno
= sysfs_devname_to_devno(disk
, NULL
);
740 if (sysfs_init(&cxt
, devno
, NULL
) != 0)
743 /* open /sys/block/<wholedisk> */
744 if (!(dir
= sysfs_opendir(&cxt
, NULL
)))
747 /* scan for partition subdirs */
748 while ((d
= readdir(dir
))) {
749 if (!strcmp(d
->d_name
, ".") || !strcmp(d
->d_name
, ".."))
752 if (sysfs_is_partition_dirent(dir
, d
, disk
)) {
754 char *dev
= find_device(d
->d_name
);
756 if (dev
&& device_get_mountpoint(&dev
, &mnt
) == 0) {
757 verbose(_("%s: mounted at %s"), dev
, mnt
);
770 static int is_hotpluggable_subsystem(const char *name
)
774 for (i
= 0; i
< ARRAY_SIZE(hotplug_subsystems
); i
++)
775 if (strcmp(name
, hotplug_subsystems
[i
]) == 0)
781 #define SUBSYSTEM_LINKNAME "/subsystem"
786 * chain: /sys/dev/block/../../devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.2/ \
787 * 1-1.2:1.0/host65/target65:0:0/65:0:0:0/block/sdb
789 * The function check if <chain>/subsystem symlink exists, if yes then returns
790 * basename of the readlink result, and remove the last subdirectory from the
793 static char *get_subsystem(char *chain
, char *buf
, size_t bufsz
)
798 if (!chain
|| !*chain
)
802 if (len
+ sizeof(SUBSYSTEM_LINKNAME
) > PATH_MAX
)
808 /* append "/subsystem" to the path */
809 memcpy(chain
+ len
, SUBSYSTEM_LINKNAME
, sizeof(SUBSYSTEM_LINKNAME
));
811 /* try if subsystem symlink exists */
812 sz
= readlink(chain
, buf
, bufsz
);
814 /* remove last subsystem from chain */
816 p
= strrchr(chain
, '/');
823 /* we found symlink to subsystem, return basename */
825 return basename(buf
);
833 static int is_hotpluggable(const char* device
)
835 struct sysfs_cxt cxt
= UL_SYSFSCXT_EMPTY
;
836 char devchain
[PATH_MAX
];
837 char subbuf
[PATH_MAX
];
843 devno
= sysfs_devname_to_devno(device
, NULL
);
844 if (sysfs_init(&cxt
, devno
, NULL
) != 0)
847 /* check /sys/dev/block/<maj>:<min>/removable attribute */
848 if (sysfs_read_int(&cxt
, "removable", &rc
) == 0 && rc
== 1) {
849 verbose(_("%s: is removable device"), device
);
853 /* read /sys/dev/block/<maj>:<min> symlink */
854 sz
= sysfs_readlink(&cxt
, NULL
, devchain
, sizeof(devchain
));
855 if (sz
<= 0 || sz
+ sizeof(_PATH_SYS_DEVBLOCK
"/") > sizeof(devchain
))
858 devchain
[sz
++] = '\0';
860 /* create absolute patch from the link */
861 memmove(devchain
+ sizeof(_PATH_SYS_DEVBLOCK
"/") - 1, devchain
, sz
);
862 memcpy(devchain
, _PATH_SYS_DEVBLOCK
"/",
863 sizeof(_PATH_SYS_DEVBLOCK
"/") - 1);
865 while ((sub
= get_subsystem(devchain
, subbuf
, sizeof(subbuf
)))) {
866 rc
= is_hotpluggable_subsystem(sub
);
868 verbose(_("%s: connected by hotplug subsystem: %s"),
880 /* handle -x option */
881 void set_device_speed(char *name
)
889 verbose(_("setting CD-ROM speed to auto"));
891 verbose(_("setting CD-ROM speed to %ldX"), x_arg
);
893 fd
= open_device(name
);
894 select_speed(fd
, x_arg
);
900 int main(int argc
, char **argv
)
904 char *mountpoint
= NULL
;
905 int worked
= 0; /* set to 1 when successfully ejected */
906 int fd
; /* file descriptor for device */
908 setlocale(LC_ALL
,"");
909 bindtextdomain(PACKAGE
, LOCALEDIR
);
912 /* parse the command line arguments */
913 parse_args(argc
, argv
, &device
);
915 /* handle -d option */
917 info(_("default device: `%s'"), EJECT_DEFAULT_DEVICE
);
922 device
= mnt_resolve_path(EJECT_DEFAULT_DEVICE
, NULL
);
923 verbose(_("using default device `%s'"), device
);
927 if (device
[strlen(device
)-1] == '/')
928 device
[strlen(device
)-1] = '\0';
930 /* figure out full device or mount point name */
931 p
= find_device(device
);
934 device
= mnt_resolve_path(p
, NULL
);
939 errx(EXIT_FAILURE
, _("%s: unable to find device"), device
);
941 verbose(_("device name is `%s'"), device
);
943 device_get_mountpoint(&device
, &mountpoint
);
945 verbose(_("%s: mounted at %s"), device
, mountpoint
);
947 verbose(_("%s: not mounted"), device
);
949 disk
= get_disk_devname(device
);
951 verbose(_("%s: disc device: %s (disk device will be used for eject)"), device
, disk
);
956 verbose(_("%s: is whole-disk device"), device
);
958 if (F_option
== 0 && is_hotpluggable(device
) == 0)
959 errx(EXIT_FAILURE
, _("%s: is not hot-pluggable device"), device
);
961 /* handle -n option */
963 info(_("device is `%s'"), device
);
964 verbose(_("exiting due to -n/--noop option"));
968 /* handle -i option */
970 fd
= open_device(device
);
971 manual_eject(fd
, i_arg
);
975 /* handle -a option */
978 verbose(_("%s: enabling auto-eject mode"), device
);
980 verbose(_("%s: disabling auto-eject mode"), device
);
981 fd
= open_device(device
);
982 auto_eject(fd
, a_arg
);
986 /* handle -t option */
988 verbose(_("%s: closing tray"), device
);
989 fd
= open_device(device
);
991 set_device_speed(device
);
995 /* handle -T option */
997 verbose(_("%s: toggling tray"), device
);
998 fd
= open_device(device
);
1000 set_device_speed(device
);
1001 return EXIT_SUCCESS
;
1004 /* handle -X option */
1006 verbose(_("%s: listing CD-ROM speed"), device
);
1007 fd
= open_device(device
);
1008 list_speeds(device
, fd
);
1009 return EXIT_SUCCESS
;
1012 /* handle -x option only */
1014 set_device_speed(device
);
1017 /* if it is a multipartition device, unmount any other partitions on
1019 if (m_option
!= 1) {
1021 umount_one(mountpoint
); /* usually whole-disk */
1022 umount_partitions(device
);
1025 /* handle -c option */
1027 verbose(_("%s: selecting CD-ROM disc #%ld"), device
, c_arg
);
1028 fd
= open_device(device
);
1029 changer_select(fd
, c_arg
);
1030 set_device_speed(device
);
1031 return EXIT_SUCCESS
;
1034 /* if user did not specify type of eject, try all four methods */
1035 if (r_option
+ s_option
+ f_option
+ q_option
== 0)
1036 r_option
= s_option
= f_option
= q_option
= 1;
1039 fd
= open_device(device
);
1041 /* try various methods of ejecting until it works */
1043 verbose(_("%s: trying to eject using CD-ROM eject command"), device
);
1044 worked
= eject_cdrom(fd
);
1045 verbose(worked
? _("CD-ROM eject command succeeded") :
1046 _("CD-ROM eject command failed"));
1049 if (s_option
&& !worked
) {
1050 verbose(_("%s: trying to eject using SCSI commands"), device
);
1051 worked
= eject_scsi(fd
);
1052 verbose(worked
? _("SCSI eject succeeded") :
1053 _("SCSI eject failed"));
1056 if (f_option
&& !worked
) {
1057 verbose(_("%s: trying to eject using floppy eject command"), device
);
1058 worked
= eject_floppy(fd
);
1059 verbose(worked
? _("floppy eject command succeeded") :
1060 _("floppy eject command failed"));
1063 if (q_option
&& !worked
) {
1064 verbose(_("%s: trying to eject using tape offline command"), device
);
1065 worked
= eject_tape(fd
);
1066 verbose(worked
? _("tape offline command succeeded") :
1067 _("tape offline command failed"));
1071 errx(EXIT_FAILURE
, _("unable to eject"));
1078 mnt_free_table(mtab
);
1079 mnt_free_cache(cache
);
1081 return EXIT_SUCCESS
;