2013-03-08 25 views
4

我想在我的玩具文件系統模塊中調用ioctl函數。我只想讓這個ioctl設置一個由調用者傳入的變量。到目前爲止,我已經建立了允許我進行ioctl呼叫的ioctl基礎設施。我在我的模塊中有這個功能來處理ioctl。試圖將參數傳遞給ioctl調用零參數

int ospfs_ioctl(struct inode *inode, struct file *filp, 
     unsigned int cmd, unsigned long arg) 
{ 
    if(cmd == OSPFSIOCRASH) 
    { 
     eprintk("crash: %ld\n", arg); 
     return 0; 
    } 
    else 
     return -ENOTTY; 
} 

而我的測試功能看起來像這樣。

#include <stdio.h> 
#include <fcntl.h> 
#include <sys/ioctl.h> 
#define OSPFSIOCRASH 42 

int main() 
{ 
    int fd = open("/tmp/cs111/lab3-thief/test/hello.txt", O_RDWR); 
    printf("ioctl call: %d\n", ioctl(fd, OSPFSIOCRASH, 100)); 
    close(fd); 
} 

我希望可以將輸出爲

crash: 100 
ioctl call: 0 

但輸出實際上是

crash: 0 
ioctl call: 0 

我敢打賭,我在做一些簡單的錯誤。有人能幫忙指出問題是什麼嗎?非常感謝你。

+0

您定位的是哪個平臺? 86? – jleahy 2013-03-08 20:57:33

+0

你如何設置OSPFSIOCRASH?它需要編碼參數的方向和大小,可能通過'_IOW(type,nr,unsigned long)' – 2013-03-08 20:58:02

+0

這是一個運行Debian 2.6.18-6-486的x86模擬器(QEMU)。如果這有什麼區別。你如何設置_IOW(type,nr,unsigned long)? – user1174472 2013-03-08 20:59:45

回答

0

這可能不是解決您的問題的解決方案,但基於您的問題和評論中的有限信息,這是我可以收集的內容。

基於問題和意見,看起來你已經以這種方式定義的struct file_operations結構:

struct file_operations fops = { .ioctl=ospfs_ioctl }; 

和你ospfs_ioctl的簽名表明您使用的是舊的ioctl。使用最近的內核(至少在2.6.35+之後),推薦使用.unlocked_ioctl而不是.ioctl

struct file_operations fops = { .unlocked_ioctl=ospfs_ioctl }; 

而且ospfs_ioctl函數的定義將變爲:

long ospfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) 

unlocked_ioctl和常規IOCTL之間的差異,可以發現here。簡而言之,在調用ioctl之前,並不需要使用可怕的BKL

也根據Chris Dodd的建議,你應該仔細檢查你如何定義你的OSPFIOCRASH。推薦的方法是利用_IO(magic, some_num_for_ioctl)

+0

嘿tuxdude,這很有趣,我稍後可能會嘗試更改爲'.unlocked_ioctl'。現在我已經得到了ioctl的工作感謝一些評論的建議。我的修復程序在下面的答案中。 – user1174472 2013-03-08 21:42:16

0

根據克里斯多德的建議,我將#define OSPFIOCRASH 42更改爲#define OSPFSIOCRASH _IO(magic, 0),並從此獲得所需的行爲。