blob: 282090d38e9c6399ce5b0ceeba85395436674afa [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
Rob Landleydfba7412006-03-06 20:47:33 +000024int eject_main(int argc, char **argv)
Mike Frysinger55e2cf62005-05-11 00:25:47 +000025{
26 unsigned long flags;
Rob Landley4079b002005-05-15 01:32:47 +000027 char *device;
28 struct mntent *m;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000029
Mike Frysinger55e2cf62005-05-11 00:25:47 +000030 flags = bb_getopt_ulflags(argc, argv, "t");
Rob Landley2fe4eac2006-02-28 04:45:24 +000031 device = argv[optind] ? : DEFAULT_CDROM;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000032
Rob Landley2fe4eac2006-02-28 04:45:24 +000033 if ((m = find_mount_point(device, bb_path_mtab_file))) {
34 if (umount(m->mnt_dir)) {
Rob Landley4079b002005-05-15 01:32:47 +000035 bb_error_msg_and_die("Can't umount");
Rob Landley2fe4eac2006-02-28 04:45:24 +000036 } else if (ENABLE_FEATURE_MTAB_SUPPORT) {
37 erase_mtab(m->mnt_fsname);
38 }
Rob Landley4079b002005-05-15 01:32:47 +000039 }
Rob Landley86b4d642006-08-03 17:58:17 +000040 if (ioctl(xopen(device, (O_RDONLY | O_NONBLOCK)),
Rob Landley2fe4eac2006-02-28 04:45:24 +000041 (flags ? CDROMCLOSETRAY : CDROMEJECT))) {
"Vladimir N. Oleynik"73804d62006-02-28 08:23:27 +000042 bb_perror_msg_and_die("%s", device);
Mike Frysinger55e2cf62005-05-11 00:25:47 +000043 }
Rob Landley2fe4eac2006-02-28 04:45:24 +000044 return (EXIT_SUCCESS);
Mike Frysinger55e2cf62005-05-11 00:25:47 +000045}