一、Kobject
类似于C++中的基类,Kobject常被嵌入于其他类型(即:容器)中。如bus,devices, drivers 都是典型的容器。这些容器通过kobject连接起来,形成了一个树状结构。
- struct kobject {
- const char *name;
- struct list_head entry;
- struct kobject *parent;
- struct kset *kset;
- struct kobj_type *ktype;
- struct sysfs_dirent *sd;
- struct kref kref;
- unsigned 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;
- };
void kobject_init(struct kobject * kobj)
初始化kobject结构
int kobject_add(struct kobject * kobj)
将kobject对象注册到Linux系统
int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,structkobject *parent, const char
*name, ...)
初始化kobject,并将其注册到linux系统
void kobject_del(struct kobject * kobj)
从Linux系统中删除kobject对象
struct kobject *kobject_get(struct kobject *kobj)
将kobject对象的引用计数加1,同时返回该对象指针。
void kobject_put(struct kobject * kobj)
将kobject对象的引用计数减1,如果引用计数降为0,则调用release方法释放该kobject对象。
二、struct kobj_type
kobject的ktype成员是一个指向kobj_type结构的指针,该结构中记录了kobject对象的一些属性。
struct kobj_type {
void(*release)(struct kobject *kobj);
struct sysfs_ops *sysfs_ops;
struct attribute **default_attrs;
};
release:用于释放kobject占用的资源,当kobject的引用计数为0时被调用。
三、struct attributestruct attribute {
char* name; /*属性文件名*/
structmodule * owner;
mode_t mode; /*属性的保护位*/
};
struct attribute (属性):对应于kobject的目录下的一个文件,Name成员就是文件名。
四、struct sysfs_opsstruct sysfs_ops {
ssize_t (*show)(struct kobject *, struct attribute *,char *);
ssize_t (*store)(struct kobject *,struct attribute *,const char *,size_t);
};
show:当用户读属性文件时,该函数被调用,该函数将属性值存入buffer中返回给用户态;
store:当用户写属性文件时,该函数被调用,用于存储用户传入的属性值。
五、实例分析
1、内核模块源代码
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kobject.h>
#include <linux/sysfs.h>
struct kobject kobj;
static struct attribute test_attr = {
.name = "kobj_config",
.mode = S_IRWXUGO,
};
static struct attribute *def_attrs[] = {
&test_attr,
NULL,
};
static void obj_test_release(struct kobject *kobject)
{
printk("eric_test: release .\n");
}
static ssize_t
kobj_test_show(struct kobject *kobject, struct attribute *attr,char *buf)
{
printk("have show.\n");
printk("attrname:%s.\n", attr->name);
sprintf(buf,"%s\n",attr->name);
return strlen(attr->name)+2;
}
static ssize_t
kobj_test_store(struct kobject *kobject,struct attribute *attr,const char *buf, size_t count)
{
printk("havestore\n");
printk("write: %s\n",buf);
return count;
}
static const struct sysfs_ops obj_test_sysops ={
.show = kobj_test_show,
.store = kobj_test_store,
};
struct kobj_type ktype = {
.release = obj_test_release,
.sysfs_ops=&obj_test_sysops,
.default_attrs=def_attrs,
};
static __init int kobj_test_init(void)
{
printk("kboject test init.\n");
kobject_init(&kobj, &ktype);// 初始化kobject
kobject_add(&kobj, NULL, "kobject_test");// 添加kobject到内核
return 0;
}
static __exit void kobj_test_exit(void)
{
printk("kobject test exit.\n");
kobject_del(&kobj); // 删除kobject
}
module_init(kobj_test_init);
module_exit(kobj_test_exit);
MODULE_LICENSE("Dual BSD/GPL");
2、Makefile
ifeq ($(KERNELRELEASE),)
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
all:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
$(MAKE) -C $(KERNELDIR) M=$(PWD) clean
else
obj-m := kobj_test.o
endif