Linux 在用户空间从程序调用内核模块函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10816801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
call a kernel module function from program at user space
提问by Ricardo
I developed a kernel module and some functions on it. Now i need to develop a program in the user space and call some functions which are in the kernel module.
我开发了一个内核模块和它的一些功能。现在我需要在用户空间开发一个程序并调用内核模块中的一些函数。
I also need to access some global variable that are in the kernel module on my program at the user space.
我还需要在用户空间访问我的程序内核模块中的一些全局变量。
回答by osgx
There is complete overview of linux-kernel module and user-space program interacting http://wiki.tldp.org/kernel_user_space_howto"Kernel Space, User Space Interfaces" by Ariane Keller (it is from 2008-09-28, but about 2.6 kernels; only major new way is relayfs)
有关于 linux-kernel 模块和用户空间程序交互的完整概述http://wiki.tldp.org/kernel_user_space_howto" Kernel Space, User Space Interfaces" by Ariane Keller(来自 2008-09-28,但大约 2.6内核;唯一的主要新方法是relayfs)
No ordinary function call from user space to kernel space is listed, only syscall (adding new syscall is not easy) and upcall (call in inverse direction).
没有列出从用户空间到内核空间的普通函数调用,只有syscall(添加新的syscall不容易)和upcall(反方向调用)。
One of easiest interface is ioctl; but you can't start to use ioctl before creating procfs, sysfs or similiar file.
ioctl 是最简单的接口之一;但是在创建 procfs、sysfs 或类似文件之前,您不能开始使用 ioctl。
Other is sysctl; but sysctl is more eligible to reading/writing to global variable. (It is hard to pass several parameters via sysctl interface).
另一个是sysctl;但 sysctl 更有资格读取/写入全局变量。(很难通过 sysctl 接口传递几个参数)。
回答by Nikolai Fetissov
You seem to be missing the point of kernel and userland separation. If your user program could modify data inside the kernel directly, that would quickly lead to disaster.
您似乎错过了内核和用户空间分离的重点。如果您的用户程序可以直接修改内核内部的数据,那将很快导致灾难。
There's only one conventional way for a user program to explicitly request services from the kernel - make a system call
.
用户程序只有一种传统的方式来显式地从内核请求服务 - make a system call
.
There are also trapsand some Linux-specific userland-kernel communication mechanisms, but those are not relevant here.
还有陷阱和一些特定于 Linux 的用户空间-内核通信机制,但这些与此处无关。
回答by Jay
You'll need to install a new kernel to make use of the new call unless you already have some mechanism to update the kernel ... http://www.cyberciti.biz/tips/how-to-patch-running-linux-kernel.html
你需要安装一个新内核来使用新的调用,除非你已经有了一些更新内核的机制...... http://www.cyberciti.biz/tips/how-to-patch-running-linux -kernel.html
回答by ajpyles
As other posters have mentioned, there is a clear distinction between kernel and user space. So no you can't call a kernel function directly from user space. I think the easiest way to send messages between userspace and kernel space is via netlink sockets. A netlink socket allows you to easily pass arbitrary data structures between user level and kernel level.
正如其他海报所提到的,内核空间和用户空间之间有明显的区别。所以不,你不能直接从用户空间调用内核函数。我认为在用户空间和内核空间之间发送消息的最简单方法是通过 netlink 套接字。netlink 套接字允许您轻松地在用户级和内核级之间传递任意数据结构。
Yes ioctl, system calls are viable alternatives, they are not as flexible as the netlink socket for passing arbitrary information.
是的 ioctl,系统调用是可行的替代方案,它们不如用于传递任意信息的 netlink 套接字灵活。