Linux proc文件系统

一、头文件

linux/proc_fs.h

1、数据结构

struct proc_dir_entry {
unsigned int low_ino;
unsigned int namelen;
const char *name;
mode_t mode;
nlink_t nlink;
uid_t uid;
gid_t gid;
loff_t size;
const struct inode_operations *proc_iops;
/*
* NULL ->proc_fops means "PDE is going away RSN" or
* "PDE is just created". In either case, e.g. ->read_proc won't be
* called because it's too late or too early, respectively.
*
* If you're allocating ->proc_fops dynamically, save a pointer
* somewhere.
*/
const struct file_operations *proc_fops;
struct proc_dir_entry *next, *parent, *subdir;
void *data;
read_proc_t *read_proc;
write_proc_t *write_proc;
atomic_t count; /* use count */
int pde_users; /* number of callers into module in progress */
spinlock_t pde_unload_lock; /* proc_fops checks and pde_users bumps */
struct completion *pde_unload_completion;
struct list_head pde_openers; /* who did ->open, but not ->release */

};

2、创建目录

struct proc_dir_entry *proc_mkdir(const char *,struct proc_dir_entry *);

3、创建文件

struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode,

struct proc_dir_entry *parent);

4、删除目录/文件

void remove_proc_entry(const char *name, struct proc_dir_entry *parent);

二、源代码文件

1、在proc目录下创建test目录,并在test目录下创建test文件,并实现对/proc/test/test文件进行读写操作

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

static struct proc_dir_entry *test = NULL; 
static struct proc_dir_entry *proc_file = NULL;
static char msg[255];

static int 
proc_read(char *page, char **start, off_t off, int count, int *eof, void *data)
{
    int len = strlen(msg);

    if (off >= len)
        return 0;

    if (count > len - off)
        count = len - off;

    memcpy(page + off, msg + off, count);
    return (off + count);
}

static int 
proc_write(struct file *file, const char __user *buffer, unsigned long count, void *data)
{
    unsigned long count2 = count;

    if (count2 >= sizeof(msg))
        count2 = sizeof(msg) - 1;

    if (copy_from_user(msg, buffer, count2)) /* 从内核空间到用户空间 */
        return -EFAULT;

    msg[count2] = '\0';
    return count;
}

static __init proc_init(void)
{
test = proc_mkdir("test", NULL);
if (!test) {
        printk(KERN_ERR "Can't create /proc/test\n");
        return -1;
    }

proc_file = create_proc_entry("test", 0666, test);
        if (!proc_file) {
                printk(KERN_ERR "Can't create /proc/test/test\n");
                remove_proc_entry("test", NULL);// 移除test目录
                return -1;
        }

proc_file->read_proc = proc_read;
        proc_file->write_proc = proc_write;

printk(KERN_INFO "proc_init\n");
return 0;

}


static __exit proc_exit(void)
{
remove_proc_entry("test", test); // 移除test/test
        remove_proc_entry("test", NULL); // 移除test目录

printk(KERN_INFO "proc_exit\n");
}

module_init(proc_init);
module_exit(proc_exit);

MODULE_LICENSE("GPL");
MODULE_VERSION("v1.0");
MODULE_AUTHOR("xz@vichip.com.cn");
MODULE_DESCRIPTION("Proc Module");
MODULE_ALIAS("proc module");

2、Makefile

ifeq ($(KERNELRELEASE),)
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
all:                               
        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:                                             
        $(MAKE) -C $(KERNELDIR) M=$(PWD) clean
else
    obj-m := proc_test.o

endif


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值