《Linux设备节点创建》内核kobject上报uevent过滤规则

2024-03-15 14:32

本文主要是介绍《Linux设备节点创建》内核kobject上报uevent过滤规则,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

说明:本文基于Android2.3和Linux2.6,其余版本仅供参考。

Android2.3及Linux2.6.29内核模拟器版本编译与调试

阅读本文前请阅读:Linux总线、设备、驱动与设备节点创建、用户测试程序 

一、netlink监听程序

testnet.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <asm/types.h>
//该头文件需要放在netlink.h前面防止编译出现__kernel_sa_family未定义
#include <sys/socket.h>  
#include <linux/netlink.h>void MonitorNetlinkUevent()
{int sockfd;struct sockaddr_nl sa;int len;char buf[4096];struct iovec iov;struct msghdr msg;int i;memset(&sa,0,sizeof(sa));sa.nl_family=AF_NETLINK;sa.nl_groups=NETLINK_KOBJECT_UEVENT;sa.nl_pid = 0;//getpid(); both is okmemset(&msg,0,sizeof(msg));iov.iov_base=(void *)buf;iov.iov_len=sizeof(buf);msg.msg_name=(void *)&sa;msg.msg_namelen=sizeof(sa);msg.msg_iov=&iov;msg.msg_iovlen=1;sockfd=socket(AF_NETLINK,SOCK_RAW,NETLINK_KOBJECT_UEVENT);if(sockfd==-1)printf("socket creating failed:%s\n",strerror(errno));if(bind(sockfd,(struct sockaddr *)&sa,sizeof(sa))==-1)printf("bind error:%s\n",strerror(errno));while(1){memset(buf,0,sizeof(buf));len=recvmsg(sockfd,&msg,0);if(len<0){}//printf("receive error\n");else if(len<32||len>sizeof(buf))printf("invalid message");for(i=0;i<len;i++)if(*(buf+i)=='\0')buf[i]='\n';printf("received %d bytes\n%s\n",len,buf);}
}int main(int argc,char **argv)
{MonitorNetlinkUevent();return 0;
}
Android.mk
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)LOCAL_SRC_FILES:= \testnet.cLOCAL_SHARED_LIBRARIES := \libutils LOCAL_MODULE:= testnetLOCAL_MODULE_TAGS := optionalinclude $(BUILD_EXECUTABLE)

二、如下是添加打印后的信息(Android用户空间udev为ueventd,属于init的一个软连接)

1.总线驱动

root@android:/data/tank # insmod testbus.ko 

//内核模块加载成功,会上报netlink                                  
TK------->>>>>>kobject_uevent.c>>>>>kobject_uevent_env
kobject: 'testbus' (bf02d1c8): kobject_uevent_env
TK------->>>>>>kobject_uevent.c>>>>>kobject_uevent_env>>>>netlink_broadcast
//总线注册上报netlink
TK---testlddbus.c----->>>>ldd_bus_init
TK--------->>>>>>bus.c>>>>>>bus_register,,,bus->name is ldd
TK-------_>>>>kobject.c>>>>>>>kset_register
TK-------_>>>>kobject.c>>>>>>>kset_register>>>kobject_uevent
TK------->>>>>>kobject_uevent.c>>>>>kobject_uevent_env
kobject: 'ldd' (cfcdb1c8): kobject_uevent_env
TK------->>>>>>kobject_uevent.c>>>>>kobject_uevent_env>>>>netlink_broadcast
//设备注册由于没有分配主设备号,不上报netlink
TK-------_>>>>>>>>core.c>>>>>device_register
TK-------_>>>>>>>>core.c>>>>>device_add
TK-------_>>>>>>>>core.c>>>>>device_add>>>>>kobject_uevent
TK------->>>>>>kobject_uevent.c>>>>>kobject_uevent_env
TK-------_>>>>>>drivers/base/core.c>>dev_uevent_filter>>ktype is 0xc032137c,&device_ktype is 0xc032137c
TK-------->>>>drivers/base/core.c>>dev_uevent_filter>>dev->uevent_suppress is 0
TK-------->>>>drivers/base/core.c>>dev_uevent_filter>>dev->bus is 0x0
TK-------->>>>drivers/base/core.c>>dev_uevent_filter>>dev->class is 0x0
kobject: 'ldd0' (bf02d074): kobject_uevent_env
kobject: 'ldd0' (bf02d074): kobject_uevent_env: filter function caused the event to drop!
//用户空间ueventd的处理,2个uevent事件
init: TK-------_>>>>>uevent.c>>>>>>ueventd_main>>>>ufd.fd is 4
init: TK-------_>>>>>>device.c.>>>>>>handle_device_fd
init: TK------------>>>>device.c>>>>>>handle_device_event>>>>uevent->major is -1,uevent->minor is -1
init: TK------------>>>>device.c>>>>>>handle_device_event>>>>>>uevent->major and minor < 0
init: TK------------>>>>device.c>>>>>>handle_device_event>>>>uevent->major is -1,uevent->minor is -1
init: TK------------>>>>device.c>>>>>>handle_device_event>>>>>>uevent->major and minor < 0
received 83 bytes
add@/module/testbus
ACTION=add
DEVPATH=/module/testbus
SUBSYSTEM=module
SEQNUM=649received 66 bytes
add@/bus/ldd
ACTION=add
DEVPATH=/bus/ldd
SUBSYSTEM=bus
SEQNUM=650
说明:

