在我的Python(2.7.3)代碼中,我試圖使用一個ioctl調用,接受一個long int(64位)作爲參數。我在64位系統上,所以64位int與指針的大小相同。64位參數fcntl.ioctl()
我的問題是,Python似乎不接受64位int作爲fcntl.ioctl()調用的參數。它愉快地接受一個32位int或一個64位指針 - 但我需要的是通過一個64位int。
這裏是我的IOCTL處理程序:
static long trivial_driver_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
long err = 0;
switch (cmd)
{
case 1234:
printk("=== (%u) Driver got arg %lx; arg<<32 is %lx\n", cmd, arg, arg<<32);
break;
case 5678:
printk("=== (%u) Driver got arg %lx\n", cmd, arg);
break;
default:
printk("=== OH NOES!!! %u %lu\n", cmd, arg);
err = -EINVAL;
}
return err;
}
在現有的C代碼,我用這樣的電話:
static int trivial_ioctl_test(){
int ret;
int fd = open(DEV_NAME, O_RDWR);
unsigned long arg = 0xffff;
ret = ioctl(fd, 1234, arg); // ===(1234) Driver got arg ffff; arg<<32 is ffff00000000
arg = arg<<32;
ret = ioctl(fd, 5678, arg); // === (5678) Driver got arg ffff00000000
close(fd);
}
在Python中,我打開設備文件,然後我得到的以下結果:
>>> from fcntl import ioctl
>>> import os
>>> fd = os.open (DEV_NAME, os.O_RDWR, 0666)
>>> ioctl(fd, 1234, 0xffff)
0
>>> arg = 0xffff<<32
>>> # Kernel log: === (1234) Driver got arg ffff; arg<<32 is ffff00000000
>>> # This demonstrates that ioctl() happily accepts a 32-bit int as an argument.
>>> import struct
>>> ioctl(fd, 5678, struct.pack("L",arg))
'\x00\x00\x00\x00\xff\xff\x00\x00'
>>> # Kernel log: === (5678) Driver got arg 7fff9eb1fcb0
>>> # This demonstrates that ioctl() happily accepts a 64-bit pointer as an argument.
>>> ioctl(fd, 5678, arg)
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
ioctl(fd, 5678, arg)
OverflowError: signed integer is greater than maximum
>>> # Kernel log: (no change - OverflowError is within python)
>>> # Oh no! Can't pass a 64-bit int!
>>>
Python有沒有辦法通過m 64位參數ioctl()?
如果可能的話,將有助於提供可重複的示例。鑑於'ioctl()'調用是特定於設備的,所以用'IOC_GET_VAL'代替正在使用的實際請求代碼使得測試變得很困難。 – Aya
@Aya:感謝您的評論。我是新手設備驅動程序,並且在構建一個微不足道的功能示例時遇到了一些問題。但我會看看我能做些什麼。 :) – Ziv
與此同時,我發佈了一個基於ctypes的解決方案。 – Aya