如何从用户空间与Linux内核模块进行通信而不会乱丢/ dev使用新节点?

时间:2022-04-23 17:49:44

What are the ways to communicate with a kernel module from user space? By communication i mean sending information and commands between the kernel module and a user space process.

从用户空间与内核模块通信的方式有哪些?通过通信,我的意思是在内核模块和用户空间进程之间发送信息和命令。

I currently know of two way:

我目前知道两种方式:

  1. open/close/read/write/ioctl on published device node.
  2. 在已发布的设备节点上打开/关闭/读取/写入/ ioctl。
  3. read/write on exported and hooked /proc file.
  4. 读/导出和挂钩/ proc文件。

More specifically, can someone advice the best way to communicate with a kernel module that does not actually drives any hardware and therefore should not be littering /dev with stub nodes that exists solely for ioctl calls? I mostly need to check its various status variables and send it a block of data with a request type tag and see if the request succeeded.

更具体地说,有人可以建议与内核模块进行通信的最佳方式,该内核模块实际上并不驱动任何硬件,因此不应该使用仅存在于ioctl调用的存根节点乱丢/ dev吗?我主要需要检查其各种状态变量,并向其发送带有请求类型标记的数据块,并查看请求是否成功。

6 个解决方案

#1


8  

Netlink sockets are designed for that kind of requirements, too...

Netlink套接字也是为满足这种要求而设计的......

Also see

另见

#2


8  

There's also the /sys filesystem (sysfs):

还有/ sys文件系统(sysfs):

Sysfs exports information about devices and drivers from the kernel device model to userspace, and is also used for configuration.

Sysfs将有关设备和驱动程序的信息从内核设备模型导出到用户空间,也用于配置。

(from Wikipedia)

(来自*)

#3


3  

You could also read/write from /dev device nodes.

您还可以从/ dev设备节点读取/写入。

IMHO, /dev is already littered with stuff and adding your own nodes there isn't a big issue. Don't forget that you can have lots of ioctl codes for a single device node, and the ioctl paramters are passed by reference so can be as big as you like.

恕我直言,/ dev已经乱七八糟的东西,并添加自己的节点,这不是一个大问题。不要忘记,您可以为单个设备节点提供大量ioctl代码,并且ioctl参数通过引用传递,因此可以根据需要进行调整。

#4


2  

Third one is add a new syscall, but the two you have written are the preferred ones, I think. I've found this document that might help, but I still think this option is unadvised: http://www.csee.umbc.edu/courses/undergraduate/CMSC421/fall02/burt/projects/howto_add_systemcall.html

第三个是添加一个新的系统调用,但我认为你写的两个是首选的系统调用。我发现这个文档可能有所帮助,但我仍然认为这个选项没有被修改:http://www.csee.umbc.edu/courses/undergraduate/CMSC421/fall02/burt/projects/howto_add_systemcall.html

Another acceptable option might be sharing memory.

另一个可接受的选择可能是共享内存

#5


0  

You could also use Shared Memory and IOCTL

您还可以使用共享内存和IOCTL

#6


0  

debugfs is another good possibility for APIs that are less stable than sysfs, but the API is basically the same. Here is a minimal runnable example.

对于不如sysfs稳定的API,debugfs是另一个很好的可能性,但API基本相同。这是一个最小的可运行示例。

configfs is another one. It allows easy dynamic creation of kernel objects from userspace through the filesystem: https://www.kernel.org/doc/Documentation/filesystems/configfs/configfs.txt

configfs是另一个。它允许通过文件系统从用户空间轻松动态创建内核对象:https://www.kernel.org/doc/Documentation/filesystems/configfs/configfs.txt

In any case, you will have to dirty some namespace... a filesystem entry in case of sysfs and debugfs. Just choose your poison.

无论如何,在sysfs和debugfs的情况下,你将不得不弄脏一些命名空间......一个文件系统条目。只要选择你的毒药。

Also, udev rules make /dev very similar to sysfs and debugfs: How to create a device in /dev automatically upon loading of the kernel module for a device driver?

此外,udev规则使/ dev非常类似于sysfs和debugfs:如何在为设备驱动程序加载内核模块时自动在/ dev中创建设备?

#1


8  

Netlink sockets are designed for that kind of requirements, too...

Netlink套接字也是为满足这种要求而设计的......

Also see

另见

#2


8  

There's also the /sys filesystem (sysfs):

还有/ sys文件系统(sysfs):

Sysfs exports information about devices and drivers from the kernel device model to userspace, and is also used for configuration.

Sysfs将有关设备和驱动程序的信息从内核设备模型导出到用户空间,也用于配置。

(from Wikipedia)

(来自*)

#3


3  

You could also read/write from /dev device nodes.

您还可以从/ dev设备节点读取/写入。

IMHO, /dev is already littered with stuff and adding your own nodes there isn't a big issue. Don't forget that you can have lots of ioctl codes for a single device node, and the ioctl paramters are passed by reference so can be as big as you like.

恕我直言,/ dev已经乱七八糟的东西,并添加自己的节点,这不是一个大问题。不要忘记,您可以为单个设备节点提供大量ioctl代码,并且ioctl参数通过引用传递,因此可以根据需要进行调整。

#4


2  

Third one is add a new syscall, but the two you have written are the preferred ones, I think. I've found this document that might help, but I still think this option is unadvised: http://www.csee.umbc.edu/courses/undergraduate/CMSC421/fall02/burt/projects/howto_add_systemcall.html

第三个是添加一个新的系统调用,但我认为你写的两个是首选的系统调用。我发现这个文档可能有所帮助,但我仍然认为这个选项没有被修改:http://www.csee.umbc.edu/courses/undergraduate/CMSC421/fall02/burt/projects/howto_add_systemcall.html

Another acceptable option might be sharing memory.

另一个可接受的选择可能是共享内存

#5


0  

You could also use Shared Memory and IOCTL

您还可以使用共享内存和IOCTL

#6


0  

debugfs is another good possibility for APIs that are less stable than sysfs, but the API is basically the same. Here is a minimal runnable example.

对于不如sysfs稳定的API,debugfs是另一个很好的可能性,但API基本相同。这是一个最小的可运行示例。

configfs is another one. It allows easy dynamic creation of kernel objects from userspace through the filesystem: https://www.kernel.org/doc/Documentation/filesystems/configfs/configfs.txt

configfs是另一个。它允许通过文件系统从用户空间轻松动态创建内核对象:https://www.kernel.org/doc/Documentation/filesystems/configfs/configfs.txt

In any case, you will have to dirty some namespace... a filesystem entry in case of sysfs and debugfs. Just choose your poison.

无论如何,在sysfs和debugfs的情况下,你将不得不弄脏一些命名空间......一个文件系统条目。只要选择你的毒药。

Also, udev rules make /dev very similar to sysfs and debugfs: How to create a device in /dev automatically upon loading of the kernel module for a device driver?

此外,udev规则使/ dev非常类似于sysfs和debugfs:如何在为设备驱动程序加载内核模块时自动在/ dev中创建设备?