1、kobject介绍
kobject的全称是kernel object,即内核对象。每一个kobject都会对应系统/sys/下的一个目录。
2、相关结构体和api介绍
2.1 struct kobject
// include/linux/kobject.h
2.2 kobject_create_and_add
kobject_create_and_add
kobject_create
kobject_add
kobject_add_varg
2.3 kobject_init_and_add
kobject_init_and_add
kobject_init
kobject_init_internal
kobject_add_varg
2.4 kobject_put
kobject_put
kref_put
kref_sub
3、实验操作
#include<linux/module.h>
#include<linux/init.h>
#include<linux/kernel.h>
#include<linux/kobject.h>
#include<linux/slab.h>
struct kobject *my_kobject01;
struct kobject *my_kobject02;
struct kobject *my_kobject03;
struct kobj_type mytype;
/*
创建kobject的两种方法:
1、kobject_create_and_add
2、kzalloc->kobject_init_and_add
*/
static int my_kobject_init(void)
{
int ret;
my_kobject01 = kobject_create_and_add("my_kobject01",NULL); // /sys/my_kobject01
my_kobject02 = kobject_create_and_add("my_kobject02",my_kobject01); // /sys/my_kobject01/my_kobject02
my_kobject03 = kzalloc(sizeof(struct kobject),GFP_KERNEL);
ret = kobject_init_and_add(my_kobject03,&mytype,NULL,"%s","my_kobject03");// /sys/my_kobject03
return 0;
}
static void my_kobject_exit(void)
{
kobject_put(my_kobject01);
kobject_put(my_kobject02);
kobject_put(my_kobject03);
}
module_init(my_kobject_init);
module_exit(my_kobject_exit);
MODULE_LICENSE("GPL");
//Makefile
obj-m += kobject.o
KDIR := /home/johan/share/rk3588/linux_sdk/kernel
PWD ?= $(shell pwd)
all:
make -C $(KDIR) M=$(PWD) modules
clean:
rm -f *.ko *.o *.mod.o *.mod.c