uevent应用:内核发送uevent,用户空间接收uevent

news/2025/3/14 18:27:01/

1,内核发送uevent

内核发送uevent的API由lib/kobject_event.c文件实现,include/linux/kobject.h是头文件。

enum kobject_action {KOBJ_ADD,KOBJ_REMOVE,KOBJ_CHANGE,KOBJ_MOVE,KOBJ_ONLINE,KOBJ_OFFLINE,KOBJ_MAX
};/* kobject_uevent不能用在中断上下文 */
int kobject_uevent(struct kobject *kobj, enum kobject_action action);
int kobject_uevent_env(struct kobject *kobj, enum kobject_action action, char *envp[]);

在driver中可以调用kobject_uevent或者kobject_uevent_env来向用户空间发送uevent

kobject_uevent默认会发送”ACTION=xxx”,”DEVPATH=xxx”,”SUBSYSTEM=xxx”这三个uevent环境变量。

kobject_uevent_env可以发送一些如”xxx=xxx”的自定义的uevent环境变量。

static ssize_t esd_info_store(struct device *dev,struct device_attribute *attr,const char *buf, size_t count)
{if (!buf || count <= 0)return -EINVAL;kobject_uevent(&core_data->pdev->dev.kobj, KOBJ_CHANGE);return count;
}static int test_uevent(struct device *dev,struct kobj_uevent_env *env)
{int ret = 0;ret = add_uevent_var(env,"COMMENT=%s", "test uevent message");if(ret)return ret;return 0;
}const struct device_type my_dev_type ={.name ="william_ts",.uevent= test_uevent,
};static int xxx_probe(struct platform_device *pdev)
{pdev->dev.type = &my_dev_type;
}

2,用户空间解析uevent

#define UEVENT_MSG_LEN 2048static void uevent_event(uint32_t /*epevents*/, int event_fd)
{char msg[UEVENT_MSG_LEN + 2];int n;char *cp;n = uevent_kernel_multicast_recv(event_fd, msg, UEVENT_MSG_LEN);if (n <= 0)return;if (n >= UEVENT_MSG_LEN) /* overflow -- discard */return;msg[n] = '\0';msg[n + 1] = '\0';cp = msg;ALOGE("william get the uevent size n = %d, msg = %s", n, msg);while(*cp) {ALOGE("william receive the msg = %s", cp);/* advance to after the next \0 */while(*cp++);}ALOGE("william uevent received %s", msg);
}void *MT::uevent_thread_loop(void)
{int epoll_fd, uevent_fd;struct epoll_event ev;int nevents = 0;ALOGE("creating uevent thread");uevent_fd = uevent_open_socket(64 * 1024, true);if (uevent_fd < 0) {ALOGE("uevent_init: uevent_open_socket failed\n");return NULL;}fcntl(uevent_fd, F_SETFL, O_NONBLOCK);ev.events = EPOLLIN;ev.data.ptr = (void *)uevent_event;epoll_fd = epoll_create(64);if (epoll_fd == -1) {ALOGE("epoll_create failed; errno=%d", errno);goto error;}if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, uevent_fd, &ev) == -1) {ALOGE("epoll_ctl failed; errno=%d", errno);goto error;}while (!destroyThread) {struct epoll_event events[64];nevents = epoll_wait(epoll_fd, events, 64, -1);if (nevents == -1) {if (errno == EINTR) continue;ALOGE("usb epoll_wait failed; errno=%d", errno);break;}for (int n = 0; n < nevents; ++n) {if (events[n].data.ptr)(*(void (*)(uint32_t, int event_fd))events[n].data.ptr)(events[n].events, uevent_fd);}}ALOGI("exiting worker thread");
error:close(uevent_fd);if (epoll_fd >= 0)close(epoll_fd);return NULL;
}void sighandler(int sig) {if (sig == SIGUSR1) {destroyThread = true;ALOGI("destroy set");return;}signal(SIGUSR1, sighandler);
}

