]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
eject: unlock door before issuing CDROMEJECT command
authorAaron Lu <aaron.lu@intel.com>
Thu, 6 Jun 2013 08:28:37 +0000 (16:28 +0800)
committerKarel Zak <kzak@redhat.com>
Thu, 13 Jun 2013 11:28:04 +0000 (13:28 +0200)
If user has inserted a disc into the drive, the drive will normally be
locked. When using eject command to eject the drive, we need to unlock
the door first, or the CDROMEJECT command will fail.

Though the 2nd attmpt to eject the drive with eject_scsi will succeed,
it actually does two things: first to unlock the door and then to eject
the tray, both with the SG_IO ioctl. The problem is, Linux SCSI driver
keeps track of if a device is in locked state or not, if we go with
SG_IO to do the unlocking, the driver will not be aware of the unlocking
and would think the drive is locked while actually it has already been
unlocked by the first SG_IO command.

Fix this by issuing a unlock door command before the CDROMEJECT command
in cdrom_eject. Prior to this fix, the following output is expected when
there is a disc inside:

[aaron@aaronlu util-linux-2.22.2]$ eject -v /dev/sr0
eject: device name is `/dev/sr0'
eject: /dev/sr0: mounted on /run/media/aaron/CD_ROM
eject: /dev/sr0: is whole-disk device
eject: /dev/sr0: is removable device
eject: /run/media/aaron/CD_ROM: unmounting
eject: /dev/sr0: trying to eject using CD-ROM eject command
eject: CD-ROM eject command failed
eject: /dev/sr0: trying to eject using SCSI commands
eject: SCSI eject succeeded

After this fix, the following output is expected:
[aaron@aaronlu util-linux-2.22.2]$ ./eject -v /dev/sr0
lt-eject: device name is `/dev/sr0'
lt-eject: /dev/sr0: mounted on /run/media/aaron/CD_ROM
lt-eject: /dev/sr0: is whole-disk device
lt-eject: /dev/sr0: is removable device
lt-eject: /run/media/aaron/CD_ROM: unmounting
lt-eject: /dev/sr0: trying to eject using CD-ROM eject command
lt-eject: CD-ROM eject command succeeded

And the SCSI device's locked state is correct now.

Signed-off-by: Aaron Lu <aaron.lu@intel.com>
sys-utils/eject.c

index a5b56565d89bf23b007868ea43b69465ea3c5bb3..4ec69e72992db938ac1ff83874ff44331cbe3a84 100644 (file)
@@ -398,6 +398,9 @@ static void close_tray(int fd)
 static int eject_cdrom(int fd)
 {
 #if defined(CDROMEJECT)
+       int ret = ioctl(fd, CDROM_LOCKDOOR, 0);
+       if (ret < 0)
+               return 0;
        return ioctl(fd, CDROMEJECT) >= 0;
 #elif defined(CDIOCEJECT)
        return ioctl(fd, CDIOCEJECT) >= 0;