How to use ioctl () from kernel space on Linux?

Is it possible to call ioctl from a Linux kernel module? Can someone provide a usage example?

+6
source share
1 answer

You can try calling sys_ioctl .
It is exported if the kernel is compiled using CONFIG_COMPAT .

Or, if you have a struct file_operations device driver, you can directly call its ioctl handler.

However, the ioctl descriptor expects the pointer parameters to be in the address space of the current process, and not in the address space of the kernel. copy_from_user will be used to read them. If you point to the kernel address space, copy_from_user will fail. I do not understand how you will get around this.

Edit:

If you call the ioctl handler between the code below than copy_from_user , it will never fail.

  mm_segment_t fs; fs = get_fs(); /* save previous value */ set_fs (get_ds()); /* use kernel limit */ /* system calls can be invoked */ set_fs(fs); /* restore before returning to user space */ 
+6
source

Source: https://habr.com/ru/post/918536/


All Articles