Kernel object Kobject of Linux device model

Source: Internet
Author: User

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), &AMP;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.