试图将参数传递给ioctl调用零参数
我想在我的玩具文件系统模块中调用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
我敢打赌,我在做一些简单的错误。有人能帮忙指出问题是什么吗?非常感谢你。
这可能不是解决您的问题的解决方案,但基于您的问题和评论中的有限信息,这是我可以收集的内容。
基于问题和意见,看起来你已经以这种方式定义的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)
嘿tuxdude,这很有趣,我稍后可能会尝试更改为'.unlocked_ioctl'。现在我已经得到了ioctl的工作感谢一些评论的建议。我的修复程序在下面的答案中。 – user1174472 2013-03-08 21:42:16
根据克里斯多德的建议,我将#define OSPFIOCRASH 42
更改为#define OSPFSIOCRASH _IO(magic, 0)
,并从此获得所需的行为。
您定位的是哪个平台? 86? – jleahy 2013-03-08 20:57:33
你如何设置OSPFSIOCRASH?它需要编码参数的方向和大小,可能通过'_IOW(type,nr,unsigned long)' – 2013-03-08 20:58:02
这是一个运行Debian 2.6.18-6-486的x86模拟器(QEMU)。如果这有什么区别。你如何设置_IOW(type,nr,unsigned long)? – user1174472 2013-03-08 20:59:45