有三个文件,分别是proc_rw.c、test.c和test.h,现在把这三个文件编译成proc.ko
1、proc_rw.c
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/sched.h>
#include <linux/uaccess.h> //copy_to|from_user
#include "test.h"
//在str中存储用户态write到文件的字符串
static char *str;
//proc文件的读函数
static int my_proc_read(char *page, char **start, off_t off, int count, int *eof, void *data)
{
int ret = 0,val = 0;
//先输出时间,后输出从外部写入的内容
//ret = sprintf(page, "kernel time: %ld\n", jiffies);
val = test_func();
ret = sprintf(page, "test func: %d\n", val);
ret += sprintf(page+ret, "str is %s\n", str);
return ret;
}
//proc文件的写函数
static int my_proc_write(struct file *filp, const char __user *buf, unsigned long count, void *data)
{
//为从用户态拷贝字符串分配空间
char *tmp = kzalloc(count, GF