Question

I need to implement a Linux Kernel Driver, that (in the first step) only forwards all file operations to another file (in later steps, this should be managed and manipulated, but I don't want to discuss this here).

My idea is the following, but when reading, the kernel crashes:

static struct {
    struct file *file;
    char *file_name;
    int open;
} file_out_data = {
.file_name  = "/any_file",
.open       = 0, 
};


int memory_open(struct inode *inode, struct file *filp) {
  PRINTK("<1>open memory module\n");
    /* 
     * We don't want to talk to two processes at the same time 
     */
    if (file_out_data.open)
        return -EBUSY;
    /*
     * Initialize the message 
     */
    Message_Ptr = Message;
    try_module_get(THIS_MODULE);

        file_out_data.file = filp_open(file_out_data.file_name, filp->f_flags, filp->f_mode); //here should be another return handling in case of fail
    file_out_data.open++;

  /* Success */
  return 0;
}

int memory_release(struct inode *inode, struct file *filp) {
      PRINTK("<1>release memory module\n");
    /* 
     * We're now ready for our next caller 
     */
    file_out_data.open--;
    filp_close(file_out_data.file,NULL);

    module_put(THIS_MODULE);
  /* Success */
  return 0;
}

ssize_t memory_read(struct file *filp, char *buf, 
                    size_t count, loff_t *f_pos) { 
   PRINTK("<1>read memory module \n");
ret=file_out_data.file->f_op->read(file_out_data.file,buf,count,f_pos); //corrected one, false one is to find in the history
    return ret;

}

So, can anyone please tell me why?

Was it helpful?

Solution

  1. Don't use set_fs() as there is no reason to do it.
  2. Use file->f_fop->read() instead of the vfs_read. Take a look at the file and file_operations structures.

OTHER TIPS

Why are you incrementing file_out_data.open twice and decrementing it once? This could cause you to use file_out_data.file after it has been closed.

You want to write memory in your file ou read? Because you are reading and not writing... possible i'm wrong

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top