eject.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * eject implementation for busybox
  4. *
  5. * Copyright (C) 2004 Peter Willis <psyphreak@phreaker.net>
  6. * Copyright (C) 2005 Tito Ragusa <farmatito@tiscali.it>
  7. *
  8. * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  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 "libbb.h"
  15. /* various defines swiped from linux/cdrom.h */
  16. #define CDROMCLOSETRAY 0x5319 /* pendant of CDROMEJECT */
  17. #define CDROMEJECT 0x5309 /* Ejects the cdrom media */
  18. #define CDROM_DRIVE_STATUS 0x5326 /* Get tray position, etc. */
  19. /* drive status possibilities returned by CDROM_DRIVE_STATUS ioctl */
  20. #define CDS_TRAY_OPEN 2
  21. #define FLAG_CLOSE 1
  22. #define FLAG_SMART 2
  23. int eject_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  24. int eject_main(int argc, char **argv)
  25. {
  26. unsigned long flags;
  27. const char *device;
  28. int dev, cmd;
  29. opt_complementary = "?1:t--T:T--t";
  30. flags = getopt32(argv, "tT");
  31. device = argv[optind] ? : "/dev/cdrom";
  32. // We used to do "umount <device>" here, but it was buggy
  33. // if something was mounted OVER cdrom and
  34. // if cdrom is mounted many times.
  35. //
  36. // This works equally well (or better):
  37. // #!/bin/sh
  38. // umount /dev/cdrom
  39. // eject
  40. dev = xopen(device, O_RDONLY|O_NONBLOCK);
  41. cmd = CDROMEJECT;
  42. if (flags & FLAG_CLOSE
  43. || (flags & FLAG_SMART && ioctl(dev, CDROM_DRIVE_STATUS) == CDS_TRAY_OPEN))
  44. cmd = CDROMCLOSETRAY;
  45. ioctl_or_perror_and_die(dev, cmd, NULL, "%s", device);
  46. if (ENABLE_FEATURE_CLEAN_UP)
  47. close(dev);
  48. return EXIT_SUCCESS;
  49. }