drivers/base/bus.c

int bus_register(struct bus_type *bus){retval = kset_register(&priv->subsys);/*lib/kobject.cint kset_register(struct kset *k){kobject_uevent(&k->kobj, KOBJ_ADD);}*/retval = bus_create_file(bus, &bus_attr_uevent);priv->devices_kset = kset_create_and_add("devices", NULL, &priv->subsys.kobj);priv->drivers_kset = kset_create_and_add("drivers", NULL, &priv->subsys.kobj);
}

2.插入设备

root@android:/data/tank # insmod testdev.ko 

//内核模块加载会上报netlink                          
TK------->>>>>>kobject_uevent.c>>>>>kobject_uevent_env
kobject: 'testdev' (bf0334e0): kobject_uevent_env
TK------->>>>>>kobject_uevent.c>>>>>kobject_uevent_env>>>>netlink_broadcast
//没有主设备号,不会上报netlink
TK---testdev.c----->>>>mini_init
TK-------_>>>>>>>>core.c>>>>>device_register
TK-------_>>>>>>>>core.c>>>>>device_add
TK-------_>>>>>>>>core.c>>>>>device_add>>>>>kobject_uevent
TK------->>>>>>kobject_uevent.c>>>>>kobject_uevent_env
kobject: 'mini' (bf0333dc): kobject_uevent_env
TK-------_>>>>>>drivers/base/core.c>>dev_uevent_filter>>ktype is 0xc032137c,&device_ktype is 0xc032137c
TK-------->>>>drivers/base/core.c>>dev_uevent_filter>>dev->uevent_suppress is 0
TK-------->>>>drivers/base/core.c>>dev_uevent_filter>>dev->bus is 0xbf001130
TK---testlddbus.c----->>>>ldd_uevent
TK-------_>>>>>>drivers/base/core.c>>dev_uevent>>retval is -12
kobject: 'mini' (bf0333dc): kobject_uevent_env: uevent() returned -12  
//用户空间ueventd的处理,1个uevent事件
init: TK-------_>>>>>uevent.c>>>>>>ueventd_main>>>>ufd.fd is 4
init: TK-------_>>>>>>device.c.>>>>>>handle_device_fd
init: TK------------>>>>device.c>>>>>>handle_device_event>>>>uevent->major is -1,uevent->minor is -1
init: TK------------>>>>device.c>>>>>>handle_device_event>>>>>>uevent->major and minor < 0
received 83 bytes
add@/module/testdev
ACTION=add
DEVPATH=/module/testdev
SUBSYSTEM=module
SEQNUM=651
3.设备驱动

root@android:/data/tank # insmod testdriver.ko  

