Chinaunix首页 | 论坛 | 博客
  • 博客访问: 326336
  • 博文数量: 90
  • 博客积分: 4010
  • 博客等级: 上校
  • 技术积分: 694
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-22 10:51
文章分类

全部博文(90)

文章存档

2011年(3)

2010年(35)

2009年(52)

我的朋友

分类: LINUX

2009-11-27 11:29:05

#include
#include
#include
#include
#include
#include
#include
#include
MODULE_AUTHOR("yangweihao");
MODULE_LICENSE("Dual BSD/GPL");
 
 struct kobject *kobj;
 struct kobject *ywhkobj;
 char *ywhname;
 
void obj_test_release(struct kobject *kobject);
ssize_t ywh_test_show(struct kobject * kobject,struct attribute * attr,char * buf)
{
 printk("have show .\n");
 printk("attrname:%s .\n",attr->name);
 sprintf(buf,"%s\n",attr->name);
 return strlen(attr->name)+2;
}
ssize_t ywh_test_store(struct kobject * kobject,struct attribute * attr,char * buf,size_t count)
{
 printk("havwstore\n");
 kfree(ywhname);
 ywhname = kmalloc(count,GFP_KERNEL);
 if(!ywhname)
 {
  printk("kmalloc error\n");
  return ENOMEM;
 }
 for(;*buf !='\0';buf++)
 {
  *(ywhname)++ = *buf;
 }
 *(ywhname) = '\0';
 ywhname = ywhname - count;
 attr->name = ywhname;
 printk("write: %s\n",attr->name);
 
 return count;
}
/*
 默认属性添加
*/
struct attribute test_attr ={
 .name = "yangweihao",
 .mode = S_IRWXUGO,
};
static struct attribute *def_attrs[] ={
 &test_attr,
 NULL
};
struct sysfs_ops obj_test_sysops ={
 .show = ywh_test_show,
 .store = ywh_test_store
};
struct kobj_type ktype = {
 .release = obj_test_release,
 .sysfs_ops = &obj_test_sysops,
 .default_attrs= def_attrs
};
void obj_test_release(struct kobject * kobject)
{
 printk("kobject_test: release .\n");
}
static int kobject_test_init()
{
 printk("kobject test init.\n");
 kobj = kmalloc(sizeof(struct kobject),GFP_KERNEL);
 ywhkobj= kmalloc(sizeof(struct kobject),GFP_KERNEL);
 ywhname = kmalloc(0,GFP_KERNEL);
 if(!kobj)
 {
  printk("kmalloc error\n");
  return ENOMEM;
 }
 memset(kobj,0,sizeof(struct kobject));
 memset(ywhkobj,0,sizeof(struct kobject));
 
 kobject_init_and_add(kobj,&ktype,NULL,"ywh");
 kobject_init_and_add(ywhkobj,&ktype,kobj,"hlp");
 
 return 0;
}
static int kobject_test_exit()
{
 printk("kobject test exit.\n");
 kobject_del(kobj);
 kfree(kobj);
 kfree(ywhkobj);
 kfree(ywhname);
 return 0;
}
module_init(kobject_test_init);
module_exit(kobject_test_exit);
 
运行如下:
[root@FriendlyARM /sys]# ls
block     class     devices   fs        module
bus       dev       firmware  kernel    ywh
[root@FriendlyARM ywh]# ls
hlp         yangweihao
[root@FriendlyARM hlp]# ls
yangweihao
 
不做讲述了....
阅读(775) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~