一、頭檔案
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;
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;
int pde_users;
spinlock_t pde_unload_lock;
struct completion *pde_unload_completion;
struct list_head pde_openers;
};
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("[email protected]");
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