我寫了一個模塊來從/ 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/mytest
和cat /dev/mytest
它分別在/var/log/messages
提供寫功能和讀功能的消息。但終端上沒有輸出。
這樣做實際上我將讀寫函數指向file_operations讀寫函數,而不是proc_dir_entry,並且沒有檢查count。
您發佈之前,請用'astyle'或'格式化你的代碼indent'。 – Dave 2012-02-01 18:18:07
當您編譯提示行號的警告時,您應該評論或以其他方式顯示哪些行正在拋出它們。 – 2012-02-01 18:25:03