[Python-Dev] enhanced ioctl?

Neal Becker ndbecker2 at gmail.com
Mon Apr 28 14:02:57 CEST 2008


IIUC, current ioctl is not capable of handling arbitrary argument types.
This code will allow any arg type (such as structures with pointers to
embedded structures).

The code for _IOC is taken from linux and might not be portable.

from ctypes import *

libc = CDLL ('/lib/libc.so.6')
#print libc.ioctl

def set_ioctl_argtype (arg_type):
    libc.ioctl.argtypes = (c_int, c_int, arg_type)

_IOC_WRITE = 0x1

_IOC_NRBITS=    8
_IOC_TYPEBITS=  8
_IOC_SIZEBITS=  14
_IOC_DIRBITS=   2

_IOC_NRSHIFT=   0
_IOC_TYPESHIFT= (_IOC_NRSHIFT+_IOC_NRBITS)
_IOC_SIZESHIFT= (_IOC_TYPESHIFT+_IOC_TYPEBITS)
_IOC_DIRSHIFT=  (_IOC_SIZESHIFT+_IOC_SIZEBITS)


def _IOC (dir, type, nr, size):
    return (((dir)  << _IOC_DIRSHIFT) | \
         ((type) << _IOC_TYPESHIFT) | \
         ((nr)   << _IOC_NRSHIFT) | \
         ((size) << _IOC_SIZESHIFT))

def ioctl (fd, request, args):
    return libc.ioctl (fd, request, args)
-------------------
Example usage:

from ioctl import *

set_ioctl_argtype (POINTER (eos_dl_args_t))

EOS_IOC_MAGIC = 0xF4
request = _IOC(_IOC_WRITE, EOS_IOC_MAGIC, 0x00, 0) # ignore size

err = ioctl (eos_file.fileno(), request, args)




More information about the Python-Dev mailing list