blob: 99f01aba9256af7fc3842cfc51372bf481d083a7 [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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000016#include "libbb.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 Vlasenko9b49a5e2007-10-11 10:05:36 +000028int eject_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
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 Vlasenko09196572007-07-21 13:27:44 +000035 opt_complementary = "?1:t--T:T--t";
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000036 flags = getopt32(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;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000053
54 ioctl_or_perror_and_die(dev, cmd, NULL, "%s", device);
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}