CD-ROM通过C代码弹出

时间:2014-06-26 19:13:33

标签: c cd-rom

我试图通过C代码弹出我的光盘。它不想工作。 ioctl返回“5”I / O错误,哪里可能有问题?

#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;
}

当我尝试这个功能时

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

完美无缺。但是我想通过第一个函数(eject_cdrom)来完成它。

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 +++

1 个答案:

答案 0 :(得分:1)

为了让任何在Google上闲逛的人受益,我会列出我看到的所有可能原因:

  1. 您已在代码中明确指定/dev/cdrom,但允许eject自动检测该内容。

    某些发行版未将/dev/cdrom设置为/dev/sr0或同等的符号链接。排序&#34;是否已插入?&#34;问题,但包括完整性。

  2. 我不确定原始ioctl是否会解析符号链接

    可能/usr/bin/eject必须进行额外的API调用才能取得成功。

  3. 您只是假设eject将使用该API

    根据man eject/usr/bin/eject实用程序支持至少四种不同的弹出API,其中两种(CDROM和SCSI)可以应用于光学设备。

  4. 我实际上是在我的系统上对此进行了测试,当我通过使用eject选项运行-r来强制使用API​​时,它失败并出现相同的错误。

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

    但是,强制SCSI API(扩展为覆盖SATA,然后是PATA)对我有用。

    $ eject -s /dev/cdrom