Bernhard Reutner-Fischer | c89982d | 2006-06-03 19:49:21 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Mike Frysinger | 55e2cf6 | 2005-05-11 00:25:47 +0000 | [diff] [blame] | 2 | /* |
| 3 | * eject implementation for busybox |
| 4 | * |
| 5 | * Copyright (C) 2004 Peter Willis <psyphreak@phreaker.net> |
Rob Landley | 2fe4eac | 2006-02-28 04:45:24 +0000 | [diff] [blame] | 6 | * Copyright (C) 2005 Tito Ragusa <farmatito@tiscali.it> |
Mike Frysinger | 55e2cf6 | 2005-05-11 00:25:47 +0000 | [diff] [blame] | 7 | * |
Rob Landley | 2fe4eac | 2006-02-28 04:45:24 +0000 | [diff] [blame] | 8 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Mike Frysinger | 55e2cf6 | 2005-05-11 00:25:47 +0000 | [diff] [blame] | 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 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 16 | #include "libbb.h" |
Mike Frysinger | 55e2cf6 | 2005-05-11 00:25:47 +0000 | [diff] [blame] | 17 | |
| 18 | /* various defines swiped from linux/cdrom.h */ |
| 19 | #define CDROMCLOSETRAY 0x5319 /* pendant of CDROMEJECT */ |
| 20 | #define CDROMEJECT 0x5309 /* Ejects the cdrom media */ |
Denis Vlasenko | 000b9ba | 2006-10-05 23:12:49 +0000 | [diff] [blame] | 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 |
Rob Landley | 9ea8836 | 2005-05-14 00:46:18 +0000 | [diff] [blame] | 24 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 25 | #define dev_fd 3 |
Bernhard Reutner-Fischer | afdad65 | 2008-02-08 15:04:00 +0000 | [diff] [blame] | 26 | |
| 27 | /* Code taken from the original eject (http://eject.sourceforge.net/), |
| 28 | * refactored it a bit for busybox (ne-bb@nicoerfurth.de) */ |
Bernhard Reutner-Fischer | afdad65 | 2008-02-08 15:04:00 +0000 | [diff] [blame] | 29 | |
| 30 | #include <scsi/sg.h> |
| 31 | #include <scsi/scsi.h> |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 32 | |
| 33 | static void eject_scsi(const char *dev) |
Bernhard Reutner-Fischer | afdad65 | 2008-02-08 15:04:00 +0000 | [diff] [blame] | 34 | { |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 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 | }; |
Bernhard Reutner-Fischer | afdad65 | 2008-02-08 15:04:00 +0000 | [diff] [blame] | 40 | |
Denis Vlasenko | 77ad97f | 2008-05-13 02:27:31 +0000 | [diff] [blame] | 41 | unsigned i; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 42 | unsigned char sense_buffer[32]; |
| 43 | unsigned char inqBuff[2]; |
| 44 | sg_io_hdr_t io_hdr; |
Bernhard Reutner-Fischer | afdad65 | 2008-02-08 15:04:00 +0000 | [diff] [blame] | 45 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 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"); |
Bernhard Reutner-Fischer | afdad65 | 2008-02-08 15:04:00 +0000 | [diff] [blame] | 48 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 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; |
Bernhard Reutner-Fischer | afdad65 | 2008-02-08 15:04:00 +0000 | [diff] [blame] | 58 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 59 | for (i = 0; i < 3; i++) { |
Denis Vlasenko | c4f12f5 | 2008-05-12 14:35:56 +0000 | [diff] [blame] | 60 | io_hdr.cmdp = (void *)sg_commands[i]; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 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); |
Bernhard Reutner-Fischer | afdad65 | 2008-02-08 15:04:00 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 68 | #define FLAG_CLOSE 1 |
| 69 | #define FLAG_SMART 2 |
| 70 | #define FLAG_SCSI 4 |
| 71 | |
| 72 | static void eject_cdrom(unsigned flags, const char *dev) |
Bernhard Reutner-Fischer | afdad65 | 2008-02-08 15:04:00 +0000 | [diff] [blame] | 73 | { |
| 74 | int cmd = CDROMEJECT; |
| 75 | |
| 76 | if (flags & FLAG_CLOSE |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 77 | || (flags & FLAG_SMART && ioctl(dev_fd, CDROM_DRIVE_STATUS) == CDS_TRAY_OPEN) |
| 78 | ) { |
Bernhard Reutner-Fischer | afdad65 | 2008-02-08 15:04:00 +0000 | [diff] [blame] | 79 | cmd = CDROMCLOSETRAY; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 80 | } |
Bernhard Reutner-Fischer | afdad65 | 2008-02-08 15:04:00 +0000 | [diff] [blame] | 81 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 82 | ioctl_or_perror_and_die(dev_fd, cmd, NULL, "%s", dev); |
Bernhard Reutner-Fischer | afdad65 | 2008-02-08 15:04:00 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 85 | int eject_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 86 | int eject_main(int argc ATTRIBUTE_UNUSED, char **argv) |
Mike Frysinger | 55e2cf6 | 2005-05-11 00:25:47 +0000 | [diff] [blame] | 87 | { |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 88 | unsigned flags; |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 89 | const char *device; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 90 | |
Denis Vlasenko | 0919657 | 2007-07-21 13:27:44 +0000 | [diff] [blame] | 91 | opt_complementary = "?1:t--T:T--t"; |
Bernhard Reutner-Fischer | afdad65 | 2008-02-08 15:04:00 +0000 | [diff] [blame] | 92 | flags = getopt32(argv, "tT" USE_FEATURE_EJECT_SCSI("s")); |
| 93 | device = argv[optind] ? argv[optind] : "/dev/cdrom"; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 94 | |
Bernhard Reutner-Fischer | afdad65 | 2008-02-08 15:04:00 +0000 | [diff] [blame] | 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 |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 102 | eject /dev/cdrom |
Bernhard Reutner-Fischer | afdad65 | 2008-02-08 15:04:00 +0000 | [diff] [blame] | 103 | */ |
Denis Vlasenko | 2e864cd | 2006-10-02 20:49:25 +0000 | [diff] [blame] | 104 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 105 | xmove_fd(xopen(device, O_RDONLY|O_NONBLOCK), dev_fd); |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 106 | |
Bernhard Reutner-Fischer | afdad65 | 2008-02-08 15:04:00 +0000 | [diff] [blame] | 107 | if (ENABLE_FEATURE_EJECT_SCSI && (flags & FLAG_SCSI)) |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 108 | eject_scsi(device); |
Bernhard Reutner-Fischer | afdad65 | 2008-02-08 15:04:00 +0000 | [diff] [blame] | 109 | else |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 110 | eject_cdrom(flags, device); |
Denis Vlasenko | 2e864cd | 2006-10-02 20:49:25 +0000 | [diff] [blame] | 111 | |
Denis Vlasenko | 000b9ba | 2006-10-05 23:12:49 +0000 | [diff] [blame] | 112 | if (ENABLE_FEATURE_CLEAN_UP) |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 113 | close(dev_fd); |
Denis Vlasenko | 2e864cd | 2006-10-02 20:49:25 +0000 | [diff] [blame] | 114 | |
| 115 | return EXIT_SUCCESS; |
Mike Frysinger | 55e2cf6 | 2005-05-11 00:25:47 +0000 | [diff] [blame] | 116 | } |