uevent 从内核发送到用户空间 (udev) [英] uevent sent from kernel to user space (udev)

查看:27
本文介绍了uevent 从内核发送到用户空间 (udev)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 udev 在 linux 系统上播放,它通过 netlink 套接字接收内核发送的 uevent.

I knew that udev plays on the linux system and it receives uevents sent from the kernel through netlink socket.

但是,我的问题是:

  1. 内核如何发送事件?它必须是通过添加/删除设备触发的,然后向 udev 发送事件.内核如何做到这一点?(有没有我能找到的代码示例?)

  1. How kernel sends out the event ? It must be something triggered by adding/removing device and then sends out events to udev. How does kernel do this? (Is there any code example I can find? )

udev 仅通过 netlink 套接字接收这些 uevent.这是 udev 唯一的方法.这是正确的吗?

udev receives these uevents only through netlink socket. This is the only way that udev does it. Is this correct?

当uevent从内核发出时,我知道它可以进行广播.但是,它可以进行单播吗?

When uevent is sent out from the kernel, I knew it can do broadcast. However, Can it do unicast?

感谢您的任何反馈.

推荐答案

  1. 它发送名为 uevent 的 netlink 消息.uevent 只是通过 netlink 套接字发送的某种特殊格式的字符串.示例:

  1. It sends netlink message called uevent. uevent is just string of some special format that is sent via netlink socket. Example:

"add@/class/input/input9/mouse2    // message
ACTION=add                         // action type
DEVPATH=/class/input/input9/mouse2 // path in /sys
SUBSYSTEM=input                    // subsystem (class)
SEQNUM=1064                        // sequence number
PHYSDEVPATH=/devices/pci0000:00/0000:00:1d.1/usb2/2­2/2­2:1.0  // device path in /sys
PHYSDEVBUS=usb       // bus
PHYSDEVDRIVER=usbhid // driver
MAJOR=13             // major number
MINOR=34",           // minor number

实际发送uevent的内核函数是kobject_uevent_env 和它的包装器 kobject_uevent 被称为 在很多地方.

Kernel function that actually sends uevent is kobject_uevent_env and it's wrapper kobject_uevent that is called in many places.

是的,udev 通过从 netlink 套接字接收 uevents 来工作.但是有一个选项 - 内核可以调用用户模式助手.在这种情况下,内核为每个热插拔事件生成一个进程,为每个描述该特定热插拔事件的新进程提供环境变量.如果您查看 kobject_uevent_env,您会看到 netlink 消息 实际上是 #ifdef'ed 并且默认操作是调用该用户模式助手

Yes, udev works by receiving uevents from netlink socket. But there is an option - kernel can call usermode helper. In this case kernel spawns one process per hotplug event, supplying environment variables to each new process describing that particular hotplug event. If you look at kobject_uevent_env you'll see that netlink message is actually #ifdef'ed and default action is to call that usermode helper

理论上netlink消息可以广播、多播和单播,但内核使用 netlink_broadcast_filtered 调用发送广播消息.无论如何,该消息发送到 NETLINK_KOBJECT_UEVENT 家庭.您可以在 uevent_net_init.

In theory netlink messages can be broadcast, multicast and unicast, but kernel sends broadcast message with netlink_broadcast_filtered call. Anyway that message goes to socket of NETLINK_KOBJECT_UEVENT family. You can see netlink socket creation in uevent_net_init.

回答您的评论问题.您不会在内核中看到任何 send 函数.send 是一个系统调用——它是内核提供给用户空间的接口,但内核本身不使用任何系统调用.有一长串函数调用(在 net/netlink/af_netlink.cnet/core/dev.c) 从 kobject_uevent_env 到不包含任何 send 的最终发送 - 在内核中发送 skb(套接字缓冲区)类似于将缓冲区放入队列然后调用调度程序来传递该缓冲区并通知正在等待系统调用 recv

Answering your comment question. You will not see any send function in kernel. send is a system call - it's interface that kernel provides to userspace, but kernel itself does not use any of syscalls. There is a long chain of function calls (in net/netlink/af_netlink.c and net/core/dev.c) from kobject_uevent_env to final sending that doesn't contain any send - in kernel sending skb (socket buffer) is something like placing buffer in queue and then calling scheduler to deliver that buffer and notify userspace that is waiting on syscall recv

资源:

  1. lib/kobject_uevent.c
  2. https://www.kernel.org/doc/pending/hotplug.txt - 具有监听 uevents 并打印它的用户空间程序.
  3. https://bootlin.com/doc/legacy/udev/udev.pdf
  1. lib/kobject_uevent.c
  2. https://www.kernel.org/doc/pending/hotplug.txt - has userspace program that listens for uevents and prints it.
  3. https://bootlin.com/doc/legacy/udev/udev.pdf

这篇关于uevent 从内核发送到用户空间 (udev)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