3,测试结果

上报的event会通知到所有的用户进程,这里能够收到系统中所有的uevent消息,这里只打印了我们自己添加测试的event。

william get the uevent size n = 234, msg = change@/devices/platform/william_ts.0
william receive the msg = change@/devices/platform/william_ts.0
william receive the msg = ACTION=change
william receive the msg = DEVPATH=/devices/platform/william_ts.0
william receive the msg = SUBSYSTEM=platform
william receive the msg = DEVTYPE=william_ts
william receive the msg = DRIVER=william_ts
william receive the msg = MODALIAS=platform:william_ts
william receive the msg = MAC=05:04:03:02:01:00
william receive the msg = COMMENT=test uevent message
william receive the msg = SEQNUM=13492
william uevent received change@/devices/platform/william_ts.0

http://www.ppmy.cn/news/1045796.html

相关文章

设计模式详解-桥接模式

类型&#xff1a;结构型模式 实现原理&#xff1a;将抽象类和实现类分离&#xff0c;使其独立&#xff0c;然后使用接口再将二者连接起来。 意图&#xff1a;将抽象部分与实现部分分离&#xff0c;使它们都可以独立的变化。 主要解决&#xff1a;类变化频繁时用继承可能会出…

STP生成树总结

一、什么是STP&#xff08;802.1D&#xff09; STP协议生来就是为了冗余而存在的&#xff0c;单纯树型的网络无法提供足够的可靠性&#xff0c;由此我们引入了额外的链路&#xff0c;这才出现了环路这样的问题。但单纯是标准的802.1D STP协议并不能实现真正的冗余与负载…

深入浅出Pytorch函数——torch.nn.init.kaiming_normal_

分类目录&#xff1a;《深入浅出Pytorch函数》总目录 相关文章&#xff1a; 深入浅出Pytorch函数——torch.nn.init.calculate_gain 深入浅出Pytorch函数——torch.nn.init.uniform_ 深入浅出Pytorch函数——torch.nn.init.normal_ 深入浅出Pytorch函数——torch.nn.init.c…

熔断降级的理解和基于feign的熔断降级

什么是熔断降级&#xff1a; 在微服务保护中我们使用sentinel进行了熔断降级&#xff0c;熔断降级时为了防止雪崩效应&#xff0c;什么是雪崩效应&#xff0c;因为微服务是一层调用一层的&#xff0c;如果下面某一个微服务宕机了&#xff0c;就会导致全部的微服务宕机&#xf…

linux安装vagrant

vagrant下载 https://developer.hashicorp.com/vagrant/downloads 我的系统是fedora sudo dnf install -y dnf-plugins-core sudo dnf config-manager --add-repo https://rpm.releases.hashicorp.com/fedora/hashicorp.repo sudo dnf -y install vagrant

Android Studio 新建module报错:No signature of method

android平台uni原生插件开发过程中&#xff0c;使用Android Studio 新增 module 报错 选择app --> create new module &#xff0c;填写相关信息 Android Studio 新建module报错&#xff1a; 原因&#xff1a;Android Studio 版本过高&#xff0c;新增了namespace&#x…

Hlang--用Python写个编程语言-逻辑运算

文章目录 前言关键词解析token解析器解释器语法表示前言 在上一篇文章里面,实现了基本的变量,并且详细地阐述了基本原理,所以的话,这里要实现的就是这个判断,由于架子基本上打好了,后面的操作无法就是确定这个AST的一个执行顺序,也就是我们希望解释器执行的一个情况。 …

第 7 章 排序算法(1)(介绍,分类,时间复杂度,空间复杂度)

7.1排序算法的介绍 排序也称排序算法(Sort Algorithm)&#xff0c;排序是将一组数据&#xff0c;依指定的顺序进行排列的过程。 7.2排序的分类&#xff1a; 内部排序: 指将需要处理的所有数据都加载到**内部存储器(内存)**中进行排序。外部排序法&#xff1a; 数据量过大&am…