【问题标题】:ioct multiple arguments passingioct 多个参数传递
【发布时间】:2014-10-27 23:39:18
【问题描述】:

我尝试从用户空间程序传递两个参数来更改 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 程序

#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));

【问题讨论】:

标签: c kernel-module ioctl


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-02
    • 2012-08-13
    • 2016-02-03
    • 2018-10-12
    • 2021-09-12
    • 2014-06-04
    • 1970-01-01
    相关资源
    最近更新 更多