blob: adff9e0e285b33ad5df44a5a84d95447df4b528d [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 *
Rob Landley2fe4eac2006-02-28 04:45:24 +00008 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
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 */
15
Denys Vlasenkoda49f582009-07-08 02:58:38 +020016#include <sys/mount.h>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000017#include "libbb.h"
Denys Vlasenkoda49f582009-07-08 02:58:38 +020018/* Must be after libbb.h: they need size_t */
19#include <scsi/sg.h>
20#include <scsi/scsi.h>
Mike Frysinger55e2cf62005-05-11 00:25:47 +000021
22/* various defines swiped from linux/cdrom.h */
23#define CDROMCLOSETRAY 0x5319 /* pendant of CDROMEJECT */
24#define CDROMEJECT 0x5309 /* Ejects the cdrom media */
Denis Vlasenko000b9ba2006-10-05 23:12:49 +000025#define CDROM_DRIVE_STATUS 0x5326 /* Get tray position, etc. */
26/* drive status possibilities returned by CDROM_DRIVE_STATUS ioctl */
27#define CDS_TRAY_OPEN 2
Rob Landley9ea88362005-05-14 00:46:18 +000028
Denis Vlasenkob44c7902008-03-17 09:29:43 +000029#define dev_fd 3
Bernhard Reutner-Fischerafdad652008-02-08 15:04:00 +000030
31/* Code taken from the original eject (http://eject.sourceforge.net/),
32 * refactored it a bit for busybox (ne-bb@nicoerfurth.de) */
Bernhard Reutner-Fischerafdad652008-02-08 15:04:00 +000033
Denis Vlasenkob44c7902008-03-17 09:29:43 +000034static void eject_scsi(const char *dev)
Bernhard Reutner-Fischerafdad652008-02-08 15:04:00 +000035{
Denis Vlasenkob44c7902008-03-17 09:29:43 +000036 static const char sg_commands[3][6] = {
37 { ALLOW_MEDIUM_REMOVAL, 0, 0, 0, 0, 0 },
38 { START_STOP, 0, 0, 0, 1, 0 },
39 { START_STOP, 0, 0, 0, 2, 0 }
40 };
Bernhard Reutner-Fischerafdad652008-02-08 15:04:00 +000041
Denis Vlasenko77ad97f2008-05-13 02:27:31 +000042 unsigned i;
Denis Vlasenkob44c7902008-03-17 09:29:43 +000043 unsigned char sense_buffer[32];
44 unsigned char inqBuff[2];
45 sg_io_hdr_t io_hdr;
Bernhard Reutner-Fischerafdad652008-02-08 15:04:00 +000046
Denis Vlasenkob44c7902008-03-17 09:29:43 +000047 if ((ioctl(dev_fd, SG_GET_VERSION_NUM, &i) < 0) || (i < 30000))
48 bb_error_msg_and_die("not a sg device or old sg driver");
Bernhard Reutner-Fischerafdad652008-02-08 15:04:00 +000049
Denis Vlasenkob44c7902008-03-17 09:29:43 +000050 memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
51 io_hdr.interface_id = 'S';
52 io_hdr.cmd_len = 6;
53 io_hdr.mx_sb_len = sizeof(sense_buffer);
54 io_hdr.dxfer_direction = SG_DXFER_NONE;
55 /* io_hdr.dxfer_len = 0; */
56 io_hdr.dxferp = inqBuff;
57 io_hdr.sbp = sense_buffer;
58 io_hdr.timeout = 2000;
Bernhard Reutner-Fischerafdad652008-02-08 15:04:00 +000059
Denis Vlasenkob44c7902008-03-17 09:29:43 +000060 for (i = 0; i < 3; i++) {
Denis Vlasenkoc4f12f52008-05-12 14:35:56 +000061 io_hdr.cmdp = (void *)sg_commands[i];
Denis Vlasenkob44c7902008-03-17 09:29:43 +000062 ioctl_or_perror_and_die(dev_fd, SG_IO, (void *)&io_hdr, "%s", dev);
63 }
64
65 /* force kernel to reread partition table when new disc is inserted */
66 ioctl(dev_fd, BLKRRPART);
Bernhard Reutner-Fischerafdad652008-02-08 15:04:00 +000067}
68
Denis Vlasenkob44c7902008-03-17 09:29:43 +000069#define FLAG_CLOSE 1
70#define FLAG_SMART 2
71#define FLAG_SCSI 4
72
73static void eject_cdrom(unsigned flags, const char *dev)
Bernhard Reutner-Fischerafdad652008-02-08 15:04:00 +000074{
75 int cmd = CDROMEJECT;
76
77 if (flags & FLAG_CLOSE
Denis Vlasenkob44c7902008-03-17 09:29:43 +000078 || (flags & FLAG_SMART && ioctl(dev_fd, CDROM_DRIVE_STATUS) == CDS_TRAY_OPEN)
79 ) {
Bernhard Reutner-Fischerafdad652008-02-08 15:04:00 +000080 cmd = CDROMCLOSETRAY;
Denis Vlasenkob44c7902008-03-17 09:29:43 +000081 }
Bernhard Reutner-Fischerafdad652008-02-08 15:04:00 +000082
Denis Vlasenkob44c7902008-03-17 09:29:43 +000083 ioctl_or_perror_and_die(dev_fd, cmd, NULL, "%s", dev);
Bernhard Reutner-Fischerafdad652008-02-08 15:04:00 +000084}
85
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000086int eject_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000087int eject_main(int argc UNUSED_PARAM, char **argv)
Mike Frysinger55e2cf62005-05-11 00:25:47 +000088{
Denis Vlasenkob44c7902008-03-17 09:29:43 +000089 unsigned flags;
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000090 const char *device;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000091
Denis Vlasenko09196572007-07-21 13:27:44 +000092 opt_complementary = "?1:t--T:T--t";
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000093 flags = getopt32(argv, "tT" IF_FEATURE_EJECT_SCSI("s"));
Bernhard Reutner-Fischerafdad652008-02-08 15:04:00 +000094 device = argv[optind] ? argv[optind] : "/dev/cdrom";
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000095
Bernhard Reutner-Fischerafdad652008-02-08 15:04:00 +000096 /* We used to do "umount <device>" here, but it was buggy
97 if something was mounted OVER cdrom and
98 if cdrom is mounted many times.
99
100 This works equally well (or better):
101 #!/bin/sh
102 umount /dev/cdrom
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000103 eject /dev/cdrom
Bernhard Reutner-Fischerafdad652008-02-08 15:04:00 +0000104 */
Denis Vlasenko2e864cd2006-10-02 20:49:25 +0000105
Bernhard Reutner-Fischera4830872009-10-26 23:27:08 +0100106 xmove_fd(xopen_nonblocking(device), dev_fd);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000107
Bernhard Reutner-Fischerafdad652008-02-08 15:04:00 +0000108 if (ENABLE_FEATURE_EJECT_SCSI && (flags & FLAG_SCSI))
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000109 eject_scsi(device);
Bernhard Reutner-Fischerafdad652008-02-08 15:04:00 +0000110 else
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000111 eject_cdrom(flags, device);
Denis Vlasenko2e864cd2006-10-02 20:49:25 +0000112
Denis Vlasenko000b9ba2006-10-05 23:12:49 +0000113 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000114 close(dev_fd);
Denis Vlasenko2e864cd2006-10-02 20:49:25 +0000115
116 return EXIT_SUCCESS;
Mike Frysinger55e2cf62005-05-11 00:25:47 +0000117}