关于/proc读写函数
我编写了一个模块来读取和写入 /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, 来自/home/karan/practice/procf/testproc.c:4:
在函数“copy_from_user”中, 内联自 /home/karan/practice/procf/testproc.c:33:18 处的“proc_write”:
当我使用 insmod 然后 echo hi>/dev/mytest
和 cat 时/dev/mytest
分别在 /var/log/messages
中的写入函数和读取函数中给出消息。但终端上没有输出。
实际上我已经完成了,我将读取和写入函数指向 file_operations 读取和写入函数而不是 proc_dir_entry 并且没有检查计数。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如编译器在警告中指出的那样,您的
proc_read
和proc_write
函数与您使用它们的位置不匹配。在您的 struct file_operations 中,您有:这些都在 struct file_operations 中使用,但在 include/linux/fs.h 该
struct
中的函数指针类型为:
int
与ssize_t
不同int
与size_t
不同(不太可能,因为一个人已签名并且另一个不是)那么你会看到问题,但是你的read
那里有更严重的问题 - 你有一个char **
,它需要一个char *。
编译器很高兴相信你的话,这就是你的意思,但我认为事实并非如此。
此
read
看起来更像 read_proc_t rel="nofollow">struct proc_dir_entry
,但这不是您在dev_proc_ops
中设置的内容。(作为旁注,我认为您可能也希望将其余函数设为静态,因为它们是通过函数指针公开的)
Your functions for
proc_read
andproc_write
don't match the place you're using them, as the compiler pointed out with its warnings. In yourstruct file_operations
you have:These are both being used in a
struct file_operations
, but in include/linux/fs.h the function pointer types in thatstruct
are:If
int
isn't the same assize_t
int
isn't the same assize_t
(unlikely since one's signed and the other isn't) then you'll see problems, but yourread
there has more serious problems - you have achar **
where it expects achar *
.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 likeread_proc_t
instruct proc_dir_entry
, but that's not what you're setting in yourdev_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)