eject.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 GPLv2 or later, see file LICENSE in this source tree.
  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 <sys/mount.h>
  15. #include "libbb.h"
  16. /* Must be after libbb.h: they need size_t */
  17. #include "fix_u32.h"
  18. #include <scsi/sg.h>
  19. #include <scsi/scsi.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 CDROM_DRIVE_STATUS 0x5326 /* Get tray position, etc. */
  24. /* drive status possibilities returned by CDROM_DRIVE_STATUS ioctl */
  25. #define CDS_TRAY_OPEN 2
  26. #define dev_fd 3
  27. /* Code taken from the original eject (http://eject.sourceforge.net/),
  28. * refactored it a bit for busybox (ne-bb@nicoerfurth.de) */
  29. static void eject_scsi(const char *dev)
  30. {
  31. static const char sg_commands[3][6] = {
  32. { ALLOW_MEDIUM_REMOVAL, 0, 0, 0, 0, 0 },
  33. { START_STOP, 0, 0, 0, 1, 0 },
  34. { START_STOP, 0, 0, 0, 2, 0 }
  35. };
  36. unsigned i;
  37. unsigned char sense_buffer[32];
  38. unsigned char inqBuff[2];
  39. sg_io_hdr_t io_hdr;
  40. if ((ioctl(dev_fd, SG_GET_VERSION_NUM, &i) < 0) || (i < 30000))
  41. bb_error_msg_and_die("not a sg device or old sg driver");
  42. memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
  43. io_hdr.interface_id = 'S';
  44. io_hdr.cmd_len = 6;
  45. io_hdr.mx_sb_len = sizeof(sense_buffer);
  46. io_hdr.dxfer_direction = SG_DXFER_NONE;
  47. /* io_hdr.dxfer_len = 0; */
  48. io_hdr.dxferp = inqBuff;
  49. io_hdr.sbp = sense_buffer;
  50. io_hdr.timeout = 2000;
  51. for (i = 0; i < 3; i++) {
  52. io_hdr.cmdp = (void *)sg_commands[i];
  53. ioctl_or_perror_and_die(dev_fd, SG_IO, (void *)&io_hdr, "%s", dev);
  54. }
  55. /* force kernel to reread partition table when new disc is inserted */
  56. ioctl(dev_fd, BLKRRPART);
  57. }
  58. #define FLAG_CLOSE 1
  59. #define FLAG_SMART 2
  60. #define FLAG_SCSI 4
  61. static void eject_cdrom(unsigned flags, const char *dev)
  62. {
  63. int cmd = CDROMEJECT;
  64. if (flags & FLAG_CLOSE
  65. || ((flags & FLAG_SMART) && ioctl(dev_fd, CDROM_DRIVE_STATUS) == CDS_TRAY_OPEN)
  66. ) {
  67. cmd = CDROMCLOSETRAY;
  68. }
  69. ioctl_or_perror_and_die(dev_fd, cmd, NULL, "%s", dev);
  70. }
  71. int eject_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  72. int eject_main(int argc UNUSED_PARAM, char **argv)
  73. {
  74. unsigned flags;
  75. const char *device;
  76. opt_complementary = "?1:t--T:T--t";
  77. flags = getopt32(argv, "tT" IF_FEATURE_EJECT_SCSI("s"));
  78. device = argv[optind] ? argv[optind] : "/dev/cdrom";
  79. /* We used to do "umount <device>" here, but it was buggy
  80. if something was mounted OVER cdrom and
  81. if cdrom is mounted many times.
  82. This works equally well (or better):
  83. #!/bin/sh
  84. umount /dev/cdrom
  85. eject /dev/cdrom
  86. */
  87. xmove_fd(xopen_nonblocking(device), dev_fd);
  88. if (ENABLE_FEATURE_EJECT_SCSI && (flags & FLAG_SCSI))
  89. eject_scsi(device);
  90. else
  91. eject_cdrom(flags, device);
  92. if (ENABLE_FEATURE_CLEAN_UP)
  93. close(dev_fd);
  94. return EXIT_SUCCESS;
  95. }