Search code examples
clinuxlinux-kernelheader-filesioctl

Find IOCTL number in linux


I see into header file

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

How can I know that is the ioctl number of IOCTL_NAME ?


Solution

  • The way of constructing of ioctl number is described at the top of the 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

    That is, an ioctl number is constructed from 4 fields, from upper to lower:

    1. dir - direction, 2 bits.
      • Upper bit denotes that a user writes an argument,
      • Lower bit denotes that a user reads an argument.
    2. size - size of the argument, 14 bits.
    3. type - a number uniquely representing a driver, 8 bit.
    4. nr - a number which is unique for the type (for the driver), 8 bit.

    For decode

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

    you need to know the size of ioctl_param structure (sizeof(ioctl_param)).

    E.g., if the size of the structure is 16 bytes, then ioctl fields are:

    1. dir - 0x3 (both read and write).
    2. size - 0x10 (size of the structure, 16).
    3. type - 0x41 (ASCII code of the character A).
    4. nr - 0x2 (the second argument).

    and the ioctl number itself is 0xc0104102.