一、头文件
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