ioct多个参数传递

时间:2014-09-03 12:52:03

标签: c kernel-module ioctl

我尝试从用户空间程序传递两个参数来更改char设备上的缓冲区大小和缓冲区数。我尝试了多次演员,我总是犯错误

 error: cannot convert to a pointer type copy_from_user((char *)msg, arg, sizeof(msg));

cannot convert to a pointer typecopy_from_user((char *)msg, (char *)arg, sizeof(msg));

header.h

struct ioctl_arguments {
         int block_number;
         int block_size;
 };

内核模块和c程序都包含header.h

c program

#define DEVICE_PATH "/dev/driver"
#define MAGIC_NO 'k'
struct ioctl_arguments args;
#define IOCTL_CMD _IOWR(MAGIC_NO, 0, args)
int main()
{
    int fd;
    args.block_number = 10;
    args.block_size = 10;
    fd = open(DEVICE_PATH, O_RDWR);
    ioctl(fd, IOCTL_CMD,&args);
    close(fd);
    return 0;
}

设备驱动程序

static int BlockNumber = 20;
static int BlockSize = 5;
struct ioctl_arguments msg;

static int sample_ioctl (struct file *filp, unsigned int cmd, unsigned long arg);

static int sample_ioctl (struct file *filp, unsigned int cmd, unsigned long arg) 
{

  // copy two parameters from outside, we use struct
  copy_from_user((char *)msg, (char *)arg, sizeof(msg));

1 个答案:

答案 0 :(得分:1)

在您的设备驱动程序函数中arg实际上是一个指针,以便强制转换,但msg不是指针,因此转换为指针无效。您应该使用&msg(就像您在用户空间代码中使用&args一样)。