8

I'm making an application that requires the knowledge of whether a CD drive is open or closed.

eject opens the CD drive, and checks how long it takes to open (a shorter amount of time says it's open, and a longer, well...), but I cannot use this technique, because the application actually opens the drive (and I do not want to re-open the drive if it's closed, neither do I want to close the drive if it is open).

How would I do this on linux? I saw that it is possible to do this under Windows (might be wrong though), but I haven't seen a way of doing this on linux.

If it's not possible using linux API calls, is it possible to implement a low-level function that could do this?

10
  • I'm not sure this is possible. I don't believe that the IDE interface exposes the state of the drive tray, SATA might though. Also, what about slot and caddy drives that don't have any concept of being open or close?
    – Dai
    Mar 27, 2013 at 5:40
  • @Dai, I wonder then how linux can detect when a CD has been put in the drive then... if I knew that, maybe that could answer my question.
    – MiJyn
    Mar 27, 2013 at 5:43
  • The drive does notify the system when the media changes, but that's not the same thing as the status of the tray.
    – Dai
    Mar 27, 2013 at 5:45
  • @Dai, oh... quite disappointing :/ Oh well, I guess my question is solved then :(
    – MiJyn
    Mar 27, 2013 at 5:47
  • There must be ioctl() controlling the opening/closing behaviour , do check the driver of the cd-drive , to find out if any such ioctl exists , ps: I think , this question is better suited for unix and linux Mar 27, 2013 at 5:52

2 Answers 2

8

To make the example code work, you should do it this way:

#include <sys/ioctl.h>
#include <linux/cdrom.h>

int result=ioctl(fd, CDROM_DRIVE_STATUS, CDSL_NONE);

switch(result) {
  case CDS_NO_INFO: ... break;
  case CDS_NO_DISC: ... break;
  case CDS_TRAY_OPEN: ... break;
  case CDS_DRIVE_NOT_READY: ... break;
  case CDS_DISC_OK: ... break;
  default: /* error */
}

i.e. the result is returned as ioctl() function result, not into slot argument.

3
  • Is there a reason to use CDSL_NONE? The doc says "Slot number to be tested, always zero except for jukeboxes."
    – Étienne
    Jan 7, 2016 at 16:46
  • Invoking this ioctl causes the tray to be loaded; is there a way to prevent that?
    – Jason
    May 15, 2018 at 0:13
  • 1
    @Jason Open the device on non-blocking mode (O_NONBLOCK). See this comment in linux/cdrom.h. Jul 30, 2018 at 0:46
6

You can get tray state by using the CDROM_DRIVE_STATUS ioctl. All ioctls for CD-drives can be found in /usr/include/linux/cdrom.h

#define CDROM_DRIVE_STATUS      0x5326  /* Get tray position, etc. */

Taken from here

int slot;
ioctl(fd, CDROM_DRIVE_STATUS, slot);

switch(slot) {
  case CDS_NO_INFO: ... break;
  case CDS_NO_DISC: ... break;
  case CDS_TRAY_OPEN: ... break;
  case CDS_DRIVE_NOT_READY: ... break;
  case CDS_DISC_OK: ... break;
  default: /* error */
}
10
  • Interesting: CDS_TRAY_OPEN, there's our answer.
    – Dai
    Mar 27, 2013 at 7:05
  • Doesn't work :/ Here is my source code: pastebin.com/y48ZYZ8x . It just returns false, regardless if it's open or not
    – MiJyn
    Mar 27, 2013 at 21:46
  • If you add debug output for the other variables, which do you get?
    – fredrik
    Mar 28, 2013 at 11:12
  • 1
    @fredrik, thanks, I figured out the issue. I used "switched" slot instead of the return value of ioctl
    – MiJyn
    Mar 28, 2013 at 23:13
  • 1
    @fredrik, yeah, and the "int slot" without any pointers or default value doesn't seem good either...
    – MiJyn
    Mar 29, 2013 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.