blob: 5d7ef1c28ddabbcdff19cd4814f023ce5c2fa95d [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
Denis Vlasenko06af2162007-02-03 17:28:39 +000028int eject_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +000029int eject_main(int argc, char **argv)
Mike Frysinger55e2cf62005-05-11 00:25:47 +000030{
31 unsigned long flags;
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000032 const char *device;
Denis Vlasenko000b9ba2006-10-05 23:12:49 +000033 int dev, cmd;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000034
Denis Vlasenko000b9ba2006-10-05 23:12:49 +000035 opt_complementary = "?:?1:t--T:T--t";
Denis Vlasenko67b23e62006-10-03 21:00:06 +000036 flags = getopt32(argc, argv, "tT");
Denis Vlasenko000b9ba2006-10-05 23:12:49 +000037 device = argv[optind] ? : "/dev/cdrom";
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000038
Denis Vlasenkof7a57842006-10-07 15:00:29 +000039 // 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 Vlasenko2e864cd2006-10-02 20:49:25 +000047
48 dev = xopen(device, O_RDONLY|O_NONBLOCK);
Denis Vlasenko000b9ba2006-10-05 23:12:49 +000049 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 Frysinger55e2cf62005-05-11 00:25:47 +000055 }
Denis Vlasenko2e864cd2006-10-02 20:49:25 +000056
Denis Vlasenko000b9ba2006-10-05 23:12:49 +000057 if (ENABLE_FEATURE_CLEAN_UP)
58 close(dev);
Denis Vlasenko2e864cd2006-10-02 20:49:25 +000059
60 return EXIT_SUCCESS;
Mike Frysinger55e2cf62005-05-11 00:25:47 +000061}