问题描述
是否可以从Linux内核模块调用ioctl
?谁能提供一个使用示例?
Is it possible to call ioctl
from a Linux kernel module?Can anyone provide an example of how it's used?
推荐答案
您可以尝试致电sys_ioctl
.
如果内核是使用CONFIG_COMPAT
编译的,则将其导出.
You can try to call sys_ioctl
.
It's exported if the kernel is compiled with CONFIG_COMPAT
.
或者,如果您拥有设备驱动程序的struct file_operations
,则可以直接调用其ioctl
处理程序.
Or, if you have the device driver's struct file_operations
, you can call its ioctl
handler directly.
但是,ioctl句柄希望指针参数位于当前正在运行的进程的地址空间中,而不是内核地址空间中. copy_from_user
将用于读取它们.如果您提供指向内核地址空间的指针,则copy_from_user
将失败.我看不出你将如何解决这个问题.
However, the ioctl handle would expect pointer parameters to be in the address space of the process currently running, not in the kernel address space. copy_from_user
would be used to read them. If you give pointers to the kernel address space, copy_from_user
will fail. I don't see how you would get around this.
如果您要在以下代码之间调用ioctl处理程序,则copy_from_user
永远不会失败.
If you will call ioctl handler between below code than copy_from_user
will not ever 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 */
这篇关于如何在Linux的内核空间中使用ioctl()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!