2

Im trying eject my CD rom via C code.And it dont want to work. ioctl returned "5" I/O error, where could be a problem?

#include <stdio.h>
#include <fcntl.h>
#include <linux/cdrom.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
int eject_cdrom()
{
    char path_to_cdrom[20]="/dev/cdrom";
    int fd = open("/dev/cdrom", O_RDONLY| O_NONBLOCK);
    printf("%d\n",fd );
    int lala = ioctl(fd, CDROMEJECT);
    printf("%d\n",lala);
    close(fd);
    return lala;
}

int main(int argc, char* argv[])
{
    //eject_cdrom_system();
    int value = eject_cdrom();
    printf("%d\n",value );
    if (value == -1) 
    {
        int errsv = errno;
        printf("somecall() failed\n");
        printf("%d\n",errsv );
    }

    return 0;
}

When I tried this function

void eject_cdrom_system()
    {
        system("/usr/bin/eject");
    }

It works perfectly. But I want to do it by first function (eject_cdrom).

Output from strace:

strace -f ./cdrom

execve("./cdrom", ["./cdrom"], [/* 76 vars */]) = 0
brk(0)                                  = 0xf9c000
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fcb50cd7000
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=131512, ...}) = 0
mmap(NULL, 131512, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fcb50cb6000
close(3)                                = 0
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\320\37\2\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=1845024, ...}) = 0
mmap(NULL, 3953344, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fcb506f1000
mprotect(0x7fcb508ad000, 2093056, PROT_NONE) = 0
mmap(0x7fcb50aac000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1bb000) = 0x7fcb50aac000
mmap(0x7fcb50ab2000, 17088, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7fcb50ab2000
close(3)                                = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fcb50cb5000
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fcb50cb3000
arch_prctl(ARCH_SET_FS, 0x7fcb50cb3740) = 0
mprotect(0x7fcb50aac000, 16384, PROT_READ) = 0
mprotect(0x600000, 4096, PROT_READ)     = 0
mprotect(0x7fcb50cd9000, 4096, PROT_READ) = 0
munmap(0x7fcb50cb6000, 131512)          = 0
open("/dev/cdrom", O_RDONLY|O_NONBLOCK) = 3
ioctl(3, CDROMEJECT, 0)                 = -1 EIO (Input/output error)
close(3)                                = 0
exit_group(0)                           = ?
+++ exited with 0 +++
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140

1 Answers1

3

For the benefit of anyone who wanders in off Google, I'll list all of the possible causes I see:

  1. You're explicitly specifying /dev/cdrom in your code but allowing eject to autodetect that.

    Some distros don't set up /dev/cdrom as a symlink to /dev/sr0 or equivalent. Sort of an "Is it plugged in?" question, but included for completeness.

  2. I'm not sure if the raw ioctl will resolve symbolic links

    It could be that /usr/bin/eject has to make an extra API call in order to achieve success.

  3. You're just assuming that eject will use that API

    According to man eject, the /usr/bin/eject utility supports at least four different ejecting APIs, two of which (CDROM and SCSI) could apply to an optical device.

I actually tested this on my system and, when I forced the API you're using by running eject with the -r option, it failed with the same error.

$ eject -r /dev/cdrom
eject: unable to eject, last error: Input/output error

However, forcing the SCSI API (which was expanded to also cover SATA and then PATA) works for me.

$ eject -s /dev/cdrom
Community
  • 1
  • 1
ssokolow
  • 14,938
  • 7
  • 52
  • 57