[Linux]proc文件系统

Linux的procfs文件系统是一个虚拟文件系统,Linux不只有一个虚拟文件系统,sysfs和debugfs也是虚拟文件系统,sysfs在procfs的基础上进行了优化。虚拟文件系统在系统运行时创建,存在于内存。

在/proc目录下,可以自己创建procfs文件目录或者文件,例如创建/proc/test/debug文件,/proc目录下的文件需要struct proc_dir_entry结构体定义。

proc文件的创建有两种方式:create_proc_entryproc_create

1.create_proc_entry

  • 创建目录
    struct proc_dir_entry *proc_mkdir(const char *name,struct proc_dir_entry *parent)
    如果parent为proc,使用NULL代替。
  • 创建文件
    struct proc_dir_entry *create_proc_entry(constchar *name, mode_t mode, 
    struct proc_dir_entry *parent)

  • 删除文件/目录
    void remove_proc_entry(const char *name, struct proc_dir_entry *parent)

//包含proc头文件
#include <linux/proc_fs.h>

//定义proc接口
static struct proc_dir_entry *test_dir = NULL;
static struct proc_dir_entry *debug_dir = NULL;

//proc read的实现函数
static int proc_debug_read(char* buf, char** start, off_t off, int count, 
int* eof, void* data){
    return 0;
}

//proc write的实现函数
static int proc_debug_write(struct file* filp, const char* buf, 
unsigned long count, void* data){
    return 0;
}

//proc接口创建
static int proc_test_create(void)
{
    test_dir = proc_mkdir("test", NULL);
    if(!test_dir)
        return -ENOMEM;

    debug_dir = create_proc_entry("debug", 0644, test_dir);
    if(!debug_dir )
        return -ENOMEM;
    debug_dir ->read_proc = proc_debug_read;
    debug_dir ->write_proc = proc_debug_write;
}

2. proc_create

  • 创建目录
    struct proc_dir_entry *proc_mkdir(const char *name,
    		struct proc_dir_entry *parent)
    如果parent为proc,使用NULL代替。
  • 创建文件 (包含proc_read和proc_write回调函数)
    struct proc_dir_entry *proc_create(const char *name, umode_t mode,
    				   struct proc_dir_entry *parent,
    				   const struct file_operations *proc_fops)

  • 使用file_operations结构体包含了read和write的回调函数

proc_create是在kernel 3.10以及之后的版本中新增的,用于替换之create_proc_entry

//包含proc头文件
#include <linux/proc_fs.h>

//定义proc接口
static struct proc_dir_entry *test_dir = NULL;
static struct proc_dir_entry *debug_dir = NULL;

//proc open的实现函数
static int proc_debug_open(struct inode *inode, struct file *file)
{
    return 0;
}

//proc read的实现函数
static int proc_debug_read(char* buf, char** start, off_t off, 
int count, int* eof, void* data){
    return 0;
}

//proc write的实现函数
static int proc_debug_write(struct file* filp, const char* buf,
 unsigned long count, void* data){
    return 0;
}

static struct file_operations proc_debug_fops = {
    .owner      = THIS_MODULE,
    .open       = proc_debug_open,
    .write      = proc_debug_write,
    .read       = proc_debug_read,
    .release    = proc_debug_release,
    .ioctl      = proc_debug_ioctl,
};

//proc接口创建
static int proc_test_create(void)
{
    test_dir = proc_mkdir("test", NULL);
    if(!test_dir)
        return -ENOMEM;

    debug_dir = proc_create("debug", 0644, test_dir, &proc_debug_fops);
    if(!debug_dir )
        return -ENOMEM;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值