blob: 6c30facd2624a2def33319b35c2b931db4e6691f [file] [log] [blame]
Bernhard Reutner-Fischerc89982d2006-06-03 19:49:21 +00001/* vi: set sw=4 ts=4: */
Mike Frysinger55e2cf62005-05-11 00:25:47 +00002/*
3 * eject implementation for busybox
4 *
5 * Copyright (C) 2004 Peter Willis <psyphreak@phreaker.net>
Rob Landley2fe4eac2006-02-28 04:45:24 +00006 * Copyright (C) 2005 Tito Ragusa <farmatito@tiscali.it>
Mike Frysinger55e2cf62005-05-11 00:25:47 +00007 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Mike Frysinger55e2cf62005-05-11 00:25:47 +00009 */
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 Vlasenkofb4da162016-11-22 23:14:24 +010015//config:config EJECT
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020016//config: bool "eject (4.1 kb)"
Denys Vlasenkofb4da162016-11-22 23:14:24 +010017//config: default y
18//config: select PLATFORM_LINUX
19//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020020//config: Used to eject cdroms. (defaults to /dev/cdrom)
Denys Vlasenkofb4da162016-11-22 23:14:24 +010021//config:
22//config:config FEATURE_EJECT_SCSI
23//config: bool "SCSI support"
24//config: default y
25//config: depends on EJECT
26//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020027//config: Add the -s option to eject, this allows to eject SCSI-Devices and
28//config: usb-storage devices.
Mike Frysinger55e2cf62005-05-11 00:25:47 +000029
Denys Vlasenkof88e3bf2016-11-22 23:54:17 +010030//applet:IF_EJECT(APPLET(eject, BB_DIR_USR_BIN, BB_SUID_DROP))
31
32//kbuild:lib-$(CONFIG_EJECT) += eject.o
33
Pere Orga5bc8c002011-04-11 03:29:49 +020034//usage:#define eject_trivial_usage
35//usage: "[-t] [-T] [DEVICE]"
36//usage:#define eject_full_usage "\n\n"
37//usage: "Eject DEVICE or default /dev/cdrom\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020038//usage: IF_FEATURE_EJECT_SCSI(
39//usage: "\n -s SCSI device"
40//usage: )
41//usage: "\n -t Close tray"
42//usage: "\n -T Open/close tray (toggle)"
43
Denys Vlasenkoda49f582009-07-08 02:58:38 +020044#include <sys/mount.h>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000045#include "libbb.h"
Denys Vlasenkodbb58a32016-03-14 18:23:33 +010046#if ENABLE_FEATURE_EJECT_SCSI
Denys Vlasenkoda49f582009-07-08 02:58:38 +020047/* Must be after libbb.h: they need size_t */
Denys Vlasenkodbb58a32016-03-14 18:23:33 +010048# include "fix_u32.h"
49# include <scsi/sg.h>
50# include <scsi/scsi.h>
51#endif
Rob Landley9ea88362005-05-14 00:46:18 +000052
Denis Vlasenkob44c7902008-03-17 09:29:43 +000053#define dev_fd 3
Bernhard Reutner-Fischerafdad652008-02-08 15:04:00 +000054
55/* Code taken from the original eject (http://eject.sourceforge.net/),
56 * refactored it a bit for busybox (ne-bb@nicoerfurth.de) */
Bernhard Reutner-Fischerafdad652008-02-08 15:04:00 +000057
Denys Vlasenkodbb58a32016-03-14 18:23:33 +010058#if ENABLE_FEATURE_EJECT_SCSI
Denis Vlasenkob44c7902008-03-17 09:29:43 +000059static void eject_scsi(const char *dev)
Bernhard Reutner-Fischerafdad652008-02-08 15:04:00 +000060{
Denys Vlasenko3e134eb2016-04-22 18:09:21 +020061 static const char sg_commands[3][6] ALIGN1 = {
Denis Vlasenkob44c7902008-03-17 09:29:43 +000062 { ALLOW_MEDIUM_REMOVAL, 0, 0, 0, 0, 0 },
63 { START_STOP, 0, 0, 0, 1, 0 },
64 { START_STOP, 0, 0, 0, 2, 0 }
65 };
Bernhard Reutner-Fischerafdad652008-02-08 15:04:00 +000066
Denis Vlasenko77ad97f2008-05-13 02:27:31 +000067 unsigned i;
Denis Vlasenkob44c7902008-03-17 09:29:43 +000068 unsigned char sense_buffer[32];
69 unsigned char inqBuff[2];
70 sg_io_hdr_t io_hdr;
Bernhard Reutner-Fischerafdad652008-02-08 15:04:00 +000071
Denis Vlasenkob44c7902008-03-17 09:29:43 +000072 if ((ioctl(dev_fd, SG_GET_VERSION_NUM, &i) < 0) || (i < 30000))
73 bb_error_msg_and_die("not a sg device or old sg driver");
Bernhard Reutner-Fischerafdad652008-02-08 15:04:00 +000074
Denis Vlasenkob44c7902008-03-17 09:29:43 +000075 memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
76 io_hdr.interface_id = 'S';
77 io_hdr.cmd_len = 6;
78 io_hdr.mx_sb_len = sizeof(sense_buffer);
79 io_hdr.dxfer_direction = SG_DXFER_NONE;
80 /* io_hdr.dxfer_len = 0; */
81 io_hdr.dxferp = inqBuff;
82 io_hdr.sbp = sense_buffer;
83 io_hdr.timeout = 2000;
Bernhard Reutner-Fischerafdad652008-02-08 15:04:00 +000084
Denis Vlasenkob44c7902008-03-17 09:29:43 +000085 for (i = 0; i < 3; i++) {
Denis Vlasenkoc4f12f52008-05-12 14:35:56 +000086 io_hdr.cmdp = (void *)sg_commands[i];
Denis Vlasenkob44c7902008-03-17 09:29:43 +000087 ioctl_or_perror_and_die(dev_fd, SG_IO, (void *)&io_hdr, "%s", dev);
88 }
89
90 /* force kernel to reread partition table when new disc is inserted */
91 ioctl(dev_fd, BLKRRPART);
Bernhard Reutner-Fischerafdad652008-02-08 15:04:00 +000092}
Denys Vlasenkodbb58a32016-03-14 18:23:33 +010093#else
Denys Vlasenko23961b22016-03-14 19:34:15 +010094# define eject_scsi(dev) ((void)0)
Denys Vlasenkodbb58a32016-03-14 18:23:33 +010095#endif
96
97/* various defines swiped from linux/cdrom.h */
98#define CDROMCLOSETRAY 0x5319 /* pendant of CDROMEJECT */
99#define CDROMEJECT 0x5309 /* Ejects the cdrom media */
100#define CDROM_DRIVE_STATUS 0x5326 /* Get tray position, etc. */
101/* drive status possibilities returned by CDROM_DRIVE_STATUS ioctl */
102#define CDS_TRAY_OPEN 2
Bernhard Reutner-Fischerafdad652008-02-08 15:04:00 +0000103
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000104#define FLAG_CLOSE 1
105#define FLAG_SMART 2
106#define FLAG_SCSI 4
107
108static void eject_cdrom(unsigned flags, const char *dev)
Bernhard Reutner-Fischerafdad652008-02-08 15:04:00 +0000109{
110 int cmd = CDROMEJECT;
111
112 if (flags & FLAG_CLOSE
Denys Vlasenko72ac6902009-11-15 02:48:01 +0100113 || ((flags & FLAG_SMART) && ioctl(dev_fd, CDROM_DRIVE_STATUS) == CDS_TRAY_OPEN)
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000114 ) {
Bernhard Reutner-Fischerafdad652008-02-08 15:04:00 +0000115 cmd = CDROMCLOSETRAY;
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000116 }
Bernhard Reutner-Fischerafdad652008-02-08 15:04:00 +0000117
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000118 ioctl_or_perror_and_die(dev_fd, cmd, NULL, "%s", dev);
Bernhard Reutner-Fischerafdad652008-02-08 15:04:00 +0000119}
120
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000121int eject_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000122int eject_main(int argc UNUSED_PARAM, char **argv)
Mike Frysinger55e2cf62005-05-11 00:25:47 +0000123{
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000124 unsigned flags;
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000125 const char *device;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000126
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200127 flags = getopt32(argv, "^" "tT"IF_FEATURE_EJECT_SCSI("s")
128 "\0" "?1:t--T:T--t"
129 );
Bernhard Reutner-Fischerafdad652008-02-08 15:04:00 +0000130 device = argv[optind] ? argv[optind] : "/dev/cdrom";
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000131
Bernhard Reutner-Fischerafdad652008-02-08 15:04:00 +0000132 /* We used to do "umount <device>" here, but it was buggy
133 if something was mounted OVER cdrom and
134 if cdrom is mounted many times.
135
136 This works equally well (or better):
137 #!/bin/sh
138 umount /dev/cdrom
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000139 eject /dev/cdrom
Bernhard Reutner-Fischerafdad652008-02-08 15:04:00 +0000140 */
Denis Vlasenko2e864cd2006-10-02 20:49:25 +0000141
Bernhard Reutner-Fischera4830872009-10-26 23:27:08 +0100142 xmove_fd(xopen_nonblocking(device), dev_fd);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000143
Bernhard Reutner-Fischerafdad652008-02-08 15:04:00 +0000144 if (ENABLE_FEATURE_EJECT_SCSI && (flags & FLAG_SCSI))
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000145 eject_scsi(device);
Bernhard Reutner-Fischerafdad652008-02-08 15:04:00 +0000146 else
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000147 eject_cdrom(flags, device);
Denis Vlasenko2e864cd2006-10-02 20:49:25 +0000148
Denis Vlasenko000b9ba2006-10-05 23:12:49 +0000149 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000150 close(dev_fd);
Denis Vlasenko2e864cd2006-10-02 20:49:25 +0000151
152 return EXIT_SUCCESS;
Mike Frysinger55e2cf62005-05-11 00:25:47 +0000153}