blob: 667932f6ca612ba87eba1d85a8c01a82681782e2
1 | /* vi: set sw=4 ts=4: */ |
2 | /* |
3 | * eject implementation for busybox |
4 | * |
5 | * Copyright (C) 2004 Peter Willis <psyphreak@phreaker.net> |
6 | * Copyright (C) 2005 Tito Ragusa <farmatito@tiscali.it> |
7 | * |
8 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
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 | */ |
15 | //config:config EJECT |
16 | //config: bool "eject" |
17 | //config: default y |
18 | //config: select PLATFORM_LINUX |
19 | //config: help |
20 | //config: Used to eject cdroms. (defaults to /dev/cdrom) |
21 | //config: |
22 | //config:config FEATURE_EJECT_SCSI |
23 | //config: bool "SCSI support" |
24 | //config: default y |
25 | //config: depends on EJECT |
26 | //config: help |
27 | //config: Add the -s option to eject, this allows to eject SCSI-Devices and |
28 | //config: usb-storage devices. |
29 | |
30 | //applet:IF_EJECT(APPLET(eject, BB_DIR_USR_BIN, BB_SUID_DROP)) |
31 | |
32 | //kbuild:lib-$(CONFIG_EJECT) += eject.o |
33 | |
34 | //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" |
38 | //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 | |
44 | #include <sys/mount.h> |
45 | #include "libbb.h" |
46 | #if ENABLE_FEATURE_EJECT_SCSI |
47 | /* Must be after libbb.h: they need size_t */ |
48 | # include "fix_u32.h" |
49 | # include <scsi/sg.h> |
50 | # include <scsi/scsi.h> |
51 | #endif |
52 | |
53 | #define dev_fd 3 |
54 | |
55 | /* Code taken from the original eject (http://eject.sourceforge.net/), |
56 | * refactored it a bit for busybox (ne-bb@nicoerfurth.de) */ |
57 | |
58 | #if ENABLE_FEATURE_EJECT_SCSI |
59 | static void eject_scsi(const char *dev) |
60 | { |
61 | static const char sg_commands[3][6] ALIGN1 = { |
62 | { 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 | }; |
66 | |
67 | unsigned i; |
68 | unsigned char sense_buffer[32]; |
69 | unsigned char inqBuff[2]; |
70 | sg_io_hdr_t io_hdr; |
71 | |
72 | 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"); |
74 | |
75 | 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; |
84 | |
85 | for (i = 0; i < 3; i++) { |
86 | io_hdr.cmdp = (void *)sg_commands[i]; |
87 | 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); |
92 | } |
93 | #else |
94 | # define eject_scsi(dev) ((void)0) |
95 | #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 |
103 | |
104 | #define FLAG_CLOSE 1 |
105 | #define FLAG_SMART 2 |
106 | #define FLAG_SCSI 4 |
107 | |
108 | static void eject_cdrom(unsigned flags, const char *dev) |
109 | { |
110 | int cmd = CDROMEJECT; |
111 | |
112 | if (flags & FLAG_CLOSE |
113 | || ((flags & FLAG_SMART) && ioctl(dev_fd, CDROM_DRIVE_STATUS) == CDS_TRAY_OPEN) |
114 | ) { |
115 | cmd = CDROMCLOSETRAY; |
116 | } |
117 | |
118 | ioctl_or_perror_and_die(dev_fd, cmd, NULL, "%s", dev); |
119 | } |
120 | |
121 | int eject_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
122 | int eject_main(int argc UNUSED_PARAM, char **argv) |
123 | { |
124 | unsigned flags; |
125 | const char *device; |
126 | |
127 | opt_complementary = "?1:t--T:T--t"; |
128 | flags = getopt32(argv, "tT" IF_FEATURE_EJECT_SCSI("s")); |
129 | device = argv[optind] ? argv[optind] : "/dev/cdrom"; |
130 | |
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 |
138 | eject /dev/cdrom |
139 | */ |
140 | |
141 | xmove_fd(xopen_nonblocking(device), dev_fd); |
142 | |
143 | if (ENABLE_FEATURE_EJECT_SCSI && (flags & FLAG_SCSI)) |
144 | eject_scsi(device); |
145 | else |
146 | eject_cdrom(flags, device); |
147 | |
148 | if (ENABLE_FEATURE_CLEAN_UP) |
149 | close(dev_fd); |
150 | |
151 | return EXIT_SUCCESS; |
152 | } |
153 |