天天看点

proc文件系统接口

内核提供的proc接口函数

头文件linux/proc_fs.h

struct proc_dir_entry *​

​proc_mkdir​

​​(const char *name, struct proc_dir_entry *parent);

在parent目录创建一个名为name的目录

struct proc_dir_entry *​

​create_proc_entry​

​​(const char *name, mode_t mode, struct proc_dir_entry *parent);

在parent目录创建一个名为name,权限为mode的文件

在create_proc_entry函数执行后返回struct proc_dir_entry可以自己指定read, write等函数

struct proc_dir_entry* ​

​create_proc_read_entry​

​(const char name, mode_t mode, struct proc_dir_entry *base, read_proc_t *read_proc, void data);

如果只需要read函数,可以使用他,他其实是对create_proc_entry进行了封装,把create_proc_read_entry的传入值read_proc赋给了create_proc_entry返回值的read_proc成员。

struct proc_dir_entry *​

​proc_create​

​​(const char *name, umode_t mode, struct proc_dir_entry *parent, const struct file_operations *proc_fops)

新的内核版本中,至少在3.10内核中已经看不到create_proc_entry了,取而代之的是使用proc_create创建文件,proc_create中需要指定proc_fops中的read和write等方法,以方便用户空间的操作。

struct proc_dir_entry *​

​proc_symlink​

​​(const char *name, struct proc_dir_entry *parent, const char *dest);

在parent目录创建指定dest目录的名为name的符号链接。

void ​

​remove_proc_entry​

​​( const char *name, struct proc_dir_entry *parent );

删除parent目录中名为name的文件或者目录。

使用proc接口函数例子

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <linux/sched.h>
#include <asm/uaccess.h>

#ifndef LINUX_VERSION_CODE
#define LINUX_VERSION_CODE 199163       /* kernel version 3.10 */
#endif

#ifndef KERNEL_VERSION(a, b, c)
#define KERNEL_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + (c))
#endif

#define STRINGLEN 1024
char global_buffer[STRINGLEN] = "hello";
struct proc_dir_entry *example_dir, *data_file;

#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 0, 0)
int proc_read_hello(char *page, char **start, off_t off, int count,
                int *eof, void *data)
{
        int len;
        len = sprintf(page, global_buffer); //把global_buffer的内容显示给访问者

        return len;
}

int proc_write_hello(struct file *file, const char *buffer,
                unsigned long count, void *data)
{
        int len;

        if (count >= STRINGLEN)
                len = STRINGLEN - 1;
        else
                len = count;

        /*
         * copy_from_user函数将数据从用户空间拷贝到内核空间
         * 此处将用户写入的数据存入global_buffer
         */
        copy_from_user(global_buffer, buffer, len);
        global_buffer[len] = '\0';

        return len;
}
#else
static ssize_t proc_read(struct file *file, char __user *buf,
                        size_t count, loff_t *ppos)
{
        int len;

        if (*ppos > 0)
                return 0;

        len = strlen(global_buffer) + 1;
        if (copy_to_user(buf, global_buffer, len))
                return -EFAULT;

        *ppos += len;

        return len;
}

static ssize_t proc_write(struct file *file, const char __user *buf,
                         size_t count, loff_t *ppos)
{
        int len;

        if (count >= STRINGLEN)
                len = STRINGLEN - 1;
        else
                len = count;

        if (copy_from_user(global_buffer, buf, len))
                return -EFAULT;

        global_buffer[len] = '\0';

        return len;
}

static const struct file_operations proc_fops = {
        .owner = THIS_MODULE,
        .read = proc_read,
        .write = proc_write,
};
#endif

static int __init proc_test_init(void)
{
        example_dir = proc_mkdir("proc_test", NULL);

#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 0, 0)
        data_file = create_proc_entry("data", S_IRUGO, example_dir);
        data_file->read_proc = proc_read_hello;
        data_file->write_proc = proc_write_hello;
#else
        data_file = proc_create("data", S_IRUGO, example_dir, &proc_fops);
#endif
        return 0;
}

static void __exit proc_test_exit(void)
{
        remove_proc_entry("data", example_dir);
        remove_proc_entry("proc_test", NULL);
}

module_init(proc_test_init);
module_exit(proc_test_exit);      

参考文章

参考资源

继续阅读