试图将参数传递给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: %ldn", 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: %dn", 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之间的差异可以在这里找到。简而言之,它在调用ioctl之前不需要使用可怕的BKL

并且根据Chris Dodd的建议,你应该仔细检查你如何定义你的OSPFIOCRASH。推荐的方法是使用_IO(magic, some_num_for_ioctl)

根据Chris Dodd的建议,我将#define OSPFIOCRASH 42更改为#define OSPFSIOCRASH _IO(magic, 0),并且已经获得了所需的行为。

最新更新