This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: enhanced ioctl
Type: enhancement Stage:
Components: Extension Modules Versions:
process
Status: closed Resolution: works for me
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, loewis, ndbecker
Priority: normal Keywords:

Created on 2008-04-28 21:01 by ndbecker, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (8)
msg65933 - (view) Author: (ndbecker) Date: 2008-04-28 21:01
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.import 

-----
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 (not complete):
class eos_dl_args_t (Structure):
    _fields_ = [("length", c_ulong),
                ("data", c_void_p)]

args = eos_dl_args_t()
args.length = len (c)
args.data = cast (pointer (c), c_void_p)

from eioctl 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 (fd, request, args)
msg65938 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2008-04-28 22:11
> IIUC, current ioctl is not capable of handling arbitrary argument 
> types.

Can you please be a bit more explicit? What limitation do you see in
fcntl.ioctl, and how does this fragment overcome the limitation?
AFAICT, they do exactly the same thing.
msg65940 - (view) Author: (ndbecker) Date: 2008-04-28 22:35
On Monday 28 April 2008, Martin v. Löwis wrote:
> Martin v. Löwis <martin@v.loewis.de> added the comment:
> > IIUC, current ioctl is not capable of handling arbitrary argument
> > types.
>
> Can you please be a bit more explicit? What limitation do you see in
> fcntl.ioctl, and how does this fragment overcome the limitation?
> AFAICT, they do exactly the same thing.
>

In the example I gave, the ioctl passes a structure:

class eos_dl_args_t (Structure):
    _fields_ = [("length", c_ulong),
                ("data", c_void_p)]

AFAIK, there is no way to do that with fcntl.ioctl, is there?
msg65941 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2008-04-28 22:55
Passing structures is certainly possible. I'd try

args = struct.pack("iP", len(c), cast (pointer (c), c_void_p).value)
fcntl.ioctl(fd, request, args)

Alternatively,

args = eos_dl_args_t()
...
args_p = cast(pointer(args), c_void_ptr).value
fcntl.ioctl(fd, request, args_p)

should also work, IIUC.
msg65949 - (view) Author: (ndbecker) Date: 2008-04-29 01:23
On Monday 28 April 2008, Martin v. Löwis wrote:
> Martin v. Löwis <martin@v.loewis.de> added the comment:
>
> Passing structures is certainly possible. I'd try
>
> args = struct.pack("iP", len(c), cast (pointer (c), c_void_p).value)
> fcntl.ioctl(fd, request, args)
>
> Alternatively,
>
> args = eos_dl_args_t()
> ...
> args_p = cast(pointer(args), c_void_ptr).value
> fcntl.ioctl(fd, request, args_p)
>
> should also work, IIUC.
>

You are correct, both of the above work (at least on x86)
msg65950 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2008-04-29 02:11
So, should we close this?
msg65957 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2008-04-29 05:48
I think we should close it.
msg65959 - (view) Author: (ndbecker) Date: 2008-04-29 10:27
OK.
History
Date User Action Args
2022-04-11 14:56:33adminsetgithub: 46964
2008-04-29 10:27:33ndbeckersetmessages: + msg65959
2008-04-29 05:48:38loewissetstatus: open -> closed
resolution: works for me
messages: + msg65957
2008-04-29 02:11:13benjamin.petersonsetnosy: + benjamin.peterson
messages: + msg65950
2008-04-29 01:23:35ndbeckersetmessages: + msg65949
2008-04-28 22:55:43loewissetmessages: + msg65941
2008-04-28 22:35:36ndbeckersetmessages: + msg65940
2008-04-28 22:11:42loewissetnosy: + loewis
messages: + msg65938
2008-04-28 21:01:20ndbeckercreate