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