在 linux 中查找 IOCTL 编号

Find IOCTL number in linux

我看头文件

#define IOCTL_MAGIC 'A'
#define IOCTL_NAME _IOWR(IOCTL_MAGIC, 2, ioctl_param)

我怎么知道那是IOCTL_NAME的ioctl号?

ioctl number的构造方法在header顶部描述include/uapi/asm-generic/ioctl.h:

ioctl command encoding: 32 bits total, command in lower 16 bits, size of the parameter structure in the lower 14 bits of the upper 16 bits.

...

De facto, however, the top 8 bits of the lower 16 bits are indeed used as a type field

即一个ioctl数由4个字段构成,从上到下:

  1. dir - 方向,2 位。
    • 高位表示用户写入参数,
    • 较低的位表示用户阅读了一个参数。
  2. size - 参数的大小,14 位。
  3. type - 唯一代表驱动程序的数字,8 位。
  4. nr - 类型唯一的数字(对于驱动程序),8 位。

待解码

#define IOCTL_MAGIC 'A'
#define IOCTL_NAME _IOWR(IOCTL_MAGIC, 2, ioctl_param)

您需要知道 ioctl_param 结构 (sizeof(ioctl_param)) 的 大小

例如,如果结构的大小为 16 字节,则 ioctl 字段为:

  1. dir - 0x3(读写)。
  2. size - 0x10(结构大小,16)。
  3. type - 0x41(字符A的ASCII码)。
  4. nr - 0x2(第二个参数)。

并且 ioctl 编号本身是 0xc0104102。