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
;
109 struct libmnt_cache
*cache
;
111 static void vinfo(const char *fmt
, va_list va
)
113 fprintf(stdout
, "%s: ", program_invocation_short_name
);
118 static inline void verbose(const char *fmt
, ...)
130 static inline void info(const char *fmt
, ...)
138 static void __attribute__ ((__noreturn__
)) usage(FILE * out
)
140 fputs(USAGE_HEADER
, out
);
143 _(" %s [options] [<device>|<mountpoint>]\n"), program_invocation_short_name
);
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 fputs(USAGE_HELP
, out
);
168 fputs(USAGE_VERSION
, out
);
170 fputs(_("\nBy default tries -r, -s, -f, and -q in order until success.\n"), out
);
171 fprintf(out
, USAGE_MAN_TAIL("eject(1)"));
173 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
177 /* Handle command line options. */
178 static void parse_args(int argc
, char **argv
, char **device
)
180 static const struct option long_opts
[] =
182 {"auto", required_argument
, NULL
, 'a'},
183 {"cdrom", no_argument
, NULL
, 'r'},
184 {"cdspeed", required_argument
, NULL
, 'x'},
185 {"changerslot", required_argument
, NULL
, 'c'},
186 {"default", no_argument
, NULL
, 'd'},
187 {"floppy", no_argument
, NULL
, 'f'},
188 {"force", no_argument
, NULL
, 'F'},
189 {"help", no_argument
, NULL
, 'h'},
190 {"listspeed", no_argument
, NULL
, 'X'},
191 {"manualeject", required_argument
, NULL
, 'i'},
192 {"noop", no_argument
, NULL
, 'n'},
193 {"no-unmount", no_argument
, NULL
, 'm'},
194 {"no-partitions-unmount", no_argument
, NULL
, 'M' },
195 {"proc", no_argument
, NULL
, 'p'},
196 {"scsi", no_argument
, NULL
, 's'},
197 {"tape", no_argument
, NULL
, 'q'},
198 {"trayclose", no_argument
, NULL
, 't'},
199 {"traytoggle", no_argument
, NULL
, 'T'},
200 {"verbose", no_argument
, NULL
, 'v'},
201 {"version", no_argument
, NULL
, 'V'},
206 while ((c
= getopt_long(argc
, argv
,
207 "a:c:i:x:dfFhnqrstTXvVpmM", long_opts
, NULL
)) != -1) {
211 if (!strcmp(optarg
, "0") || !strcmp(optarg
, "off"))
213 else if (!strcmp(optarg
, "1") || !strcmp(optarg
, "on"))
216 errx(EXIT_FAILURE
, _("invalid argument to --auto/-a option"));
220 c_arg
= strtoul_or_err(optarg
, _("invalid argument to --changerslot/-c option"));
224 x_arg
= strtoul_or_err(optarg
, _("invalid argument to --cdspeed/-x option"));
240 if (!strcmp(optarg
, "0") || !strcmp(optarg
, "off"))
242 else if (!strcmp(optarg
, "1") || !strcmp(optarg
, "on"))
245 errx(EXIT_FAILURE
, _("invalid argument to --manualeject/-i option"));
281 printf(UTIL_LINUX_VERSION
);
291 /* check for a single additional argument */
292 if ((argc
- optind
) > 1)
293 errx(EXIT_FAILURE
, _("too many arguments"));
295 if ((argc
- optind
) == 1)
296 *device
= xstrdup(argv
[optind
]);
300 * Given name, such as foo, see if any of the following exist:
302 * foo (if foo starts with '.' or '/')
305 * If found, return the full path. If not found, return 0.
306 * Returns pointer to dynamically allocated string.
308 static char *find_device(const char *name
)
313 if ((*name
== '.' || *name
== '/') && access(name
, F_OK
) == 0)
314 return xstrdup(name
);
318 snprintf(buf
, sizeof(buf
), "/dev/%s", name
);
319 if (access(buf
, F_OK
) == 0)
326 /* Set or clear auto-eject mode. */
327 static void auto_eject(int fd
, int on
)
331 #if defined(CDROM_SET_OPTIONS) && defined(CDROM_CLEAR_OPTIONS)
333 status
= ioctl(fd
, CDROM_SET_OPTIONS
, CDO_AUTO_EJECT
);
335 status
= ioctl(fd
, CDROM_CLEAR_OPTIONS
, CDO_AUTO_EJECT
);
340 err(EXIT_FAILURE
,_("CD-ROM auto-eject command failed"));
344 * Stops CDROM from opening on manual eject pressing the button.
345 * This can be useful when you carry your laptop
346 * in your bag while it's on and no CD inserted in it's drive.
347 * Implemented as found in Documentation/ioctl/cdrom.txt
349 * TODO: Maybe we should check this also:
350 * EDRIVE_CANT_DO_THIS Door lock function not supported.
351 * EBUSY Attempt to unlock when multiple users
352 * have the drive open and not CAP_SYS_ADMIN
354 static void manual_eject(int fd
, int on
)
356 if (ioctl(fd
, CDROM_LOCKDOOR
, on
) < 0)
357 err(EXIT_FAILURE
, _("CD-ROM lock door command failed"));
360 info(_("CD-Drive may NOT be ejected with device button"));
362 info(_("CD-Drive may be ejected with device button"));
366 * Changer select. CDROM_SELECT_DISC is preferred, older kernels used
369 static void changer_select(int fd
, int slot
)
371 #ifdef CDROM_SELECT_DISC
372 if (ioctl(fd
, CDROM_SELECT_DISC
, slot
) < 0)
373 err(EXIT_FAILURE
, _("CD-ROM select disc command failed"));
375 #elif defined CDROMLOADFROMSLOT
376 if (ioctl(fd
, CDROMLOADFROMSLOT
, slot
) != 0)
377 err(EXIT_FAILURE
, _("CD-ROM load from slot command failed"));
379 warnx(_("IDE/ATAPI CD-ROM changer not supported by this kernel\n") );
384 * Close tray. Not supported by older kernels.
386 static void close_tray(int fd
)
390 #if defined(CDROMCLOSETRAY) || defined(CDIOCCLOSE)
391 #if defined(CDROMCLOSETRAY)
392 status
= ioctl(fd
, CDROMCLOSETRAY
);
393 #elif defined(CDIOCCLOSE)
394 status
= ioctl(fd
, CDIOCCLOSE
);
397 err(EXIT_FAILURE
, _("CD-ROM tray close command failed"));
399 warnx(_("CD-ROM tray close command not supported by this kernel\n"));
404 * Eject using CDROMEJECT ioctl.
406 static int eject_cdrom(int fd
)
408 #if defined(CDROMEJECT)
409 int ret
= ioctl(fd
, CDROM_LOCKDOOR
, 0);
412 return ioctl(fd
, CDROMEJECT
) >= 0;
413 #elif defined(CDIOCEJECT)
414 return ioctl(fd
, CDIOCEJECT
) >= 0;
416 warnx(_("CD-ROM eject unsupported"));
425 * Written by Benjamin Schwenk <benjaminschwenk@yahoo.de> and
426 * Sybren Stuvel <sybren@thirdtower.com>
428 * Not supported by older kernels because it might use
432 static void toggle_tray(int fd
)
434 struct timeval time_start
, time_stop
;
437 #ifdef CDROM_DRIVE_STATUS
438 /* First ask the CDROM for info, otherwise fall back to manual. */
439 switch (ioctl(fd
, CDROM_DRIVE_STATUS
)) {
446 if (!eject_cdrom(fd
))
447 err(EXIT_FAILURE
, _("CD-ROM eject command failed"));
450 warnx(_("no CD-ROM information available"));
452 case CDS_DRIVE_NOT_READY
:
453 warnx(_("CD-ROM drive is not ready"));
460 /* Try to open the CDROM tray and measure the time therefor
461 * needed. In my experience the function needs less than 0.05
462 * seconds if the tray was already open, and at least 1.5 seconds
463 * if it was closed. */
464 gettimeofday(&time_start
, NULL
);
466 /* Send the CDROMEJECT command to the device. */
467 if (!eject_cdrom(fd
))
468 err(EXIT_FAILURE
, _("CD-ROM eject command failed"));
470 /* Get the second timestamp, to measure the time needed to open
472 gettimeofday(&time_stop
, NULL
);
474 time_elapsed
= (time_stop
.tv_sec
* 1000000 + time_stop
.tv_usec
) -
475 (time_start
.tv_sec
* 1000000 + time_start
.tv_usec
);
477 /* If the tray "opened" too fast, we can be nearly sure, that it
478 * was already open. In this case, close it now. Else the tray was
479 * closed before. This would mean that we are done. */
480 if (time_elapsed
< TRAY_WAS_ALREADY_OPEN_USECS
)
485 * Select Speed of CD-ROM drive.
486 * Thanks to Roland Krivanek (krivanek@fmph.uniba.sk)
487 * http://dmpc.dbp.fmph.uniba.sk/~krivanek/cdrom_speed/
489 static void select_speed(int fd
, int speed
)
491 #ifdef CDROM_SELECT_SPEED
492 if (ioctl(fd
, CDROM_SELECT_SPEED
, speed
) != 0)
493 err(EXIT_FAILURE
, _("CD-ROM select speed command failed"));
495 warnx(_("CD-ROM select speed command not supported by this kernel"));
500 * Read Speed of CD-ROM drive. From Linux 2.6.13, the current speed
501 * is correctly reported
503 static int read_speed(const char *devname
)
505 int drive_number
= -1;
509 f
= fopen(_PATH_PROC_CDROMINFO
, "r");
511 err(EXIT_FAILURE
, _("cannot open %s"), _PATH_PROC_CDROMINFO
);
513 name
= strrchr(devname
, '/') + 1;
515 while (name
&& !feof(f
)) {
519 if (!fgets(line
, sizeof(line
), f
))
522 /* find drive number in line "drive name" */
523 if (drive_number
== -1) {
524 if (strncmp(line
, "drive name:", 11) == 0) {
525 str
= strtok(&line
[11], "\t ");
527 while (str
&& strncmp(name
, str
, strlen(name
)) != 0) {
529 str
= strtok(NULL
, "\t ");
532 _("%s: failed to finding CD-ROM name"),
533 _PATH_PROC_CDROMINFO
);
536 /* find line "drive speed" and read the correct speed */
538 if (strncmp(line
, "drive speed:", 12) == 0) {
541 str
= strtok(&line
[12], "\t ");
542 for (i
= 1; i
< drive_number
; i
++)
543 str
= strtok(NULL
, "\t ");
547 _("%s: failed to read speed"),
548 _PATH_PROC_CDROMINFO
);
555 errx(EXIT_FAILURE
, _("failed to read speed"));
559 * List Speed of CD-ROM drive.
561 static void list_speeds(const char *name
, int fd
)
563 #ifdef CDROM_SELECT_SPEED
564 int max_speed
, curr_speed
= 0, prev_speed
;
567 max_speed
= read_speed(name
);
569 while (curr_speed
< max_speed
) {
570 prev_speed
= curr_speed
;
571 select_speed(fd
, prev_speed
+ 1);
572 curr_speed
= read_speed(name
);
573 if (curr_speed
> prev_speed
)
574 printf("%d ", curr_speed
);
576 curr_speed
= prev_speed
+ 1;
581 warnx(_("CD-ROM select speed command not supported by this kernel"));
586 * Eject using SCSI SG_IO commands. Return 1 if successful, 0 otherwise.
588 static int eject_scsi(int fd
)
592 unsigned char allowRmBlk
[6] = {ALLOW_MEDIUM_REMOVAL
, 0, 0, 0, 0, 0};
593 unsigned char startStop1Blk
[6] = {START_STOP
, 0, 0, 0, 1, 0};
594 unsigned char startStop2Blk
[6] = {START_STOP
, 0, 0, 0, 2, 0};
595 unsigned char inqBuff
[2];
596 unsigned char sense_buffer
[32];
598 if ((ioctl(fd
, SG_GET_VERSION_NUM
, &k
) < 0) || (k
< 30000)) {
599 verbose(_("not an sg device, or old sg driver"));
603 memset(&io_hdr
, 0, sizeof(sg_io_hdr_t
));
604 io_hdr
.interface_id
= 'S';
606 io_hdr
.mx_sb_len
= sizeof(sense_buffer
);
607 io_hdr
.dxfer_direction
= SG_DXFER_NONE
;
608 io_hdr
.dxfer_len
= 0;
609 io_hdr
.dxferp
= inqBuff
;
610 io_hdr
.sbp
= sense_buffer
;
611 io_hdr
.timeout
= 10000;
613 io_hdr
.cmdp
= allowRmBlk
;
614 status
= ioctl(fd
, SG_IO
, (void *)&io_hdr
);
615 if (status
< 0 || io_hdr
.host_status
|| io_hdr
.driver_status
)
618 io_hdr
.cmdp
= startStop1Blk
;
619 status
= ioctl(fd
, SG_IO
, (void *)&io_hdr
);
620 if (status
< 0 || io_hdr
.host_status
)
623 /* Ignore errors when there is not medium -- in this case driver sense
624 * buffer sets MEDIUM NOT PRESENT (3a) bit. For more details see:
625 * http://www.tldp.org/HOWTO/archived/SCSI-Programming-HOWTO/SCSI-Programming-HOWTO-22.html#sec-sensecodes
628 if (io_hdr
.driver_status
!= 0 &&
629 !(io_hdr
.driver_status
== DRIVER_SENSE
&& io_hdr
.sbp
&&
630 io_hdr
.sbp
[12] == 0x3a))
633 io_hdr
.cmdp
= startStop2Blk
;
634 status
= ioctl(fd
, SG_IO
, (void *)&io_hdr
);
635 if (status
< 0 || io_hdr
.host_status
|| io_hdr
.driver_status
)
638 /* force kernel to reread partition table when new disc inserted */
639 ioctl(fd
, BLKRRPART
);
644 * Eject using FDEJECT ioctl. Return 1 if successful, 0 otherwise.
646 static int eject_floppy(int fd
)
648 return ioctl(fd
, FDEJECT
) >= 0;
653 * Rewind and eject using tape ioctl. Return 1 if successful, 0 otherwise.
655 static int eject_tape(int fd
)
657 struct mtop op
= { .mt_op
= MTOFFL
, .mt_count
= 0 };
659 return ioctl(fd
, MTIOCTOP
, &op
) >= 0;
663 /* umount a device. */
664 static void umount_one(const char *name
)
671 verbose(_("%s: unmounting"), name
);
675 if (setgid(getgid()) < 0)
676 err(EXIT_FAILURE
, _("cannot set group id"));
678 if (setuid(getuid()) < 0)
679 err(EXIT_FAILURE
, _("cannot set user id"));
682 execl("/bin/umount", "/bin/umount", name
, "-n", NULL
);
684 execl("/bin/umount", "/bin/umount", name
, NULL
);
686 errx(EXIT_FAILURE
, _("unable to exec /bin/umount of `%s'"), name
);
689 warn( _("unable to fork"));
692 default: /* parent */
694 if (WIFEXITED(status
) == 0)
696 _("unmount of `%s' did not exit normally"), name
);
698 if (WEXITSTATUS(status
) != 0)
699 errx(EXIT_FAILURE
, _("unmount of `%s' failed\n"), name
);
704 /* Open a device file. */
705 static int open_device(const char *name
)
707 int fd
= open(name
, O_RDWR
|O_NONBLOCK
);
710 fd
= open(name
, O_RDONLY
|O_NONBLOCK
);
712 err(EXIT_FAILURE
, _("cannot open %s"), name
);
717 * See if device has been mounted by looking in mount table. If so, set
718 * device name and mount point name, and return 1, otherwise return 0.
720 static int device_get_mountpoint(char **devname
, char **mnt
)
722 struct libmnt_fs
*fs
;
728 mtab
= mnt_new_table();
730 err(EXIT_FAILURE
, _("failed to initialize libmount table"));
733 cache
= mnt_new_cache();
734 mnt_table_set_cache(mtab
, cache
);
737 rc
= mnt_table_parse_file(mtab
, _PATH_PROC_MOUNTINFO
);
739 rc
= mnt_table_parse_mtab(mtab
, NULL
);
741 err(EXIT_FAILURE
, _("failed to parse mount table"));
744 fs
= mnt_table_find_source(mtab
, *devname
, MNT_ITER_BACKWARD
);
746 /* maybe 'devname' is mountpoint rather than a real device */
747 fs
= mnt_table_find_target(mtab
, *devname
, MNT_ITER_BACKWARD
);
750 *devname
= xstrdup(mnt_fs_get_source(fs
));
755 *mnt
= xstrdup(mnt_fs_get_target(fs
));
756 return *mnt
? 0 : -1;
759 static char *get_disk_devname(const char *device
)
765 if (stat(device
, &st
) != 0)
768 /* get whole-disk devno */
769 if (sysfs_devno_to_wholedisk(st
.st_rdev
, diskname
,
770 sizeof(diskname
), &diskno
) != 0)
773 return st
.st_rdev
== diskno
? NULL
: find_device(diskname
);
776 static int umount_partitions(const char *disk
, int checkonly
)
778 struct sysfs_cxt cxt
= UL_SYSFSCXT_EMPTY
;
784 devno
= sysfs_devname_to_devno(disk
, NULL
);
785 if (sysfs_init(&cxt
, devno
, NULL
) != 0)
788 /* open /sys/block/<wholedisk> */
789 if (!(dir
= sysfs_opendir(&cxt
, NULL
)))
792 /* scan for partition subdirs */
793 while ((d
= readdir(dir
))) {
794 if (!strcmp(d
->d_name
, ".") || !strcmp(d
->d_name
, ".."))
797 if (sysfs_is_partition_dirent(dir
, d
, disk
)) {
799 char *dev
= find_device(d
->d_name
);
801 if (dev
&& device_get_mountpoint(&dev
, &mnt
) == 0) {
802 verbose(_("%s: mounted on %s"), dev
, mnt
);
820 static int is_hotpluggable_subsystem(const char *name
)
824 for (i
= 0; i
< ARRAY_SIZE(hotplug_subsystems
); i
++)
825 if (strcmp(name
, hotplug_subsystems
[i
]) == 0)
831 #define SUBSYSTEM_LINKNAME "/subsystem"
836 * chain: /sys/dev/block/../../devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.2/ \
837 * 1-1.2:1.0/host65/target65:0:0/65:0:0:0/block/sdb
839 * The function check if <chain>/subsystem symlink exists, if yes then returns
840 * basename of the readlink result, and remove the last subdirectory from the
843 static char *get_subsystem(char *chain
, char *buf
, size_t bufsz
)
848 if (!chain
|| !*chain
)
852 if (len
+ sizeof(SUBSYSTEM_LINKNAME
) > PATH_MAX
)
858 /* append "/subsystem" to the path */
859 memcpy(chain
+ len
, SUBSYSTEM_LINKNAME
, sizeof(SUBSYSTEM_LINKNAME
));
861 /* try if subsystem symlink exists */
862 sz
= readlink(chain
, buf
, bufsz
- 1);
864 /* remove last subsystem from chain */
866 p
= strrchr(chain
, '/');
873 /* we found symlink to subsystem, return basename */
875 return basename(buf
);
883 static int is_hotpluggable(const char* device
)
885 struct sysfs_cxt cxt
= UL_SYSFSCXT_EMPTY
;
886 char devchain
[PATH_MAX
];
887 char subbuf
[PATH_MAX
];
893 devno
= sysfs_devname_to_devno(device
, NULL
);
894 if (sysfs_init(&cxt
, devno
, NULL
) != 0)
897 /* check /sys/dev/block/<maj>:<min>/removable attribute */
898 if (sysfs_read_int(&cxt
, "removable", &rc
) == 0 && rc
== 1) {
899 verbose(_("%s: is removable device"), device
);
903 /* read /sys/dev/block/<maj>:<min> symlink */
904 sz
= sysfs_readlink(&cxt
, NULL
, devchain
, sizeof(devchain
));
905 if (sz
<= 0 || sz
+ sizeof(_PATH_SYS_DEVBLOCK
"/") > sizeof(devchain
))
908 devchain
[sz
++] = '\0';
910 /* create absolute patch from the link */
911 memmove(devchain
+ sizeof(_PATH_SYS_DEVBLOCK
"/") - 1, devchain
, sz
);
912 memcpy(devchain
, _PATH_SYS_DEVBLOCK
"/",
913 sizeof(_PATH_SYS_DEVBLOCK
"/") - 1);
915 while ((sub
= get_subsystem(devchain
, subbuf
, sizeof(subbuf
)))) {
916 rc
= is_hotpluggable_subsystem(sub
);
918 verbose(_("%s: connected by hotplug subsystem: %s"),
930 /* handle -x option */
931 static void set_device_speed(char *name
)
939 verbose(_("setting CD-ROM speed to auto"));
941 verbose(_("setting CD-ROM speed to %ldX"), x_arg
);
943 fd
= open_device(name
);
944 select_speed(fd
, x_arg
);
950 int main(int argc
, char **argv
)
954 char *mountpoint
= NULL
;
955 int worked
= 0; /* set to 1 when successfully ejected */
956 int fd
; /* file descriptor for device */
958 setlocale(LC_ALL
,"");
959 bindtextdomain(PACKAGE
, LOCALEDIR
);
961 atexit(close_stdout
);
963 /* parse the command line arguments */
964 parse_args(argc
, argv
, &device
);
966 /* handle -d option */
968 info(_("default device: `%s'"), EJECT_DEFAULT_DEVICE
);
973 device
= mnt_resolve_path(EJECT_DEFAULT_DEVICE
, NULL
);
974 verbose(_("using default device `%s'"), device
);
978 if (device
[strlen(device
)-1] == '/')
979 device
[strlen(device
)-1] = '\0';
981 /* figure out full device or mount point name */
982 p
= find_device(device
);
988 device
= mnt_resolve_spec(p
, NULL
);
993 errx(EXIT_FAILURE
, _("%s: unable to find device"), device
);
995 verbose(_("device name is `%s'"), device
);
997 device_get_mountpoint(&device
, &mountpoint
);
999 verbose(_("%s: mounted on %s"), device
, mountpoint
);
1001 verbose(_("%s: not mounted"), device
);
1003 disk
= get_disk_devname(device
);
1005 verbose(_("%s: disc device: %s (disk device will be used for eject)"), device
, disk
);
1012 if (stat(device
, &st
) != 0 || !S_ISBLK(st
.st_mode
))
1013 errx(EXIT_FAILURE
, _("%s: not found mountpoint or device "
1014 "with the given name"), device
);
1016 verbose(_("%s: is whole-disk device"), device
);
1019 if (F_option
== 0 && is_hotpluggable(device
) == 0)
1020 errx(EXIT_FAILURE
, _("%s: is not hot-pluggable device"), device
);
1022 /* handle -n option */
1024 info(_("device is `%s'"), device
);
1025 verbose(_("exiting due to -n/--noop option"));
1026 return EXIT_SUCCESS
;
1029 /* handle -i option */
1031 fd
= open_device(device
);
1032 manual_eject(fd
, i_arg
);
1033 return EXIT_SUCCESS
;
1036 /* handle -a option */
1039 verbose(_("%s: enabling auto-eject mode"), device
);
1041 verbose(_("%s: disabling auto-eject mode"), device
);
1042 fd
= open_device(device
);
1043 auto_eject(fd
, a_arg
);
1044 return EXIT_SUCCESS
;
1047 /* handle -t option */
1049 verbose(_("%s: closing tray"), device
);
1050 fd
= open_device(device
);
1052 set_device_speed(device
);
1053 return EXIT_SUCCESS
;
1056 /* handle -T option */
1058 verbose(_("%s: toggling tray"), device
);
1059 fd
= open_device(device
);
1061 set_device_speed(device
);
1062 return EXIT_SUCCESS
;
1065 /* handle -X option */
1067 verbose(_("%s: listing CD-ROM speed"), device
);
1068 fd
= open_device(device
);
1069 list_speeds(device
, fd
);
1070 return EXIT_SUCCESS
;
1073 /* handle -x option only */
1075 set_device_speed(device
);
1079 * Unmount all partitions if -m is not specified; or umount given
1080 * mountpoint if -M is specified, otherwise print error of another
1081 * partition is mounted.
1084 int ct
= umount_partitions(device
, M_option
);
1086 if (ct
== 0 && mountpoint
)
1087 umount_one(mountpoint
); /* probably whole-device */
1090 if (ct
== 1 && mountpoint
)
1091 umount_one(mountpoint
);
1093 errx(EXIT_FAILURE
, _("error: %s: device in use"), device
);
1097 /* handle -c option */
1099 verbose(_("%s: selecting CD-ROM disc #%ld"), device
, c_arg
);
1100 fd
= open_device(device
);
1101 changer_select(fd
, c_arg
);
1102 set_device_speed(device
);
1103 return EXIT_SUCCESS
;
1106 /* if user did not specify type of eject, try all four methods */
1107 if (r_option
+ s_option
+ f_option
+ q_option
== 0)
1108 r_option
= s_option
= f_option
= q_option
= 1;
1111 fd
= open_device(device
);
1113 /* try various methods of ejecting until it works */
1115 verbose(_("%s: trying to eject using CD-ROM eject command"), device
);
1116 worked
= eject_cdrom(fd
);
1117 verbose(worked
? _("CD-ROM eject command succeeded") :
1118 _("CD-ROM eject command failed"));
1121 if (s_option
&& !worked
) {
1122 verbose(_("%s: trying to eject using SCSI commands"), device
);
1123 worked
= eject_scsi(fd
);
1124 verbose(worked
? _("SCSI eject succeeded") :
1125 _("SCSI eject failed"));
1128 if (f_option
&& !worked
) {
1129 verbose(_("%s: trying to eject using floppy eject command"), device
);
1130 worked
= eject_floppy(fd
);
1131 verbose(worked
? _("floppy eject command succeeded") :
1132 _("floppy eject command failed"));
1135 if (q_option
&& !worked
) {
1136 verbose(_("%s: trying to eject using tape offline command"), device
);
1137 worked
= eject_tape(fd
);
1138 verbose(worked
? _("tape offline command succeeded") :
1139 _("tape offline command failed"));
1143 errx(EXIT_FAILURE
, _("unable to eject"));
1150 mnt_free_table(mtab
);
1151 mnt_unref_cache(cache
);
1153 return EXIT_SUCCESS
;