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/scsi.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 */
99 long int c_arg
; /* changer slot number */
100 long int x_arg
; /* cd speed */
103 static void vinfo(const char *fmt
, va_list va
)
105 fprintf(stdout
, "%s: ", program_invocation_short_name
);
110 static inline void verbose(const struct eject_control
*ctl
, const char *fmt
, ...)
122 static inline void info(const char *fmt
, ...)
130 static void __attribute__((__noreturn__
)) usage(void)
133 fputs(USAGE_HEADER
, out
);
135 _(" %s [options] [<device>|<mountpoint>]\n"), program_invocation_short_name
);
137 fputs(USAGE_SEPARATOR
, out
);
138 fputs(_("Eject removable media.\n"), out
);
140 fputs(USAGE_OPTIONS
, out
);
141 fputs(_(" -a, --auto <on|off> turn auto-eject feature on or off\n"
142 " -c, --changerslot <slot> switch discs on a CD-ROM changer\n"
143 " -d, --default display default device\n"
144 " -f, --floppy eject floppy\n"
145 " -F, --force don't care about device type\n"
146 " -i, --manualeject <on|off> toggle manual eject protection on/off\n"
147 " -m, --no-unmount do not unmount device even if it is mounted\n"
148 " -M, --no-partitions-unmount do not unmount another partitions\n"
149 " -n, --noop don't eject, just show device found\n"
150 " -p, --proc use /proc/mounts instead of /etc/mtab\n"
151 " -q, --tape eject tape\n"
152 " -r, --cdrom eject CD-ROM\n"
153 " -s, --scsi eject SCSI device\n"
154 " -t, --trayclose close tray\n"
155 " -T, --traytoggle toggle tray\n"
156 " -v, --verbose enable verbose output\n"
157 " -x, --cdspeed <speed> set CD-ROM max speed\n"
158 " -X, --listspeed list CD-ROM available speeds\n"),
161 fputs(USAGE_SEPARATOR
, out
);
162 printf(USAGE_HELP_OPTIONS(29));
164 fputs(_("\nBy default tries -r, -s, -f, and -q in order until success.\n"), out
);
165 printf(USAGE_MAN_TAIL("eject(1)"));
171 /* Handle command line options. */
172 static void parse_args(struct eject_control
*ctl
, int argc
, char **argv
)
174 static const struct option long_opts
[] =
176 {"auto", required_argument
, NULL
, 'a'},
177 {"cdrom", no_argument
, NULL
, 'r'},
178 {"cdspeed", required_argument
, NULL
, 'x'},
179 {"changerslot", required_argument
, NULL
, 'c'},
180 {"default", no_argument
, NULL
, 'd'},
181 {"floppy", no_argument
, NULL
, 'f'},
182 {"force", no_argument
, NULL
, 'F'},
183 {"help", no_argument
, NULL
, 'h'},
184 {"listspeed", no_argument
, NULL
, 'X'},
185 {"manualeject", required_argument
, NULL
, 'i'},
186 {"noop", no_argument
, NULL
, 'n'},
187 {"no-unmount", no_argument
, NULL
, 'm'},
188 {"no-partitions-unmount", no_argument
, NULL
, 'M' },
189 {"proc", no_argument
, NULL
, 'p'},
190 {"scsi", no_argument
, NULL
, 's'},
191 {"tape", no_argument
, NULL
, 'q'},
192 {"trayclose", no_argument
, NULL
, 't'},
193 {"traytoggle", no_argument
, NULL
, 'T'},
194 {"verbose", no_argument
, NULL
, 'v'},
195 {"version", no_argument
, NULL
, 'V'},
200 while ((c
= getopt_long(argc
, argv
,
201 "a:c:i:x:dfFhnqrstTXvVpmM", long_opts
, NULL
)) != -1) {
205 ctl
->a_arg
= parse_switch(optarg
, _("argument error"),
206 "on", "off", "1", "0", NULL
);
210 ctl
->c_arg
= strtoul_or_err(optarg
, _("invalid argument to --changerslot/-c option"));
214 ctl
->x_arg
= strtoul_or_err(optarg
, _("invalid argument to --cdspeed/-x option"));
230 ctl
->i_arg
= parse_switch(optarg
, _("argument error"),
231 "on", "off", "1", "0", NULL
);
267 printf(UTIL_LINUX_VERSION
);
271 errtryhelp(EXIT_FAILURE
);
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 ctl
->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(const struct eject_control
*ctl
)
316 #if defined(CDROM_SET_OPTIONS) && defined(CDROM_CLEAR_OPTIONS)
318 status
= ioctl(ctl
->fd
, CDROM_SET_OPTIONS
, CDO_AUTO_EJECT
);
320 status
= ioctl(ctl
->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 button press.
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 static void manual_eject(const struct eject_control
*ctl
)
336 if (ioctl(ctl
->fd
, CDROM_LOCKDOOR
, ctl
->i_arg
) < 0) {
338 case EDRIVE_CANT_DO_THIS
:
339 errx(EXIT_FAILURE
, _("CD-ROM door lock is not supported"));
341 errx(EXIT_FAILURE
, _("other users have the drive open and not CAP_SYS_ADMIN"));
343 err(EXIT_FAILURE
, _("CD-ROM lock door command failed"));
348 info(_("CD-Drive may NOT be ejected with device button"));
350 info(_("CD-Drive may be ejected with device button"));
354 * Changer select. CDROM_SELECT_DISC is preferred, older kernels used
357 static void changer_select(const struct eject_control
*ctl
)
359 #ifdef CDROM_SELECT_DISC
360 if (ioctl(ctl
->fd
, CDROM_SELECT_DISC
, ctl
->c_arg
) < 0)
361 err(EXIT_FAILURE
, _("CD-ROM select disc command failed"));
363 #elif defined CDROMLOADFROMSLOT
364 if (ioctl(ctl
->fd
, CDROMLOADFROMSLOT
, ctl
->c_arg
) != 0)
365 err(EXIT_FAILURE
, _("CD-ROM load from slot command failed"));
367 warnx(_("IDE/ATAPI CD-ROM changer not supported by this kernel\n") );
372 * Close tray. Not supported by older kernels.
374 static void close_tray(int fd
)
378 #if defined(CDROMCLOSETRAY) || defined(CDIOCCLOSE)
379 #if defined(CDROMCLOSETRAY)
380 status
= ioctl(fd
, CDROMCLOSETRAY
);
381 #elif defined(CDIOCCLOSE)
382 status
= ioctl(fd
, CDIOCCLOSE
);
385 err(EXIT_FAILURE
, _("CD-ROM tray close command failed"));
387 warnx(_("CD-ROM tray close command not supported by this kernel\n"));
392 * Eject using CDROMEJECT ioctl.
394 static int eject_cdrom(int fd
)
396 #if defined(CDROMEJECT)
397 int ret
= ioctl(fd
, CDROM_LOCKDOOR
, 0);
400 return ioctl(fd
, CDROMEJECT
) >= 0;
401 #elif defined(CDIOCEJECT)
402 return ioctl(fd
, CDIOCEJECT
) >= 0;
404 warnx(_("CD-ROM eject unsupported"));
413 * Written by Benjamin Schwenk <benjaminschwenk@yahoo.de> and
414 * Sybren Stuvel <sybren@thirdtower.com>
416 * Not supported by older kernels because it might use
420 static void toggle_tray(int fd
)
422 #ifdef CDROM_DRIVE_STATUS
423 /* First ask the CDROM for info, otherwise fall back to manual. */
424 switch (ioctl(fd
, CDROM_DRIVE_STATUS
)) {
431 if (!eject_cdrom(fd
))
432 err(EXIT_FAILURE
, _("CD-ROM eject command failed"));
435 warnx(_("no CD-ROM information available"));
437 case CDS_DRIVE_NOT_READY
:
438 warnx(_("CD-ROM drive is not ready"));
444 struct timeval time_start
, time_stop
;
447 /* Try to open the CDROM tray and measure the time therefor
448 * needed. In my experience the function needs less than 0.05
449 * seconds if the tray was already open, and at least 1.5 seconds
450 * if it was closed. */
451 gettime_monotonic(&time_start
);
453 /* Send the CDROMEJECT command to the device. */
454 if (!eject_cdrom(fd
))
455 err(EXIT_FAILURE
, _("CD-ROM eject command failed"));
457 /* Get the second timestamp, to measure the time needed to open
459 gettime_monotonic(&time_stop
);
461 time_elapsed
= (time_stop
.tv_sec
* 1000000 + time_stop
.tv_usec
) -
462 (time_start
.tv_sec
* 1000000 + time_start
.tv_usec
);
464 /* If the tray "opened" too fast, we can be nearly sure, that it
465 * was already open. In this case, close it now. Else the tray was
466 * closed before. This would mean that we are done. */
467 if (time_elapsed
< TRAY_WAS_ALREADY_OPEN_USECS
)
473 * Select Speed of CD-ROM drive.
474 * Thanks to Roland Krivanek (krivanek@fmph.uniba.sk)
475 * http://dmpc.dbp.fmph.uniba.sk/~krivanek/cdrom_speed/
477 static void select_speed(const struct eject_control
*ctl
)
479 #ifdef CDROM_SELECT_SPEED
480 if (ioctl(ctl
->fd
, CDROM_SELECT_SPEED
, ctl
->x_arg
) != 0)
481 err(EXIT_FAILURE
, _("CD-ROM select speed command failed"));
483 warnx(_("CD-ROM select speed command not supported by this kernel"));
488 * Read Speed of CD-ROM drive. From Linux 2.6.13, the current speed
489 * is correctly reported
491 static int read_speed(const char *devname
)
493 int drive_number
= -1;
497 f
= fopen(_PATH_PROC_CDROMINFO
, "r");
499 err(EXIT_FAILURE
, _("cannot open %s"), _PATH_PROC_CDROMINFO
);
501 name
= strrchr(devname
, '/') + 1;
503 while (name
&& !feof(f
)) {
507 if (!fgets(line
, sizeof(line
), f
))
510 /* find drive number in line "drive name" */
511 if (drive_number
== -1) {
512 if (strncmp(line
, "drive name:", 11) == 0) {
513 str
= strtok(&line
[11], "\t ");
515 while (str
&& strncmp(name
, str
, strlen(name
)) != 0) {
517 str
= strtok(NULL
, "\t ");
520 _("%s: failed to finding CD-ROM name"),
521 _PATH_PROC_CDROMINFO
);
524 /* find line "drive speed" and read the correct speed */
526 if (strncmp(line
, "drive speed:", 12) == 0) {
529 str
= strtok(&line
[12], "\t ");
530 for (i
= 1; i
< drive_number
; i
++)
531 str
= strtok(NULL
, "\t ");
535 _("%s: failed to read speed"),
536 _PATH_PROC_CDROMINFO
);
543 errx(EXIT_FAILURE
, _("failed to read speed"));
547 * List Speed of CD-ROM drive.
549 static void list_speeds(struct eject_control
*ctl
)
551 int max_speed
, curr_speed
= 0;
554 max_speed
= read_speed(ctl
->device
);
556 while (curr_speed
< max_speed
) {
557 ctl
->x_arg
= curr_speed
+ 1;
559 curr_speed
= read_speed(ctl
->device
);
560 if (ctl
->x_arg
< curr_speed
)
561 printf("%d ", curr_speed
);
563 curr_speed
= ctl
->x_arg
+ 1;
570 * Eject using SCSI SG_IO commands. Return 1 if successful, 0 otherwise.
572 static int eject_scsi(const struct eject_control
*ctl
)
576 unsigned char allowRmBlk
[6] = {ALLOW_MEDIUM_REMOVAL
, 0, 0, 0, 0, 0};
577 unsigned char startStop1Blk
[6] = {START_STOP
, 0, 0, 0, 1, 0};
578 unsigned char startStop2Blk
[6] = {START_STOP
, 0, 0, 0, 2, 0};
579 unsigned char inqBuff
[2];
580 unsigned char sense_buffer
[32];
582 if ((ioctl(ctl
->fd
, SG_GET_VERSION_NUM
, &k
) < 0) || (k
< 30000)) {
583 verbose(ctl
, _("not an sg device, or old sg driver"));
587 memset(&io_hdr
, 0, sizeof(sg_io_hdr_t
));
588 io_hdr
.interface_id
= 'S';
590 io_hdr
.mx_sb_len
= sizeof(sense_buffer
);
591 io_hdr
.dxfer_direction
= SG_DXFER_NONE
;
592 io_hdr
.dxfer_len
= 0;
593 io_hdr
.dxferp
= inqBuff
;
594 io_hdr
.sbp
= sense_buffer
;
595 io_hdr
.timeout
= 10000;
597 io_hdr
.cmdp
= allowRmBlk
;
598 status
= ioctl(ctl
->fd
, SG_IO
, (void *)&io_hdr
);
599 if (status
< 0 || io_hdr
.host_status
|| io_hdr
.driver_status
)
602 io_hdr
.cmdp
= startStop1Blk
;
603 status
= ioctl(ctl
->fd
, SG_IO
, (void *)&io_hdr
);
604 if (status
< 0 || io_hdr
.host_status
)
607 /* Ignore errors when there is not medium -- in this case driver sense
608 * buffer sets MEDIUM NOT PRESENT (3a) bit. For more details see:
609 * http://www.tldp.org/HOWTO/archived/SCSI-Programming-HOWTO/SCSI-Programming-HOWTO-22.html#sec-sensecodes
612 if (io_hdr
.driver_status
!= 0 &&
613 !(io_hdr
.driver_status
== DRIVER_SENSE
&& io_hdr
.sbp
&&
614 io_hdr
.sbp
[12] == 0x3a))
617 io_hdr
.cmdp
= startStop2Blk
;
618 status
= ioctl(ctl
->fd
, SG_IO
, (void *)&io_hdr
);
619 if (status
< 0 || io_hdr
.host_status
|| io_hdr
.driver_status
)
622 /* force kernel to reread partition table when new disc inserted */
623 ioctl(ctl
->fd
, BLKRRPART
);
628 * Eject using FDEJECT ioctl. Return 1 if successful, 0 otherwise.
630 static int eject_floppy(int fd
)
632 return ioctl(fd
, FDEJECT
) >= 0;
637 * Rewind and eject using tape ioctl. Return 1 if successful, 0 otherwise.
639 static int eject_tape(int fd
)
641 struct mtop op
= { .mt_op
= MTOFFL
, .mt_count
= 0 };
643 return ioctl(fd
, MTIOCTOP
, &op
) >= 0;
647 /* umount a device. */
648 static void umount_one(const struct eject_control
*ctl
, const char *name
)
655 verbose(ctl
, _("%s: unmounting"), name
);
659 if (setgid(getgid()) < 0)
660 err(EXIT_FAILURE
, _("cannot set group id"));
662 if (setuid(getuid()) < 0)
663 err(EXIT_FAILURE
, _("cannot set user id"));
666 execl("/bin/umount", "/bin/umount", name
, "-n", NULL
);
668 execl("/bin/umount", "/bin/umount", name
, NULL
);
670 errexec("/bin/umount");
673 warn( _("unable to fork"));
676 default: /* parent */
678 if (WIFEXITED(status
) == 0)
680 _("unmount of `%s' did not exit normally"), name
);
682 if (WEXITSTATUS(status
) != 0)
683 errx(EXIT_FAILURE
, _("unmount of `%s' failed\n"), name
);
688 /* Open a device file. */
689 static void open_device(struct eject_control
*ctl
)
691 ctl
->fd
= open(ctl
->device
, O_RDWR
| O_NONBLOCK
);
693 ctl
->fd
= open(ctl
->device
, O_RDONLY
| O_NONBLOCK
);
695 err(EXIT_FAILURE
, _("cannot open %s"), ctl
->device
);
699 * See if device has been mounted by looking in mount table. If so, set
700 * device name and mount point name, and return 1, otherwise return 0.
702 static int device_get_mountpoint(struct eject_control
*ctl
, char **devname
, char **mnt
)
704 struct libmnt_fs
*fs
;
710 struct libmnt_cache
*cache
;
712 ctl
->mtab
= mnt_new_table();
714 err(EXIT_FAILURE
, _("failed to initialize libmount table"));
716 cache
= mnt_new_cache();
717 mnt_table_set_cache(ctl
->mtab
, cache
);
718 mnt_unref_cache(cache
);
721 rc
= mnt_table_parse_file(ctl
->mtab
, _PATH_PROC_MOUNTINFO
);
723 rc
= mnt_table_parse_mtab(ctl
->mtab
, NULL
);
725 err(EXIT_FAILURE
, _("failed to parse mount table"));
728 fs
= mnt_table_find_source(ctl
->mtab
, *devname
, MNT_ITER_BACKWARD
);
730 /* maybe 'devname' is mountpoint rather than a real device */
731 fs
= mnt_table_find_target(ctl
->mtab
, *devname
, MNT_ITER_BACKWARD
);
734 *devname
= xstrdup(mnt_fs_get_source(fs
));
739 *mnt
= xstrdup(mnt_fs_get_target(fs
));
740 return *mnt
? 0 : -1;
743 static char *get_disk_devname(const char *device
)
749 if (stat(device
, &st
) != 0)
752 /* get whole-disk devno */
753 if (sysfs_devno_to_wholedisk(st
.st_rdev
, diskname
,
754 sizeof(diskname
), &diskno
) != 0)
757 return st
.st_rdev
== diskno
? NULL
: find_device(diskname
);
760 static int umount_partitions(struct eject_control
*ctl
)
762 struct path_cxt
*pc
= NULL
;
768 devno
= sysfs_devname_to_devno(ctl
->device
);
770 pc
= ul_new_sysfs_path(devno
, NULL
, NULL
);
774 /* open /sys/block/<wholedisk> */
775 if (!(dir
= ul_path_opendir(pc
, NULL
)))
778 /* scan for partition subdirs */
779 while ((d
= readdir(dir
))) {
780 if (!strcmp(d
->d_name
, ".") || !strcmp(d
->d_name
, ".."))
783 if (sysfs_blkdev_is_partition_dirent(dir
, d
, ctl
->device
)) {
785 char *dev
= find_device(d
->d_name
);
787 if (dev
&& device_get_mountpoint(ctl
, &dev
, &mnt
) == 0) {
788 verbose(ctl
, _("%s: mounted on %s"), dev
, mnt
);
790 umount_one(ctl
, mnt
);
806 static int is_hotpluggable(const struct eject_control
*ctl
)
808 struct path_cxt
*pc
= NULL
;
812 devno
= sysfs_devname_to_devno(ctl
->device
);
814 pc
= ul_new_sysfs_path(devno
, NULL
, NULL
);
818 rc
= sysfs_blkdev_is_hotpluggable(pc
);
824 /* handle -x option */
825 static void set_device_speed(struct eject_control
*ctl
)
831 verbose(ctl
, _("setting CD-ROM speed to auto"));
833 verbose(ctl
, _("setting CD-ROM speed to %ldX"), ctl
->x_arg
);
842 int main(int argc
, char **argv
)
845 char *mountpoint
= NULL
;
846 int worked
= 0; /* set to 1 when successfully ejected */
847 struct eject_control ctl
= { NULL
};
849 setlocale(LC_ALL
,"");
850 bindtextdomain(PACKAGE
, LOCALEDIR
);
852 atexit(close_stdout
);
854 /* parse the command line arguments */
855 parse_args(&ctl
, argc
, argv
);
857 /* handle -d option */
859 info(_("default device: `%s'"), EJECT_DEFAULT_DEVICE
);
864 ctl
.device
= mnt_resolve_path(EJECT_DEFAULT_DEVICE
, NULL
);
865 verbose(&ctl
, _("using default device `%s'"), ctl
.device
);
869 if (ctl
.device
[strlen(ctl
.device
) - 1] == '/')
870 ctl
.device
[strlen(ctl
.device
) - 1] = '\0';
872 /* figure out full device or mount point name */
873 p
= find_device(ctl
.device
);
879 ctl
.device
= mnt_resolve_spec(p
, NULL
);
884 errx(EXIT_FAILURE
, _("%s: unable to find device"), ctl
.device
);
886 verbose(&ctl
, _("device name is `%s'"), ctl
.device
);
888 device_get_mountpoint(&ctl
, &ctl
.device
, &mountpoint
);
890 verbose(&ctl
, _("%s: mounted on %s"), ctl
.device
, mountpoint
);
892 verbose(&ctl
, _("%s: not mounted"), ctl
.device
);
894 disk
= get_disk_devname(ctl
.device
);
896 verbose(&ctl
, _("%s: disc device: %s (disk device will be used for eject)"), ctl
.device
, disk
);
903 if (stat(ctl
.device
, &st
) != 0 || !S_ISBLK(st
.st_mode
))
904 errx(EXIT_FAILURE
, _("%s: not found mountpoint or device "
905 "with the given name"), ctl
.device
);
907 verbose(&ctl
, _("%s: is whole-disk device"), ctl
.device
);
910 if (ctl
.F_option
== 0 && is_hotpluggable(&ctl
) == 0)
911 errx(EXIT_FAILURE
, _("%s: is not hot-pluggable device"), ctl
.device
);
913 /* handle -n option */
915 info(_("device is `%s'"), ctl
.device
);
916 verbose(&ctl
, _("exiting due to -n/--noop option"));
920 /* handle -i option */
927 /* handle -a option */
930 verbose(&ctl
, _("%s: enabling auto-eject mode"), ctl
.device
);
932 verbose(&ctl
, _("%s: disabling auto-eject mode"), ctl
.device
);
938 /* handle -t option */
940 verbose(&ctl
, _("%s: closing tray"), ctl
.device
);
943 set_device_speed(&ctl
);
947 /* handle -T option */
949 verbose(&ctl
, _("%s: toggling tray"), ctl
.device
);
952 set_device_speed(&ctl
);
956 /* handle -X option */
958 verbose(&ctl
, _("%s: listing CD-ROM speed"), ctl
.device
);
964 /* handle -x option only */
966 set_device_speed(&ctl
);
970 * Unmount all partitions if -m is not specified; or umount given
971 * mountpoint if -M is specified, otherwise print error of another
972 * partition is mounted.
975 int ct
= umount_partitions(&ctl
);
977 if (ct
== 0 && mountpoint
)
978 umount_one(&ctl
, mountpoint
); /* probably whole-device */
981 if (ct
== 1 && mountpoint
)
982 umount_one(&ctl
, mountpoint
);
984 errx(EXIT_FAILURE
, _("error: %s: device in use"), ctl
.device
);
988 /* handle -c option */
990 verbose(&ctl
, _("%s: selecting CD-ROM disc #%ld"), ctl
.device
, ctl
.c_arg
);
992 changer_select(&ctl
);
993 set_device_speed(&ctl
);
997 /* if user did not specify type of eject, try all four methods */
998 if (ctl
.r_option
+ ctl
.s_option
+ ctl
.f_option
+ ctl
.q_option
== 0)
999 ctl
.r_option
= ctl
.s_option
= ctl
.f_option
= ctl
.q_option
= 1;
1004 /* try various methods of ejecting until it works */
1006 verbose(&ctl
, _("%s: trying to eject using CD-ROM eject command"), ctl
.device
);
1007 worked
= eject_cdrom(ctl
.fd
);
1008 verbose(&ctl
, worked
? _("CD-ROM eject command succeeded") :
1009 _("CD-ROM eject command failed"));
1012 if (ctl
.s_option
&& !worked
) {
1013 verbose(&ctl
, _("%s: trying to eject using SCSI commands"), ctl
.device
);
1014 worked
= eject_scsi(&ctl
);
1015 verbose(&ctl
, worked
? _("SCSI eject succeeded") :
1016 _("SCSI eject failed"));
1019 if (ctl
.f_option
&& !worked
) {
1020 verbose(&ctl
, _("%s: trying to eject using floppy eject command"), ctl
.device
);
1021 worked
= eject_floppy(ctl
.fd
);
1022 verbose(&ctl
, worked
? _("floppy eject command succeeded") :
1023 _("floppy eject command failed"));
1026 if (ctl
.q_option
&& !worked
) {
1027 verbose(&ctl
, _("%s: trying to eject using tape offline command"), ctl
.device
);
1028 worked
= eject_tape(ctl
.fd
);
1029 verbose(&ctl
, worked
? _("tape offline command succeeded") :
1030 _("tape offline command failed"));
1034 errx(EXIT_FAILURE
, _("unable to eject"));
1041 mnt_unref_table(ctl
.mtab
);
1043 return EXIT_SUCCESS
;