目录
要求:实现一个proc文件,一个只读文件output,一个可写文件input;功能是:向input文件写入字符串后,可以从output文件中,读出input文件中反转后的字符串
1、编写proc文件
vim my_proc.c
#include <linux/module.h> #include <linux/init.h> #include <linux/kernel.h> #include <linux/proc_fs.h> #include <linux/uaccess.h> #include <linux/slab.h> #define BUFSIZE 1024 static struct proc_dir_entry *input, *output; static char *buf; static unsigned int len; void reverse(void) { int i = 0; int j = len - 2; printk("in reverse = %u\n", len); while(i < j) { char c; c = buf[i]; buf[i++] = buf[j]; buf[j--] = c; } } static ssize_t mywrite(struct file *file, const char __user *ubuf,size_t count, loff_t *ppos) { if(count <= 0) return -EFAULT; printk("---start write---\n"); len = count > BUFSIZE ? BUFSIZE : count; // kfree memory by kmalloc before if(buf != NULL) kfree(buf); buf = (char*)kmalloc(len+1, GFP_KERNEL); if(buf == NULL) { printk("kmalloc failure\n"); return -EFAULT; } //memset(buf, 0, sizeof(buf)); memset(buf, 0, len+1); if(copy_from_user(buf, ubuf, len)) return -EFAULT; printk("writing :%s",buf); return len; } static ssize_t myread(struct file *file, char __user *ubuf,size_t count, loff_t *ppos) { if(*ppos > 0) return 0; printk("---start read---\n"); printk("origin = %s\n", buf); reverse(); printk("after = %s\n", buf); if(copy_to_user(ubuf, buf, len)) return -EFAULT; *ppos = *ppos + len; return len; } static struct file_operations fo_input = { .owner = THIS_MODULE, .write = mywrite, }; static struct file_operations fo_output = { .owner = THIS_MODULE, .read = myread, }; static int proc_init(void) { input=proc_create("input",0660,NULL,&fo_input); if(input == NULL) return -ENOMEM; output=proc_create("output",0660,NULL,&fo_output); if(output == NULL) return -ENOMEM; printk("input & ouput init success!\n"); return 0; } static void proc_cleanup(void) { proc_remove(input); proc_remove(output); printk("input & output cleanup\n"); } module_init(proc_init); module_exit(proc_cleanup); MODULE_LICENSE("GPL");
2、编写配套的Makefile
vim Makefile:
obj-m += my_proc.o all: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules clean: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
3、测试
make 编译一下
如下
make -C /lib/modules/4.18.11/build M=/home/zj/neihe/proc_module_making/my_proc clean
make[1]: 正在进入目录 `/usr/src/linux-4.18.11'
CLEAN /home/zj/neihe/proc_module_making/my_proc/.tmp_versions
CLEAN /home/zj/neihe/proc_module_making/my_proc/Module.symvers
make[1]:正在离开目录 `/usr/src/linux-4.18.11'
如果觉得文章对你有帮助,欢迎关注微信公众号:小牛呼噜噜
然后sudo su 切换的root用户下
ls /proc/
发现多了2个文件:input ,output
ehco "test" >> /proc/input
cat /proc/input
打印:tset
ps:dmesg
内核探索暂时先到这里,先告一段落= =