//内核模块成功加载,会上报netlink                              
TK------->>>>>>kobject_uevent.c>>>>>kobject_uevent_env
kobject: 'testdriver' (bf039c34): kobject_uevent_env
TK------->>>>>>kobject_uevent.c>>>>>kobject_uevent_env>>>>netlink_broadcast
//类注册会上报netlink(class_creat)
TK---testdriver.c----->>>>driver_init
Tk--------->>>>>driver.c>>>>>>driver_register
TK---------_>>>>>bus.c>>>>>>>bus_add_driver
TK---testbus.c----->>>>ldd_match
TK---testbus.c----->>>>ldd_match
TK---testlddbus.c----->>>>ldd_drv_probe
TK----testdriver.c------>>>>driver_probe mini
TK-------_>>>>kobject.c>>>>>>>kset_register
TK-------_>>>>kobject.c>>>>>>>kset_register>>>kobject_uevent
TK------->>>>>>kobject_uevent.c>>>>>kobject_uevent_env
kobject: 'mymodule' (cfee5b68): kobject_uevent_env
TK------->>>>>>kobject_uevent.c>>>>>kobject_uevent_env>>>>netlink_broadcast
//有主设备号时设备注册上报netlink
TK-------->>>>>core.c>>>>device_create
TK-------_>>>>>>>>core.c>>>>>device_register
TK-------_>>>>>>>>core.c>>>>>device_add
TK-------_>>>>>>>>core.c>>>>>device_add>>>>>kobject_uevent
TK------->>>>>>kobject_uevent.c>>>>>kobject_uevent_env
kobject: 'mymod0' (caa4e724): kobject_uevent_env
TK-------_>>>>>>drivers/base/core.c>>dev_uevent_filter>>ktype is 0xc032137c,&device_ktype is 0xc032137c
TK-------->>>>drivers/base/core.c>>dev_uevent_filter>>dev->uevent_suppress is 0
TK-------->>>>drivers/base/core.c>>dev_uevent_filter>>dev->bus is 0x0
TK-------->>>>drivers/base/core.c>>dev_uevent_filter>>dev->class is 0xcaade760
TK-------_>>>>>>drivers/base/core.c>>dev_uevent>>retval is 0
TK------->>>>>>kobject_uevent.c>>>>>kobject_uevent_env>>>>netlink_broadcast
//驱动注册都会上报netlink
TK---------_>>>>>bus.c>>>>>>>bus_add_driver>>>>kobject_uevent
TK------->>>>>>kobject_uevent.c>>>>>kobject_uevent_env
kobject: 'mini' (c8aa48c0): kobject_uevent_env
TK------->>>>>>kobject_uevent.c>>>>>kobject_uevent_env>>>>netlink_broadcast
//用户空间ueventd的处理,4个uevent事件
init: TK-------_>>>>>uevent.c>>>>>>ueventd_main>>>>ufd.fd is 4
init: TK-------_>>>>>>device.c.>>>>>>handle_device_fd
init: TK------------>>>>device.c>>>>>>handle_device_event>>>>uevent->major is -1,uevent->minor is -1
init: TK------------>>>>device.c>>>>>>handle_device_event>>>>>>uevent->major and minor < 0
init: TK------------>>>>device.c>>>>>>handle_device_event>>>>uevent->major is -1,uevent->minor is -1
init: TK------------>>>>device.c>>>>>>handle_device_event>>>>>>uevent->major and minor < 0
init: TK------------>>>>device.c>>>>>>handle_device_event>>>>uevent->major is 248,uevent->minor is 0
init: TK------_>>>>>>>>>make_devices
init: TK------------>>>>device.c>>>>>>handle_device_event>>>>uevent->major is -1,uevent->minor is -1
init: TK------------>>>>device.c>>>>>>handle_device_event>>>>>>uevent->major and minor < 0
received 89 bytes
add@/module/testdriver
ACTION=add
DEVPATH=/module/testdriver
SUBSYSTEM=module
SEQNUM=652received 82 bytes
add@/class/mymodule
ACTION=add
DEVPATH=/class/mymodule
SUBSYSTEM=class
SEQNUM=653received 137 bytes
add@/devices/virtual/mymodule/mymod0
ACTION=add
DEVPATH=/devices/virtual/mymodule/mymod0
SUBSYSTEM=mymodule
MAJOR=248
MINOR=0
SEQNUM=654received 96 bytes
add@/bus/ldd/drivers/mini
ACTION=add
DEVPATH=/bus/ldd/drivers/mini
SUBSYSTEM=drivers
SEQNUM=655
说明:

drivers/base/driver.c  

int driver_register(struct device_driver *drv){ret = bus_add_driver(drv);/*drivers/base/bus.cint bus_add_driver(struct device_driver *drv){kobject_uevent(&priv->kobj, KOBJ_ADD);}*/
}
include/linux/device.h
#define class_create(owner, name)		\
({						\static struct lock_class_key __key;	\__class_create(owner, name, &__key);	\/*drivers/base/class.cretval = __class_register(cls, key);int __class_register(struct class *cls, struct lock_class_key *key){error = kset_register(&cp->subsys);}lib/kobject.cint kset_register(struct kset *k){kobject_uevent(&k->kobj, KOBJ_ADD);}*/ 
})

