- 论坛徽章:
- 0
|
struct attribute test_attr = {
.name = "kobj_config",
.mode = S_IRWXUGO,
};
static struct attribute *def_attrs[] = {
&test_attr,
NULL,
};
struct sysfs_ops obj_test_sysops =
{
.show = kobj_test_show,
.store = kobj_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(KERN_EMERG "eric_test: release .\n");
}
ssize_t kobj_test_show(struct kobject *kobject, struct attribute *attr,char *buf)
{
printk(KERN_DEBUG "have show.\n");
printk(KERN_DEBUG "attrname:%s.\n", attr->name);
sprintf(buf,"%s\n",attr->name);
return strlen(attr->name)+2;
}
ssize_t kobj_test_store(struct kobject *kobject,struct attribute *attr,const char *buf, size_t count)
{
printk("havestore\n")
printk("write: %s,count=%d\n",buf,count);
copy_from_user((void*)attr->name,buf,count);
printk(KERN_DEBUG "mgd attrname:%s.\n", attr->name);
return count;
}
struct kobject kobj;
static int __init kobj_test_init(void)
{
int len=kobject_init_and_add(&kobj,&ktype,NULL,"kobject_test");//初始化并添加
printk(KERN_DEBUG "kboject test init.\n");
return len;
}
static void __exit kobj_test_exit(void)
{
printk(KERN_DEBUG "kobject test exit.\n");
kobject_del(&kobj);//删除注册
}
module_init(kobj_test_init);
module_exit(kobj_test_exit);
程序编译后,在串口上运行,
[root@mgd home]# echo abc>/sys/kobject_test/kobject_config
[root@mgd home]# ls /sys/kobject_test/
[root@mgd home]#dmesg
运行#ls /sys/kobject_test/目录内容为空。
运行dmesg查看attrname为空。
kobject test exit.
kboject test init.
havestore
write: abc
,count=4
attrname:.
若将kobj_test_store中 copy_from_user改为strcpy,attrname:abc.
这就正常了。
请问:kobj_test_store函数是将数据从用户空间传到内核么?为什么用copy_from_user不好用? |
|