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>
49 #include "closestream.h"
53 #include "pathnames.h"
56 #define EJECT_DEFAULT_DEVICE "/dev/cdrom"
59 /* Used by the toggle_tray() function. If ejecting the tray takes this
60 * time or less, the tray was probably already ejected, so we close it
63 #define TRAY_WAS_ALREADY_OPEN_USECS 200000 /* about 0.2 seconds */
65 /* eject(1) is able to eject only 'removable' devices (attribute in /sys)
66 * _or_ devices connected by hotplug subsystem.
68 static const char * const hotplug_subsystems
[] = {
76 /* Global Variables */
77 static int a_option
; /* command flags and arguments */
97 static long int c_arg
;
98 static long int x_arg
;
100 struct libmnt_table
*mtab
;
101 struct libmnt_cache
*cache
;
103 static void vinfo(const char *fmt
, va_list va
)
105 fprintf(stdout
, "%s: ", program_invocation_short_name
);
110 static inline void verbose(const char *fmt
, ...)
122 static inline void info(const char *fmt
, ...)
130 static void __attribute__ ((__noreturn__
)) usage(FILE * out
)
132 fputs(USAGE_HEADER
, out
);
135 _(" %s [options] [<device>|<mountpoint>]\n"), program_invocation_short_name
);
137 fputs(USAGE_OPTIONS
, out
);
138 fputs(_(" -a, --auto <on|off> turn auto-eject feature on or off\n"
139 " -c, --changerslot <slot> switch discs on a CD-ROM changer\n"
140 " -d, --default display default device\n"
141 " -f, --floppy eject floppy\n"
142 " -F, --force don't care about device type\n"
143 " -i, --manualeject <on|off> toggle manual eject protection on/off\n"
144 " -m, --no-unmount do not unmount device even if it is mounted\n"
145 " -M, --no-partitions-unmount do not unmount another partitions\n"
146 " -n, --noop don't eject, just show device found\n"
147 " -p, --proc use /proc/mounts instead of /etc/mtab\n"
148 " -q, --tape eject tape\n"
149 " -r, --cdrom eject CD-ROM\n"
150 " -s, --scsi eject SCSI device\n"
151 " -t, --trayclose close tray\n"
152 " -T, --traytoggle toggle tray\n"
153 " -v, --verbose enable verbose output\n"
154 " -x, --cdspeed <speed> set CD-ROM max speed\n"
155 " -X, --listspeed list CD-ROM available speeds\n"),
158 fputs(USAGE_SEPARATOR
, out
);
159 fputs(USAGE_HELP
, out
);
160 fputs(USAGE_VERSION
, out
);
162 fputs(_("\nBy default tries -r, -s, -f, and -q in order until success.\n"), out
);
163 fprintf(out
, USAGE_MAN_TAIL("eject(1)"));
165 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
169 /* Handle command line options. */
170 static void parse_args(int argc
, char **argv
, char **device
)
172 static const struct option long_opts
[] =
174 {"auto", required_argument
, NULL
, 'a'},
175 {"cdrom", no_argument
, NULL
, 'r'},
176 {"cdspeed", required_argument
, NULL
, 'x'},
177 {"changerslot", required_argument
, NULL
, 'c'},
178 {"default", no_argument
, NULL
, 'd'},
179 {"floppy", no_argument
, NULL
, 'f'},
180 {"force", no_argument
, NULL
, 'F'},
181 {"help", no_argument
, NULL
, 'h'},
182 {"listspeed", no_argument
, NULL
, 'X'},
183 {"manualeject", required_argument
, NULL
, 'i'},
184 {"noop", no_argument
, NULL
, 'n'},
185 {"no-unmount", no_argument
, NULL
, 'm'},
186 {"no-partitions-unmount", no_argument
, NULL
, 'M' },
187 {"proc", no_argument
, NULL
, 'p'},
188 {"scsi", no_argument
, NULL
, 's'},
189 {"tape", no_argument
, NULL
, 'q'},
190 {"trayclose", no_argument
, NULL
, 't'},
191 {"traytoggle", no_argument
, NULL
, 'T'},
192 {"verbose", no_argument
, NULL
, 'v'},
193 {"version", no_argument
, NULL
, 'V'},
198 while ((c
= getopt_long(argc
, argv
,
199 "a:c:i:x:dfFhnqrstTXvVpmM", long_opts
, NULL
)) != -1) {
203 if (!strcmp(optarg
, "0") || !strcmp(optarg
, "off"))
205 else if (!strcmp(optarg
, "1") || !strcmp(optarg
, "on"))
208 errx(EXIT_FAILURE
, _("invalid argument to --auto/-a option"));
212 c_arg
= strtoul_or_err(optarg
, _("invalid argument to --changerslot/-c option"));
216 x_arg
= strtoul_or_err(optarg
, _("invalid argument to --cdspeed/-x option"));
232 if (!strcmp(optarg
, "0") || !strcmp(optarg
, "off"))
234 else if (!strcmp(optarg
, "1") || !strcmp(optarg
, "on"))
237 errx(EXIT_FAILURE
, _("invalid argument to --manualeject/-i option"));
273 printf(UTIL_LINUX_VERSION
);
283 /* check for a single additional argument */
284 if ((argc
- optind
) > 1)
285 errx(EXIT_FAILURE
, _("too many arguments"));
287 if ((argc
- optind
) == 1)
288 *device
= xstrdup(argv
[optind
]);
292 * Given name, such as foo, see if any of the following exist:
294 * foo (if foo starts with '.' or '/')
297 * If found, return the full path. If not found, return 0.
298 * Returns pointer to dynamically allocated string.
300 static char *find_device(const char *name
)
305 if ((*name
== '.' || *name
== '/') && access(name
, F_OK
) == 0)
306 return xstrdup(name
);
310 snprintf(buf
, sizeof(buf
), "/dev/%s", name
);
311 if (access(buf
, F_OK
) == 0)
318 /* Set or clear auto-eject mode. */
319 static void auto_eject(int fd
, int on
)
323 #if defined(CDROM_SET_OPTIONS) && defined(CDROM_CLEAR_OPTIONS)
325 status
= ioctl(fd
, CDROM_SET_OPTIONS
, CDO_AUTO_EJECT
);
327 status
= ioctl(fd
, CDROM_CLEAR_OPTIONS
, CDO_AUTO_EJECT
);
332 err(EXIT_FAILURE
,_("CD-ROM auto-eject command failed"));
336 * Stops CDROM from opening on manual eject pressing the button.
337 * This can be useful when you carry your laptop
338 * in your bag while it's on and no CD inserted in it's drive.
339 * Implemented as found in Documentation/ioctl/cdrom.txt
341 * TODO: Maybe we should check this also:
342 * EDRIVE_CANT_DO_THIS Door lock function not supported.
343 * EBUSY Attempt to unlock when multiple users
344 * have the drive open and not CAP_SYS_ADMIN
346 static void manual_eject(int fd
, int on
)
348 if (ioctl(fd
, CDROM_LOCKDOOR
, on
) < 0)
349 err(EXIT_FAILURE
, _("CD-ROM lock door command failed"));
352 info(_("CD-Drive may NOT be ejected with device button"));
354 info(_("CD-Drive may be ejected with device button"));
358 * Changer select. CDROM_SELECT_DISC is preferred, older kernels used
361 static void changer_select(int fd
, int slot
)
363 #ifdef CDROM_SELECT_DISC
364 if (ioctl(fd
, CDROM_SELECT_DISC
, slot
) < 0)
365 err(EXIT_FAILURE
, _("CD-ROM select disc command failed"));
367 #elif defined CDROMLOADFROMSLOT
368 if (ioctl(fd
, CDROMLOADFROMSLOT
, slot
) != 0)
369 err(EXIT_FAILURE
, _("CD-ROM load from slot command failed"));
371 warnx(_("IDE/ATAPI CD-ROM changer not supported by this kernel\n") );
376 * Close tray. Not supported by older kernels.
378 static void close_tray(int fd
)
382 #if defined(CDROMCLOSETRAY) || defined(CDIOCCLOSE)
383 #if defined(CDROMCLOSETRAY)
384 status
= ioctl(fd
, CDROMCLOSETRAY
);
385 #elif defined(CDIOCCLOSE)
386 status
= ioctl(fd
, CDIOCCLOSE
);
389 err(EXIT_FAILURE
, _("CD-ROM tray close command failed"));
391 warnx(_("CD-ROM tray close command not supported by this kernel\n"));
396 * Eject using CDROMEJECT ioctl.
398 static int eject_cdrom(int fd
)
400 #if defined(CDROMEJECT)
401 return ioctl(fd
, CDROMEJECT
);
402 #elif defined(CDIOCEJECT)
403 return ioctl(fd
, CDIOCEJECT
);
405 warnx(_("CD-ROM eject unsupported"));
414 * Written by Benjamin Schwenk <benjaminschwenk@yahoo.de> and
415 * Sybren Stuvel <sybren@thirdtower.com>
417 * Not supported by older kernels because it might use
421 static void toggle_tray(int fd
)
423 struct timeval time_start
, time_stop
;
426 #ifdef CDROM_DRIVE_STATUS
427 /* First ask the CDROM for info, otherwise fall back to manual. */
428 switch (ioctl(fd
, CDROM_DRIVE_STATUS
)) {
436 err(EXIT_FAILURE
, _("CD-ROM eject command failed"));
439 warnx(_("no CD-ROM information available"));
441 case CDS_DRIVE_NOT_READY
:
442 warnx(_("CD-ROM drive is not ready"));
449 /* Try to open the CDROM tray and measure the time therefor
450 * needed. In my experience the function needs less than 0.05
451 * seconds if the tray was already open, and at least 1.5 seconds
452 * if it was closed. */
453 gettimeofday(&time_start
, NULL
);
455 /* Send the CDROMEJECT command to the device. */
456 if (eject_cdrom(fd
) < 0)
457 err(EXIT_FAILURE
, _("CD-ROM eject command failed"));
459 /* Get the second timestamp, to measure the time needed to open
461 gettimeofday(&time_stop
, NULL
);
463 time_elapsed
= (time_stop
.tv_sec
* 1000000 + time_stop
.tv_usec
) -
464 (time_start
.tv_sec
* 1000000 + time_start
.tv_usec
);
466 /* If the tray "opened" too fast, we can be nearly sure, that it
467 * was already open. In this case, close it now. Else the tray was
468 * closed before. This would mean that we are done. */
469 if (time_elapsed
< TRAY_WAS_ALREADY_OPEN_USECS
)
474 * Select Speed of CD-ROM drive.
475 * Thanks to Roland Krivanek (krivanek@fmph.uniba.sk)
476 * http://dmpc.dbp.fmph.uniba.sk/~krivanek/cdrom_speed/
478 static void select_speed(int fd
, int speed
)
480 #ifdef CDROM_SELECT_SPEED
481 if (ioctl(fd
, CDROM_SELECT_SPEED
, speed
) != 0)
482 err(EXIT_FAILURE
, _("CD-ROM select speed command failed"));
484 warnx(_("CD-ROM select speed command not supported by this kernel"));
489 * Read Speed of CD-ROM drive. From Linux 2.6.13, the current speed
490 * is correctly reported
492 static int read_speed(const char *devname
)
494 int drive_number
= -1;
498 f
= fopen(_PATH_PROC_CDROMINFO
, "r");
500 err(EXIT_FAILURE
, _("cannot open %s"), _PATH_PROC_CDROMINFO
);
502 name
= rindex(devname
, '/') + 1;
508 if (!fgets(line
, sizeof(line
), f
))
511 /* find drive number in line "drive name" */
512 if (drive_number
== -1) {
513 if (strncmp(line
, "drive name:", 11) == 0) {
514 str
= strtok(&line
[11], "\t ");
516 while (strncmp(name
, str
, strlen(name
)) != 0) {
518 str
= strtok(NULL
, "\t ");
521 _("%s: failed to finding CD-ROM name"),
522 _PATH_PROC_CDROMINFO
);
525 /* find line "drive speed" and read the correct speed */
527 if (strncmp(line
, "drive speed:", 12) == 0) {
531 str
= strtok(&line
[12], "\t ");
532 for (i
= 1; i
< drive_number
; i
++)
533 str
= strtok(NULL
, "\t ");
537 _("%s: failed to read speed"),
538 _PATH_PROC_CDROMINFO
);
545 errx(EXIT_FAILURE
, _("failed to read speed"));
549 * List Speed of CD-ROM drive.
551 static void list_speeds(const char *name
, int fd
)
553 #ifdef CDROM_SELECT_SPEED
554 int max_speed
, curr_speed
= 0, prev_speed
;
557 max_speed
= read_speed(name
);
559 while (curr_speed
< max_speed
) {
560 prev_speed
= curr_speed
;
561 select_speed(fd
, prev_speed
+ 1);
562 curr_speed
= read_speed(name
);
563 if (curr_speed
> prev_speed
)
564 printf("%d ", curr_speed
);
566 curr_speed
= prev_speed
+ 1;
571 warnx(_("CD-ROM select speed command not supported by this kernel"));
576 * Eject using SCSI SG_IO commands. Return 1 if successful, 0 otherwise.
578 static int eject_scsi(int fd
)
582 unsigned char allowRmBlk
[6] = {ALLOW_MEDIUM_REMOVAL
, 0, 0, 0, 0, 0};
583 unsigned char startStop1Blk
[6] = {START_STOP
, 0, 0, 0, 1, 0};
584 unsigned char startStop2Blk
[6] = {START_STOP
, 0, 0, 0, 2, 0};
585 unsigned char inqBuff
[2];
586 unsigned char sense_buffer
[32];
588 if ((ioctl(fd
, SG_GET_VERSION_NUM
, &k
) < 0) || (k
< 30000)) {
589 verbose(_("not an sg device, or old sg driver"));
593 memset(&io_hdr
, 0, sizeof(sg_io_hdr_t
));
594 io_hdr
.interface_id
= 'S';
596 io_hdr
.mx_sb_len
= sizeof(sense_buffer
);
597 io_hdr
.dxfer_direction
= SG_DXFER_NONE
;
598 io_hdr
.dxfer_len
= 0;
599 io_hdr
.dxferp
= inqBuff
;
600 io_hdr
.sbp
= sense_buffer
;
601 io_hdr
.timeout
= 10000;
603 io_hdr
.cmdp
= allowRmBlk
;
604 status
= ioctl(fd
, SG_IO
, (void *)&io_hdr
);
608 io_hdr
.cmdp
= startStop1Blk
;
609 status
= ioctl(fd
, SG_IO
, (void *)&io_hdr
);
613 io_hdr
.cmdp
= startStop2Blk
;
614 status
= ioctl(fd
, SG_IO
, (void *)&io_hdr
);
618 /* force kernel to reread partition table when new disc inserted */
619 status
= ioctl(fd
, BLKRRPART
);
624 * Eject using FDEJECT ioctl. Return 1 if successful, 0 otherwise.
626 static int eject_floppy(int fd
)
628 return ioctl(fd
, FDEJECT
) >= 0;
633 * Rewind and eject using tape ioctl. Return 1 if successful, 0 otherwise.
635 static int eject_tape(int fd
)
637 struct mtop op
= { .mt_op
= MTOFFL
, .mt_count
= 0 };
639 return ioctl(fd
, MTIOCTOP
, &op
) >= 0;
643 /* umount a device. */
644 static void umount_one(const char *name
)
651 verbose(_("%s: unmounting"), name
);
655 if (setgid(getgid()) < 0)
656 err(EXIT_FAILURE
, _("cannot set group id"));
658 if (setuid(getuid()) < 0)
659 err(EXIT_FAILURE
, _("eject: cannot set user id"));
662 execl("/bin/umount", "/bin/umount", name
, "-n", NULL
);
664 execl("/bin/umount", "/bin/umount", name
, NULL
);
666 errx(EXIT_FAILURE
, _("unable to exec /bin/umount of `%s'"), name
);
669 warn( _("unable to fork"));
672 default: /* parent */
674 if (WIFEXITED(status
) == 0)
676 _("unmount of `%s' did not exit normally"), name
);
678 if (WEXITSTATUS(status
) != 0)
679 errx(EXIT_FAILURE
, _("unmount of `%s' failed\n"), name
);
684 /* Open a device file. */
685 static int open_device(const char *name
)
687 int fd
= open(name
, O_RDWR
|O_NONBLOCK
);
690 fd
= open(name
, O_RDONLY
|O_NONBLOCK
);
692 err(EXIT_FAILURE
, _("cannot open %s"), name
);
697 * See if device has been mounted by looking in mount table. If so, set
698 * device name and mount point name, and return 1, otherwise return 0.
700 static int device_get_mountpoint(char **devname
, char **mnt
)
702 struct libmnt_fs
*fs
;
708 mtab
= mnt_new_table();
710 err(EXIT_FAILURE
, _("failed to initialize libmount table"));
712 cache
= mnt_new_cache();
713 mnt_table_set_cache(mtab
, cache
);
716 rc
= mnt_table_parse_file(mtab
, _PATH_PROC_MOUNTINFO
);
718 rc
= mnt_table_parse_mtab(mtab
, NULL
);
720 err(EXIT_FAILURE
, _("failed to parse mount table"));
723 fs
= mnt_table_find_source(mtab
, *devname
, MNT_ITER_BACKWARD
);
725 /* maybe 'devname' is mountpoint rather than a real device */
726 fs
= mnt_table_find_target(mtab
, *devname
, MNT_ITER_BACKWARD
);
729 *devname
= xstrdup(mnt_fs_get_source(fs
));
734 *mnt
= xstrdup(mnt_fs_get_target(fs
));
735 return *mnt
? 0 : -1;
738 static char *get_disk_devname(const char *device
)
744 if (stat(device
, &st
) != 0)
747 /* get whole-disk devno */
748 if (sysfs_devno_to_wholedisk(st
.st_rdev
, diskname
,
749 sizeof(diskname
), &diskno
) != 0)
752 return st
.st_rdev
== diskno
? NULL
: find_device(diskname
);
755 static int umount_partitions(const char *disk
, int checkonly
)
757 struct sysfs_cxt cxt
= UL_SYSFSCXT_EMPTY
;
763 devno
= sysfs_devname_to_devno(disk
, NULL
);
764 if (sysfs_init(&cxt
, devno
, NULL
) != 0)
767 /* open /sys/block/<wholedisk> */
768 if (!(dir
= sysfs_opendir(&cxt
, NULL
)))
771 /* scan for partition subdirs */
772 while ((d
= readdir(dir
))) {
773 if (!strcmp(d
->d_name
, ".") || !strcmp(d
->d_name
, ".."))
776 if (sysfs_is_partition_dirent(dir
, d
, disk
)) {
778 char *dev
= find_device(d
->d_name
);
780 if (dev
&& device_get_mountpoint(&dev
, &mnt
) == 0) {
781 verbose(_("%s: mounted on %s"), dev
, mnt
);
799 static int is_hotpluggable_subsystem(const char *name
)
803 for (i
= 0; i
< ARRAY_SIZE(hotplug_subsystems
); i
++)
804 if (strcmp(name
, hotplug_subsystems
[i
]) == 0)
810 #define SUBSYSTEM_LINKNAME "/subsystem"
815 * chain: /sys/dev/block/../../devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.2/ \
816 * 1-1.2:1.0/host65/target65:0:0/65:0:0:0/block/sdb
818 * The function check if <chain>/subsystem symlink exists, if yes then returns
819 * basename of the readlink result, and remove the last subdirectory from the
822 static char *get_subsystem(char *chain
, char *buf
, size_t bufsz
)
827 if (!chain
|| !*chain
)
831 if (len
+ sizeof(SUBSYSTEM_LINKNAME
) > PATH_MAX
)
837 /* append "/subsystem" to the path */
838 memcpy(chain
+ len
, SUBSYSTEM_LINKNAME
, sizeof(SUBSYSTEM_LINKNAME
));
840 /* try if subsystem symlink exists */
841 sz
= readlink(chain
, buf
, bufsz
);
843 /* remove last subsystem from chain */
845 p
= strrchr(chain
, '/');
852 /* we found symlink to subsystem, return basename */
854 return basename(buf
);
862 static int is_hotpluggable(const char* device
)
864 struct sysfs_cxt cxt
= UL_SYSFSCXT_EMPTY
;
865 char devchain
[PATH_MAX
];
866 char subbuf
[PATH_MAX
];
872 devno
= sysfs_devname_to_devno(device
, NULL
);
873 if (sysfs_init(&cxt
, devno
, NULL
) != 0)
876 /* check /sys/dev/block/<maj>:<min>/removable attribute */
877 if (sysfs_read_int(&cxt
, "removable", &rc
) == 0 && rc
== 1) {
878 verbose(_("%s: is removable device"), device
);
882 /* read /sys/dev/block/<maj>:<min> symlink */
883 sz
= sysfs_readlink(&cxt
, NULL
, devchain
, sizeof(devchain
));
884 if (sz
<= 0 || sz
+ sizeof(_PATH_SYS_DEVBLOCK
"/") > sizeof(devchain
))
887 devchain
[sz
++] = '\0';
889 /* create absolute patch from the link */
890 memmove(devchain
+ sizeof(_PATH_SYS_DEVBLOCK
"/") - 1, devchain
, sz
);
891 memcpy(devchain
, _PATH_SYS_DEVBLOCK
"/",
892 sizeof(_PATH_SYS_DEVBLOCK
"/") - 1);
894 while ((sub
= get_subsystem(devchain
, subbuf
, sizeof(subbuf
)))) {
895 rc
= is_hotpluggable_subsystem(sub
);
897 verbose(_("%s: connected by hotplug subsystem: %s"),
909 /* handle -x option */
910 void set_device_speed(char *name
)
918 verbose(_("setting CD-ROM speed to auto"));
920 verbose(_("setting CD-ROM speed to %ldX"), x_arg
);
922 fd
= open_device(name
);
923 select_speed(fd
, x_arg
);
929 int main(int argc
, char **argv
)
933 char *mountpoint
= NULL
;
934 int worked
= 0; /* set to 1 when successfully ejected */
935 int fd
; /* file descriptor for device */
937 setlocale(LC_ALL
,"");
938 bindtextdomain(PACKAGE
, LOCALEDIR
);
940 atexit(close_stdout
);
942 /* parse the command line arguments */
943 parse_args(argc
, argv
, &device
);
945 /* handle -d option */
947 info(_("default device: `%s'"), EJECT_DEFAULT_DEVICE
);
952 device
= mnt_resolve_path(EJECT_DEFAULT_DEVICE
, NULL
);
953 verbose(_("using default device `%s'"), device
);
957 if (device
[strlen(device
)-1] == '/')
958 device
[strlen(device
)-1] = '\0';
960 /* figure out full device or mount point name */
961 p
= find_device(device
);
967 device
= mnt_resolve_spec(p
, NULL
);
972 errx(EXIT_FAILURE
, _("%s: unable to find device"), device
);
974 verbose(_("device name is `%s'"), device
);
976 device_get_mountpoint(&device
, &mountpoint
);
978 verbose(_("%s: mounted on %s"), device
, mountpoint
);
980 verbose(_("%s: not mounted"), device
);
982 disk
= get_disk_devname(device
);
984 verbose(_("%s: disc device: %s (disk device will be used for eject)"), device
, disk
);
991 if (stat(device
, &st
) != 0 || !S_ISBLK(st
.st_mode
))
992 errx(EXIT_FAILURE
, _("%s: not found mountpoint or device "
993 "with the given name"), device
);
995 verbose(_("%s: is whole-disk device"), device
);
998 if (F_option
== 0 && is_hotpluggable(device
) == 0)
999 errx(EXIT_FAILURE
, _("%s: is not hot-pluggable device"), device
);
1001 /* handle -n option */
1003 info(_("device is `%s'"), device
);
1004 verbose(_("exiting due to -n/--noop option"));
1005 return EXIT_SUCCESS
;
1008 /* handle -i option */
1010 fd
= open_device(device
);
1011 manual_eject(fd
, i_arg
);
1012 return EXIT_SUCCESS
;
1015 /* handle -a option */
1018 verbose(_("%s: enabling auto-eject mode"), device
);
1020 verbose(_("%s: disabling auto-eject mode"), device
);
1021 fd
= open_device(device
);
1022 auto_eject(fd
, a_arg
);
1023 return EXIT_SUCCESS
;
1026 /* handle -t option */
1028 verbose(_("%s: closing tray"), device
);
1029 fd
= open_device(device
);
1031 set_device_speed(device
);
1032 return EXIT_SUCCESS
;
1035 /* handle -T option */
1037 verbose(_("%s: toggling tray"), device
);
1038 fd
= open_device(device
);
1040 set_device_speed(device
);
1041 return EXIT_SUCCESS
;
1044 /* handle -X option */
1046 verbose(_("%s: listing CD-ROM speed"), device
);
1047 fd
= open_device(device
);
1048 list_speeds(device
, fd
);
1049 return EXIT_SUCCESS
;
1052 /* handle -x option only */
1054 set_device_speed(device
);
1058 * Unmount all partitions if -m is not specified; or umount given
1059 * mountpoint if -M is specified, otherwise print error of another
1060 * partition is mounted.
1063 int ct
= umount_partitions(device
, M_option
);
1065 if (ct
== 0 && mountpoint
)
1066 umount_one(mountpoint
); /* probably whole-device */
1069 if (ct
== 1 && mountpoint
)
1070 umount_one(mountpoint
);
1072 errx(EXIT_FAILURE
, _("error: %s: device in use"), device
);
1076 /* handle -c option */
1078 verbose(_("%s: selecting CD-ROM disc #%ld"), device
, c_arg
);
1079 fd
= open_device(device
);
1080 changer_select(fd
, c_arg
);
1081 set_device_speed(device
);
1082 return EXIT_SUCCESS
;
1085 /* if user did not specify type of eject, try all four methods */
1086 if (r_option
+ s_option
+ f_option
+ q_option
== 0)
1087 r_option
= s_option
= f_option
= q_option
= 1;
1090 fd
= open_device(device
);
1092 /* try various methods of ejecting until it works */
1094 verbose(_("%s: trying to eject using CD-ROM eject command"), device
);
1095 worked
= eject_cdrom(fd
);
1096 verbose(worked
? _("CD-ROM eject command succeeded") :
1097 _("CD-ROM eject command failed"));
1100 if (s_option
&& !worked
) {
1101 verbose(_("%s: trying to eject using SCSI commands"), device
);
1102 worked
= eject_scsi(fd
);
1103 verbose(worked
? _("SCSI eject succeeded") :
1104 _("SCSI eject failed"));
1107 if (f_option
&& !worked
) {
1108 verbose(_("%s: trying to eject using floppy eject command"), device
);
1109 worked
= eject_floppy(fd
);
1110 verbose(worked
? _("floppy eject command succeeded") :
1111 _("floppy eject command failed"));
1114 if (q_option
&& !worked
) {
1115 verbose(_("%s: trying to eject using tape offline command"), device
);
1116 worked
= eject_tape(fd
);
1117 verbose(worked
? _("tape offline command succeeded") :
1118 _("tape offline command failed"));
1122 errx(EXIT_FAILURE
, _("unable to eject"));
1129 mnt_free_table(mtab
);
1130 mnt_free_cache(cache
);
1132 return EXIT_SUCCESS
;