Bernhard Reutner-Fischer | c89982d | 2006-06-03 19:49:21 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Mike Frysinger | 55e2cf6 | 2005-05-11 00:25:47 +0000 | [diff] [blame] | 2 | /* |
| 3 | * eject implementation for busybox |
| 4 | * |
| 5 | * Copyright (C) 2004 Peter Willis <psyphreak@phreaker.net> |
Rob Landley | 2fe4eac | 2006-02-28 04:45:24 +0000 | [diff] [blame] | 6 | * Copyright (C) 2005 Tito Ragusa <farmatito@tiscali.it> |
Mike Frysinger | 55e2cf6 | 2005-05-11 00:25:47 +0000 | [diff] [blame] | 7 | * |
Rob Landley | 2fe4eac | 2006-02-28 04:45:24 +0000 | [diff] [blame] | 8 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Mike Frysinger | 55e2cf6 | 2005-05-11 00:25:47 +0000 | [diff] [blame] | 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 | |
Bernhard Reutner-Fischer | c89982d | 2006-06-03 19:49:21 +0000 | [diff] [blame] | 16 | #include "busybox.h" |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 17 | #include <mntent.h> |
Mike Frysinger | 55e2cf6 | 2005-05-11 00:25:47 +0000 | [diff] [blame] | 18 | |
| 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 Landley | 9ea8836 | 2005-05-14 00:46:18 +0000 | [diff] [blame] | 23 | |
Denis Vlasenko | 2e864cd | 2006-10-02 20:49:25 +0000 | [diff] [blame^] | 24 | #define FLAG_CLOSE 1 |
| 25 | #define FLAG_SMART 2 |
| 26 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 27 | int eject_main(int argc, char **argv) |
Mike Frysinger | 55e2cf6 | 2005-05-11 00:25:47 +0000 | [diff] [blame] | 28 | { |
| 29 | unsigned long flags; |
Rob Landley | 4079b00 | 2005-05-15 01:32:47 +0000 | [diff] [blame] | 30 | char *device; |
| 31 | struct mntent *m; |
Denis Vlasenko | 2e864cd | 2006-10-02 20:49:25 +0000 | [diff] [blame^] | 32 | int dev; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 33 | |
Denis Vlasenko | 2e864cd | 2006-10-02 20:49:25 +0000 | [diff] [blame^] | 34 | /*bb_opt_complementally = "t--T:T--t";*/ |
| 35 | flags = bb_getopt_ulflags(argc, argv, "tT"); |
Rob Landley | 2fe4eac | 2006-02-28 04:45:24 +0000 | [diff] [blame] | 36 | device = argv[optind] ? : DEFAULT_CDROM; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 37 | |
Denis Vlasenko | 2e864cd | 2006-10-02 20:49:25 +0000 | [diff] [blame^] | 38 | m = find_mount_point(device, bb_path_mtab_file); |
| 39 | if (m) { |
Rob Landley | 2fe4eac | 2006-02-28 04:45:24 +0000 | [diff] [blame] | 40 | if (umount(m->mnt_dir)) { |
Denis Vlasenko | 2e864cd | 2006-10-02 20:49:25 +0000 | [diff] [blame^] | 41 | bb_error_msg_and_die("can't umount"); |
Rob Landley | 2fe4eac | 2006-02-28 04:45:24 +0000 | [diff] [blame] | 42 | } else if (ENABLE_FEATURE_MTAB_SUPPORT) { |
| 43 | erase_mtab(m->mnt_fsname); |
| 44 | } |
Rob Landley | 4079b00 | 2005-05-15 01:32:47 +0000 | [diff] [blame] | 45 | } |
Denis Vlasenko | 2e864cd | 2006-10-02 20:49:25 +0000 | [diff] [blame^] | 46 | |
| 47 | dev = xopen(device, O_RDONLY|O_NONBLOCK); |
| 48 | |
| 49 | if (flags & FLAG_CLOSE) goto close_tray; |
| 50 | |
| 51 | if (ioctl(dev, CDROMEJECT)) { |
| 52 | close_tray: |
| 53 | if (ioctl(dev, CDROMCLOSETRAY)) |
| 54 | bb_perror_msg_and_die("%s", device); |
Mike Frysinger | 55e2cf6 | 2005-05-11 00:25:47 +0000 | [diff] [blame] | 55 | } |
Denis Vlasenko | 2e864cd | 2006-10-02 20:49:25 +0000 | [diff] [blame^] | 56 | |
| 57 | if (ENABLE_FEATURE_CLEAN_UP) close(dev); |
| 58 | |
| 59 | return EXIT_SUCCESS; |
Mike Frysinger | 55e2cf6 | 2005-05-11 00:25:47 +0000 | [diff] [blame] | 60 | } |