有关/ proc中读写功能 [英] about /proc read and write functions
问题描述
我已经写了一个模块,读取和/ proc文件编写。在code是显示为注释和code.the code后显示警告如下:
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:
在文件从/usr/src/linux-2.6.34.10-0.6/arch/x86/include/asm/uaccess.h:571:0包括,
从/home/karan/practice/procf/testproc.c:4:
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:
在功能'调用copy_from_user',
在/home/karan/practice/procf/testproc.c:33:18从'proc_write'内联:
In function ‘copy_from_user’, inlined from ‘proc_write’ at /home/karan/practice/procf/testproc.c:33:18:
当我用insmod,然后回声喜&GT;的/ dev / mytest,这时
和猫的/ dev / mytest,这时
在写入功能和读取功能分别在其提供的消息的/ var / log / messages中
。但对终端没有输出。
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.
这实际上已经完成了我指着读,并给file_operations的读写功能和写函数,而不是体struct proc_dir_entry并没有检查计数。
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.
推荐答案
您函数 proc_read
和 proc_write
唐T匹配您正在使用他们的地方,因为编译器与它警告指出。在你的结构的file_operations
您有:
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);
这些都被在结构的file_operations
,但在的在include / linux / fs.h中在该函数指针类型结构
是:
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 *);
如果 INT
是不一样的一个 ssize_t供
INT
是不一样的为size_t
(不可能的,因为一个人的签字,其他的不是),那么你会看到问题,但你的阅读
出现较为严重的问题 - 你有一个的char **
其中,预计一的char *
。
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.
本读
看起来更像的 read_proc_t
中的 结构体struct proc_dir_entry
,但是这不是你在你的<$ C $设置什么C> 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)
这篇关于有关/ proc中读写功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!