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 | |
Bernhard Reutner-Fischer | c89982d | 2006-06-03 19:49:21 +0000 | [diff] [blame] | 16 | #include "busybox.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 | 2e864cd | 2006-10-02 20:49:25 +0000 | [diff] [blame] | 25 | #define FLAG_CLOSE 1 |
| 26 | #define FLAG_SMART 2 |
| 27 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 28 | int eject_main(int argc, char **argv); |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 29 | int eject_main(int argc, char **argv) |
Mike Frysinger | 55e2cf6 | 2005-05-11 00:25:47 +0000 | [diff] [blame] | 30 | { |
| 31 | unsigned long flags; |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 32 | const char *device; |
Denis Vlasenko | 000b9ba | 2006-10-05 23:12:49 +0000 | [diff] [blame] | 33 | int dev, cmd; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 34 | |
Denis Vlasenko | 000b9ba | 2006-10-05 23:12:49 +0000 | [diff] [blame] | 35 | opt_complementary = "?:?1:t--T:T--t"; |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 36 | flags = getopt32(argc, argv, "tT"); |
Denis Vlasenko | 000b9ba | 2006-10-05 23:12:49 +0000 | [diff] [blame] | 37 | device = argv[optind] ? : "/dev/cdrom"; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 38 | |
Denis Vlasenko | f7a5784 | 2006-10-07 15:00:29 +0000 | [diff] [blame] | 39 | // We used to do "umount <device>" here, but it was buggy |
| 40 | // if something was mounted OVER cdrom and |
| 41 | // if cdrom is mounted many times. |
| 42 | // |
| 43 | // This works equally well (or better): |
| 44 | // #!/bin/sh |
| 45 | // umount /dev/cdrom |
| 46 | // eject |
Denis Vlasenko | 2e864cd | 2006-10-02 20:49:25 +0000 | [diff] [blame] | 47 | |
| 48 | dev = xopen(device, O_RDONLY|O_NONBLOCK); |
Denis Vlasenko | 000b9ba | 2006-10-05 23:12:49 +0000 | [diff] [blame] | 49 | cmd = CDROMEJECT; |
| 50 | if (flags & FLAG_CLOSE |
| 51 | || (flags & FLAG_SMART && ioctl(dev, CDROM_DRIVE_STATUS) == CDS_TRAY_OPEN)) |
| 52 | cmd = CDROMCLOSETRAY; |
| 53 | if (ioctl(dev, cmd)) { |
| 54 | bb_perror_msg_and_die("%s", device); |
Mike Frysinger | 55e2cf6 | 2005-05-11 00:25:47 +0000 | [diff] [blame] | 55 | } |
Denis Vlasenko | 2e864cd | 2006-10-02 20:49:25 +0000 | [diff] [blame] | 56 | |
Denis Vlasenko | 000b9ba | 2006-10-05 23:12:49 +0000 | [diff] [blame] | 57 | if (ENABLE_FEATURE_CLEAN_UP) |
| 58 | close(dev); |
Denis Vlasenko | 2e864cd | 2006-10-02 20:49:25 +0000 | [diff] [blame] | 59 | |
| 60 | return EXIT_SUCCESS; |
Mike Frysinger | 55e2cf6 | 2005-05-11 00:25:47 +0000 | [diff] [blame] | 61 | } |