Linux关于proc_create函数的修改

简介: Linux关于proc_create函数的修改

错误提示:"passing argument 4 of ‘proc_create’ from incompatiable pointer type"如下图

可以看到提示参数不匹配,通过打开proc_fs.h文件可以看到有这个函数的定义,见下图。

可以看到,第四个参数定义为const struct proc_ops *proc_ops,而非参考代码中的const struct file_operations *proc_fops。因而需要对proc_ops结构体进行修改,通过查阅proc_ops结构体的定义,见下图

可以看到只需对其中的成员proc_read赋值为read_proc即可。

参考代码如下:

#include <linux/module.h>
#include <linux/kernel.h>
#include<linux/sched.h>
#include <asm/uaccess.h>
#include <linux/proc_fs.h>
static struct proc_dir_entry *proc_parent;
int len,temp;
char *msg;
static ssize_t read_proc(struct file *filp,char __user *buf,size_t count,loff_t *offp ) 
{
  int a;  //used to recept the value return from the function 
       if(count>temp)
              count=temp;
        temp=temp-count; 
        a=raw_copy_to_user(buf,msg, count);
        if(count==0)
              temp=len; 
        return count;
}
static struct proc_ops proc_fops={
    .proc_read = read_proc
};
void create_new_proc_entry(void)
{
      /*create a new directory named hello, and return a pointer point to this dir*/
      proc_parent = proc_mkdir("hello",NULL);
      if(!proc_parent)
      {
            printk(KERN_INFO "Error creating proc entry");
       }
      /*create a file named world, add read attribute to this file using proc_fops*/
      proc_create("world",0,proc_parent,&proc_fops);
      msg="hello world\n"; /*file content*/
      len=strlen(msg);
      temp=len;
      printk(KERN_INFO "1.len=%d",len);
      printk(KERN_INFO "proc initialized");
}
static int __init proc_init(void)
{
      create_new_proc_entry();
      return 0;
}
static void __exit proc_cleanup(void)
{
       printk(KERN_INFO " Inside cleanup_module\n");
       remove_proc_entry("hello",proc_parent);
       remove_proc_entry("world",NULL);
}
MODULE_LICENSE("GPL");
module_init(proc_init);
module_exit(proc_cleanup);


相关文章
|
29天前
|
Linux
关于Linux目录访问函数总结
关于Linux目录访问函数总结
13 1
|
2月前
|
Linux
【Linux 系统编程】wait函数详解
【Linux 系统编程】wait函数详解
22 0
|
2月前
|
算法 Linux C++
【Linux系统编程】深入解析Linux中read函数的错误场景
【Linux系统编程】深入解析Linux中read函数的错误场景
205 0
|
2月前
|
存储 监控 Linux
【Linux IO多路复用 】 Linux下select函数全解析:驾驭I-O复用的高效之道
【Linux IO多路复用 】 Linux下select函数全解析:驾驭I-O复用的高效之道
57 0
|
2月前
|
存储 算法 Linux
【Linux系统编程】深入理解Linux目录扫描函数:scandir目录函数(按条件扫描目录
【Linux系统编程】深入理解Linux目录扫描函数:scandir目录函数(按条件扫描目录
50 0
|
2月前
|
缓存 Ubuntu 网络协议
Linux系统编程之文件I/O函数的使用:介绍文件I/O函数的基本概念、用法和实现方式
Linux系统编程之文件I/O函数的使用:介绍文件I/O函数的基本概念、用法和实现方式
22 1
|
2月前
|
算法 Linux C++
【Linux系统编程】深入理解Linux中的chmod函数和mode_t类型
【Linux系统编程】深入理解Linux中的chmod函数和mode_t类型
35 0
|
2月前
|
网络协议 Linux API
Linux网络编程:shutdown() 与 close() 函数详解:剖析 shutdown()、close() 函数的实现原理、参数说明和使用技巧
Linux网络编程:shutdown() 与 close() 函数详解:剖析 shutdown()、close() 函数的实现原理、参数说明和使用技巧
91 0
|
2月前
|
存储 安全 数据管理
Linux系统编程教程之Linux线程函数的使用:讲解Linux线程函数
Linux系统编程教程之Linux线程函数的使用:讲解Linux线程函数
19 1
|
8天前
|
算法 Linux Shell
【linux进程(二)】如何创建子进程?--fork函数深度剖析
【linux进程(二)】如何创建子进程?--fork函数深度剖析