add and use xopen_nonblocking (-18b)
[oweals/busybox.git] / miscutils / eject.c
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 the GPL v2 or later, see the file LICENSE in this tarball.
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
16 #include <sys/mount.h>
17 #include "libbb.h"
18 /* Must be after libbb.h: they need size_t */
19 #include <scsi/sg.h>
20 #include <scsi/scsi.h>
21
22 /* various defines swiped from linux/cdrom.h */
23 #define CDROMCLOSETRAY            0x5319  /* pendant of CDROMEJECT  */
24 #define CDROMEJECT                0x5309  /* Ejects the cdrom media */
25 #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
28
29 #define dev_fd 3
30
31 /* Code taken from the original eject (http://eject.sourceforge.net/),
32  * refactored it a bit for busybox (ne-bb@nicoerfurth.de) */
33
34 static void eject_scsi(const char *dev)
35 {
36         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         };
41
42         unsigned i;
43         unsigned char sense_buffer[32];
44         unsigned char inqBuff[2];
45         sg_io_hdr_t io_hdr;
46
47         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");
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 = (void *)sg_commands[i];
62                 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);
67 }
68
69 #define FLAG_CLOSE  1
70 #define FLAG_SMART  2
71 #define FLAG_SCSI   4
72
73 static void eject_cdrom(unsigned flags, const char *dev)
74 {
75         int cmd = CDROMEJECT;
76
77         if (flags & FLAG_CLOSE
78          || (flags & FLAG_SMART && ioctl(dev_fd, CDROM_DRIVE_STATUS) == CDS_TRAY_OPEN)
79         ) {
80                 cmd = CDROMCLOSETRAY;
81         }
82
83         ioctl_or_perror_and_die(dev_fd, cmd, NULL, "%s", dev);
84 }
85
86 int eject_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
87 int eject_main(int argc UNUSED_PARAM, char **argv)
88 {
89         unsigned flags;
90         const char *device;
91
92         opt_complementary = "?1:t--T:T--t";
93         flags = getopt32(argv, "tT" IF_FEATURE_EJECT_SCSI("s"));
94         device = argv[optind] ? argv[optind] : "/dev/cdrom";
95
96         /* 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
103            eject /dev/cdrom
104         */
105
106         xmove_fd(xopen_nonblocking(device), dev_fd);
107
108         if (ENABLE_FEATURE_EJECT_SCSI && (flags & FLAG_SCSI))
109                 eject_scsi(device);
110         else
111                 eject_cdrom(flags, device);
112
113         if (ENABLE_FEATURE_CLEAN_UP)
114                 close(dev_fd);
115
116         return EXIT_SUCCESS;
117 }