3
0

eject.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. * eject implementation for busybox
  3. *
  4. * Copyright (C) 2004 Peter Willis <psyphreak@phreaker.net>
  5. * Copyright (C) 2005 Tito Ragusa <farmatito@tiscali.it>
  6. *
  7. * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  8. *
  9. */
  10. /*
  11. * This is a simple hack of eject based on something Erik posted in #uclibc.
  12. * Most of the dirty work blatantly ripped off from cat.c =)
  13. */
  14. #include <fcntl.h>
  15. #include <sys/ioctl.h>
  16. #include <unistd.h>
  17. #include <sys/mount.h>
  18. #include <mntent.h>
  19. #include "busybox.h"
  20. /* various defines swiped from linux/cdrom.h */
  21. #define CDROMCLOSETRAY 0x5319 /* pendant of CDROMEJECT */
  22. #define CDROMEJECT 0x5309 /* Ejects the cdrom media */
  23. #define DEFAULT_CDROM "/dev/cdrom"
  24. int eject_main(int argc, char **argv)
  25. {
  26. unsigned long flags;
  27. char *device;
  28. struct mntent *m;
  29. flags = bb_getopt_ulflags(argc, argv, "t");
  30. device = argv[optind] ? : DEFAULT_CDROM;
  31. if ((m = find_mount_point(device, bb_path_mtab_file))) {
  32. if (umount(m->mnt_dir)) {
  33. bb_error_msg_and_die("Can't umount");
  34. } else if (ENABLE_FEATURE_MTAB_SUPPORT) {
  35. erase_mtab(m->mnt_fsname);
  36. }
  37. }
  38. if (ioctl(bb_xopen(device, (O_RDONLY | O_NONBLOCK)),
  39. (flags ? CDROMCLOSETRAY : CDROMEJECT))) {
  40. bb_perror_msg_and_die("%s", device);
  41. }
  42. return (EXIT_SUCCESS);
  43. }