Linux ioctl命令在用户空间和内核空间之间进行更改

时间:2012-08-15 14:52:34

标签: c linux ioctl

不确定这里是否有任何想法,我以前没见过。我正在编写一个存根来测试我的内核模块,当我在用户空间中检查命令的值时,我得到一个不同的值,而不是当我查看内核空间时。

存根的一部分:

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "ain.h"
#include "ain_ioctl.h"
#define AI_DEVICE   "/dev/ain"

void main()
{
    int fd, error, ioctl_par = 0;
    char* dev;
    long ret;

    dev = AI_DEVICE;

    printf("Starting driver test\n");

    fd = open(dev, O_RDWR);
    if (fd < 0) {
    /* Failed to open -> Print error-message and exit */
    printf("%s failed to open, error: %s\n", dev, strerror(errno));
    }

    printf("Doing the IOCTL now... cmd: %d\n", AIN_IOC_GET_AN0_CONF);
    fflush(stdout);

    ret = ioctl(fd, AIN_IOC_GET_AN0_CONF, &ioctl_par);

ain_ioctl.h文件:

#define AIN_IOC_MAGIC  'e'
#define AIN_IOC_GET_AN0_CONF    _IOR(AIN_IOC_MAGIC, 46, int)

内核中的ioctl例程:

int ain_ioctl (struct inode * inodep, struct file * filp, unsigned int cmd, unsigned long arg)
{
    printk("In the ain_ioctl function, cmd: %d. type: %d, dir: %d, nr: %d, size: %d\n", 
        cmd, _IOC_TYPE(cmd), _IOC_DIR(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));

    printk("Testing against command: %d. type: %d, dir: %d, nr: %d, size: %d\n",
        AIN_IOC_GET_AN0_CONF, _IOC_TYPE(AIN_IOC_GET_AN0_CONF), _IOC_DIR(AIN_IOC_GET_AN0_CONF), 
        _IOC_NR(AIN_IOC_GET_AN0_CONF), _IOC_SIZE(AIN_IOC_GET_AN0_CONF));

现在我希望用户空间中的输出与内核中的输出相同。并且在内核中的第一组打印到第二组。然而,这不是我所看到的......

输出:

mike@linux-4puc:~> ./a.out 
Starting driver test
Doing the IOCTL now... cmd: -2147195602

mike@linux-4puc:~> dmesg | tail
[75253.205136] In the ain_ioctl function, cmd: -1078168112. type: 117, dir: 2, nr: 208, size: 16316
[75253.205140] Testing against            cmd: -2147195602. type: 101, dir: 2, nr: 46, size: 4

当我通过ioctl命令将命令传递给内核时,任何人都有任何关于为什么我的命令行为不同的想法,而当我通过硬编码来检查值时(就像我在我的打印中那样)?

我在构建时遇到的唯一警告似乎与ioctl调用无关:

makedepend: warning: ignoring option -Wall 
makedepend: warning: ignoring option -Wall 
makedepend: warning: ain.c (reading /usr/src/linux/include/linux/compiler-gcc.h), line 94: incomplete include == "#include gcc_header(__GNUC__)" 
makedepend: warning: ain.c (reading /usr/src/linux/include/linux/string.h, line 13): cannot find include file "stdarg.h" 

感谢。

2 个答案:

答案 0 :(得分:4)

-1078168112(为什么不用十六进制打印这些?)看起来像堆栈指针。可能&ioctl_par。这表明您的ioctl方法接收的参数与您预期的不同。

在当前的内核源代码中,我看到ioctl方法采用3个参数,而不是4个.4参数ioctl似乎是一个较旧的接口。

在模块编译期间,您是否收到任何警告?注意它们!

答案 1 :(得分:1)

Alan Curry的回答并不是完整的“正确”答案,而是引导我找到解决方案。该命令的十六进制值已关闭,因此我查看了内核中的其他ioctl调用。

我所使用的系统基于较旧的2.4X内核,我将其更新为3.1。这里的问题是ioctl调用的参数列表。在参数列表中使用inode指针导致问题,因为它将文件指针作为命令。

正确的解决方案:

long ain_ioctl (struct file * filp, unsigned int cmd, unsigned long arg) { 
    ...
相关问题