blob: 3d27ce545235751259c038fc95cfad14b455c6b8 [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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000016#include "libbb.h"
Mike Frysinger55e2cf62005-05-11 00:25:47 +000017
18/* various defines swiped from linux/cdrom.h */
19#define CDROMCLOSETRAY 0x5319 /* pendant of CDROMEJECT */
20#define CDROMEJECT 0x5309 /* Ejects the cdrom media */
Denis Vlasenko000b9ba2006-10-05 23:12:49 +000021#define CDROM_DRIVE_STATUS 0x5326 /* Get tray position, etc. */
22/* drive status possibilities returned by CDROM_DRIVE_STATUS ioctl */
23#define CDS_TRAY_OPEN 2
Rob Landley9ea88362005-05-14 00:46:18 +000024
Denis Vlasenko2e864cd2006-10-02 20:49:25 +000025#define FLAG_CLOSE 1
26#define FLAG_SMART 2
27
Bernhard Reutner-Fischerafdad652008-02-08 15:04:00 +000028
29/* Code taken from the original eject (http://eject.sourceforge.net/),
30 * refactored it a bit for busybox (ne-bb@nicoerfurth.de) */
31#define FLAG_SCSI 4
32
33#include <scsi/sg.h>
34#include <scsi/scsi.h>
35static void eject_scsi(const int fd, const char * const dev)
36{
37 int i;
38 unsigned char sense_buffer[32];
39 unsigned char inqBuff[2];
40 sg_io_hdr_t io_hdr;
41 char sg_commands[3][6] = {
42 {ALLOW_MEDIUM_REMOVAL, 0, 0, 0, 0, 0},
43 {START_STOP, 0, 0, 0, 1, 0},
44 {START_STOP, 0, 0, 0, 2, 0}
45 };
46
47 if ((ioctl(fd, SG_GET_VERSION_NUM, &i) < 0) || (i < 30000))
48 bb_error_msg_and_die("not an sg device or old sg driver");
49
50 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;
59
60 for (i=0; i < 3; i++) {
61 io_hdr.cmdp = sg_commands[i];
62 ioctl_or_perror_and_die(fd, SG_IO, (void *)&io_hdr, "%s", dev);
63 }
64
65 /* force kernel to reread partition table when new disc inserted */
66 ioctl(fd, BLKRRPART);
67}
68
69static void eject_cdrom(const int fd, const unsigned long flags,
70 const char * const dev)
71{
72 int cmd = CDROMEJECT;
73
74 if (flags & FLAG_CLOSE
75 || (flags & FLAG_SMART && ioctl(fd, CDROM_DRIVE_STATUS) == CDS_TRAY_OPEN))
76 cmd = CDROMCLOSETRAY;
77
78 return ioctl_or_perror_and_die(fd, cmd, NULL, "%s", dev);
79}
80
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000081int eject_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko68404f12008-03-17 09:00:54 +000082int eject_main(int argc ATTRIBUTE_UNUSED, char **argv)
Mike Frysinger55e2cf62005-05-11 00:25:47 +000083{
84 unsigned long flags;
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000085 const char *device;
Bernhard Reutner-Fischerafdad652008-02-08 15:04:00 +000086 int dev;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000087
Denis Vlasenko09196572007-07-21 13:27:44 +000088 opt_complementary = "?1:t--T:T--t";
Bernhard Reutner-Fischerafdad652008-02-08 15:04:00 +000089 flags = getopt32(argv, "tT" USE_FEATURE_EJECT_SCSI("s"));
90 device = argv[optind] ? argv[optind] : "/dev/cdrom";
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000091
Bernhard Reutner-Fischerafdad652008-02-08 15:04:00 +000092 /* We used to do "umount <device>" here, but it was buggy
93 if something was mounted OVER cdrom and
94 if cdrom is mounted many times.
95
96 This works equally well (or better):
97 #!/bin/sh
98 umount /dev/cdrom
99 eject
100 */
Denis Vlasenko2e864cd2006-10-02 20:49:25 +0000101
102 dev = xopen(device, O_RDONLY|O_NONBLOCK);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000103
Bernhard Reutner-Fischerafdad652008-02-08 15:04:00 +0000104 if (ENABLE_FEATURE_EJECT_SCSI && (flags & FLAG_SCSI))
105 eject_scsi(dev, device);
106 else
107 eject_cdrom(dev, flags, device);
Denis Vlasenko2e864cd2006-10-02 20:49:25 +0000108
Denis Vlasenko000b9ba2006-10-05 23:12:49 +0000109 if (ENABLE_FEATURE_CLEAN_UP)
110 close(dev);
Denis Vlasenko2e864cd2006-10-02 20:49:25 +0000111
112 return EXIT_SUCCESS;
Mike Frysinger55e2cf62005-05-11 00:25:47 +0000113}