+#define dev_fd 3
+
+/* Code taken from the original eject (http://eject.sourceforge.net/),
+ * refactored it a bit for busybox (ne-bb@nicoerfurth.de) */
+
+static void eject_scsi(const char *dev)
+{
+ static const char sg_commands[3][6] = {
+ { ALLOW_MEDIUM_REMOVAL, 0, 0, 0, 0, 0 },
+ { START_STOP, 0, 0, 0, 1, 0 },
+ { START_STOP, 0, 0, 0, 2, 0 }
+ };
+
+ unsigned i;
+ unsigned char sense_buffer[32];
+ unsigned char inqBuff[2];
+ sg_io_hdr_t io_hdr;
+
+ if ((ioctl(dev_fd, SG_GET_VERSION_NUM, &i) < 0) || (i < 30000))
+ bb_error_msg_and_die("not a sg device or old sg driver");
+
+ memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
+ io_hdr.interface_id = 'S';
+ io_hdr.cmd_len = 6;
+ io_hdr.mx_sb_len = sizeof(sense_buffer);
+ io_hdr.dxfer_direction = SG_DXFER_NONE;
+ /* io_hdr.dxfer_len = 0; */
+ io_hdr.dxferp = inqBuff;
+ io_hdr.sbp = sense_buffer;
+ io_hdr.timeout = 2000;
+
+ for (i = 0; i < 3; i++) {
+ io_hdr.cmdp = (void *)sg_commands[i];
+ ioctl_or_perror_and_die(dev_fd, SG_IO, (void *)&io_hdr, "%s", dev);
+ }
+
+ /* force kernel to reread partition table when new disc is inserted */
+ ioctl(dev_fd, BLKRRPART);
+}
+