char device catch multiple (int) ioctl-arguments

十年热恋 提交于 2019-12-19 04:09:32

问题


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:


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


来源:https://stackoverflow.com/questions/8849445/char-device-catch-multiple-int-ioctl-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!