从用户空间的程序调用内核模块函数

时间:2022-05-07 17:50:05

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.

我还需要访问我的程序在用户空间的内核模块中的一些全局变量。

4 个解决方案

#1


23  

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内核模块和用户空间程序交互http://riiki.tldp.org/kernel_user_space_howto“内核空间,用户空间接口”,作者是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(添加新的系统调用并不容易)和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接口传递几个参数)。

#2


5  

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.

用户程序只有一种传统方式可以显式地从内核请求服务 - 进行系统调用。

There are also traps and some Linux-specific userland-kernel communication mechanisms, but those are not relevant here.

还有一些陷阱和一些特定于Linux的用户空间 - 内核通信机制,但这些并不重要。

#3


5  

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套接字那样灵活。

Sample code here: http://people.ee.ethz.ch/~arkeller/linux/multi/kernel_user_space_howto-3.html

示例代码:http://people.ee.ethz.ch/~arkeller/linux/multi/kernel_user_space_howto-3.html

#4


1  

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

#1


23  

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内核模块和用户空间程序交互http://riiki.tldp.org/kernel_user_space_howto“内核空间,用户空间接口”,作者是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(添加新的系统调用并不容易)和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接口传递几个参数)。

#2


5  

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.

用户程序只有一种传统方式可以显式地从内核请求服务 - 进行系统调用。

There are also traps and some Linux-specific userland-kernel communication mechanisms, but those are not relevant here.

还有一些陷阱和一些特定于Linux的用户空间 - 内核通信机制,但这些并不重要。

#3


5  

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套接字那样灵活。

Sample code here: http://people.ee.ethz.ch/~arkeller/linux/multi/kernel_user_space_howto-3.html

示例代码:http://people.ee.ethz.ch/~arkeller/linux/multi/kernel_user_space_howto-3.html

#4


1  

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