ioct传递多个参数
我尝试从用户空间程序中传递两个参数来更改char设备上的缓冲区大小和缓冲区数量。我尝试了多次施放,但总是出现施放错误ioct传递多个参数,c,kernel-module,ioctl,C,Kernel Module,Ioctl,我尝试从用户空间程序中传递两个参数来更改char设备上的缓冲区大小和缓冲区数量。我尝试了多次施放,但总是出现施放错误 error: cannot convert to a pointer type copy_from_user((char *)msg, arg, sizeof(msg)); 或 标题.h struct ioctl_arguments { int block_number; int block_size; }; 内核模块和c程序都包含he
error: cannot convert to a pointer type copy_from_user((char *)msg, arg, sizeof(msg));
或
标题.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));
在设备驱动程序中,函数
arg
实际上是一个指针,因此强制转换是有效的,但是msg
不是指针,因此强制转换到指针是无效的。您应该使用&msg
(就像您在用户空间代码中使用&args
。可能重复的
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));