ubiupdatevol: new applet
[platform/upstream/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 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
16 //usage:#define eject_trivial_usage
17 //usage:       "[-t] [-T] [DEVICE]"
18 //usage:#define eject_full_usage "\n\n"
19 //usage:       "Eject DEVICE or default /dev/cdrom\n"
20 //usage:     "\nOptions:"
21 //usage:        IF_FEATURE_EJECT_SCSI(
22 //usage:     "\n        -s      SCSI device"
23 //usage:        )
24 //usage:     "\n        -t      Close tray"
25 //usage:     "\n        -T      Open/close tray (toggle)"
26
27 #include <sys/mount.h>
28 #include "libbb.h"
29 /* Must be after libbb.h: they need size_t */
30 #include "fix_u32.h"
31 #include <scsi/sg.h>
32 #include <scsi/scsi.h>
33
34 /* various defines swiped from linux/cdrom.h */
35 #define CDROMCLOSETRAY            0x5319  /* pendant of CDROMEJECT  */
36 #define CDROMEJECT                0x5309  /* Ejects the cdrom media */
37 #define CDROM_DRIVE_STATUS        0x5326  /* Get tray position, etc. */
38 /* drive status possibilities returned by CDROM_DRIVE_STATUS ioctl */
39 #define CDS_TRAY_OPEN        2
40
41 #define dev_fd 3
42
43 /* Code taken from the original eject (http://eject.sourceforge.net/),
44  * refactored it a bit for busybox (ne-bb@nicoerfurth.de) */
45
46 static void eject_scsi(const char *dev)
47 {
48         static const char sg_commands[3][6] = {
49                 { ALLOW_MEDIUM_REMOVAL, 0, 0, 0, 0, 0 },
50                 { START_STOP, 0, 0, 0, 1, 0 },
51                 { START_STOP, 0, 0, 0, 2, 0 }
52         };
53
54         unsigned i;
55         unsigned char sense_buffer[32];
56         unsigned char inqBuff[2];
57         sg_io_hdr_t io_hdr;
58
59         if ((ioctl(dev_fd, SG_GET_VERSION_NUM, &i) < 0) || (i < 30000))
60                 bb_error_msg_and_die("not a sg device or old sg driver");
61
62         memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
63         io_hdr.interface_id = 'S';
64         io_hdr.cmd_len = 6;
65         io_hdr.mx_sb_len = sizeof(sense_buffer);
66         io_hdr.dxfer_direction = SG_DXFER_NONE;
67         /* io_hdr.dxfer_len = 0; */
68         io_hdr.dxferp = inqBuff;
69         io_hdr.sbp = sense_buffer;
70         io_hdr.timeout = 2000;
71
72         for (i = 0; i < 3; i++) {
73                 io_hdr.cmdp = (void *)sg_commands[i];
74                 ioctl_or_perror_and_die(dev_fd, SG_IO, (void *)&io_hdr, "%s", dev);
75         }
76
77         /* force kernel to reread partition table when new disc is inserted */
78         ioctl(dev_fd, BLKRRPART);
79 }
80
81 #define FLAG_CLOSE  1
82 #define FLAG_SMART  2
83 #define FLAG_SCSI   4
84
85 static void eject_cdrom(unsigned flags, const char *dev)
86 {
87         int cmd = CDROMEJECT;
88
89         if (flags & FLAG_CLOSE
90          || ((flags & FLAG_SMART) && ioctl(dev_fd, CDROM_DRIVE_STATUS) == CDS_TRAY_OPEN)
91         ) {
92                 cmd = CDROMCLOSETRAY;
93         }
94
95         ioctl_or_perror_and_die(dev_fd, cmd, NULL, "%s", dev);
96 }
97
98 int eject_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
99 int eject_main(int argc UNUSED_PARAM, char **argv)
100 {
101         unsigned flags;
102         const char *device;
103
104         opt_complementary = "?1:t--T:T--t";
105         flags = getopt32(argv, "tT" IF_FEATURE_EJECT_SCSI("s"));
106         device = argv[optind] ? argv[optind] : "/dev/cdrom";
107
108         /* We used to do "umount <device>" here, but it was buggy
109            if something was mounted OVER cdrom and
110            if cdrom is mounted many times.
111
112            This works equally well (or better):
113            #!/bin/sh
114            umount /dev/cdrom
115            eject /dev/cdrom
116         */
117
118         xmove_fd(xopen_nonblocking(device), dev_fd);
119
120         if (ENABLE_FEATURE_EJECT_SCSI && (flags & FLAG_SCSI))
121                 eject_scsi(device);
122         else
123                 eject_cdrom(flags, device);
124
125         if (ENABLE_FEATURE_CLEAN_UP)
126                 close(dev_fd);
127
128         return EXIT_SUCCESS;
129 }