drivers/base/core.c

struct device *device_create(struct class *class, struct device *parent,dev_t devt, void *drvdata, const char *fmt, ...){dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);/*struct device *device_create_vargs(struct class *class, struct device *parent,dev_t devt, void *drvdata, const char *fmt,va_list args){retval = device_register(dev);}int device_register(struct device *dev){return device_add(dev);}int device_add(struct device *dev){error = kobject_add(&dev->kobj, dev->kobj.parent, "%s", dev_name(dev));if (MAJOR(dev->devt)) {  //有设备号才会上报uevent事件!!error = device_create_file(dev, &devt_attr);if (error)  goto ueventattrError;error = device_create_sys_dev_entry(dev);if (error) goto devtattrError;}kobject_uevent(&dev->kobj, KOBJ_ADD);}*/
}
总结:

bus_register,driver_register,class_create,device_create以及内核模块加载有uevent通过netlink上报,

device_register的uevent会根据是否有设备号来决定是否上报事件

三、最后分析下内核kobject_uevent函数:

对于uevent通过netlink的事件上报,其实还是kobject这个类起作用。

1.看总线注册给Linux内核总线模型的情况,即bus_register

先回忆下内核启动时总线模型初始化:

drivers/base/bus.c

int __init buses_init(void)
{bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL);
}
static struct kset_uevent_ops bus_uevent_ops = {.filter = bus_uevent_filter,
};

具体过滤规则可以看代码;我想说的是Linux内核总线模型的过滤函数很简单、没有特别限制,这样在我们注册一个新的总线(bus_register)时、都会上报uevent事件。

2.看设备注册给Linux内核设备模型的情况,即device_register

先会议下内核启动时设备模型初始化:

drivers/base/core.c

int __init devices_init(void)
{devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
}
static struct kset_uevent_ops device_uevent_ops = {.filter =	dev_uevent_filter,.name =		dev_uevent_name,.uevent =	dev_uevent,
}

具体过滤规则可以看代码;我想说的是Linux内核设备模型的过滤函数和是否uevent上报函数比较复杂,其中过滤函数功能是:如果新注册到设备模型的设备不属于总线、不会上报uevent,如果新注册设备没有主次设备号也不会上报uevent;只有上述两个同时满足时,才会上报。

lib/kobject_uevent.c

int kobject_uevent(struct kobject *kobj, enum kobject_action action){return kobject_uevent_env(kobj, action, NULL);
}
EXPORT_SYMBOL_GPL(kobject_uevent);
int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,char *envp_ext[]){struct kobject *top_kobj;struct kset *kset;struct kset_uevent_ops *uevent_ops;top_kobj = kobj;while (!top_kobj->kset && top_kobj->parent)top_kobj = top_kobj->parent;kset = top_kobj->kset;uevent_ops = kset->uevent_ops;if (uevent_ops && uevent_ops->filter) //kset的过滤规则,详细看drivers/base/core.c的dev_uevent_filter函数if (!uevent_ops->filter(kset, kobj)) {/*drivers/base/core.cint __init devices_init(void){devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);}static struct kset_uevent_ops device_uevent_ops = {.filter =	dev_uevent_filter,//上处uevent_ops->filter(kset, kobj)就是这个.name =		dev_uevent_name,.uevent =	dev_uevent,//下边uevent_ops->uevent(kset, kobj, env)就是这个};*/pr_debug("kobject: '%s' (%p): %s: filter function ""caused the event to drop!\n",kobject_name(kobj), kobj, __func__); //change by tank from pr_debug to printkreturn 0;}if (uevent_ops && uevent_ops->uevent) {//无设备号的设备注册不会被上报!详细看drivers/base/core.c的dev_uevent函数retval = uevent_ops->uevent(kset, kobj, env);if (retval) {pr_debug("kobject: '%s' (%p): %s: uevent() returned ""%d\n", kobject_name(kobj), kobj,__func__, retval); //change by tank from pr_debug to printkgoto exit;}}retval = netlink_broadcast_filtered(uevent_sock, skb, 0, 1, GFP_KERNEL,kobj_bcast_filter, kobj);  //netlink上报事件
}
net/netlink/af_netlink.c
int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 pid,u32 group, gfp_t allocation,int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),void *filter_data){}

