Chinaunix首页 | 论坛 | 博客
  • 博客访问: 631123
  • 博文数量: 1008
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 5175
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-31 09:44
文章分类
文章存档

2012年(1008)

我的朋友

分类:

2012-08-01 11:28:18

原文地址:kobject 作者:luozhiyong131

  1. #include <linux/device.h>
  2. #include <linux/module.h>
  3. #include <linux/kernel.h>
  4. #include <linux/init.h>
  5. #include <linux/string.h>
  6. #include <linux/sysfs.h>
  7. #include <linux/stat.h>

  8. MODULE_AUTHOR("David Xie");
  9. MODULE_LICENSE("Dual BSD/GPL");

  10. void obj_test_release(struct kobject *kobject);
  11. ssize_t kobj_test_show(struct kobject *kobject, struct attribute *attr,char *buf);
  12. ssize_t kobj_test_store(struct kobject *kobject,struct attribute *attr,const char *buf, size_t count);

  13. struct attribute test_attr = {
  14.         .name = "kobj_config",
  15.         .mode = S_IRWXUGO,
  16. };

  17. static struct attribute *def_attrs[] = {
  18.         &test_attr,
  19.         NULL,
  20. };


  21. struct sysfs_ops obj_test_sysops =
  22. {
  23.         .show = kobj_test_show,
  24.         .store = kobj_test_store,
  25. };

  26. struct kobj_type ktype =
  27. {
  28.         .release = obj_test_release,
  29.         .sysfs_ops=&obj_test_sysops,
  30.         .default_attrs=def_attrs,
  31. };

  32. void obj_test_release(struct kobject *kobject)
  33. {
  34.         printk("eric_test: release .\n");
  35. }

  36. ssize_t kobj_test_show(struct kobject *kobject, struct attribute *attr,char *buf)
  37. {
  38.         printk("have show.\n");
  39.         printk("attrname:%s.\n", attr->name);
  40.         sprintf(buf,"%s\n",attr->name);
  41.         return strlen(attr->name)+2;
  42. }

  43. ssize_t kobj_test_store(struct kobject *kobject,struct attribute *attr,const char *buf, size_t count)
  44. {
  45.         printk("havestore\n");
  46.         printk("write: %s\n",buf);
  47.         return count;
  48. }

  49. struct kobject kobj;
  50. static int kobj_test_init()
  51. {
  52.         printk("kboject test init.\n");
  53.         kobject_init_and_add(&kobj,&ktype,NULL,"kobject_test");
  54.         return 0;
  55. }

  56. static int kobj_test_exit()
  57. {
  58.         printk("kobject test exit.\n");
  59.         kobject_del(&kobj);
  60.         return 0;
  61. }

  62. module_init(kobj_test_init);
  63. module_exit(kobj_test_exit);
阅读(159) | 评论(0) | 转发(0) |
0

上一篇:kset

下一篇:内核集合与对象例程

给主人留下些什么吧!~~