All files in Linux
1. Kernel Object Kobject
structKobject {Const Char*name;//object's name structList_head entry;structKobject *parent;//upper layer of object structKset *kset;the Kset pointer to which the current object belongs structKobj_type *ktype;//File operation set structSysfs_dirent *SD;structKref Kref;unsigned intState_initialized:1;unsigned intSTATE_IN_SYSFS:1;unsigned intState_add_uevent_sent:1;unsigned intState_remove_uevent_sent:1;unsigned intUevent_suppress:1;}; Related operation functions://Initialization of a kobjectvoidKobject_init (structKobject *kobj,structKobj_type *ktype)//Add a kobjectintKobject_add (structKobject *kobj,structKobject *parent,Const Char*FMT, ...) Kobject_add_varg (kobj, parent, FMT, args); Kobject_set_name_vargs (Kobj, FMT, Vargs); Kobject_add_internal (structKobject *kobj)if(Kobj->kset) {if(!parent) parent = Kobject_get (&kobj->kset->kobj); Kobj_kset_join (Kobj); Kobj->parent = parent; } error = Create_dir (kobj);//Create a directoryError = Sysfs_create_dir (kobj); Create_diif (kobj->parent) PARENT_SD = kobj->parent->sd;ElsePARENT_SD = &sysfs_root; Create_dir (Kobj, PARENT_SD, type, NS, Kobject_name (kobj), &SD);//Initialize and addintKobject_init_and_add (structKobject *kobj,structKobj_type *ktype,structKobject *parent,Const Char*FMT, ...)//Create and addstructKobject *kobject_create_and_add (Const Char*name,structKobject *parent) Kobj = Kobject_create (); Kobject_init (Kobj, &dynamic_kobj_ktype); retval = Kobject_add (kobj, parent,'%s ', name);extern voidKobject_del (structKobject *kobj); Example:#include <linux/module.h>#include <linux/kobject.h>Static structKobject * parent =NULL;Static structKobject * Child =NULL;Static int__init Kobject_test_init (void) {PRINTK (kern_info"%s\n", __function__); Parent = Kobject_create_and_add ("Father_obj",NULL); Child = Kobject_create_and_add ("Child_obj", parent);return 0;}Static void__exit Kobject_test_exit (void) {PRINTK (kern_info"%s\n", __function__); Kobject_del (child); Kobject_del (parent);} Module_init (Kobject_test_init); Module_exit (Kobject_test_exit); Module_author ("Derrick Email: [Email protected]"); Module_license ("GPL v2"); Module_description ("Kobject test Module"); Module_alias ("Kobject test Module");//In the SYS directory://sys/father_obj/child_obj Directory
Kernel object Kobject of Linux device model