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"
57 * sg_io_hdr_t driver_status -- see kernel include/scsi/scsi.h
60 # define DRIVER_SENSE 0x08
64 #define EJECT_DEFAULT_DEVICE "/dev/cdrom"
67 /* Used by the toggle_tray() function. If ejecting the tray takes this
68 * time or less, the tray was probably already ejected, so we close it
71 #define TRAY_WAS_ALREADY_OPEN_USECS 200000 /* about 0.2 seconds */
73 /* eject(1) is able to eject only 'removable' devices (attribute in /sys)
74 * _or_ devices connected by hotplug subsystem.
76 static const char * const hotplug_subsystems
[] = {
84 /* Global Variables */
85 static int a_option
; /* command flags and arguments */
105 static long int c_arg
;
106 static long int x_arg
;
108 struct libmnt_table
*mtab
;
110 static void vinfo(const char *fmt
, va_list va
)
112 fprintf(stdout
, "%s: ", program_invocation_short_name
);
117 static inline void verbose(const char *fmt
, ...)
129 static inline void info(const char *fmt
, ...)
137 static void __attribute__ ((__noreturn__
)) usage(FILE * out
)
139 fputs(USAGE_HEADER
, out
);
142 _(" %s [options] [<device>|<mountpoint>]\n"), program_invocation_short_name
);
144 fputs(USAGE_OPTIONS
, out
);
145 fputs(_(" -a, --auto <on|off> turn auto-eject feature on or off\n"
146 " -c, --changerslot <slot> switch discs on a CD-ROM changer\n"
147 " -d, --default display default device\n"
148 " -f, --floppy eject floppy\n"
149 " -F, --force don't care about device type\n"
150 " -i, --manualeject <on|off> toggle manual eject protection on/off\n"
151 " -m, --no-unmount do not unmount device even if it is mounted\n"
152 " -M, --no-partitions-unmount do not unmount another partitions\n"
153 " -n, --noop don't eject, just show device found\n"
154 " -p, --proc use /proc/mounts instead of /etc/mtab\n"
155 " -q, --tape eject tape\n"
156 " -r, --cdrom eject CD-ROM\n"
157 " -s, --scsi eject SCSI device\n"
158 " -t, --trayclose close tray\n"
159 " -T, --traytoggle toggle tray\n"
160 " -v, --verbose enable verbose output\n"
161 " -x, --cdspeed <speed> set CD-ROM max speed\n"
162 " -X, --listspeed list CD-ROM available speeds\n"),
165 fputs(USAGE_SEPARATOR
, out
);
166 fputs(USAGE_HELP
, out
);
167 fputs(USAGE_VERSION
, out
);
169 fputs(_("\nBy default tries -r, -s, -f, and -q in order until success.\n"), out
);
170 fprintf(out
, USAGE_MAN_TAIL("eject(1)"));
172 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
176 /* Handle command line options. */
177 static void parse_args(int argc
, char **argv
, char **device
)
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 if (!strcmp(optarg
, "0") || !strcmp(optarg
, "off"))
212 else if (!strcmp(optarg
, "1") || !strcmp(optarg
, "on"))
215 errx(EXIT_FAILURE
, _("invalid argument to --auto/-a option"));
219 c_arg
= strtoul_or_err(optarg
, _("invalid argument to --changerslot/-c option"));
223 x_arg
= strtoul_or_err(optarg
, _("invalid argument to --cdspeed/-x option"));
239 if (!strcmp(optarg
, "0") || !strcmp(optarg
, "off"))
241 else if (!strcmp(optarg
, "1") || !strcmp(optarg
, "on"))
244 errx(EXIT_FAILURE
, _("invalid argument to --manualeject/-i option"));
280 printf(UTIL_LINUX_VERSION
);
290 /* check for a single additional argument */
291 if ((argc
- optind
) > 1)
292 errx(EXIT_FAILURE
, _("too many arguments"));
294 if ((argc
- optind
) == 1)
295 *device
= xstrdup(argv
[optind
]);
299 * Given name, such as foo, see if any of the following exist:
301 * foo (if foo starts with '.' or '/')
304 * If found, return the full path. If not found, return 0.
305 * Returns pointer to dynamically allocated string.
307 static char *find_device(const char *name
)
312 if ((*name
== '.' || *name
== '/') && access(name
, F_OK
) == 0)
313 return xstrdup(name
);
317 snprintf(buf
, sizeof(buf
), "/dev/%s", name
);
318 if (access(buf
, F_OK
) == 0)
325 /* Set or clear auto-eject mode. */
326 static void auto_eject(int fd
, int on
)
330 #if defined(CDROM_SET_OPTIONS) && defined(CDROM_CLEAR_OPTIONS)
332 status
= ioctl(fd
, CDROM_SET_OPTIONS
, CDO_AUTO_EJECT
);
334 status
= ioctl(fd
, CDROM_CLEAR_OPTIONS
, CDO_AUTO_EJECT
);
339 err(EXIT_FAILURE
,_("CD-ROM auto-eject command failed"));
343 * Stops CDROM from opening on manual eject pressing the button.
344 * This can be useful when you carry your laptop
345 * in your bag while it's on and no CD inserted in it's drive.
346 * Implemented as found in Documentation/ioctl/cdrom.txt
348 * TODO: Maybe we should check this also:
349 * EDRIVE_CANT_DO_THIS Door lock function not supported.
350 * EBUSY Attempt to unlock when multiple users
351 * have the drive open and not CAP_SYS_ADMIN
353 static void manual_eject(int fd
, int on
)
355 if (ioctl(fd
, CDROM_LOCKDOOR
, on
) < 0)
356 err(EXIT_FAILURE
, _("CD-ROM lock door command failed"));
359 info(_("CD-Drive may NOT be ejected with device button"));
361 info(_("CD-Drive may be ejected with device button"));
365 * Changer select. CDROM_SELECT_DISC is preferred, older kernels used
368 static void changer_select(int fd
, int slot
)
370 #ifdef CDROM_SELECT_DISC
371 if (ioctl(fd
, CDROM_SELECT_DISC
, slot
) < 0)
372 err(EXIT_FAILURE
, _("CD-ROM select disc command failed"));
374 #elif defined CDROMLOADFROMSLOT
375 if (ioctl(fd
, CDROMLOADFROMSLOT
, slot
) != 0)
376 err(EXIT_FAILURE
, _("CD-ROM load from slot command failed"));
378 warnx(_("IDE/ATAPI CD-ROM changer not supported by this kernel\n") );
383 * Close tray. Not supported by older kernels.
385 static void close_tray(int fd
)
389 #if defined(CDROMCLOSETRAY) || defined(CDIOCCLOSE)
390 #if defined(CDROMCLOSETRAY)
391 status
= ioctl(fd
, CDROMCLOSETRAY
);
392 #elif defined(CDIOCCLOSE)
393 status
= ioctl(fd
, CDIOCCLOSE
);
396 err(EXIT_FAILURE
, _("CD-ROM tray close command failed"));
398 warnx(_("CD-ROM tray close command not supported by this kernel\n"));
403 * Eject using CDROMEJECT ioctl.
405 static int eject_cdrom(int fd
)
407 #if defined(CDROMEJECT)
408 int ret
= ioctl(fd
, CDROM_LOCKDOOR
, 0);
411 return ioctl(fd
, CDROMEJECT
) >= 0;
412 #elif defined(CDIOCEJECT)
413 return ioctl(fd
, CDIOCEJECT
) >= 0;
415 warnx(_("CD-ROM eject unsupported"));
424 * Written by Benjamin Schwenk <benjaminschwenk@yahoo.de> and
425 * Sybren Stuvel <sybren@thirdtower.com>
427 * Not supported by older kernels because it might use
431 static void toggle_tray(int fd
)
433 struct timeval time_start
, time_stop
;
436 #ifdef CDROM_DRIVE_STATUS
437 /* First ask the CDROM for info, otherwise fall back to manual. */
438 switch (ioctl(fd
, CDROM_DRIVE_STATUS
)) {
445 if (!eject_cdrom(fd
))
446 err(EXIT_FAILURE
, _("CD-ROM eject command failed"));
449 warnx(_("no CD-ROM information available"));
451 case CDS_DRIVE_NOT_READY
:
452 warnx(_("CD-ROM drive is not ready"));
459 /* Try to open the CDROM tray and measure the time therefor
460 * needed. In my experience the function needs less than 0.05
461 * seconds if the tray was already open, and at least 1.5 seconds
462 * if it was closed. */
463 gettimeofday(&time_start
, NULL
);
465 /* Send the CDROMEJECT command to the device. */
466 if (!eject_cdrom(fd
))
467 err(EXIT_FAILURE
, _("CD-ROM eject command failed"));
469 /* Get the second timestamp, to measure the time needed to open
471 gettimeofday(&time_stop
, NULL
);
473 time_elapsed
= (time_stop
.tv_sec
* 1000000 + time_stop
.tv_usec
) -
474 (time_start
.tv_sec
* 1000000 + time_start
.tv_usec
);
476 /* If the tray "opened" too fast, we can be nearly sure, that it
477 * was already open. In this case, close it now. Else the tray was
478 * closed before. This would mean that we are done. */
479 if (time_elapsed
< TRAY_WAS_ALREADY_OPEN_USECS
)
484 * Select Speed of CD-ROM drive.
485 * Thanks to Roland Krivanek (krivanek@fmph.uniba.sk)
486 * http://dmpc.dbp.fmph.uniba.sk/~krivanek/cdrom_speed/
488 static void select_speed(int fd
, int speed
)
490 #ifdef CDROM_SELECT_SPEED
491 if (ioctl(fd
, CDROM_SELECT_SPEED
, speed
) != 0)
492 err(EXIT_FAILURE
, _("CD-ROM select speed command failed"));
494 warnx(_("CD-ROM select speed command not supported by this kernel"));
499 * Read Speed of CD-ROM drive. From Linux 2.6.13, the current speed
500 * is correctly reported
502 static int read_speed(const char *devname
)
504 int drive_number
= -1;
508 f
= fopen(_PATH_PROC_CDROMINFO
, "r");
510 err(EXIT_FAILURE
, _("cannot open %s"), _PATH_PROC_CDROMINFO
);
512 name
= strrchr(devname
, '/') + 1;
514 while (name
&& !feof(f
)) {
518 if (!fgets(line
, sizeof(line
), f
))
521 /* find drive number in line "drive name" */
522 if (drive_number
== -1) {
523 if (strncmp(line
, "drive name:", 11) == 0) {
524 str
= strtok(&line
[11], "\t ");
526 while (str
&& strncmp(name
, str
, strlen(name
)) != 0) {
528 str
= strtok(NULL
, "\t ");
531 _("%s: failed to finding CD-ROM name"),
532 _PATH_PROC_CDROMINFO
);
535 /* find line "drive speed" and read the correct speed */
537 if (strncmp(line
, "drive speed:", 12) == 0) {
540 str
= strtok(&line
[12], "\t ");
541 for (i
= 1; i
< drive_number
; i
++)
542 str
= strtok(NULL
, "\t ");
546 _("%s: failed to read speed"),
547 _PATH_PROC_CDROMINFO
);
554 errx(EXIT_FAILURE
, _("failed to read speed"));
558 * List Speed of CD-ROM drive.
560 static void list_speeds(const char *name
, int fd
)
562 #ifdef CDROM_SELECT_SPEED
563 int max_speed
, curr_speed
= 0, prev_speed
;
566 max_speed
= read_speed(name
);
568 while (curr_speed
< max_speed
) {
569 prev_speed
= curr_speed
;
570 select_speed(fd
, prev_speed
+ 1);
571 curr_speed
= read_speed(name
);
572 if (curr_speed
> prev_speed
)
573 printf("%d ", curr_speed
);
575 curr_speed
= prev_speed
+ 1;
580 warnx(_("CD-ROM select speed command not supported by this kernel"));
585 * Eject using SCSI SG_IO commands. Return 1 if successful, 0 otherwise.
587 static int eject_scsi(int fd
)
591 unsigned char allowRmBlk
[6] = {ALLOW_MEDIUM_REMOVAL
, 0, 0, 0, 0, 0};
592 unsigned char startStop1Blk
[6] = {START_STOP
, 0, 0, 0, 1, 0};
593 unsigned char startStop2Blk
[6] = {START_STOP
, 0, 0, 0, 2, 0};
594 unsigned char inqBuff
[2];
595 unsigned char sense_buffer
[32];
597 if ((ioctl(fd
, SG_GET_VERSION_NUM
, &k
) < 0) || (k
< 30000)) {
598 verbose(_("not an sg device, or old sg driver"));
602 memset(&io_hdr
, 0, sizeof(sg_io_hdr_t
));
603 io_hdr
.interface_id
= 'S';
605 io_hdr
.mx_sb_len
= sizeof(sense_buffer
);
606 io_hdr
.dxfer_direction
= SG_DXFER_NONE
;
607 io_hdr
.dxfer_len
= 0;
608 io_hdr
.dxferp
= inqBuff
;
609 io_hdr
.sbp
= sense_buffer
;
610 io_hdr
.timeout
= 10000;
612 io_hdr
.cmdp
= allowRmBlk
;
613 status
= ioctl(fd
, SG_IO
, (void *)&io_hdr
);
614 if (status
< 0 || io_hdr
.host_status
|| io_hdr
.driver_status
)
617 io_hdr
.cmdp
= startStop1Blk
;
618 status
= ioctl(fd
, SG_IO
, (void *)&io_hdr
);
619 if (status
< 0 || io_hdr
.host_status
)
622 /* Ignore errors when there is not medium -- in this case driver sense
623 * buffer sets MEDIUM NOT PRESENT (3a) bit. For more details see:
624 * http://www.tldp.org/HOWTO/archived/SCSI-Programming-HOWTO/SCSI-Programming-HOWTO-22.html#sec-sensecodes
627 if (io_hdr
.driver_status
!= 0 &&
628 !(io_hdr
.driver_status
== DRIVER_SENSE
&& io_hdr
.sbp
&&
629 io_hdr
.sbp
[12] == 0x3a))
632 io_hdr
.cmdp
= startStop2Blk
;
633 status
= ioctl(fd
, SG_IO
, (void *)&io_hdr
);
634 if (status
< 0 || io_hdr
.host_status
|| io_hdr
.driver_status
)
637 /* force kernel to reread partition table when new disc inserted */
638 ioctl(fd
, BLKRRPART
);
643 * Eject using FDEJECT ioctl. Return 1 if successful, 0 otherwise.
645 static int eject_floppy(int fd
)
647 return ioctl(fd
, FDEJECT
) >= 0;
652 * Rewind and eject using tape ioctl. Return 1 if successful, 0 otherwise.
654 static int eject_tape(int fd
)
656 struct mtop op
= { .mt_op
= MTOFFL
, .mt_count
= 0 };
658 return ioctl(fd
, MTIOCTOP
, &op
) >= 0;
662 /* umount a device. */
663 static void umount_one(const char *name
)
670 verbose(_("%s: unmounting"), name
);
674 if (setgid(getgid()) < 0)
675 err(EXIT_FAILURE
, _("cannot set group id"));
677 if (setuid(getuid()) < 0)
678 err(EXIT_FAILURE
, _("cannot set user id"));
681 execl("/bin/umount", "/bin/umount", name
, "-n", NULL
);
683 execl("/bin/umount", "/bin/umount", name
, NULL
);
685 errx(EXIT_FAILURE
, _("unable to exec /bin/umount of `%s'"), name
);
688 warn( _("unable to fork"));
691 default: /* parent */
693 if (WIFEXITED(status
) == 0)
695 _("unmount of `%s' did not exit normally"), name
);
697 if (WEXITSTATUS(status
) != 0)
698 errx(EXIT_FAILURE
, _("unmount of `%s' failed\n"), name
);
703 /* Open a device file. */
704 static int open_device(const char *name
)
706 int fd
= open(name
, O_RDWR
|O_NONBLOCK
);
709 fd
= open(name
, O_RDONLY
|O_NONBLOCK
);
711 err(EXIT_FAILURE
, _("cannot open %s"), name
);
716 * See if device has been mounted by looking in mount table. If so, set
717 * device name and mount point name, and return 1, otherwise return 0.
719 static int device_get_mountpoint(char **devname
, char **mnt
)
721 struct libmnt_fs
*fs
;
727 struct libmnt_cache
*cache
;
729 mtab
= mnt_new_table();
731 err(EXIT_FAILURE
, _("failed to initialize libmount table"));
733 cache
= mnt_new_cache();
734 mnt_table_set_cache(mtab
, cache
);
735 mnt_unref_cache(cache
);
738 rc
= mnt_table_parse_file(mtab
, _PATH_PROC_MOUNTINFO
);
740 rc
= mnt_table_parse_mtab(mtab
, NULL
);
742 err(EXIT_FAILURE
, _("failed to parse mount table"));
745 fs
= mnt_table_find_source(mtab
, *devname
, MNT_ITER_BACKWARD
);
747 /* maybe 'devname' is mountpoint rather than a real device */
748 fs
= mnt_table_find_target(mtab
, *devname
, MNT_ITER_BACKWARD
);
751 *devname
= xstrdup(mnt_fs_get_source(fs
));
756 *mnt
= xstrdup(mnt_fs_get_target(fs
));
757 return *mnt
? 0 : -1;
760 static char *get_disk_devname(const char *device
)
766 if (stat(device
, &st
) != 0)
769 /* get whole-disk devno */
770 if (sysfs_devno_to_wholedisk(st
.st_rdev
, diskname
,
771 sizeof(diskname
), &diskno
) != 0)
774 return st
.st_rdev
== diskno
? NULL
: find_device(diskname
);
777 static int umount_partitions(const char *disk
, int checkonly
)
779 struct sysfs_cxt cxt
= UL_SYSFSCXT_EMPTY
;
785 devno
= sysfs_devname_to_devno(disk
, NULL
);
786 if (sysfs_init(&cxt
, devno
, NULL
) != 0)
789 /* open /sys/block/<wholedisk> */
790 if (!(dir
= sysfs_opendir(&cxt
, NULL
)))
793 /* scan for partition subdirs */
794 while ((d
= readdir(dir
))) {
795 if (!strcmp(d
->d_name
, ".") || !strcmp(d
->d_name
, ".."))
798 if (sysfs_is_partition_dirent(dir
, d
, disk
)) {
800 char *dev
= find_device(d
->d_name
);
802 if (dev
&& device_get_mountpoint(&dev
, &mnt
) == 0) {
803 verbose(_("%s: mounted on %s"), dev
, mnt
);
821 static int is_hotpluggable_subsystem(const char *name
)
825 for (i
= 0; i
< ARRAY_SIZE(hotplug_subsystems
); i
++)
826 if (strcmp(name
, hotplug_subsystems
[i
]) == 0)
832 #define SUBSYSTEM_LINKNAME "/subsystem"
837 * chain: /sys/dev/block/../../devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.2/ \
838 * 1-1.2:1.0/host65/target65:0:0/65:0:0:0/block/sdb
840 * The function check if <chain>/subsystem symlink exists, if yes then returns
841 * basename of the readlink result, and remove the last subdirectory from the
844 static char *get_subsystem(char *chain
, char *buf
, size_t bufsz
)
849 if (!chain
|| !*chain
)
853 if (len
+ sizeof(SUBSYSTEM_LINKNAME
) > PATH_MAX
)
859 /* append "/subsystem" to the path */
860 memcpy(chain
+ len
, SUBSYSTEM_LINKNAME
, sizeof(SUBSYSTEM_LINKNAME
));
862 /* try if subsystem symlink exists */
863 sz
= readlink(chain
, buf
, bufsz
- 1);
865 /* remove last subsystem from chain */
867 p
= strrchr(chain
, '/');
874 /* we found symlink to subsystem, return basename */
876 return basename(buf
);
884 static int is_hotpluggable(const char* device
)
886 struct sysfs_cxt cxt
= UL_SYSFSCXT_EMPTY
;
887 char devchain
[PATH_MAX
];
888 char subbuf
[PATH_MAX
];
894 devno
= sysfs_devname_to_devno(device
, NULL
);
895 if (sysfs_init(&cxt
, devno
, NULL
) != 0)
898 /* check /sys/dev/block/<maj>:<min>/removable attribute */
899 if (sysfs_read_int(&cxt
, "removable", &rc
) == 0 && rc
== 1) {
900 verbose(_("%s: is removable device"), device
);
904 /* read /sys/dev/block/<maj>:<min> symlink */
905 sz
= sysfs_readlink(&cxt
, NULL
, devchain
, sizeof(devchain
));
906 if (sz
<= 0 || sz
+ sizeof(_PATH_SYS_DEVBLOCK
"/") > sizeof(devchain
))
909 devchain
[sz
++] = '\0';
911 /* create absolute patch from the link */
912 memmove(devchain
+ sizeof(_PATH_SYS_DEVBLOCK
"/") - 1, devchain
, sz
);
913 memcpy(devchain
, _PATH_SYS_DEVBLOCK
"/",
914 sizeof(_PATH_SYS_DEVBLOCK
"/") - 1);
916 while ((sub
= get_subsystem(devchain
, subbuf
, sizeof(subbuf
)))) {
917 rc
= is_hotpluggable_subsystem(sub
);
919 verbose(_("%s: connected by hotplug subsystem: %s"),
931 /* handle -x option */
932 static void set_device_speed(char *name
)
940 verbose(_("setting CD-ROM speed to auto"));
942 verbose(_("setting CD-ROM speed to %ldX"), x_arg
);
944 fd
= open_device(name
);
945 select_speed(fd
, x_arg
);
951 int main(int argc
, char **argv
)
955 char *mountpoint
= NULL
;
956 int worked
= 0; /* set to 1 when successfully ejected */
957 int fd
; /* file descriptor for device */
959 setlocale(LC_ALL
,"");
960 bindtextdomain(PACKAGE
, LOCALEDIR
);
962 atexit(close_stdout
);
964 /* parse the command line arguments */
965 parse_args(argc
, argv
, &device
);
967 /* handle -d option */
969 info(_("default device: `%s'"), EJECT_DEFAULT_DEVICE
);
974 device
= mnt_resolve_path(EJECT_DEFAULT_DEVICE
, NULL
);
975 verbose(_("using default device `%s'"), device
);
979 if (device
[strlen(device
)-1] == '/')
980 device
[strlen(device
)-1] = '\0';
982 /* figure out full device or mount point name */
983 p
= find_device(device
);
989 device
= mnt_resolve_spec(p
, NULL
);
994 errx(EXIT_FAILURE
, _("%s: unable to find device"), device
);
996 verbose(_("device name is `%s'"), device
);
998 device_get_mountpoint(&device
, &mountpoint
);
1000 verbose(_("%s: mounted on %s"), device
, mountpoint
);
1002 verbose(_("%s: not mounted"), device
);
1004 disk
= get_disk_devname(device
);
1006 verbose(_("%s: disc device: %s (disk device will be used for eject)"), device
, disk
);
1013 if (stat(device
, &st
) != 0 || !S_ISBLK(st
.st_mode
))
1014 errx(EXIT_FAILURE
, _("%s: not found mountpoint or device "
1015 "with the given name"), device
);
1017 verbose(_("%s: is whole-disk device"), device
);
1020 if (F_option
== 0 && is_hotpluggable(device
) == 0)
1021 errx(EXIT_FAILURE
, _("%s: is not hot-pluggable device"), device
);
1023 /* handle -n option */
1025 info(_("device is `%s'"), device
);
1026 verbose(_("exiting due to -n/--noop option"));
1027 return EXIT_SUCCESS
;
1030 /* handle -i option */
1032 fd
= open_device(device
);
1033 manual_eject(fd
, i_arg
);
1034 return EXIT_SUCCESS
;
1037 /* handle -a option */
1040 verbose(_("%s: enabling auto-eject mode"), device
);
1042 verbose(_("%s: disabling auto-eject mode"), device
);
1043 fd
= open_device(device
);
1044 auto_eject(fd
, a_arg
);
1045 return EXIT_SUCCESS
;
1048 /* handle -t option */
1050 verbose(_("%s: closing tray"), device
);
1051 fd
= open_device(device
);
1053 set_device_speed(device
);
1054 return EXIT_SUCCESS
;
1057 /* handle -T option */
1059 verbose(_("%s: toggling tray"), device
);
1060 fd
= open_device(device
);
1062 set_device_speed(device
);
1063 return EXIT_SUCCESS
;
1066 /* handle -X option */
1068 verbose(_("%s: listing CD-ROM speed"), device
);
1069 fd
= open_device(device
);
1070 list_speeds(device
, fd
);
1071 return EXIT_SUCCESS
;
1074 /* handle -x option only */
1076 set_device_speed(device
);
1080 * Unmount all partitions if -m is not specified; or umount given
1081 * mountpoint if -M is specified, otherwise print error of another
1082 * partition is mounted.
1085 int ct
= umount_partitions(device
, M_option
);
1087 if (ct
== 0 && mountpoint
)
1088 umount_one(mountpoint
); /* probably whole-device */
1091 if (ct
== 1 && mountpoint
)
1092 umount_one(mountpoint
);
1094 errx(EXIT_FAILURE
, _("error: %s: device in use"), device
);
1098 /* handle -c option */
1100 verbose(_("%s: selecting CD-ROM disc #%ld"), device
, c_arg
);
1101 fd
= open_device(device
);
1102 changer_select(fd
, c_arg
);
1103 set_device_speed(device
);
1104 return EXIT_SUCCESS
;
1107 /* if user did not specify type of eject, try all four methods */
1108 if (r_option
+ s_option
+ f_option
+ q_option
== 0)
1109 r_option
= s_option
= f_option
= q_option
= 1;
1112 fd
= open_device(device
);
1114 /* try various methods of ejecting until it works */
1116 verbose(_("%s: trying to eject using CD-ROM eject command"), device
);
1117 worked
= eject_cdrom(fd
);
1118 verbose(worked
? _("CD-ROM eject command succeeded") :
1119 _("CD-ROM eject command failed"));
1122 if (s_option
&& !worked
) {
1123 verbose(_("%s: trying to eject using SCSI commands"), device
);
1124 worked
= eject_scsi(fd
);
1125 verbose(worked
? _("SCSI eject succeeded") :
1126 _("SCSI eject failed"));
1129 if (f_option
&& !worked
) {
1130 verbose(_("%s: trying to eject using floppy eject command"), device
);
1131 worked
= eject_floppy(fd
);
1132 verbose(worked
? _("floppy eject command succeeded") :
1133 _("floppy eject command failed"));
1136 if (q_option
&& !worked
) {
1137 verbose(_("%s: trying to eject using tape offline command"), device
);
1138 worked
= eject_tape(fd
);
1139 verbose(worked
? _("tape offline command succeeded") :
1140 _("tape offline command failed"));
1144 errx(EXIT_FAILURE
, _("unable to eject"));
1151 mnt_unref_table(mtab
);
1153 return EXIT_SUCCESS
;