2012-02-01 34 views
0

我寫了一個模塊來從/ proc文件讀寫。代碼是表示作爲評價和示出的code.the代碼如下之後警告:about/proc讀寫功能

#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); 

示出在使用複製時,用戶如下編譯警告:

在文件中包含從/ 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:

函數'copy_from_user ', 從'proc_write'內嵌於/home/karan/practice/procf/testproc.c:33:18:

當我使用insmod,然後echo hi>/dev/mytestcat /dev/mytest它分別在/var/log/messages提供寫功能和讀功能的消息。但終端上沒有輸出。

這樣做實際上我將讀寫函數指向file_operations讀寫函數,而不是proc_dir_entry,並且沒有檢查count。

+0

您發佈之前,請用'astyle'或'格式化你的代碼indent'。 – Dave 2012-02-01 18:18:07

+1

當您編譯提示行號的警告時,您應該評論或以其他方式顯示哪些行正在拋出它們。 – 2012-02-01 18:25:03

回答

4

您的proc_readproc_write的函數與您使用它們的位置不匹配,因爲編譯器指出了它的警告。在你struct file_operations您有:

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); 

這些都被在struct file_operations使用,但在include/linux/fs.hstruct函數指針的類型有:

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

如果int是不一樣的一個ssize_tintsize_t不一樣(不太可能因爲已簽名而另一個不是),那麼你會看到問題,但你的read有更嚴重的問題 - 你有char **它期望char *

編譯器非常高興地告訴你這就是你的意思,但我不認爲它是。

這個read看起來更像read_proc_tstruct proc_dir_entry,但這不是你在dev_proc_ops中設置的。

(作爲一個方面說明,我認爲你可能想使你的函數static休息也,因爲他們是通過函數指針暴露)