I’m making a simple fifo style character module. I’m struggling to get it to behave. Here’s what’s happening:
I’m getting
close failed in file object destructor:
IOError: [Errno 14] Bad address
When i try to close a file object that I’m using to talk to my character device. I also get a bad address when I try to read from it. I’m pretty new to kernel programming so I’m not too sure what these symptoms imply. Here is some relevant code. Any assistance would be greatly appreciated:
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,
};
Error number 14 is
EFAULT.Looking at your
ent_read()function, I don’t see anywhere that you setretvalto the number of bytes written if the function succeeds, so you are just returning whatever uninitialized value is inretvalin the non-failure case. Try addingright before the
line, so that you get the correct return value in the successful read case.
As far as the error from close, does your actual file_operations structure have a
flushmethod? If so what are you returning from that? Otherwise I can’t see whyclose()would returnEFAULTfor you.