- 论坛徽章:
- 9
|
回复 1# archangle
你们不能什么不会就直接上来问,Linux不是Windows,不会什么都有教程,都要写得清清楚楚。
这种没法用一两句话说清楚,但又不是什么很难的问题,是很少有人理会的!!!
要学Linux,就要学会自己看代码,自己去学习,要说Linux最大的学习方法,就是RTFSC(Read the fucking source code)!
内核里头有大量的参考代码,要先学会查找、仿照。
你搜搜关键字,就会发现/proc/sys/net/netfilter不是由create_proc创建的!!!! 用的是sysctl!!!! 然后自己去百度一下就会有很多的教程!
至于在现成的目录低下去建文件,方法就是多仿照,多试验。因为网上的例子都是建一个全新的目录的。
我仿照的就是cdrom.c文件,至于试验,至少我没有写个测试案例以前,我也不确定走得通。
下面的代码在 Linux 3.1.0-7.fc16.i686.PAE下测试通过。- #include <linux/init.h>
- #include <linux/module.h>
- #include <linux/sysctl.h>
- #include <linux/stat.h>
- MODULE_LICENSE("GPL");
- static int MyNewEntry = 0;
- static struct ctl_table_header *root_header;
- static struct ctl_table my_table[] = {
- {
- .procname = "MyNewEntry",
- .mode = 0644,
- .data = & MyNewEntry,
- .maxlen = sizeof(int),
- .proc_handler = &proc_dointvec
- },
- {.procname = 0}
- };
- static struct ctl_table net_table[] = {
- {
- .procname = "netfilter",
- .mode = 0555,
- .child = my_table,
- },
- {.procname = 0}
- };
- static struct ctl_table root_table[] = {
- {
- .procname = "net",
- .mode = 0555,
- .child = net_table,
- },
- {.procname = 0}
- };
- struct proc_dir_entry *myproc = NULL;
- static int procfile_init(void)
- {
- printk("procfile enter\n");
- root_header = register_sysctl_table(root_table);
- return 0;
- }
- static void procfile_exit(void)
- {
- printk("procfile exit\n");
- if(root_header)
- unregister_sysctl_table(root_header);
- }
- module_init(procfile_init);
- module_exit(procfile_exit);
复制代码 |
|