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"
55 #include "monotonic.h"
58 * sg_io_hdr_t driver_status -- see kernel include/scsi/sg.h
61 # define DRIVER_SENSE 0x08
65 #define EJECT_DEFAULT_DEVICE "/dev/cdrom"
68 /* Used by the toggle_tray() function. If ejecting the tray takes this
69 * time or less, the tray was probably already ejected, so we close it
72 #define TRAY_WAS_ALREADY_OPEN_USECS 200000 /* about 0.2 seconds */
74 struct eject_control
{
75 struct libmnt_table
*mtab
;
76 char *device
; /* device or mount point to be ejected */
77 int fd
; /* file descriptor for device */
78 unsigned int /* command flags and arguments */
100 unsigned int force_exclusive
; /* use O_EXCL */
102 long int c_arg
; /* changer slot number */
103 long int x_arg
; /* cd speed */
106 static void vinfo(const char *fmt
, va_list va
)
108 fprintf(stdout
, "%s: ", program_invocation_short_name
);
113 static inline void __attribute__ ((__format__ (__printf__
, 2, 3)))
114 verbose(const struct eject_control
*ctl
, const char *fmt
, ...)
126 static inline __attribute__ ((__format__ (__printf__
, 1, 2)))
127 void info(const char *fmt
, ...)
135 static void __attribute__((__noreturn__
)) usage(void)
138 fputs(USAGE_HEADER
, out
);
140 _(" %s [options] [<device>|<mountpoint>]\n"), program_invocation_short_name
);
142 fputs(USAGE_SEPARATOR
, out
);
143 fputs(_("Eject removable media.\n"), out
);
145 fputs(USAGE_OPTIONS
, out
);
146 fputs(_(" -a, --auto <on|off> turn auto-eject feature on or off\n"
147 " -c, --changerslot <slot> switch discs on a CD-ROM changer\n"
148 " -d, --default display default device\n"
149 " -f, --floppy eject floppy\n"
150 " -F, --force don't care about device type\n"
151 " -i, --manualeject <on|off> toggle manual eject protection on/off\n"
152 " -m, --no-unmount do not unmount device even if it is mounted\n"
153 " -M, --no-partitions-unmount do not unmount another partitions\n"
154 " -n, --noop don't eject, just show device found\n"
155 " -p, --proc use /proc/mounts instead of /etc/mtab\n"
156 " -q, --tape eject tape\n"
157 " -r, --cdrom eject CD-ROM\n"
158 " -s, --scsi eject SCSI device\n"
159 " -t, --trayclose close tray\n"
160 " -T, --traytoggle toggle tray\n"
161 " -v, --verbose enable verbose output\n"
162 " -x, --cdspeed <speed> set CD-ROM max speed\n"
163 " -X, --listspeed list CD-ROM available speeds\n"),
166 fputs(USAGE_SEPARATOR
, out
);
167 fprintf(out
, USAGE_HELP_OPTIONS(29));
169 fputs(_("\nBy default tries -r, -s, -f, and -q in order until success.\n"), out
);
170 fprintf(out
, USAGE_MAN_TAIL("eject(1)"));
176 /* Handle command line options. */
177 static void parse_args(struct eject_control
*ctl
, int argc
, char **argv
)
179 static const struct option long_opts
[] =
181 {"auto", required_argument
, NULL
, 'a'},
182 {"cdrom", no_argument
, NULL
, 'r'},
183 {"cdspeed", required_argument
, NULL
, 'x'},
184 {"changerslot", required_argument
, NULL
, 'c'},
185 {"default", no_argument
, NULL
, 'd'},
186 {"floppy", no_argument
, NULL
, 'f'},
187 {"force", no_argument
, NULL
, 'F'},
188 {"help", no_argument
, NULL
, 'h'},
189 {"listspeed", no_argument
, NULL
, 'X'},
190 {"manualeject", required_argument
, NULL
, 'i'},
191 {"noop", no_argument
, NULL
, 'n'},
192 {"no-unmount", no_argument
, NULL
, 'm'},
193 {"no-partitions-unmount", no_argument
, NULL
, 'M' },
194 {"proc", no_argument
, NULL
, 'p'},
195 {"scsi", no_argument
, NULL
, 's'},
196 {"tape", no_argument
, NULL
, 'q'},
197 {"trayclose", no_argument
, NULL
, 't'},
198 {"traytoggle", no_argument
, NULL
, 'T'},
199 {"verbose", no_argument
, NULL
, 'v'},
200 {"version", no_argument
, NULL
, 'V'},
205 while ((c
= getopt_long(argc
, argv
,
206 "a:c:i:x:dfFhnqrstTXvVpmM", long_opts
, NULL
)) != -1) {
210 ctl
->a_arg
= parse_switch(optarg
, _("argument error"),
211 "on", "off", "1", "0", NULL
);
215 ctl
->c_arg
= strtoul_or_err(optarg
, _("invalid argument to --changerslot/-c option"));
219 ctl
->x_arg
= strtoul_or_err(optarg
, _("invalid argument to --cdspeed/-x option"));
232 ctl
->i_arg
= parse_switch(optarg
, _("argument error"),
233 "on", "off", "1", "0", NULL
);
272 print_version(EXIT_SUCCESS
);
274 errtryhelp(EXIT_FAILURE
);
279 /* check for a single additional argument */
280 if ((argc
- optind
) > 1)
281 errx(EXIT_FAILURE
, _("too many arguments"));
283 if ((argc
- optind
) == 1)
284 ctl
->device
= xstrdup(argv
[optind
]);
288 * Given name, such as foo, see if any of the following exist:
290 * foo (if foo starts with '.' or '/')
293 * If found, return the full path. If not found, return 0.
294 * Returns pointer to dynamically allocated string.
296 static char *find_device(const char *name
)
301 if ((*name
== '.' || *name
== '/') && access(name
, F_OK
) == 0)
302 return xstrdup(name
);
306 snprintf(buf
, sizeof(buf
), "/dev/%s", name
);
307 if (access(buf
, F_OK
) == 0)
313 /* Set or clear auto-eject mode. */
314 static void auto_eject(const struct eject_control
*ctl
)
318 #if defined(CDROM_SET_OPTIONS) && defined(CDROM_CLEAR_OPTIONS)
320 status
= ioctl(ctl
->fd
, CDROM_SET_OPTIONS
, CDO_AUTO_EJECT
);
322 status
= ioctl(ctl
->fd
, CDROM_CLEAR_OPTIONS
, CDO_AUTO_EJECT
);
327 err(EXIT_FAILURE
,_("CD-ROM auto-eject command failed"));
331 * Stops CDROM from opening on manual eject button press.
332 * This can be useful when you carry your laptop
333 * in your bag while it's on and no CD inserted in it's drive.
334 * Implemented as found in Documentation/userspace-api/ioctl/cdrom.rst
336 static void manual_eject(const struct eject_control
*ctl
)
338 if (ioctl(ctl
->fd
, CDROM_LOCKDOOR
, ctl
->i_arg
) < 0) {
340 case EDRIVE_CANT_DO_THIS
:
341 errx(EXIT_FAILURE
, _("CD-ROM door lock is not supported"));
343 errx(EXIT_FAILURE
, _("other users have the drive open and not CAP_SYS_ADMIN"));
345 err(EXIT_FAILURE
, _("CD-ROM lock door command failed"));
350 info(_("CD-Drive may NOT be ejected with device button"));
352 info(_("CD-Drive may be ejected with device button"));
356 * Changer select. CDROM_SELECT_DISC is preferred, older kernels used
359 static void changer_select(const struct eject_control
*ctl
)
361 #ifdef CDROM_SELECT_DISC
362 if (ioctl(ctl
->fd
, CDROM_SELECT_DISC
, ctl
->c_arg
) < 0)
363 err(EXIT_FAILURE
, _("CD-ROM select disc command failed"));
365 #elif defined CDROMLOADFROMSLOT
366 if (ioctl(ctl
->fd
, CDROMLOADFROMSLOT
, ctl
->c_arg
) != 0)
367 err(EXIT_FAILURE
, _("CD-ROM load from slot command failed"));
369 warnx(_("IDE/ATAPI CD-ROM changer not supported by this kernel\n") );
374 * Close tray. Not supported by older kernels.
376 static void close_tray(int fd
)
380 #if defined(CDROMCLOSETRAY) || defined(CDIOCCLOSE)
381 #if defined(CDROMCLOSETRAY)
382 status
= ioctl(fd
, CDROMCLOSETRAY
);
383 #elif defined(CDIOCCLOSE)
384 status
= ioctl(fd
, CDIOCCLOSE
);
387 err(EXIT_FAILURE
, _("CD-ROM tray close command failed"));
389 warnx(_("CD-ROM tray close command not supported by this kernel\n"));
394 * Eject using CDROMEJECT ioctl.
396 static int eject_cdrom(int fd
)
398 #if defined(CDROMEJECT)
399 int ret
= ioctl(fd
, CDROM_LOCKDOOR
, 0);
402 return ioctl(fd
, CDROMEJECT
) >= 0;
403 #elif defined(CDIOCEJECT)
404 return ioctl(fd
, CDIOCEJECT
) >= 0;
406 warnx(_("CD-ROM eject unsupported"));
415 * Written by Benjamin Schwenk <benjaminschwenk@yahoo.de> and
416 * Sybren Stuvel <sybren@thirdtower.com>
418 * Not supported by older kernels because it might use
422 static void toggle_tray(int fd
)
424 #ifdef CDROM_DRIVE_STATUS
425 /* First ask the CDROM for info, otherwise fall back to manual. */
426 switch (ioctl(fd
, CDROM_DRIVE_STATUS
)) {
433 if (!eject_cdrom(fd
))
434 err(EXIT_FAILURE
, _("CD-ROM eject command failed"));
437 warnx(_("no CD-ROM information available"));
439 case CDS_DRIVE_NOT_READY
:
440 warnx(_("CD-ROM drive is not ready"));
443 err(EXIT_FAILURE
, _("CD-ROM status command failed"));
446 struct timeval time_start
, time_stop
;
449 /* Try to open the CDROM tray and measure the time therefore
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 gettime_monotonic(&time_start
);
455 /* Send the CDROMEJECT command to the device. */
456 if (!eject_cdrom(fd
))
457 err(EXIT_FAILURE
, _("CD-ROM eject command failed"));
459 /* Get the second timestamp, to measure the time needed to open
461 gettime_monotonic(&time_stop
);
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
)
475 * Select Speed of CD-ROM drive.
476 * Thanks to Roland Krivanek (krivanek@fmph.uniba.sk)
477 * http://dmpc.dbp.fmph.uniba.sk/~krivanek/cdrom_speed/
479 static void select_speed(const struct eject_control
*ctl
)
481 #ifdef CDROM_SELECT_SPEED
482 if (ioctl(ctl
->fd
, CDROM_SELECT_SPEED
, ctl
->x_arg
) != 0)
483 err(EXIT_FAILURE
, _("CD-ROM select speed command failed"));
485 warnx(_("CD-ROM select speed command not supported by this kernel"));
490 * Read Speed of CD-ROM drive. From Linux 2.6.13, the current speed
491 * is correctly reported
493 static int read_speed(const char *devname
)
495 int drive_number
= -1;
499 f
= fopen(_PATH_PROC_CDROMINFO
, "r");
501 err(EXIT_FAILURE
, _("cannot open %s"), _PATH_PROC_CDROMINFO
);
503 name
= strrchr(devname
, '/') + 1;
505 while (name
&& !feof(f
)) {
509 if (!fgets(line
, sizeof(line
), f
))
512 /* find drive number in line "drive name" */
513 if (drive_number
== -1) {
514 if (strncmp(line
, "drive name:", 11) == 0) {
515 str
= strtok(&line
[11], "\t ");
517 while (str
&& strncmp(name
, str
, strlen(name
)) != 0) {
519 str
= strtok(NULL
, "\t ");
522 _("%s: failed to finding CD-ROM name"),
523 _PATH_PROC_CDROMINFO
);
526 /* find line "drive speed" and read the correct speed */
528 if (strncmp(line
, "drive speed:", 12) == 0) {
534 normalize_whitespace((unsigned char *) str
);
536 if (ul_strtos32(str
, &n
, 10) == 0)
539 errx(EXIT_FAILURE
, _("%s: failed to read speed"),
540 _PATH_PROC_CDROMINFO
);
545 errx(EXIT_FAILURE
, _("failed to read speed"));
549 * List Speed of CD-ROM drive.
551 static void list_speeds(struct eject_control
*ctl
)
553 int max_speed
, curr_speed
= 0;
556 max_speed
= read_speed(ctl
->device
);
558 while (curr_speed
< max_speed
) {
559 ctl
->x_arg
= curr_speed
+ 1;
561 curr_speed
= read_speed(ctl
->device
);
562 if (ctl
->x_arg
< curr_speed
)
563 printf("%d ", curr_speed
);
565 curr_speed
= ctl
->x_arg
+ 1;
572 * Eject using SCSI SG_IO commands. Return 1 if successful, 0 otherwise.
574 static int eject_scsi(const struct eject_control
*ctl
)
578 unsigned char allowRmBlk
[6] = {ALLOW_MEDIUM_REMOVAL
, 0, 0, 0, 0, 0};
579 unsigned char startStop1Blk
[6] = {START_STOP
, 0, 0, 0, 1, 0};
580 unsigned char startStop2Blk
[6] = {START_STOP
, 0, 0, 0, 2, 0};
581 unsigned char inqBuff
[2];
582 unsigned char sense_buffer
[32];
584 if ((ioctl(ctl
->fd
, SG_GET_VERSION_NUM
, &k
) < 0) || (k
< 30000)) {
585 verbose(ctl
, _("not an sg device, or old sg driver"));
589 memset(&io_hdr
, 0, sizeof(sg_io_hdr_t
));
590 io_hdr
.interface_id
= 'S';
592 io_hdr
.mx_sb_len
= sizeof(sense_buffer
);
593 io_hdr
.dxfer_direction
= SG_DXFER_NONE
;
594 io_hdr
.dxfer_len
= 0;
595 io_hdr
.dxferp
= inqBuff
;
596 io_hdr
.sbp
= sense_buffer
;
597 io_hdr
.timeout
= 10000;
599 io_hdr
.cmdp
= allowRmBlk
;
600 status
= ioctl(ctl
->fd
, SG_IO
, (void *)&io_hdr
);
601 if (status
< 0 || io_hdr
.host_status
|| io_hdr
.driver_status
)
604 io_hdr
.cmdp
= startStop1Blk
;
605 status
= ioctl(ctl
->fd
, SG_IO
, (void *)&io_hdr
);
606 if (status
< 0 || io_hdr
.host_status
)
609 /* Ignore errors when there is not medium -- in this case driver sense
610 * buffer sets MEDIUM NOT PRESENT (3a) bit. For more details see:
611 * http://www.tldp.org/HOWTO/archived/SCSI-Programming-HOWTO/SCSI-Programming-HOWTO-22.html#sec-sensecodes
614 if (io_hdr
.driver_status
!= 0 &&
615 !(io_hdr
.driver_status
== DRIVER_SENSE
&& io_hdr
.sbp
&&
616 io_hdr
.sbp
[12] == 0x3a))
619 io_hdr
.cmdp
= startStop2Blk
;
620 status
= ioctl(ctl
->fd
, SG_IO
, (void *)&io_hdr
);
621 if (status
< 0 || io_hdr
.host_status
|| io_hdr
.driver_status
)
624 /* force kernel to reread partition table when new disc inserted */
625 ioctl(ctl
->fd
, BLKRRPART
);
630 * Eject using FDEJECT ioctl. Return 1 if successful, 0 otherwise.
632 static int eject_floppy(int fd
)
634 return ioctl(fd
, FDEJECT
) >= 0;
639 * Rewind and eject using tape ioctl. Return 1 if successful, 0 otherwise.
641 static int eject_tape(int fd
)
643 struct mtop op
= { .mt_op
= MTOFFL
, .mt_count
= 0 };
645 return ioctl(fd
, MTIOCTOP
, &op
) >= 0;
649 /* umount a device. */
650 static void umount_one(const struct eject_control
*ctl
, const char *name
)
657 verbose(ctl
, _("%s: unmounting"), name
);
661 if (drop_permissions() != 0)
662 err(EXIT_FAILURE
, _("drop permissions failed"));
664 execl("/bin/umount", "/bin/umount", name
, "-n", (char *)NULL
);
666 execl("/bin/umount", "/bin/umount", name
, (char *)NULL
);
668 errexec("/bin/umount");
671 warn( _("unable to fork"));
674 default: /* parent */
676 if (WIFEXITED(status
) == 0)
678 _("unmount of `%s' did not exit normally"), name
);
680 if (WEXITSTATUS(status
) != 0)
681 errx(EXIT_FAILURE
, _("unmount of `%s' failed\n"), name
);
686 /* Open a device file. */
687 static void open_device(struct eject_control
*ctl
)
689 int extra
= ctl
->F_option
== 0 && /* never use O_EXCL on --force */
690 ctl
->force_exclusive
? O_EXCL
: 0;
692 ctl
->fd
= open(ctl
->device
, O_RDWR
| O_NONBLOCK
| extra
);
694 ctl
->fd
= open(ctl
->device
, O_RDONLY
| O_NONBLOCK
| extra
);
696 err(EXIT_FAILURE
, _("cannot open %s"), ctl
->device
);
700 * See if device has been mounted by looking in mount table. If so, set
701 * device name and mount point name, and return 1, otherwise return 0.
703 static int device_get_mountpoint(struct eject_control
*ctl
, char **devname
, char **mnt
)
705 struct libmnt_fs
*fs
;
711 struct libmnt_cache
*cache
;
713 ctl
->mtab
= mnt_new_table();
715 err(EXIT_FAILURE
, _("failed to initialize libmount table"));
717 cache
= mnt_new_cache();
718 mnt_table_set_cache(ctl
->mtab
, cache
);
719 mnt_unref_cache(cache
);
722 rc
= mnt_table_parse_file(ctl
->mtab
, _PATH_PROC_MOUNTINFO
);
724 rc
= mnt_table_parse_mtab(ctl
->mtab
, NULL
);
726 err(EXIT_FAILURE
, _("failed to parse mount table"));
729 fs
= mnt_table_find_source(ctl
->mtab
, *devname
, MNT_ITER_BACKWARD
);
731 /* maybe 'devname' is mountpoint rather than a real device */
732 fs
= mnt_table_find_target(ctl
->mtab
, *devname
, MNT_ITER_BACKWARD
);
735 *devname
= xstrdup(mnt_fs_get_source(fs
));
740 *mnt
= xstrdup(mnt_fs_get_target(fs
));
741 return *mnt
? 0 : -1;
744 static char *get_disk_devname(const char *device
)
750 if (stat(device
, &st
) != 0)
753 /* get whole-disk devno */
754 if (sysfs_devno_to_wholedisk(st
.st_rdev
, diskname
,
755 sizeof(diskname
), &diskno
) != 0)
758 return st
.st_rdev
== diskno
? NULL
: find_device(diskname
);
761 /* umount all partitions if -M not specified, otherwise returns
762 * number of the mounted partitions only.
764 static int umount_partitions(struct eject_control
*ctl
)
766 struct path_cxt
*pc
= NULL
;
772 devno
= sysfs_devname_to_devno(ctl
->device
);
774 pc
= ul_new_sysfs_path(devno
, NULL
, NULL
);
778 /* open /sys/block/<wholedisk> */
779 if (!(dir
= ul_path_opendir(pc
, NULL
)))
782 /* scan for partition subdirs */
783 while ((d
= readdir(dir
))) {
784 if (!strcmp(d
->d_name
, ".") || !strcmp(d
->d_name
, ".."))
787 if (sysfs_blkdev_is_partition_dirent(dir
, d
, ctl
->device
)) {
789 char *dev
= find_device(d
->d_name
);
791 if (dev
&& device_get_mountpoint(ctl
, &dev
, &mnt
) == 0) {
792 verbose(ctl
, _("%s: mounted on %s"), dev
, mnt
);
794 umount_one(ctl
, mnt
);
810 static int is_ejectable(const struct eject_control
*ctl
)
812 struct path_cxt
*pc
= NULL
;
816 devno
= sysfs_devname_to_devno(ctl
->device
);
818 pc
= ul_new_sysfs_path(devno
, NULL
, NULL
);
822 rc
= sysfs_blkdev_is_hotpluggable(pc
) || sysfs_blkdev_is_removable(pc
);
828 /* handle -x option */
829 static void set_device_speed(struct eject_control
*ctl
)
835 verbose(ctl
, _("setting CD-ROM speed to auto"));
837 verbose(ctl
, _("setting CD-ROM speed to %ldX"), ctl
->x_arg
);
846 int main(int argc
, char **argv
)
849 char *mountpoint
= NULL
;
850 int worked
= 0; /* set to 1 when successfully ejected */
851 struct eject_control ctl
= { .fd
= -1 };
853 setlocale(LC_ALL
,"");
854 bindtextdomain(PACKAGE
, LOCALEDIR
);
856 close_stdout_atexit();
858 /* parse the command line arguments */
859 parse_args(&ctl
, argc
, argv
);
861 /* handle -d option */
863 info(_("default device: `%s'"), EJECT_DEFAULT_DEVICE
);
868 ctl
.device
= mnt_resolve_path(EJECT_DEFAULT_DEVICE
, NULL
);
869 verbose(&ctl
, _("using default device `%s'"), ctl
.device
);
873 if (ctl
.device
[strlen(ctl
.device
) - 1] == '/')
874 ctl
.device
[strlen(ctl
.device
) - 1] = '\0';
876 /* figure out full device or mount point name */
877 p
= find_device(ctl
.device
);
883 ctl
.device
= mnt_resolve_spec(p
, NULL
);
888 errx(EXIT_FAILURE
, _("unable to find device"));
890 verbose(&ctl
, _("device name is `%s'"), ctl
.device
);
892 device_get_mountpoint(&ctl
, &ctl
.device
, &mountpoint
);
894 verbose(&ctl
, _("%s: mounted on %s"), ctl
.device
, mountpoint
);
896 verbose(&ctl
, _("%s: not mounted"), ctl
.device
);
898 disk
= get_disk_devname(ctl
.device
);
900 verbose(&ctl
, _("%s: disc device: %s (disk device will be used for eject)"), ctl
.device
, disk
);
907 if (stat(ctl
.device
, &st
) != 0 || !S_ISBLK(st
.st_mode
))
908 errx(EXIT_FAILURE
, _("%s: not found mountpoint or device "
909 "with the given name"), ctl
.device
);
911 verbose(&ctl
, _("%s: is whole-disk device"), ctl
.device
);
914 if (ctl
.F_option
== 0 && is_ejectable(&ctl
) == 0)
915 errx(EXIT_FAILURE
, _("%s: is not ejectable device"), ctl
.device
);
917 /* handle -n option */
919 info(_("device is `%s'"), ctl
.device
);
920 verbose(&ctl
, _("exiting due to -n/--noop option"));
924 /* handle -i option */
931 /* handle -a option */
934 verbose(&ctl
, _("%s: enabling auto-eject mode"), ctl
.device
);
936 verbose(&ctl
, _("%s: disabling auto-eject mode"), ctl
.device
);
942 /* handle -t option */
944 verbose(&ctl
, _("%s: closing tray"), ctl
.device
);
947 set_device_speed(&ctl
);
951 /* handle -T option */
953 verbose(&ctl
, _("%s: toggling tray"), ctl
.device
);
956 set_device_speed(&ctl
);
960 /* handle -X option */
962 verbose(&ctl
, _("%s: listing CD-ROM speed"), ctl
.device
);
968 /* handle -x option only */
970 set_device_speed(&ctl
);
974 * Unmount all partitions if -m is not specified; or umount given
975 * mountpoint if -M is specified, otherwise print error of another
976 * partition is mounted.
979 int ct
= umount_partitions(&ctl
); /* umount all, or count mounted on -M */
981 if (ct
== 0 && mountpoint
)
982 umount_one(&ctl
, mountpoint
); /* probably whole-device */
985 if (ct
== 1 && mountpoint
)
986 umount_one(&ctl
, mountpoint
);
988 errx(EXIT_FAILURE
, _("error: %s: device in use"), ctl
.device
);
990 /* Now, we assume the device is no more used, use O_EXCL to be
991 * resistant against our bugs and possible races (someone else
992 * remounted the device).
994 ctl
.force_exclusive
= 1;
997 /* handle -c option */
999 verbose(&ctl
, _("%s: selecting CD-ROM disc #%ld"), ctl
.device
, ctl
.c_arg
);
1001 changer_select(&ctl
);
1002 set_device_speed(&ctl
);
1006 /* if user did not specify type of eject, try all four methods */
1007 if (ctl
.r_option
+ ctl
.s_option
+ ctl
.f_option
+ ctl
.q_option
== 0)
1008 ctl
.r_option
= ctl
.s_option
= ctl
.f_option
= ctl
.q_option
= 1;
1013 /* try various methods of ejecting until it works */
1015 verbose(&ctl
, _("%s: trying to eject using CD-ROM eject command"), ctl
.device
);
1016 worked
= eject_cdrom(ctl
.fd
);
1017 verbose(&ctl
, worked
? _("CD-ROM eject command succeeded") :
1018 _("CD-ROM eject command failed"));
1021 if (ctl
.s_option
&& !worked
) {
1022 verbose(&ctl
, _("%s: trying to eject using SCSI commands"), ctl
.device
);
1023 worked
= eject_scsi(&ctl
);
1024 verbose(&ctl
, worked
? _("SCSI eject succeeded") :
1025 _("SCSI eject failed"));
1028 if (ctl
.f_option
&& !worked
) {
1029 verbose(&ctl
, _("%s: trying to eject using floppy eject command"), ctl
.device
);
1030 worked
= eject_floppy(ctl
.fd
);
1031 verbose(&ctl
, worked
? _("floppy eject command succeeded") :
1032 _("floppy eject command failed"));
1035 if (ctl
.q_option
&& !worked
) {
1036 verbose(&ctl
, _("%s: trying to eject using tape offline command"), ctl
.device
);
1037 worked
= eject_tape(ctl
.fd
);
1038 verbose(&ctl
, worked
? _("tape offline command succeeded") :
1039 _("tape offline command failed"));
1043 errx(EXIT_FAILURE
, _("unable to eject"));
1053 mnt_unref_table(ctl
.mtab
);
1055 return EXIT_SUCCESS
;