下面所示清单为一个简单的 /proc 使用范例,这段代码在模块加载函数中创建 /proc 文件节点,在模块卸载函数中撤销 /proc 节点,文件中保存了一个32位无符号整形值。
#include <asm/uaccess.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
static struct proc_dir_entry *proc_entry;
static unsigned long val = 0x12345678;
/* 璇籶roc 鏂囦欢鎺ュ彛*/
ssize_t simple_proc_read(char *page,char **start,off_t off,int count,int *eof,void *data)
{
int len;
if(off > 0) { /*涓嶈兘鍋忕Щ璁块棶*/
*eof = 1;
return 0;
}
len = sprintf(page,"%08x\n",val);
return len;
}
/* 鍐檖roc 鏂囦欢鎺ュ彛*/
ssize_t simple_proc_write(struct file *filp,const char __user *buff,unsigned long len,void *data)
{
#define MAX_UL_LEN 8
char k_buf[MAX_UL_LEN];
char *endp;
unsigned long new;
int count = min(MAX_UL_LEN,len);
int ret;
if(copy_from_user(k_buf,buff,count)){
ret = -EFAULT;
goto err;
}else{
new = simple_strtoul(k_buf,&endp,1