关于/proc读写函数

发布于 2025-01-01 06:46:26 字数 2401 浏览 0 评论 0 原文

我编写了一个模块来读取和写入 /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/mytestcat 时/dev/mytest 分别在 /var/log/messages 中的写入函数和读取函数中给出消息。但终端上没有输出。

实际上我已经完成了,我将读取和写入函数指向 file_operations 读取和写入函数而不是 proc_dir_entry 并且没有检查计数。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原野 2025-01-08 06:46:26

正如编译器在警告中指出的那样,您的 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 *);

intssize_t 不同 intsize_t 不同(不太可能,因为一个人已签名并且另一个不是)那么你会看到问题,但是你的 read 那里有更严重的问题 - 你有一个 char ** ,它需要一个 char *

编译器很高兴相信你的话,这就是你的意思,但我认为事实并非如此。

read 看起来更像 read_proc_t rel="nofollow">struct proc_dir_entry,但这不是您在 dev_proc_ops 中设置的内容。

(作为旁注,我认为您可能也希望将其余函数设为静态,因为它们是通过函数指针公开的)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文