免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2114 | 回复: 2
打印 上一主题 下一主题

[驱动] copy_from_user的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-05-20 11:15 |只看该作者 |倒序浏览

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不好用?

论坛徽章:
0
2 [报告]
发表于 2012-05-21 09:39 |只看该作者
若将kobj_test_store中 copy_from_user改为strcpy,attrname:abc.
这就正常了

这个不已经说明了: buf指向的已经是内核地址空间了。
内核能够帮你做的事情,就会把它做好了,不会劳烦你的
这个不像ioctl,内核不可能知道arg指向的是什么,所以需要你自己去copy_from_user
如果指向的是用户空间,内核会用__user 来修饰指针

论坛徽章:
0
3 [报告]
发表于 2012-05-21 10:50 |只看该作者
回复 2# senioryzc
OK,thanks。


   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP