I am doing kernel module programming using ioctl calls to communicate between userspace and kernel space.
I plan on making the userspace api in python.
To pass data between kernel and userspace ioctl calls utilizes address, and data is copied using copy_to_user, or copy_from_user. The address here is given by unsigned long arg.
int ioctl(struct inode *inode,struct file *filp,unsigned int cmd,unsigned long arg)
In C userspace programming it is trivial to pass address.
What I want to do, is to use struct of python to create a structure which is compatible with the structure which i have defined in the kernel module, and pass the populated python struct, using ioctl from fcntl module of python.
Is it possible?
If it is possible, how do i pass the address of python struct in the ioctl call?
I do not want to use ctypes, or extend python with c. Pure python code, is what i would like.
The fcntl docs show you examples how to do this. They use
structandarray, but you can also use plain strings. If you pass a mutable buffer andmutate_flagis True, the actual backing buffer will be passed to the OS call. If it is unmutable, a copy will be passed and the resulting change in buffer will be returned by the call. See the documentation for fcntl.ioctl().So you don’t have to care about the address of the buffer backing the struct. Just pass it as a
structorstring.