How do I get the address of a struct object in python?

I am programming a kernel module with ioctl calls to communicate between user space and kernel space. I am planning to make a user api in python.

Ioctl calls use the address to transfer data between the kernel and user space, and the data is copied using copy_to_user or copy_from_user. The address is set here unsigned long arg

.

int ioctl(struct inode *inode,struct file *filp,unsigned int cmd,unsigned long arg)

In C user-space programming, it is trivial to pass an address. I want to use struct python to create a struct compatible with the struct I have defined in the kernel module and pass the filled python struct using ioctl from fcntl of python module.

Is it possible? If possible, how do I pass the address of the python structure in the ioctl call?

I don't want to use ctypes or extend python with c. Pure Python code, this is what I would like.

+3


source to share


2 answers


The fcntl docs shows you how to do this. They use struct

and array

, but you can also use simple strings. If you pass a mutable buffer and mutate_flag

is True, the actual backing buffer will be passed to the OS call. If this is not possible, a copy will be committed and the caused buffer change will be returned by the call. See the documentation for fcntl.ioctl () .



This way, you don't have to worry about the address of the buffer that supports the structure. Just pass it as struct

or string

.

0


source


Maybe ctypes-module can help you here, especially this section



0


source