话我不多说了,直接上代码了.具体可以查考proc文件系统实例. proc_net.c #include net/net_namespace.h> #include linux/module.h> /* Specifically, a module */ #include linux/kernel.h> /* We're doing kernel work */ #include linux/proc_fs.h> /* Necessary because we use the proc fs */ #define procfs_name "helloworld" struct proc_dir_entry *Our_Proc_File; int procfile_read(char *buffer, char **buffer_location, off_t offset, int buffer_length, int *eof, void *data) { int ret; printk(KERN_INFO "procfile_read (/proc/%s) called\n", procfs_name); /* * We give all of our information in one go, so if the * user asks us if we have more information the * answer should always be no. * * This is important because the standard read * function from the library would continue to issue * the read system call until the kernel replies * that it has no more information, or until its * buffer is filled. */ if (offset > 0) { printk(KERN_INFO "offset > 0\n"); /* we have finished to read, return 0 */ ret = 0; } else { /* fill the buffer, return the buffer size */ printk(KERN_INFO "offset ); ret = sprintf(buffer, "HelloWorld!\n"); } return ret; } int init_module() { Our_Proc_File = create_proc_entry(procfs_name, 0644, init_net.proc_net); if (Our_Proc_File == NULL) { remove_proc_entry(procfs_name, init_net.proc_net); printk(KERN_ALERT "Error: Could not initialize /proc/%s\n", procfs_name); return -ENOMEM; } Our_Proc_File->read_proc = procfile_read; Our_Proc_File->owner = THIS_MODULE; Our_Proc_File->mode = S_IFREG | S_IRUGO; Our_Proc_File->uid = 0; Our_Proc_File->gid = 0; Our_Proc_File->size = 37; printk(KERN_INFO "/proc/net/%s created\n", procfs_name); return 0; /* everything is ok */ } void cleanup_module() { remove_proc_entry(procfs_name, init_net.proc_net); printk(KERN_INFO "/proc/net/%s removed\n", procfs_name); } Makefile obj-m := proc_net.o KDIR := /lib/modules/$(shell uname -r)/build PWD := $(shell pwd) default: $(MAKE) -C $(KDIR) M=$(PWD) modules clean: $(RM) *.o *.mod.c *.ko *.symvers *.markers *.order [root@zj:~/Desktop/net/proc]# cat /proc/net/helloworld |
|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )
GMT+8, 2024-9-29 23:37 , Processed in 0.169387 second(s), 12 queries , Gzip On, MemCache On.
Powered by Discuz! X3.5
© 2001-2023 Discuz! Team.