blob: a402e49c64a12ada9fe128bdde433d8be582415b [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#include <fcntl.h>
18#include <sys/ioctl.h>
Rob Landley9ea88362005-05-14 00:46:18 +000019#include <unistd.h>
Rob Landley4079b002005-05-15 01:32:47 +000020#include <sys/mount.h>
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000021#include <mntent.h>
Mike Frysinger55e2cf62005-05-11 00:25:47 +000022
23/* various defines swiped from linux/cdrom.h */
24#define CDROMCLOSETRAY 0x5319 /* pendant of CDROMEJECT */
25#define CDROMEJECT 0x5309 /* Ejects the cdrom media */
26#define DEFAULT_CDROM "/dev/cdrom"
Rob Landley9ea88362005-05-14 00:46:18 +000027
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;
32 struct mntent *m;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000033
Mike Frysinger55e2cf62005-05-11 00:25:47 +000034 flags = bb_getopt_ulflags(argc, argv, "t");
Rob Landley2fe4eac2006-02-28 04:45:24 +000035 device = argv[optind] ? : DEFAULT_CDROM;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000036
Rob Landley2fe4eac2006-02-28 04:45:24 +000037 if ((m = find_mount_point(device, bb_path_mtab_file))) {
38 if (umount(m->mnt_dir)) {
Rob Landley4079b002005-05-15 01:32:47 +000039 bb_error_msg_and_die("Can't umount");
Rob Landley2fe4eac2006-02-28 04:45:24 +000040 } else if (ENABLE_FEATURE_MTAB_SUPPORT) {
41 erase_mtab(m->mnt_fsname);
42 }
Rob Landley4079b002005-05-15 01:32:47 +000043 }
Rob Landley2fe4eac2006-02-28 04:45:24 +000044 if (ioctl(bb_xopen(device, (O_RDONLY | O_NONBLOCK)),
45 (flags ? CDROMCLOSETRAY : CDROMEJECT))) {
"Vladimir N. Oleynik"73804d62006-02-28 08:23:27 +000046 bb_perror_msg_and_die("%s", device);
Mike Frysinger55e2cf62005-05-11 00:25:47 +000047 }
Rob Landley2fe4eac2006-02-28 04:45:24 +000048 return (EXIT_SUCCESS);
Mike Frysinger55e2cf62005-05-11 00:25:47 +000049}