kobject 与sysfs属性文件读写
创始人
2024-02-14 19:24:03
0

kobject和kset的简单总结

kobject是struct kobject类型的对象。Kobject 有一个名字和一个引用计数。kobject 也有一个父指针(允许 kobjects 被安排到层次结构中),一个特定的类型,也许还有一个在 sysfs 虚拟文件系统中的表示。Kobject 本身通常并不有趣;相反,它们通常嵌入在一些其他结构中,其中包含代码真正感兴趣的内容。

• ktype是与 kobject 关联的类型ktype 控制当一个 kobject 不再被引用时会发生什么,以及 kobject 在 sysfs 中的默认表示。

kset是一组 kobjects,所有这些 kobjects 都嵌入在相同类型的结构中。kset 是 kobject 集合的基本容器类型。Ksets 包含它们自己的 kobjects,这是值得的。除其他外,这意味着一个 kobject 的父对象通常是包含它的 kset,尽管事情通常不必那样。当您看到充满条目的 sysfs 目录时,通常这些条目中的每一个都对应于同一 kset 中的一个 kobject。

• A subsystem是 kset的集合,它们共同构成内核的主要子部分。子系统通常对应于 sysfs 中的顶级目录。

struct  kobject {//目录抽象---基类const char		*name;struct list_head	entry;struct kobject		*parent;struct kset		*kset;//subsystem  or dirstruct kobj_type	*ktype;struct kernfs_node	*sd;//sysfsstruct kref		kref;//refcount
#ifdef CONFIG_DEBUG_KOBJECT_RELEASEstruct delayed_work	release;
#endifunsigned int state_initialized:1;unsigned int state_in_sysfs:1;unsigned int state_add_uevent_sent:1;unsigned int state_remove_uevent_sent:1;unsigned int uevent_suppress:1;
};kobj_type代表Kobject(严格地讲,是包含了Kobject的数据结构)的属性操作集合(由于通用性,多个Kobject可能共用同一个属性操作集,因此把Ktype独立出来了)
struct   kobj_type {void (*release)(struct kobject *kobj);//kobject release func when kobject refcont is 0const struct sysfs_ops  *sysfs_ops;//目录的操作ops----》attribute中的对应的ops,也就是目录---》文件struct attribute  **default_attrs;//设置的默认属性 会被框架自动创建const struct kobj_ns_type_operations  *(*child_ns_type)(struct kobject *kobj);const void   *(*namespace)(struct kobject *kobj);
};
This structure is used to describe a particular type of kobject (or, more correctly, of containing object). 
Every kobject needs to have an associated kobj_type structure; a pointer to that structure must be specified when you
call kobject_init() or kobject_init_and_add().The release field in struct kobj_type is, of course, a pointer to the release() method for this type of kobject. The other two fields (sysfs_ops and default_attrs) control how objects of this type are represented in
sysfs; they are beyond the scope of this document.The default_attrs pointer is a list of default attributes that will be automatically created for any kobject that is registered with this ktypekset 与 kobj 都是目录,既然是目录,那么应该就是一个树状结构,每一个目录都将有一个父节点:在kset中使用kset.kobj->parent 指定,在kboject中使用  kobj->parent 指定kset 主要用途 是发送uevent和 创建同一类kobject。/*** struct kset - a set of kobjects of a specific type, belonging to a specific subsystem.** A kset defines a group of kobjects.  They can be individually  different "types" but overall these kobjects all want to be grouped* together and operated on in the same manner.  ksets are used to  define the attribute callbacks and other common events that happen to* a kobject.** @list: the list of all kobjects for this kset* @list_lock: a lock for iterating over the kobjects* @kobj: the embedded kobject for this kset (recursion, isn't it fun...)* @uevent_ops: the set of uevent operations for this kset.  These are* called whenever a kobject has something happen to it so that the kset* can add new environment variables, or filter out the uevents if so* desired.*/
struct   kset {// a group of kobjectsstruct list_head   list;//the list of all kobjects for this ksetspinlock_t   list_lock;struct kobject kobj;//基类,kset也是一种kobjconst struct kset_uevent_ops *uevent_ops;//some event to user space
};

 


kobject 注册函数

/*** kobject_init_and_add - initialize a kobject structure and add it to the kobject hierarchy* @kobj: pointer to the kobject to initialize* @ktype: pointer to the ktype for this kobject.* @parent: pointer to the parent of this kobject.* @fmt: the name of the kobject.** This function combines the call to kobject_init() and* kobject_add().  The same type of error handling after a call to* kobject_add() and kobject lifetime rules are the same here.*/
int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,struct kobject *parent, const char *fmt, ...)//ktype need init and set
{va_list args;int retval;kobject_init(kobj, ktype);//init,fill kobject structva_start(args, fmt);retval = kobject_add_varg(kobj, parent, fmt, args);va_end(args);return retval;
}

 kobject_add_varg--》kobject_add_internal

