2013-03-09 35 views
1

下面的代碼片段;基本上,我抓住活動vt並針對當前狀態的capslock/numlock/scrolllock鍵發出一個針對該終端的ioctl KDGETLED,並且無論鎖定鍵的狀態如何,我總是得到result = 0。Linux(Ubuntu)ioctl KDGETLED/KDGKBLED始終爲0

我試過在多個Linux機器上運行Ubuntu的所有變種(例如Mint)。我已經嘗試了其他的fds for KDGETLED命令,如「/ dev/tty」,「/ dev/console」,0等。我遇到了與KDGKBLED相同的問題。其他人是否遇到同樣的問題,我是在做一些愚蠢的事情,我遇到寫得不好的司機或其他事情?

int fd; 
vt_stat stat; 
fd = open("/dev/tty0", O_RDONLY); 
if (ioctl(fd, VT_GETSTATE, &stat) == -1) { 
    fprintf(stderr, "Error on VT_GETSTATE\n"); 
    exit(1); 
} 
close(fd); 
char tty[128]; 
sprintf(tty, "/dev/tty%d", stat.v_active); 
printf("Query tty: %s\n", tty); 
char result; 
fd = open(tty, O_RDWR | O_NDELAY, 0); 
if (ioctl(fd, KDGETLED, &result) == -1) { 
    fprintf(stderr, "Error on KDGETLED\n"); 
    exit(1); 
} 
close(fd); 
printf("LED flag state: %d\n", result); 

感謝,提前,所有誰審查我的問題。

回答

1

檢出驅動程序代碼,尤其是該驅動程序的struct file_operations實例,並檢查分配給.ioctl成員的函數 - 如果編碼很糟糕(我已經看到很多糟糕的東西發生在ioctls中),那肯定是你的問題。

在這種情況下,我很確定這是司機的錯。只要ioctl命令顯示沒有編譯錯誤,一切 - 特別是錯誤處理和輸入檢查 - 都是驅動程序的任務。