3

我必须编写一个 linux char 设备,它处理每个 unlock_ioctl 的 ioctl(没有 BKL)函数。目前我可以从用户空间 ioctl 命令中接收一个参数

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

如何接收多个 int 参数(例如此调用)?:

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

1 回答 1

5

是的,你必须使用结构。对于特定的 ioctl 命令,将有一些预定义的参数。您需要将这些所有参数包装到一个结构对象中并传入该对象的地址。在内核中,您需要将给定的 arg 类型转换为结构指针并访问参数。例如。

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

在内核内部,您可以这样访问它:

      struct mesg *msg;
      copy_from_user((char *)msg, (char *)arg, sizeof(*msg));
于 2012-01-18T07:14:46.007 回答