static int kobject_add_internal(struct kobject *kobj)
{int error = 0;struct kobject *parent;if (!kobj)return -ENOENT;if (!kobj->name || !kobj->name[0]) {WARN(1, "kobject: (%p): attempted to be registered with empty ""name!\n", kobj);return -EINVAL;}parent = kobject_get(kobj->parent);/* join kset if set, use it as parent if we do not already have one *//*如果当前object还没设置父对象, 则引用设置的kset对象为父对象,        如果设置了,则将其加入到其设置的kset集合中(将其挂载到kset的链表上)然后设置父对象,即初始化kobj->parent*/if (kobj->kset) { if (!parent)parent = kobject_get(&kobj->kset->kobj);kobj_kset_join(kobj);kobj->parent = parent;}pr_debug("kobject: '%s' (%p): %s: parent: '%s', set: '%s'\n",kobject_name(kobj), kobj, __func__,parent ? kobject_name(parent) : "",kobj->kset ? kobject_name(&kobj->kset->kobj) : "");error = create_dir(kobj);//creat and fillif (error) {kobj_kset_leave(kobj);kobject_put(parent);kobj->parent = NULL;/* be noisy on error issues */if (error == -EEXIST)WARN(1, "%s failed for %s with ""-EEXIST, don't try to register things with ""the same name in the same directory.\n",__func__, kobject_name(kobj));elseWARN(1, "%s failed for %s (error: %d parent: %s)\n",__func__, kobject_name(kobj), error,parent ? kobject_name(parent) : "'none'");} elsekobj->state_in_sysfs = 1;//normal is 1return error;
}

 creat_dir 部分注释

static int create_dir(struct kobject *kobj)//creat dir at sysfs
{const struct kobj_ns_type_operations *ops;int error;error = sysfs_create_dir_ns(kobj, kobject_namespace(kobj));//kobject-->sysfsif (error)return error;error = populate_dir(kobj);//有attr的话创建 attr file throught sysfsif (error) {sysfs_remove_dir(kobj);return error;}/** @kobj->sd may be deleted by an ancestor going away.  Hold an* extra reference so that it stays until @kobj is gone.*/sysfs_get(kobj->sd);/** If @kobj has ns_ops, its children need to be filtered based on* their namespace tags.  Enable namespace support on @kobj->sd.*/ops = kobj_child_ns_ops(kobj);if (ops) {BUG_ON(ops->type <= KOBJ_NS_TYPE_NONE);BUG_ON(ops->type >= KOBJ_NS_TYPES);BUG_ON(!kobj_ns_type_registered(ops->type));sysfs_enable_ns(kobj->sd);}return 0;
}

kset 注册过程

/*** kset_register - initialize and add a kset.* @k: kset.*/
int kset_register(struct kset *k)//first init,then add
{int err;if (!k)return -EINVAL;kset_init(k);err = kobject_add_internal(&k->kobj);if (err)return err;kobject_uevent(&k->kobj, KOBJ_ADD);//通过uevent机制通知用户空间return 0;
}

kobjkset关系总结

kobj和kset并不是完全的父子关系。kset算是kobj的“接盘侠”,当kobj没有所属的parent时,才让kset来接盘当parent;如果连kset也没有,那该kobj属于顶层对象,其sysfs目录将位于/sys/下。正因为kobj和kset并不是完全的父子关系,因此在注册kobj时,将同时对parent及其所属的kset增加引用计数。若parent和kset为同一对象,则会对kset增加两次引用计数。

kset内部本身也包含一个kobj对象,在sysfs中也表现为目录;所不同的是,kset要承担kobj状态变动消息的发送任务。因此,首先kset会将所属的kobj组织在kset.list下,同时,通过uevent_ops在合适时候发送消息。

对于kobject_add()来说,它的输入信息是:kobj-parent、kobj-name,kobject_add()优先使用传入的parent作为kobj->parent;其次,使用kset作为kobj->parent

kobj状态变动后,必须依靠所关联的kset来向用户空间发送消息;若无关联kset(kobj向上组成的树中,任何成员都无所属的kset),则kobj无法发送用户消息。 


sysfs 文件打开,读,写过程代码总结

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
客厅放八骏马摆件可以吗(家里摆... 今天给各位分享客厅放八骏马摆件可以吗的知识,其中也会对家里摆八骏马摆件好吗进行解释,如果能碰巧解决你...
苏州离哪个飞机场近(苏州离哪个... 本篇文章极速百科小编给大家谈谈苏州离哪个飞机场近,以及苏州离哪个飞机场近点对应的知识点,希望对各位有...