eject.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. //config:config EJECT
  15. //config: bool "eject (4 kb)"
  16. //config: default y
  17. //config: select PLATFORM_LINUX
  18. //config: help
  19. //config: Used to eject cdroms. (defaults to /dev/cdrom)
  20. //config:
  21. //config:config FEATURE_EJECT_SCSI
  22. //config: bool "SCSI support"
  23. //config: default y
  24. //config: depends on EJECT
  25. //config: help
  26. //config: Add the -s option to eject, this allows to eject SCSI-Devices and
  27. //config: usb-storage devices.
  28. //applet:IF_EJECT(APPLET(eject, BB_DIR_USR_BIN, BB_SUID_DROP))
  29. //kbuild:lib-$(CONFIG_EJECT) += eject.o
  30. //usage:#define eject_trivial_usage
  31. //usage: "[-t] [-T] [DEVICE]"
  32. //usage:#define eject_full_usage "\n\n"
  33. //usage: "Eject DEVICE or default /dev/cdrom\n"
  34. //usage: IF_FEATURE_EJECT_SCSI(
  35. //usage: "\n -s SCSI device"
  36. //usage: )
  37. //usage: "\n -t Close tray"
  38. //usage: "\n -T Open/close tray (toggle)"
  39. #include <sys/mount.h>
  40. #include "libbb.h"
  41. #if ENABLE_FEATURE_EJECT_SCSI
  42. /* Must be after libbb.h: they need size_t */
  43. # include "fix_u32.h"
  44. # include <scsi/sg.h>
  45. # include <scsi/scsi.h>
  46. #endif
  47. #define dev_fd 3
  48. /* Code taken from the original eject (http://eject.sourceforge.net/),
  49. * refactored it a bit for busybox (ne-bb@nicoerfurth.de) */
  50. #if ENABLE_FEATURE_EJECT_SCSI
  51. static void eject_scsi(const char *dev)
  52. {
  53. static const char sg_commands[3][6] ALIGN1 = {
  54. { ALLOW_MEDIUM_REMOVAL, 0, 0, 0, 0, 0 },
  55. { START_STOP, 0, 0, 0, 1, 0 },
  56. { START_STOP, 0, 0, 0, 2, 0 }
  57. };
  58. unsigned i;
  59. unsigned char sense_buffer[32];
  60. unsigned char inqBuff[2];
  61. sg_io_hdr_t io_hdr;
  62. if ((ioctl(dev_fd, SG_GET_VERSION_NUM, &i) < 0) || (i < 30000))
  63. bb_simple_error_msg_and_die("not a sg device or old sg driver");
  64. memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
  65. io_hdr.interface_id = 'S';
  66. io_hdr.cmd_len = 6;
  67. io_hdr.mx_sb_len = sizeof(sense_buffer);
  68. io_hdr.dxfer_direction = SG_DXFER_NONE;
  69. /* io_hdr.dxfer_len = 0; */
  70. io_hdr.dxferp = inqBuff;
  71. io_hdr.sbp = sense_buffer;
  72. io_hdr.timeout = 2000;
  73. for (i = 0; i < 3; i++) {
  74. io_hdr.cmdp = (void *)sg_commands[i];
  75. ioctl_or_perror_and_die(dev_fd, SG_IO, (void *)&io_hdr, "%s", dev);
  76. }
  77. /* force kernel to reread partition table when new disc is inserted */
  78. ioctl(dev_fd, BLKRRPART);
  79. }
  80. #else
  81. # define eject_scsi(dev) ((void)0)
  82. #endif
  83. /* various defines swiped from linux/cdrom.h */
  84. #define CDROMCLOSETRAY 0x5319 /* pendant of CDROMEJECT */
  85. #define CDROMEJECT 0x5309 /* Ejects the cdrom media */
  86. #define CDROM_DRIVE_STATUS 0x5326 /* Get tray position, etc. */
  87. /* drive status possibilities returned by CDROM_DRIVE_STATUS ioctl */
  88. #define CDS_TRAY_OPEN 2
  89. #define FLAG_CLOSE 1
  90. #define FLAG_SMART 2
  91. #define FLAG_SCSI 4
  92. static void eject_cdrom(unsigned flags, const char *dev)
  93. {
  94. int cmd = CDROMEJECT;
  95. if (flags & FLAG_CLOSE
  96. || ((flags & FLAG_SMART) && ioctl(dev_fd, CDROM_DRIVE_STATUS) == CDS_TRAY_OPEN)
  97. ) {
  98. cmd = CDROMCLOSETRAY;
  99. }
  100. ioctl_or_perror_and_die(dev_fd, cmd, NULL, "%s", dev);
  101. }
  102. int eject_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  103. int eject_main(int argc UNUSED_PARAM, char **argv)
  104. {
  105. unsigned flags;
  106. const char *device;
  107. flags = getopt32(argv, "^" "tT"IF_FEATURE_EJECT_SCSI("s")
  108. "\0" "?1:t--T:T--t"
  109. );
  110. device = argv[optind] ? argv[optind] : "/dev/cdrom";
  111. /* We used to do "umount <device>" here, but it was buggy
  112. if something was mounted OVER cdrom and
  113. if cdrom is mounted many times.
  114. This works equally well (or better):
  115. #!/bin/sh
  116. umount /dev/cdrom
  117. eject /dev/cdrom
  118. */
  119. xmove_fd(xopen_nonblocking(device), dev_fd);
  120. if (ENABLE_FEATURE_EJECT_SCSI && (flags & FLAG_SCSI))
  121. eject_scsi(device);
  122. else
  123. eject_cdrom(flags, device);
  124. if (ENABLE_FEATURE_CLEAN_UP)
  125. close(dev_fd);
  126. return EXIT_SUCCESS;
  127. }