字符模块布鲁斯:关闭和读取时内存地址不良

时间:2011-10-03 13:55:08

标签: linux-kernel ubuntu-10.04

我正在创建一个简单的fifo样式字符模块。我很难让它表现得好。这是正在发生的事情:

我正在

close failed in file object destructor:
IOError: [Errno 14] Bad address

当我尝试关闭我用来与我的角色设备交谈的文件对象时。当我尝试从中读取时,我也得到了一个糟糕的地址。我对内核编程很新,所以我不太清楚这些症状是什么意思。这是一些相关的代码。任何帮助将不胜感激:

int pop(char *source, char* dest, int count)
{
// take count values from source, store in dest
memcpy(dest,source,count);
memset(source,0x00,count);
return 0;
}
ssize_t ent_read(struct file *filp, char *buf, size_t count, loff_t *f_pos)
{
int retval;
char *temp;
int copy_count;
printk(KERN_ALERT "entropy_feed: module reading...\n");
if (level>=count)
{
    copy_count=count;
}
else
{
    copy_count=level;
}
printk(KERN_ALERT "entropy_feed: allocating temp memory buffer");
temp = kcalloc(copy_count,1,GFP_KERNEL);

if (down_interruptible(&sem))
{
    retval= -ERESTARTSYS;
    goto u_out;
}
printk(KERN_ALERT "entropy_feed: semaphore locked");
printk(KERN_ALERT "entropy_feed: popping");
pop(buffer+level-copy_count, temp, copy_count);
printk(KERN_ALERT "entropy_feed: popped");
level-=copy_count;

if (copy_to_user(buf,temp,copy_count))
{
     retval= -EFAULT;
     goto out;
}
out:
up(&sem);
printk(KERN_ALERT "entropy_feed: semaphore unlocked");
u_out:
kfree(temp);
printk(KERN_ALERT "entropy_feed: exiting read function")
return retval;
}

ssize_t ent_write(struct file *filp, const char __user *buf, size_t count,loff_t *f_pos)
{
int retval;
char *temp;
int copy_count;

printk(KERN_ALERT "entropy_feed module writing...\n");

copy_count=level-max_lvl;
if (count<copy_count)
    copy_count=count;

if (down_interruptible(&sem))
{
    retval= -ERESTARTSYS;
    goto u_out;
}
printk(KERN_ALERT "entropy_feed: semaphore locked");
temp = kcalloc(count,1,GFP_KERNEL);

if (copy_from_user(temp,buf,count))
{
     retval= -EFAULT;
     goto out;
}
printk(KERN_ALERT "entropy_feed: popping");
pop(temp, buffer+level, copy_count);
printk(KERN_ALERT "entropy_feed: popped");
level+=copy_count;
out:
up(&sem);
printk(KERN_ALERT "entropy_feed: semaphore unlocked");
u_out:
kfree(temp);
printk(KERN_ALERT "entropy_feed: exiting write function");
return retval;
}

struct file_operations ent_fops = {
          .owner = THIS_MODULE,
          .read = ent_read,
          .write = ent_write,
};

1 个答案:

答案 0 :(得分:1)

错误编号14是EFAULT

查看你的ent_read()函数,我没有看到你将retval设置为函数成功时写入的字节数的任何地方,所以你只是返回{{{}中未初始化的值。 1}}在非失败的情况下。尝试添加

retval

之前

    retval = copy_count;

行,以便在成功阅读案例中获得正确的返回值。

就close的错误而言,您的实际file_operations结构是否有 out: 方法?如果是这样你从那里回来了什么?否则我无法理解为什么flush会为您返回close()