char device catch multiple (int) ioctl-arguments

2019-02-15 10:15发布

I have to write a linux char device, which handles ioctl (without BKL) functions per unlock_ioctl. At the moment I can receive one argument from the userspace ioctl command per

__get_user(myint, (int __user *) arg);

How can I receive multiple int-arguments (for example this call)?:

ioctl(fp, SZ_NEW_DEV_FORMAT, 0, 1, 30);

1条回答
Fickle 薄情
2楼-- · 2019-02-15 11:02

Yes, you have to use structures. For a particular ioctl command there will be some predefined arguments. You need to wrap these all arguments into a structure object and pass in the address of the object. In side the kernel, you need to type cast the given arg to structure pointer and access the arguments. For instance.

 struct mesg {
         int size;
         char buf[100];
 };

 struct mesg msg1;

 /*Fill in the structure object here and call ioctl like this*/
 ret = ioctl(fd, SZ_NEW_DEV_FORMAT, &msg1);

Inside the kernel you access it like this:

      struct mesg *msg;
      copy_from_user((char *)msg, (char *)arg, sizeof(*msg));
查看更多
登录 后发表回答