Bernhard Reutner-Fischer | c89982d | 2006-06-03 19:49:21 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Mike Frysinger | 55e2cf6 | 2005-05-11 00:25:47 +0000 | [diff] [blame] | 2 | /* |
| 3 | * eject implementation for busybox |
| 4 | * |
| 5 | * Copyright (C) 2004 Peter Willis <psyphreak@phreaker.net> |
Rob Landley | 2fe4eac | 2006-02-28 04:45:24 +0000 | [diff] [blame] | 6 | * Copyright (C) 2005 Tito Ragusa <farmatito@tiscali.it> |
Mike Frysinger | 55e2cf6 | 2005-05-11 00:25:47 +0000 | [diff] [blame] | 7 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Mike Frysinger | 55e2cf6 | 2005-05-11 00:25:47 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | /* |
| 12 | * This is a simple hack of eject based on something Erik posted in #uclibc. |
| 13 | * Most of the dirty work blatantly ripped off from cat.c =) |
| 14 | */ |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 15 | //config:config EJECT |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 16 | //config: bool "eject (4 kb)" |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 17 | //config: default y |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 18 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 19 | //config: Used to eject cdroms. (defaults to /dev/cdrom) |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 20 | //config: |
| 21 | //config:config FEATURE_EJECT_SCSI |
| 22 | //config: bool "SCSI support" |
| 23 | //config: default y |
| 24 | //config: depends on EJECT |
| 25 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 26 | //config: Add the -s option to eject, this allows to eject SCSI-Devices and |
| 27 | //config: usb-storage devices. |
Mike Frysinger | 55e2cf6 | 2005-05-11 00:25:47 +0000 | [diff] [blame] | 28 | |
Denys Vlasenko | f88e3bf | 2016-11-22 23:54:17 +0100 | [diff] [blame] | 29 | //applet:IF_EJECT(APPLET(eject, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 30 | |
| 31 | //kbuild:lib-$(CONFIG_EJECT) += eject.o |
| 32 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 33 | //usage:#define eject_trivial_usage |
| 34 | //usage: "[-t] [-T] [DEVICE]" |
| 35 | //usage:#define eject_full_usage "\n\n" |
| 36 | //usage: "Eject DEVICE or default /dev/cdrom\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 37 | //usage: IF_FEATURE_EJECT_SCSI( |
| 38 | //usage: "\n -s SCSI device" |
| 39 | //usage: ) |
| 40 | //usage: "\n -t Close tray" |
| 41 | //usage: "\n -T Open/close tray (toggle)" |
| 42 | |
Denys Vlasenko | da49f58 | 2009-07-08 02:58:38 +0200 | [diff] [blame] | 43 | #include <sys/mount.h> |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 44 | #include "libbb.h" |
Denys Vlasenko | dbb58a3 | 2016-03-14 18:23:33 +0100 | [diff] [blame] | 45 | #if ENABLE_FEATURE_EJECT_SCSI |
Denys Vlasenko | da49f58 | 2009-07-08 02:58:38 +0200 | [diff] [blame] | 46 | /* Must be after libbb.h: they need size_t */ |
Denys Vlasenko | dbb58a3 | 2016-03-14 18:23:33 +0100 | [diff] [blame] | 47 | # include "fix_u32.h" |
| 48 | # include <scsi/sg.h> |
| 49 | # include <scsi/scsi.h> |
| 50 | #endif |
Rob Landley | 9ea8836 | 2005-05-14 00:46:18 +0000 | [diff] [blame] | 51 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 52 | #define dev_fd 3 |
Bernhard Reutner-Fischer | afdad65 | 2008-02-08 15:04:00 +0000 | [diff] [blame] | 53 | |
| 54 | /* Code taken from the original eject (http://eject.sourceforge.net/), |
| 55 | * refactored it a bit for busybox (ne-bb@nicoerfurth.de) */ |
Bernhard Reutner-Fischer | afdad65 | 2008-02-08 15:04:00 +0000 | [diff] [blame] | 56 | |
Denys Vlasenko | dbb58a3 | 2016-03-14 18:23:33 +0100 | [diff] [blame] | 57 | #if ENABLE_FEATURE_EJECT_SCSI |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 58 | static void eject_scsi(const char *dev) |
Bernhard Reutner-Fischer | afdad65 | 2008-02-08 15:04:00 +0000 | [diff] [blame] | 59 | { |
Denys Vlasenko | 3e134eb | 2016-04-22 18:09:21 +0200 | [diff] [blame] | 60 | static const char sg_commands[3][6] ALIGN1 = { |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 61 | { ALLOW_MEDIUM_REMOVAL, 0, 0, 0, 0, 0 }, |
| 62 | { START_STOP, 0, 0, 0, 1, 0 }, |
| 63 | { START_STOP, 0, 0, 0, 2, 0 } |
| 64 | }; |
Bernhard Reutner-Fischer | afdad65 | 2008-02-08 15:04:00 +0000 | [diff] [blame] | 65 | |
Denis Vlasenko | 77ad97f | 2008-05-13 02:27:31 +0000 | [diff] [blame] | 66 | unsigned i; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 67 | unsigned char sense_buffer[32]; |
| 68 | unsigned char inqBuff[2]; |
| 69 | sg_io_hdr_t io_hdr; |
Bernhard Reutner-Fischer | afdad65 | 2008-02-08 15:04:00 +0000 | [diff] [blame] | 70 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 71 | if ((ioctl(dev_fd, SG_GET_VERSION_NUM, &i) < 0) || (i < 30000)) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 72 | bb_simple_error_msg_and_die("not a sg device or old sg driver"); |
Bernhard Reutner-Fischer | afdad65 | 2008-02-08 15:04:00 +0000 | [diff] [blame] | 73 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 74 | memset(&io_hdr, 0, sizeof(sg_io_hdr_t)); |
| 75 | io_hdr.interface_id = 'S'; |
| 76 | io_hdr.cmd_len = 6; |
| 77 | io_hdr.mx_sb_len = sizeof(sense_buffer); |
| 78 | io_hdr.dxfer_direction = SG_DXFER_NONE; |
| 79 | /* io_hdr.dxfer_len = 0; */ |
| 80 | io_hdr.dxferp = inqBuff; |
| 81 | io_hdr.sbp = sense_buffer; |
| 82 | io_hdr.timeout = 2000; |
Bernhard Reutner-Fischer | afdad65 | 2008-02-08 15:04:00 +0000 | [diff] [blame] | 83 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 84 | for (i = 0; i < 3; i++) { |
Denis Vlasenko | c4f12f5 | 2008-05-12 14:35:56 +0000 | [diff] [blame] | 85 | io_hdr.cmdp = (void *)sg_commands[i]; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 86 | ioctl_or_perror_and_die(dev_fd, SG_IO, (void *)&io_hdr, "%s", dev); |
| 87 | } |
| 88 | |
| 89 | /* force kernel to reread partition table when new disc is inserted */ |
| 90 | ioctl(dev_fd, BLKRRPART); |
Bernhard Reutner-Fischer | afdad65 | 2008-02-08 15:04:00 +0000 | [diff] [blame] | 91 | } |
Denys Vlasenko | dbb58a3 | 2016-03-14 18:23:33 +0100 | [diff] [blame] | 92 | #else |
Denys Vlasenko | 23961b2 | 2016-03-14 19:34:15 +0100 | [diff] [blame] | 93 | # define eject_scsi(dev) ((void)0) |
Denys Vlasenko | dbb58a3 | 2016-03-14 18:23:33 +0100 | [diff] [blame] | 94 | #endif |
| 95 | |
| 96 | /* various defines swiped from linux/cdrom.h */ |
| 97 | #define CDROMCLOSETRAY 0x5319 /* pendant of CDROMEJECT */ |
| 98 | #define CDROMEJECT 0x5309 /* Ejects the cdrom media */ |
| 99 | #define CDROM_DRIVE_STATUS 0x5326 /* Get tray position, etc. */ |
| 100 | /* drive status possibilities returned by CDROM_DRIVE_STATUS ioctl */ |
| 101 | #define CDS_TRAY_OPEN 2 |
Bernhard Reutner-Fischer | afdad65 | 2008-02-08 15:04:00 +0000 | [diff] [blame] | 102 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 103 | #define FLAG_CLOSE 1 |
| 104 | #define FLAG_SMART 2 |
| 105 | #define FLAG_SCSI 4 |
| 106 | |
| 107 | static void eject_cdrom(unsigned flags, const char *dev) |
Bernhard Reutner-Fischer | afdad65 | 2008-02-08 15:04:00 +0000 | [diff] [blame] | 108 | { |
| 109 | int cmd = CDROMEJECT; |
| 110 | |
| 111 | if (flags & FLAG_CLOSE |
Denys Vlasenko | 72ac690 | 2009-11-15 02:48:01 +0100 | [diff] [blame] | 112 | || ((flags & FLAG_SMART) && ioctl(dev_fd, CDROM_DRIVE_STATUS) == CDS_TRAY_OPEN) |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 113 | ) { |
Bernhard Reutner-Fischer | afdad65 | 2008-02-08 15:04:00 +0000 | [diff] [blame] | 114 | cmd = CDROMCLOSETRAY; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 115 | } |
Bernhard Reutner-Fischer | afdad65 | 2008-02-08 15:04:00 +0000 | [diff] [blame] | 116 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 117 | ioctl_or_perror_and_die(dev_fd, cmd, NULL, "%s", dev); |
Bernhard Reutner-Fischer | afdad65 | 2008-02-08 15:04:00 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 120 | int eject_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 121 | int eject_main(int argc UNUSED_PARAM, char **argv) |
Mike Frysinger | 55e2cf6 | 2005-05-11 00:25:47 +0000 | [diff] [blame] | 122 | { |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 123 | unsigned flags; |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 124 | const char *device; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 125 | |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 126 | flags = getopt32(argv, "^" "tT"IF_FEATURE_EJECT_SCSI("s") |
| 127 | "\0" "?1:t--T:T--t" |
| 128 | ); |
Bernhard Reutner-Fischer | afdad65 | 2008-02-08 15:04:00 +0000 | [diff] [blame] | 129 | device = argv[optind] ? argv[optind] : "/dev/cdrom"; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 130 | |
Bernhard Reutner-Fischer | afdad65 | 2008-02-08 15:04:00 +0000 | [diff] [blame] | 131 | /* We used to do "umount <device>" here, but it was buggy |
| 132 | if something was mounted OVER cdrom and |
| 133 | if cdrom is mounted many times. |
| 134 | |
| 135 | This works equally well (or better): |
| 136 | #!/bin/sh |
| 137 | umount /dev/cdrom |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 138 | eject /dev/cdrom |
Bernhard Reutner-Fischer | afdad65 | 2008-02-08 15:04:00 +0000 | [diff] [blame] | 139 | */ |
Denis Vlasenko | 2e864cd | 2006-10-02 20:49:25 +0000 | [diff] [blame] | 140 | |
Bernhard Reutner-Fischer | a483087 | 2009-10-26 23:27:08 +0100 | [diff] [blame] | 141 | xmove_fd(xopen_nonblocking(device), dev_fd); |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 142 | |
Bernhard Reutner-Fischer | afdad65 | 2008-02-08 15:04:00 +0000 | [diff] [blame] | 143 | if (ENABLE_FEATURE_EJECT_SCSI && (flags & FLAG_SCSI)) |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 144 | eject_scsi(device); |
Bernhard Reutner-Fischer | afdad65 | 2008-02-08 15:04:00 +0000 | [diff] [blame] | 145 | else |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 146 | eject_cdrom(flags, device); |
Denis Vlasenko | 2e864cd | 2006-10-02 20:49:25 +0000 | [diff] [blame] | 147 | |
Denis Vlasenko | 000b9ba | 2006-10-05 23:12:49 +0000 | [diff] [blame] | 148 | if (ENABLE_FEATURE_CLEAN_UP) |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 149 | close(dev_fd); |
Denis Vlasenko | 2e864cd | 2006-10-02 20:49:25 +0000 | [diff] [blame] | 150 | |
| 151 | return EXIT_SUCCESS; |
Mike Frysinger | 55e2cf6 | 2005-05-11 00:25:47 +0000 | [diff] [blame] | 152 | } |