免费注册 查看新帖 |

Chinaunix

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

[内核入门] vfs_read 在某些机器上面返回-14 读取文件 EFAULT [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-12-07 17:24 |只看该作者 |倒序浏览
vfs_read 在某些机器上面返回-14 读取文件 EFAULT

代码如下

    mm_segment_t     oldfs_;
    int              ret_ = 0;
    struct inode     *inode_ = NULL;
    char             *data_ = NULL;
    loff_t           size_ = 0;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,19,0)
    inode_ = file_inode(f);
#else
    inode_ = f->f_dentry->d_inode;
#endif
    *size = inode_->i_size;
    size_ = *size;

    *data = (char*) kmalloc(size_ + 1, GFP_ATOMIC);
    data_ = *data;

    oldfs_ = get_fs();
    set_fs(get_ds());
        f->f_pos=0;//f->f_op->llseek(f,0,0);//调整初始位置
    ret_ = vfs_read(f, data_, size_, &f->f_pos);
    set_fs(oldfs_);
                       
        //pr_info("httplist  Cannot read \"%d,%d,%d,%d\"\n", ret_,EBADF,EINVAL,EFAULT);

    if (ret_ < 0){
                pr_info("Cannot read \"%d\"\n", ret_);
        return false;
        }

请问各位,这个一般是什么地方出问题了呢? 非常感谢   找了很久没有找到答案    本地机器可以  服务器不可以  然后服务器上面用内核源码编译了一下这个模块 也不行的 不知道什么原因


论坛徽章:
2
寅虎
日期:2014-11-25 21:47:342015小元宵徽章
日期:2015-03-06 15:58:18
2 [报告]
发表于 2015-12-07 20:08 |只看该作者
理论上当set_fs(get_ds());之后,vfs read不会返回-14,
有点怀疑*data = (char*) kmalloc(size_ + 1, GFP_ATOMIC);

代码贴全一点?
输入参数是多少?

论坛徽章:
0
3 [报告]
发表于 2015-12-07 20:11 |只看该作者
镇水铁牛 发表于 2015-12-07 20:08
理论上当set_fs(get_ds());之后,vfs read不会返回-14,
有点怀疑*data = (char*) kmalloc(size_ + 1, GFP ...


你好  我又单独写了一个helloworld的 写入读取文件  在那个机器上面
还是返回的-14

麻烦您看看 这是代码

#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
    static char buf[] ="hello world";
    static char buf1[10];
    int  hello_init(void)
    {
        struct file *fp;
        mm_segment_t fs;
        loff_t pos;
        //printk("hello enter/n");
        fp =filp_open("/tmp/kernel_file",O_RDWR | O_CREAT,0644);
        if (IS_ERR(fp)){
            printk("create file error\n");
            return -1;
        }
                 int              ret_ = 0;
        fs =get_fs();
        set_fs(KERNEL_DS);
        pos =0;
        ret_=vfs_write(fp,buf, sizeof(buf), &pos);
                printk("\nwrite ret is %d\n",ret_);
        pos =0;
        ret_=vfs_read(fp,buf1, sizeof(buf), &pos);
        printk("\nread  ret is  %d  read: %s\n",ret_,buf1);
        filp_close(fp,NULL);
        set_fs(fs);
        return 0;
    }
    void  hello_exit(void)
    {
        printk("hello exit/n");
    }
     
    module_init(hello_init);
    module_exit(hello_exit);
     
    MODULE_LICENSE("GPL");
这个是出问题的机器上面的版本号  centos62 2.6.32-220.el6.x86_64  GMT 2011 x86_64 x86_64 x86_64 GNU/Linux   谢谢了 非常感谢

论坛徽章:
2
寅虎
日期:2014-11-25 21:47:342015小元宵徽章
日期:2015-03-06 15:58:18
4 [报告]
发表于 2015-12-07 20:22 |只看该作者
  ret_=vfs_read(fp,buf1, sizeof(buf), &pos);中static char buf1[10];溢出了?

论坛徽章:
0
5 [报告]
发表于 2015-12-07 20:27 |只看该作者
镇水铁牛 发表于 2015-12-07 20:22
ret_=vfs_read(fp,buf1, sizeof(buf), &pos);中static char buf1[10];溢出了?

你好 刚打错了  我字符串换成hello  还是一样的 返回-14  不知道是哪里出现了问题

论坛徽章:
2
寅虎
日期:2014-11-25 21:47:342015小元宵徽章
日期:2015-03-06 15:58:18
6 [报告]
发表于 2015-12-07 21:50 |只看该作者
回复 5# appl337733

我在centos7上测试正常。
   

论坛徽章:
0
7 [报告]
发表于 2015-12-07 22:26 |只看该作者
对的 我在6.2上面测试不正常 不知道什么原因
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP