Skip to main content

Pythonified linux asm-generic/ioctl.h .

So you can replicate driver’s code computing fcntl.ioctl’s opt argument.

For example, starting from the following IOCTL declaration (taken from input.h):

#include <sys/ioctl.h>
#define EVIOCGNAME(len) _IOC(_IOC_READ, 'E', 0x06, len) /* get device name */

you could write the following:

from ioctl_opt import IOC, IOC_READ
EVIOCGNAME = lambda length: IOC(IOC_READ, ord('E'), 0x06, length)

The differences are minimal, and all come from python language or coding style:

  • macros/constants to use from ioctl_opt for not start with an underscore

  • defined macro becomes a callable (here a lambda, could be function)

  • IOC’s nr argument has to be an integer, so C’s single-quote char becomes an ord call

  • avoid shadowing built-in len function

The length argument is an object (typically serving as an I/O buffer with the fcntl.ioctl() call), the size of which is incorporated in the generated ioctl opt value. IOC() will calculate the size of the object. Supported object types are:

  • any ctypes type or instance

  • memoryview

  • bytearray

  • struct.Struct

  • array.array

You may want to then write a pythonic function to conveniently access that ioctl:

import ctypes
import fcntl

def getDeviceName(fd, length=1024):
    name = (ctypes.c_char * length)()
    actual_length = fcntl.ioctl(fd, EVIOCGNAME(length), name, True)
    if actual_length < 0:
        raise OSError(-actual_length)
    if name[actual_length - 1] == b'\x00':
        actual_length -= 1
    return name[:actual_length]

More advanced example defining hidraw ioctls, requiring structures (for more on how structures are defined, check python’s ctype documentation for your python version):

import ctypes
from ioctl_opt import IOR, IOC, IOC_READ, IOC_WRITE

# hid.h
HID_MAX_DESCRIPTOR_SIZE = 4096

# hidraw.h
class hidraw_report_descriptor(ctypes.Structure):
    _fields_ = [
        ('size', ctypes.c_uint),
        ('value', ctypes.c_ubyte * HID_MAX_DESCRIPTOR_SIZE),
    ]

class hidraw_devinfo(ctypes.Structure):
    _fields_ = [
        ('bustype', ctypes.c_uint),
        ('vendor', ctypes.c_short),
        ('product', ctypes.c_short),
    ]

HIDIOCGRDESCSIZE = IOR(ord('H'), 0x01, ctypes.c_int)
HIDIOCGRDESC = IOR(ord('H'), 0x02, hidraw_report_descriptor)
HIDIOCGRAWINFO = IOR(ord('H'), 0x03, hidraw_devinfo)
HIDIOCGRAWNAME = lambda length: IOC(IOC_READ, ord('H'), 0x04, length)
HIDIOCGRAWPHYS = lambda length: IOC(IOC_READ, ord('H'), 0x05, length)
HIDIOCSFEATURE = lambda length: IOC(IOC_WRITE|IOC_READ, ord('H'), 0x06, length)
HIDIOCGFEATURE = lambda length: IOC(IOC_WRITE|IOC_READ, ord('H'), 0x07, length)

Release files for ioctl-opt 1.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for ioctl-opt 1.3.1
File Interpreter ABI Platform
ioctl_opt-1.3.1-py3-none-any.whl Python 3 none any Details

Release files / ioctl_opt-1.3.1-py3-none-any.whl

Download URL ioctl_opt-1.3.1-py3-none-any.whl
Size 21.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7617a734e7083fd0ff5607c821ca67f75f4dd009c55b3f96db5c55621e64ee9a
BLAKE2b-256 checksum
How to use checksums
7fba2a8796dfa24e8ac9fbbffe4e70c9cdab6fa088122b258ab53354ba8af490
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.11

Release history Release notifications | RSS feed

This release

1.3.1 This release

1 release file

1.3

1 release file

1.2.2

1 release file

1.2.1

1 release file

1.2

1 release file

1.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page