ioctl in python

Perry Faulkner perryf at bigpond.NO.SPAM.com
Wed Jun 30 19:20:43 EDT 1999


Hi Angus,

The ioctl cmd code (0x80047601) is expecting a 4 byte buffer to be
passed to it. The
simplest thing is something like :

    rec = pack('BBBB', 0,0,0,0)
or
    rec = pack('L', 0)

depending on what's expected, then

    ioctl(handle, 0x80047601, rec)

By the way, the 80 part of the command also says this is an input
function, so you
are writing the data, rec in this case! For an output function you would
follow the
ioctl with an unpack of the data buffer, as well. But you still need to
supply the
packed input buffer in any case.

Hope this helps!

Regards,
    Perry

Angus MacKay wrote:

> well it seems I have sort of solved my problem. if I use a
> string for the 3rd arg then it works properly:
> >>> fo=open('/tmp')
> >>> fcntl.ioctl(fo.fileno(), 0x80047601, "hi")
> '\001\000'
> >>> fo=open('/')
> >>> fcntl.ioctl(fo.fileno(), 0x80047601, "hi")
> '\000\000'
> >>> fo=open('/proc')
> >>> fcntl.ioctl(fo.fileno(), 0x80047601, "hi")
> Traceback (innermost last):
>   File "<stdin>", line 1, in ?
> IOError: (25, 'Inappropriate ioctl for device')
>
> the question is why? it will still fail with only 2 args or an int:
> >>> fcntl.ioctl(fo.fileno(), 0x80047601)
> Traceback (innermost last):
>   File "<stdin>", line 1, in ?
> IOError: (14, 'Bad address')
> >>> fcntl.ioctl(fo.fileno(), 0x80047601, 1)
> Traceback (innermost last):
>   File "<stdin>", line 1, in ?
> IOError: (14, 'Bad address')
>
> cheers, Angus.
>
> Angus MacKay wrote:
> >
> > is there some magic to the fcntl.ioctl() call?
> >
> > I can do these ioctl calls in C no problem (with a small wrapper):
> > (amackay at phat)~/tmp$ ./ioctltest /tmp 0x80047601
> > 0x80047601: 1
> > (amackay at phat)~/tmp$ ./ioctltest / 0x80047601
> > 0x80047601: 0
> > (amackay at phat)~/tmp$ ./ioctltest /proc 0x80047601
> > /proc: Inappropriate ioctl for device
> >
> > that was the Linux ioctl for EXT2 version.
> >
> > but in python:
> > >>> a = 0
> > >>> req = 0x80047601
> > >>> import fcntl
> > >>> fo=open('/tmp')
> > >>> fcntl.ioctl(fo.fileno(), req, a)
> > Traceback (innermost last):
> >   File "<stdin>", line 1, in ?
> > IOError: (14, 'Bad address')
> > >>>





More information about the Python-list mailing list