PROC对文件进行读写
2018-01-05 10:56
302 查看
ifneq ($(KERNELRELEASE),) obj-m := proc.o else KDIR := /lib/modules/2.6.29/build all: make -C $(KDIR) M=$(PWD) modules clean: rm -f *.ko *.o *.mod.o *.mod.c *.symvers modul* endif
#include <linux/module.h> #include <linux/proc_fs.h> #include <asm/uaccess.h> static struct proc_dir_entry *mydir; static struct proc_dir_entry *pfile; static char msg[255]; static int myproc_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 myproc_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 int myproc_init(void) { mydir = proc_mkdir("mydir", NULL); if (!mydir) { printk(KERN_ERR "Can't create /proc/mydir\n"); return -1; } pfile = create_proc_entry("pool", 0666, mydir); if (!pfile) { printk(KERN_ERR "Can't create /proc/mydir/pool\n"); remove_proc_entry("mydir", NULL); return -1; } pfile->read_proc = myproc_read; pfile->write_proc = myproc_write; return 0; } static void myproc_exit(void) { remove_proc_entry("pool", mydir); remove_proc_entry("mydir", NULL); } module_init(myproc_init); module_exit(myproc_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("David Xie");
copy from user 指从用户态来读到内核态
[root@localhost ~]# echo "Hello from kernel" > /proc/mydir/pool [root@localhost ~]# cat /proc/mydir/pool Hello from kernel
运行进程 echo将文件写入pool
杀死进程 则pool文件不存在
相关文章推荐
- 对/proc文件系统进行读写操作
- C++中对文件进行读写操作
- iOS使用读写plist文件方式对数据进行增删改查
- Python实现按特定格式对文件进行读写的方法示例
- 使用FileSystem类进行文件读写及查看文件信息
- 对文件夹中所文件(csv)进行读写操作
- C++中对文件进行读写操作
- linux下如何知道一个文件被哪个进程进行读写
- 在SQL 脚本中进行 文件的读写
- Hadoop编程学习(四):使用FileSystem类进行文件读写及查看文件信息
- C#控制台进行文件读写的方法
- 使用Python进行二进制文件读写
- Metor 利用memcpy 对所有文件进行读写
- android中如何在SD卡上进行文件读写操作
- 使用avilib进行avi文件的读写
- 用C语言进行BMP文件的读写
- [Visual C++系列]3.5 利用序列化进行文件读写
- MFC 进行CSV文件的读写
- JSP 自己写的在JSP页面得到文件的服务器路径,并对XML文件进行读写(以对Tomcat的conf目录中的context.xml文件为例)
- Lucene4.2源码解析之fdt和fdx文件的读写(续)——fdx文件存储一个个的Block,每个Block管理着一批Chunk,通过docID读取到document需要完成Segment、Block、Chunk、document四级查询,引入了LZ4算法对fdt的chunk docs进行了实时压缩/解压