blob: d4548628d7d4907b39b0910cc527d7770a87d667 [file] [log] [blame]
Mike Frysinger55e2cf62005-05-11 00:25:47 +00001/*
2 * eject implementation for busybox
3 *
4 * Copyright (C) 2004 Peter Willis <psyphreak@phreaker.net>
Rob Landley2fe4eac2006-02-28 04:45:24 +00005 * Copyright (C) 2005 Tito Ragusa <farmatito@tiscali.it>
Mike Frysinger55e2cf62005-05-11 00:25:47 +00006 *
Rob Landley2fe4eac2006-02-28 04:45:24 +00007 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Mike Frysinger55e2cf62005-05-11 00:25:47 +00008 *
9 */
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
Mike Frysinger55e2cf62005-05-11 00:25:47 +000016#include <fcntl.h>
17#include <sys/ioctl.h>
Rob Landley9ea88362005-05-14 00:46:18 +000018#include <unistd.h>
Rob Landley4079b002005-05-15 01:32:47 +000019#include <sys/mount.h>
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000020#include <mntent.h>
Mike Frysinger55e2cf62005-05-11 00:25:47 +000021#include "busybox.h"
22
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}