Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Custom ioctl on Linux?

by wjblack (Initiate)
on Feb 25, 2010 at 10:33 UTC ( #825270=perlquestion: print w/ replies, xml ) Need Help??
wjblack has asked for the wisdom of the Perl Monks concerning the following question:

I have a weird device that I'm using ioctl to talk to (on Linux 2.6). The code in C to do what I want is:

int f = open("/dev/plcm_drv", O_RDWR); printf("%x\n", ioctl(f, 0xC, 0) );

Simple and straightforward--I'm just sending ioctl # 0xC to the driver and printing out the response. Works every time. The equivalent code in Perl is (I believe):

sysopen(DEVICE, "/dev/plcm_drv", O_RDWR); print sprintf("%x", ioctl(DEVICE, 0xC, 0)) . "\n";

The driver, however, never receives the ioctl (I put some debug code in the driver that printk's whenever it gets an ioctl). The driver does receive the TCGETS ioctl from sysopen (with which it responds "Operation not supported" as expected). $! after the ioctl is "Invalid argument".

The strace tells a similar story:

Working (C, ioctl 0xC): open("/dev/plcm_drv", O_RDWR) = 3 ioctl(3, 0xc, 0) = 142 Working (Perl, ioctl 1): open("/dev/plcm_drv", O_RDWR|O_LARGEFILE) = 3 ioctl(3, SNDCTL_TMR_TIMEBASE or TCGETS, 0xff86b540) = -1 EOPNOTSUPP (O +peration not supported) _llseek(3, 0, [0], SEEK_CUR) = 0 fstat64(3, {st_mode=S_IFCHR|0644, st_rdev=makedev(241, 0), ...}) = 0 fcntl64(3, F_SETFD, FD_CLOEXEC) = 0 ioctl(3, FIBMAP, 0) = 0 close(3) = 0 Not Working (Perl, ioctl 0xC): open("/dev/plcm_drv", O_RDWR|O_LARGEFILE) = 3 ioctl(3, SNDCTL_TMR_TIMEBASE or TCGETS, 0xffe34310) = -1 EOPNOTSUPP (O +peration not supported) _llseek(3, 0, [0], SEEK_CUR) = 0 fstat64(3, {st_mode=S_IFCHR|0644, st_rdev=makedev(241, 0), ...}) = 0 fcntl64(3, F_SETFD, FD_CLOEXEC) = 0 ioctl(3, 0xc, 0) = -1 EINVAL (Invalid argument) close(3) = 0

So to sum up:

  • C: Works fine with the handcoded constant.
  • Perl: Works fine with other handcoded constants, just not 0xC. Behaves correctly with TCGETS (in that it errors). Does not appear to receive 0xC at all.

Any thoughts as to why 0xC is being blocked? I'd rather not have to have a trivial C helper app for this...

Comment on Custom ioctl on Linux?
Select or Download Code
Re: Custom ioctl on Linux?
by bot403 (Beadle) on Feb 25, 2010 at 16:45 UTC

    You're probably "Suffering from buffering". Plus the recommendataion is not to mix the sys* functions with the regular versions.

    What do you get if you rewrite your print statement using syswrite() instead?

    Update: Nevermind, I thought you were using print to write to the driver...

Re: Custom ioctl on Linux?
by wjblack (Initiate) on Feb 26, 2010 at 04:17 UTC

    So it turns out it wasn't a perl problem at all. My weirdo device had a driver that didn't have the 32-to-64-bit thunking layer (in Linux parlance, that's the "compat_ioctl" member of the file_operations structure, which points to the thunking routine). As a result, my 32-bit perl wasn't allowed to send ioctls to the 64-bit kernel/module while my 64-bit C program was.

    What's really weird about this is that some IOCTLs were apparently auto-thunked because they shared ID numbers with common system IOCTLs, which is why some IOCTLs worked anyway while others faltered. I simply added the thunking routine to the driver, recompiled, and was on my merry way in no time.

    The lesson to be learned is to make sure that your perl and C code are the same bitsize before making wild accusations on PM.

    Linux folk that think they're running into this should check their module's file_operations struct declaration for .compat_ioctl.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://825270]
Approved by marto
Front-paged by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2014-12-27 12:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls