blob: 272d95980c34d85aca0afb1f8d44f8f2ef93f655 [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"
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000017#include <mntent.h>
Mike Frysinger55e2cf62005-05-11 00:25:47 +000018
19/* various defines swiped from linux/cdrom.h */
20#define CDROMCLOSETRAY 0x5319 /* pendant of CDROMEJECT */
21#define CDROMEJECT 0x5309 /* Ejects the cdrom media */
22#define DEFAULT_CDROM "/dev/cdrom"
Rob Landley9ea88362005-05-14 00:46:18 +000023
Denis Vlasenko2e864cd2006-10-02 20:49:25 +000024#define FLAG_CLOSE 1
25#define FLAG_SMART 2
26
Rob Landleydfba7412006-03-06 20:47:33 +000027int eject_main(int argc, char **argv)
Mike Frysinger55e2cf62005-05-11 00:25:47 +000028{
29 unsigned long flags;
Rob Landley4079b002005-05-15 01:32:47 +000030 char *device;
31 struct mntent *m;
Denis Vlasenko2e864cd2006-10-02 20:49:25 +000032 int dev;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000033
Denis Vlasenko2e864cd2006-10-02 20:49:25 +000034 /*bb_opt_complementally = "t--T:T--t";*/
35 flags = bb_getopt_ulflags(argc, argv, "tT");
Rob Landley2fe4eac2006-02-28 04:45:24 +000036 device = argv[optind] ? : DEFAULT_CDROM;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000037
Denis Vlasenko2e864cd2006-10-02 20:49:25 +000038 m = find_mount_point(device, bb_path_mtab_file);
39 if (m) {
Rob Landley2fe4eac2006-02-28 04:45:24 +000040 if (umount(m->mnt_dir)) {
Denis Vlasenko2e864cd2006-10-02 20:49:25 +000041 bb_error_msg_and_die("can't umount");
Rob Landley2fe4eac2006-02-28 04:45:24 +000042 } else if (ENABLE_FEATURE_MTAB_SUPPORT) {
43 erase_mtab(m->mnt_fsname);
44 }
Rob Landley4079b002005-05-15 01:32:47 +000045 }
Denis Vlasenko2e864cd2006-10-02 20:49:25 +000046
47 dev = xopen(device, O_RDONLY|O_NONBLOCK);
48
49 if (flags & FLAG_CLOSE) goto close_tray;
50
51 if (ioctl(dev, CDROMEJECT)) {
52close_tray:
53 if (ioctl(dev, CDROMCLOSETRAY))
54 bb_perror_msg_and_die("%s", device);
Mike Frysinger55e2cf62005-05-11 00:25:47 +000055 }
Denis Vlasenko2e864cd2006-10-02 20:49:25 +000056
57 if (ENABLE_FEATURE_CLEAN_UP) close(dev);
58
59 return EXIT_SUCCESS;
Mike Frysinger55e2cf62005-05-11 00:25:47 +000060}