Search code examples
clinuxdevice-driver

about /proc read and write functions


I have written a module to read and write from /proc file. the code is showing warnings as commented and shown after the code.the code is as follows:

#include<linux/module.h>
#include<linux/init.h>
#include<linux/proc_fs.h>
#include<asm/uaccess.h>

#define proc_fs_max 1024
#define proc_entry "my_test"

static struct proc_dir_entry *our_proc_file;
static char procfs_buffer[proc_fs_max];
static int proc_buffer_size = 0;

int proc_read(char *buffer,char **buffer_location,off_t offset,int buffer_length,int 
*eof,void *data)
{
    int ret;
    printk(KERN_ALERT"\n in read function");

    if(offset > 0){
        ret = 0;
    } else {
        memcpy(buffer,procfs_buffer,proc_buffer_size);
        ret = proc_buffer_size;
    }
    return ret;
}

int proc_write(struct file *file, const char *buffer, unsigned long count,void *data)
{
    printk(KERN_ALERT"\nin write function\n");
    proc_buffer_size = count;
    if(proc_buffer_size > proc_fs_max)
        proc_buffer_size = proc_fs_max; 
    if(copy_from_user(procfs_buffer,buffer,proc_buffer_size)) //showing comments on    warning as below
        return -EFAULT;
    return proc_buffer_size;
}

int proc_open(struct inode *inode,struct file *filp)
{
    try_module_get(THIS_MODULE);
    return 0;
}

int proc_close(struct inode *inode,struct file *filp)
{
    module_put(THIS_MODULE);
    return 0;
}

static struct file_operations dev_proc_ops = {
    .owner = THIS_MODULE,
    .read = proc_read,    //warning initialization from incompatible pointer type
    .write = proc_write,  //warning initialization from incompatible pointer type
    .open = proc_open,
    .release = proc_close,
};

static int dev_init(void)
{
    our_proc_file = create_proc_entry(proc_entry,0644,NULL);
    our_proc_file->proc_fops = &dev_proc_ops;
    return 0;
}

static void dev_clean(void)
{
    remove_proc_entry(proc_entry,NULL);
}

module_init(dev_init);
module_exit(dev_clean);

showing warning at compiling when using copy to user as follow:

In file included from /usr/src/linux-2.6.34.10-0.6/arch/x86/include/asm/uaccess.h:571:0, from /home/karan/practice/procf/testproc.c:4:

In function ‘copy_from_user’, inlined from ‘proc_write’ at /home/karan/practice/procf/testproc.c:33:18:

When I'm using insmod and then echo hi>/dev/mytest and cat /dev/mytest its giving messages in write function and in read function respectively in /var/log/messages. but there is no output on terminal.

It's done actually I was pointing the read and write functions to the file_operations read and write function instead of proc_dir_entry and was not checking for count.


Solution

  • Your functions for proc_read and proc_write don't match the place you're using them, as the compiler pointed out with its warnings. In your struct file_operations you have:

    int proc_read(char *buffer,char **buffer_location,off_t offset,int buffer_length,int 
    *eof,void *data);
    
    int proc_write(struct file *file, const char *buffer, unsigned long count,void *data);
    

    These are both being used in a struct file_operations, but in include/linux/fs.h the function pointer types in that struct are:

    ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
    ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
    

    If int isn't the same a ssize_t int isn't the same as size_t (unlikely since one's signed and the other isn't) then you'll see problems, but your read there has more serious problems - you have a char ** where it expects a char *.

    The compiler was quite happy to take your word that this was what you meant to do, but I don't think it was.

    This read looks more like read_proc_t in struct proc_dir_entry, but that's not what you're setting in your dev_proc_ops.

    (As a side note I think you probably want to make the rest of your functions static also, since they're exposed via function pointers)