Annotation of /tags/mkinitrd-6_1_12/busybox/miscutils/eject.c
Parent Directory
|
Revision Log
Revision 939 -
(hide annotations)
(download)
Tue Nov 17 21:24:51 2009 UTC (14 years, 7 months ago) by niro
File MIME type: text/plain
File size: 3076 byte(s)
Tue Nov 17 21:24:51 2009 UTC (14 years, 7 months ago) by niro
File MIME type: text/plain
File size: 3076 byte(s)
tagged 'mkinitrd-6_1_12'
1 | niro | 532 | /* 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 | niro | 816 | #include "libbb.h" |
17 | niro | 532 | |
18 | /* various defines swiped from linux/cdrom.h */ | ||
19 | #define CDROMCLOSETRAY 0x5319 /* pendant of CDROMEJECT */ | ||
20 | #define CDROMEJECT 0x5309 /* Ejects the cdrom media */ | ||
21 | #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 | ||
24 | |||
25 | niro | 816 | #define dev_fd 3 |
26 | |||
27 | /* Code taken from the original eject (http://eject.sourceforge.net/), | ||
28 | * refactored it a bit for busybox (ne-bb@nicoerfurth.de) */ | ||
29 | |||
30 | #include <scsi/sg.h> | ||
31 | #include <scsi/scsi.h> | ||
32 | |||
33 | static void eject_scsi(const char *dev) | ||
34 | { | ||
35 | static const char sg_commands[3][6] = { | ||
36 | { ALLOW_MEDIUM_REMOVAL, 0, 0, 0, 0, 0 }, | ||
37 | { START_STOP, 0, 0, 0, 1, 0 }, | ||
38 | { START_STOP, 0, 0, 0, 2, 0 } | ||
39 | }; | ||
40 | |||
41 | unsigned i; | ||
42 | unsigned char sense_buffer[32]; | ||
43 | unsigned char inqBuff[2]; | ||
44 | sg_io_hdr_t io_hdr; | ||
45 | |||
46 | if ((ioctl(dev_fd, SG_GET_VERSION_NUM, &i) < 0) || (i < 30000)) | ||
47 | bb_error_msg_and_die("not a sg device or old sg driver"); | ||
48 | |||
49 | memset(&io_hdr, 0, sizeof(sg_io_hdr_t)); | ||
50 | io_hdr.interface_id = 'S'; | ||
51 | io_hdr.cmd_len = 6; | ||
52 | io_hdr.mx_sb_len = sizeof(sense_buffer); | ||
53 | io_hdr.dxfer_direction = SG_DXFER_NONE; | ||
54 | /* io_hdr.dxfer_len = 0; */ | ||
55 | io_hdr.dxferp = inqBuff; | ||
56 | io_hdr.sbp = sense_buffer; | ||
57 | io_hdr.timeout = 2000; | ||
58 | |||
59 | for (i = 0; i < 3; i++) { | ||
60 | io_hdr.cmdp = (void *)sg_commands[i]; | ||
61 | ioctl_or_perror_and_die(dev_fd, SG_IO, (void *)&io_hdr, "%s", dev); | ||
62 | } | ||
63 | |||
64 | /* force kernel to reread partition table when new disc is inserted */ | ||
65 | ioctl(dev_fd, BLKRRPART); | ||
66 | } | ||
67 | |||
68 | niro | 532 | #define FLAG_CLOSE 1 |
69 | #define FLAG_SMART 2 | ||
70 | niro | 816 | #define FLAG_SCSI 4 |
71 | niro | 532 | |
72 | niro | 816 | static void eject_cdrom(unsigned flags, const char *dev) |
73 | niro | 532 | { |
74 | niro | 816 | int cmd = CDROMEJECT; |
75 | niro | 532 | |
76 | if (flags & FLAG_CLOSE | ||
77 | niro | 816 | || (flags & FLAG_SMART && ioctl(dev_fd, CDROM_DRIVE_STATUS) == CDS_TRAY_OPEN) |
78 | ) { | ||
79 | niro | 532 | cmd = CDROMCLOSETRAY; |
80 | } | ||
81 | |||
82 | niro | 816 | ioctl_or_perror_and_die(dev_fd, cmd, NULL, "%s", dev); |
83 | } | ||
84 | |||
85 | int eject_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | ||
86 | int eject_main(int argc UNUSED_PARAM, char **argv) | ||
87 | { | ||
88 | unsigned flags; | ||
89 | const char *device; | ||
90 | |||
91 | opt_complementary = "?1:t--T:T--t"; | ||
92 | flags = getopt32(argv, "tT" USE_FEATURE_EJECT_SCSI("s")); | ||
93 | device = argv[optind] ? argv[optind] : "/dev/cdrom"; | ||
94 | |||
95 | /* We used to do "umount <device>" here, but it was buggy | ||
96 | if something was mounted OVER cdrom and | ||
97 | if cdrom is mounted many times. | ||
98 | |||
99 | This works equally well (or better): | ||
100 | #!/bin/sh | ||
101 | umount /dev/cdrom | ||
102 | eject /dev/cdrom | ||
103 | */ | ||
104 | |||
105 | xmove_fd(xopen(device, O_RDONLY|O_NONBLOCK), dev_fd); | ||
106 | |||
107 | if (ENABLE_FEATURE_EJECT_SCSI && (flags & FLAG_SCSI)) | ||
108 | eject_scsi(device); | ||
109 | else | ||
110 | eject_cdrom(flags, device); | ||
111 | |||
112 | niro | 532 | if (ENABLE_FEATURE_CLEAN_UP) |
113 | niro | 816 | close(dev_fd); |
114 | niro | 532 | |
115 | return EXIT_SUCCESS; | ||
116 | } |