这篇关于《Linux设备节点创建》内核kobject上报uevent过滤规则的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/812283

相关文章

JAVA中整型数组、字符串数组、整型数和字符串 的创建与转换的方法

《JAVA中整型数组、字符串数组、整型数和字符串的创建与转换的方法》本文介绍了Java中字符串、字符数组和整型数组的创建方法,以及它们之间的转换方法,还详细讲解了字符串中的一些常用方法,如index... 目录一、字符串、字符数组和整型数组的创建1、字符串的创建方法1.1 通过引用字符数组来创建字符串1.2

Linux磁盘分区、格式化和挂载方式

《Linux磁盘分区、格式化和挂载方式》本文详细介绍了Linux系统中磁盘分区、格式化和挂载的基本操作步骤和命令,包括MBR和GPT分区表的区别、fdisk和gdisk命令的使用、常见的文件系统格式以... 目录一、磁盘分区表分类二、fdisk命令创建分区1、交互式的命令2、分区主分区3、创建扩展分区,然后

Linux中chmod权限设置方式

《Linux中chmod权限设置方式》本文介绍了Linux系统中文件和目录权限的设置方法,包括chmod、chown和chgrp命令的使用,以及权限模式和符号模式的详细说明,通过这些命令,用户可以灵活... 目录设置基本权限命令:chmod1、权限介绍2、chmod命令常见用法和示例3、文件权限详解4、ch

Linux内核之内核裁剪详解

《Linux内核之内核裁剪详解》Linux内核裁剪是通过移除不必要的功能和模块,调整配置参数来优化内核,以满足特定需求,裁剪的方法包括使用配置选项、模块化设计和优化配置参数,图形裁剪工具如makeme... 目录简介一、 裁剪的原因二、裁剪的方法三、图形裁剪工具四、操作说明五、make menuconfig

Linux使用nohup命令在后台运行脚本

《Linux使用nohup命令在后台运行脚本》在Linux或类Unix系统中,后台运行脚本是一项非常实用的技能,尤其适用于需要长时间运行的任务或服务,本文我们来看看如何使用nohup命令在后台... 目录nohup 命令简介基本用法输出重定向& 符号的作用后台进程的特点注意事项实际应用场景长时间运行的任务服

什么是cron? Linux系统下Cron定时任务使用指南

《什么是cron?Linux系统下Cron定时任务使用指南》在日常的Linux系统管理和维护中,定时执行任务是非常常见的需求,你可能需要每天执行备份任务、清理系统日志或运行特定的脚本,而不想每天... 在管理 linux 服务器的过程中,总有一些任务需要我们定期或重复执行。就比如备份任务,通常会选在服务器资

手把手教你idea中创建一个javaweb(webapp)项目详细图文教程

《手把手教你idea中创建一个javaweb(webapp)项目详细图文教程》:本文主要介绍如何使用IntelliJIDEA创建一个Maven项目,并配置Tomcat服务器进行运行,过程包括创建... 1.启动idea2.创建项目模板点击项目-新建项目-选择maven,显示如下页面输入项目名称,选择

如何安装HWE内核? Ubuntu安装hwe内核解决硬件太新的问题

《如何安装HWE内核?Ubuntu安装hwe内核解决硬件太新的问题》今天的主角就是hwe内核(hardwareenablementkernel),一般安装的Ubuntu都是初始内核,不能很好地支... 对于追求系统稳定性,又想充分利用最新硬件特性的 Ubuntu 用户来说,HWEXBQgUbdlna(Har

Linux限制ip访问的解决方案

《Linux限制ip访问的解决方案》为了修复安全扫描中发现的漏洞,我们需要对某些服务设置访问限制,具体来说,就是要确保只有指定的内部IP地址能够访问这些服务,所以本文给大家介绍了Linux限制ip访问... 目录背景:解决方案:使用Firewalld防火墙规则验证方法深度了解防火墙逻辑应用场景与扩展背景:

Mybatis拦截器如何实现数据权限过滤

《Mybatis拦截器如何实现数据权限过滤》本文介绍了MyBatis拦截器的使用,通过实现Interceptor接口对SQL进行处理,实现数据权限过滤功能,通过在本地线程变量中存储数据权限相关信息,并... 目录背景基础知识MyBATis 拦截器介绍代码实战总结背景现在的项目负责人去年年底离职,导致前期规