- 论坛徽章:
- 11
|
请教各位
在/proc下创建了一个测试文件,并手动指定了write回调函数,write中调用copy_from_user,将输入在proc文件中的内容写入到内核空间
现在的问题是,如果proc文件中包含换行符时,copy_from_user会对内容分段写入多次
程序:proc_rw.c
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/proc_fs.h>
- #include<linux/sched.h>
- #include <asm/uaccess.h>
- #include <linux/slab.h>
- #include <linux/string.h>
- int len,temp;
- char *msg;
- int read_proc(struct file *filp,char *buf,size_t count,loff_t *offp)
- {
- if(count>temp)
- {
- count=temp;
- }
- temp=temp-count;
- printk("read\n");
- printk("count = %d\n", count);
- printk("strlen(msg) = %d\n", strlen(msg));
- copy_to_user(buf,msg, count);
- if(count==0)
- temp=len;
- return count;
- }
- int write_proc(struct file *filp,const char *buf,size_t count,loff_t *offp)
- {
- if (copy_from_user(msg,buf,count))
- return -EFAULT;
- len=count;
- temp=len;
- printk("write\n");
- printk("count = %d\n", count);
- printk("strlen(msg) = %d\n", strlen(msg));
- return count;
- }
- struct file_operations proc_fops = {
- read: read_proc,
- write: write_proc
- };
- void create_new_proc_entry()
- {
- proc_create("hello",0,NULL,&proc_fops);
- msg=kmalloc(GFP_KERNEL,10*sizeof(char));
- }
- int proc_init (void) {
- create_new_proc_entry();
- return 0;
- }
- void proc_cleanup(void) {
- remove_proc_entry("hello",NULL);
- }
- MODULE_LICENSE("GPL");
- module_init(proc_init);
- module_exit(proc_cleanup);
复制代码 Makefile
- obj-m += proc_rw.o
- KDIR := /lib/modules/$(shell uname -r)/build
- PWD := $(shell pwd)
- EXTRA_CFLAGS = -g -O0 -std=gnu99 -Wno-declaration-after-statement
- all:
- make -C $(KDIR) M=$(PWD) modules
- rm -rf *.ko.unsigned *.mod.c *.mod.o *.symvers *.o
- .PHONY: clean
- clean:
- make -C $(KDIR) M=$(PWD) clean
复制代码 make并insmod
写入后查看内容
- echo -ne "a b c" > /proc/hello
复制代码 写入包含换行符的字符序列
- echo -ne "a b c\td \ne f" > /proc/hello
复制代码 请各位大神指点一下,怎样copy_from_user()一次读取包含换行符的字符序列
|
|