blob: ff23b1666fbcee845c12cc0af45ccfcea0333b1b [file] [log] [blame]
Bernhard Reutner-Fischerc89982d2006-06-03 19:49:21 +00001/* vi: set sw=4 ts=4: */
Mike Frysinger55e2cf62005-05-11 00:25:47 +00002/*
3 * eject implementation for busybox
4 *
5 * Copyright (C) 2004 Peter Willis <psyphreak@phreaker.net>
Rob Landley2fe4eac2006-02-28 04:45:24 +00006 * Copyright (C) 2005 Tito Ragusa <farmatito@tiscali.it>
Mike Frysinger55e2cf62005-05-11 00:25:47 +00007 *
Rob Landley2fe4eac2006-02-28 04:45:24 +00008 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Mike Frysinger55e2cf62005-05-11 00:25:47 +00009 */
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-Fischerc89982d2006-06-03 19:49:21 +000016#include "busybox.h"
Mike Frysinger55e2cf62005-05-11 00:25:47 +000017
18/* various defines swiped from linux/cdrom.h */
19#define CDROMCLOSETRAY 0x5319 /* pendant of CDROMEJECT */
20#define CDROMEJECT 0x5309 /* Ejects the cdrom media */
Denis Vlasenko000b9ba2006-10-05 23:12:49 +000021#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 Landley9ea88362005-05-14 00:46:18 +000024
Denis Vlasenko2e864cd2006-10-02 20:49:25 +000025#define FLAG_CLOSE 1
26#define FLAG_SMART 2
27
Rob Landleydfba7412006-03-06 20:47:33 +000028int eject_main(int argc, char **argv)
Mike Frysinger55e2cf62005-05-11 00:25:47 +000029{
30 unsigned long flags;
Rob Landley4079b002005-05-15 01:32:47 +000031 char *device;
Denis Vlasenko000b9ba2006-10-05 23:12:49 +000032 int dev, cmd;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000033
Denis Vlasenko000b9ba2006-10-05 23:12:49 +000034 opt_complementary = "?:?1:t--T:T--t";
Denis Vlasenko67b23e62006-10-03 21:00:06 +000035 flags = getopt32(argc, argv, "tT");
Denis Vlasenko000b9ba2006-10-05 23:12:49 +000036 device = argv[optind] ? : "/dev/cdrom";
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000037
Denis Vlasenkof7a57842006-10-07 15:00:29 +000038 // 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 Vlasenko2e864cd2006-10-02 20:49:25 +000046
47 dev = xopen(device, O_RDONLY|O_NONBLOCK);
Denis Vlasenko000b9ba2006-10-05 23:12:49 +000048 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 Frysinger55e2cf62005-05-11 00:25:47 +000054 }
Denis Vlasenko2e864cd2006-10-02 20:49:25 +000055
Denis Vlasenko000b9ba2006-10-05 23:12:49 +000056 if (ENABLE_FEATURE_CLEAN_UP)
57 close(dev);
Denis Vlasenko2e864cd2006-10-02 20:49:25 +000058
59 return EXIT_SUCCESS;
Mike Frysinger55e2cf62005-05-11 00:25:47 +000060}