- 论坛徽章:
- 0
|
代码如下:
- #include <linux/init.h>
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/file.h>
- #include <linux/fs.h>
- #include <linux/sched.h>
- #include <linux/syscalls.h>
- #include <linux/time.h>
- #include <asm/unistd.h>
- MODULE_LICENSE("GPL");
- unsigned long **sys_call_table;
- ssize_t (*read)(int f, const void *buf, size_t n);
- unsigned long **find_sys_call_table(void) {
- unsigned long **sctable;
- unsigned long ptr;
- extern int loops_per_jiffy;
- sctable = NULL;
- for (ptr = (unsigned long)&loops_per_jiffy;
- ptr < (unsigned long)&boot_cpu_data; ptr += sizeof(void *)){
-
- unsigned long *p;
- p = (unsigned long *)ptr;
- if (p[__NR_close] == (unsigned long) sys_close){
- sctable = (unsigned long **)p;
- return &sctable[0];
- }
- }
- return NULL;
- }
- ssize_t loggingread(int fd, const void *buf, size_t n) {
- printk("read\n");
- return read(fd, buf, n);
- }
- static int __init readlog_init(void) {
- sys_call_table = find_sys_call_table();
- printk(KERN_INFO "Loading readlog module, sys call table at %p\n", sys_call_table);
- read = (void *) (sys_call_table[__NR_read]);
- sys_call_table[__NR_read] = (void *) loggingread;
- return 0;
- }
- static void __exit readlog_exit(void) {
- printk(KERN_INFO "Unloading readlog module\n");
- sys_call_table[__NR_read] = (void *) read;
- }
- module_init(readlog_init);
- module_exit(readlog_exit);
复制代码
Makefile如下:
- bj-m := test.o
- KDIR := /lib/modules/$(shell uname -r)/build
- PWD := $(shell pwd)
- default:
- $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
复制代码
调试insmod test.ko就死机,不知道怎么办,不是很懂代码?
[ 本帖最后由 lonelyair 于 2006-7-31 12:44 编辑 ] |
|