c - about/proc读写函数

标签 c linux device-driver

我写了一个模块来读写/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/日志/消息。但是终端没有输出。

实际上我将读写函数指向 file_operations 读写函数而不是 proc_dir_entry 并且没有检查计数。

最佳答案

您的 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 ** 它期望一个字符 *

编译器很高兴接受您的话,这是您的本意,但我认为不是。

这个看起来更像read_proc_tstruct proc_dir_entry ,但这不是您在 dev_proc_ops 中设置的内容。

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

关于c - about/proc读写函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9101026/

相关文章:

c++ - 枚举所有设备路径

windows - 在 WinDbg 中定义自定义错误检查代码

c - "Your GStreamer installation is missing a plug-in."(GstURIDecodeBin)

c - 下面两个作业有什么区别?

c - 程序 : what do these errors mean? 的总线错误/段错误

c - 在 lex.yy.c 中,查找表代表什么?

sql-server - 添加供应商 MSSQL Linux

php - 来自 MSSQL Server 和 Linux 的桥接数据

node.js - 我运行了一个 npm run build,将它放在 nodejs express.static 上,pm2 启动应用程序但无法获取 '/'

c - 在 linux 内核中使一些代码不被调度