- #include<linux/device.h>
- #include<linux/module.h>
- #include<linux/kernel.h>
- #include<linux/stat.h>
- #include<linux/init.h>
- #include<linux/string.h>
- #include<linux/sysfs.h>
- #include<linux/kobject.h>
- MODULE_LICENSE("Dual BSD/GPL");
- MODULE_AUTHOR("SM");
- struct kset *set1;
- struct kset *set2;
- struct kset set3;
- struct kobject set4;
- int kset_filter1(struct kset *kset,struct kobject *kobj)
- {
- printk("filter 1 work !");
- return 1;
- }
- const char * kset_name1(struct kset* kset,struct kobject *kobj)
- {
- static char buf[20];
- printk("name 1\n");
- printk("%s ",kobj->name);
- sprintf(buf,"chage %s \n",kobj->name);
- return buf;
- }
- /*
- <linux/kobject.h>
- #define UEVENT_NUM_ENVP 32 // number of env pointers
- #define UEVENT_BUFFER_SIZE 2048 // buffer for the variables
- struct kobj_uevent_env {
- char *envp[UEVENT_NUM_ENVP];
- int envp_idx;
- char buf[UEVENT_BUFFER_SIZE];
- int buflen;
- };
- */
- int kset_uevent1(struct kset *kset,struct kobject *kobj,struct kobj_uevent_env *env)
- {
- int i = 0;
- while(i < env->envp_idx)
- {
- printk("<1>envp[%d] = %s\n",i,env->envp[i]);
- i++;
- }
-
- return 0;
- }
- /*
- extern struct kset * __must_check kset_create_and_add(const char *name,
- const struct kset_uevent_ops *u,
- struct kobject *parent_kobj);
- */
- /*
- struct kset_uevent_ops {
- int (* const filter)(struct kset *kset, struct kobject *kobj);
- const char *(* const name)(struct kset *kset, struct kobject *kobj);
- int (* const uevent)(struct kset *kset, struct kobject *kobj,
- struct kobj_uevent_env *env);
- };
- */
- struct kset_uevent_ops kset_ops1 =
- {
- .filter = kset_filter1,
- .name = kset_name1,
- .uevent = kset_uevent1,
- };
- /*
- struct kobj_type {
- void (*release)(struct kobject *kobj);
- const struct sysfs_ops *sysfs_ops;
- struct attribute **default_attrs;
- const struct kobj_ns_type_operations *(*child_ns_type)(struct kobject *kobj);
- const void *(*namespace)(struct kobject *kobj);
- };
- */
- /*
- struct attribute {
- const char *name;
- mode_t mode;
- #ifdef CONFIG_DEBUG_LOCK_ALLOC
- struct lock_class_key *key;
- struct lock_class_key skey;
- #endif
- };
- */
- struct attribute attr1 =
- {
- .name = "attr_1",
- .mode = S_IRWXU,
- };
- struct attribute attr2 =
- {
- .name = "attr_2",
- .mode = S_IRUSR,
- };
- static struct attribute * attribute_set2[] =
- {
- &attr1,
- &attr2,
- NULL,
- };
- ssize_t set2_show(struct kobject *obj,struct attribute *attr,char *buf)
- {
- sprintf(buf,"%s\n",attr->name);
- return strlen(buf);
- }
- ssize_t set2_store(struct kobject *kobj,struct attribute *attr,const char *buf,
- size_t count)
- {
- return count;
- }
- /*
- struct sysfs_ops {
- ssize_t (*show)(struct kobject *, struct attribute *,char *);
- ssize_t (*store)(struct kobject *,struct attribute *,const char *, size_t);
- const void *(*namespace)(struct kobject *, const struct attribute *);
- };
- */
- struct sysfs_ops set2_ops =
- {
- .show = set2_show,
- .store = set2_store,
- };
- void set2_release(struct kobject *obj)
- {
- printk("release set2 \n");
- }
- struct kobj_type ktype =
- {
- .release = set2_release,
- .sysfs_ops = &set2_ops,
- .default_attrs = attribute_set2,
- };
- int __init sysfs_init(void)
- {
- set1 = kset_create_and_add("set1",&kset_ops1,NULL);
- set2 = kset_create_and_add("set2",&kset_ops1,&set1->kobj);
- kobject_set_name(&set3.kobj,"set3");
- set3.kobj.kset = set1;
- set3.kobj.ktype = &ktype;
- if(kset_register(&set3))
- printk("set3 fail\n");
-
- set4.kset = set1;
- if(kobject_init_and_add(&set4,&ktype,NULL,"set4"))
- printk("set4 fail\n");
- printk("register success\n");
- return 0;
- }
- void __exit sysfs_exit(void)
- {
- kset_unregister(set1);
- kset_unregister(set2);
- kset_unregister(&set3);
- kobject_del(&set4);
- printk("unregister success\n");
- }
- module_init(sysfs_init);
- module_exit(sysfs_exit);
kset_create_and_add
() - /**
- * kset_create_and_add - create a struct kset dynamically and add it to sysfs
- *
- * @name: the name for the kset
- * @uevent_ops: a struct kset_uevent_ops for the kset
- * @parent_kobj: the parent kobject of this kset, if any.
- *
- * This function creates a kset structure dynamically and registers it
- * with sysfs. When you are finished with this structure, call
- * kset_unregister() and the structure will be dynamically freed when it
- * is no longer being used.
- *
- * If the kset was not able to be created, NULL will be returned.
- */
- struct kset *kset_create_and_add(const char *name,
- const struct kset_uevent_ops *uevent_ops,
- struct kobject *parent_kobj)
- {
- struct kset *kset;
- int error;
- kset = kset_create(name, uevent_ops, parent_kobj);
- if (!kset)
- return NULL;
- error = kset_register(kset);
- if (error) {
- kfree(kset);
- return NULL;
- }
- return kset;
- }
- EXPORT_SYMBOL_GPL(kset_create_and_add);
待续。。。
阅读(620) | 评论(0) | 转发(0) |