eject.c 4.0 KB

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