- 论坛徽章:
- 0
|
我虚拟机装的是redhat9
hello.c代码如下:- #ifndef _KERNEL_
- #define _KERNEL_
- #endif
- #ifndef MODULE
- #define MODULE
- #endif
- #include<linux/module.h>
- //#include<linux/sched.h>
- #include<linux/kernel.h>
- #include<linux/init.h>
- #include<linux/errno.h>
- int test_init();
- void test_exit();
- module_init(test_init);
- module_exit(test_exit);
- int test_init()
- {
- printk("test init ok\n");
- return 0;
- }
- void test_exit()
- {
- printk("test release ok\n");
- }
复制代码 Makefile代码如下:- CC=gcc
- INCLUDE=/usr/src/linux-2.4.20-8/
- OBJS=hello.o
- SRC=hello.c
- FLAGS=-D_KERNEL_ -DMODULE -I $(INCLUDE)
- hello.o:hello.c
- $(CC) $(FLAGS) -c $< -o $@
- clean:
- rm -f $(EXEC) *.o
复制代码 执行make后,在该目录下生成hello.o文件,然后我insmod hello.o文件,期望结果是打印出"test init ok",但是结果如下:
[root@localhost hello]# insmod hello.o
Warning: loading hello.o will taint the kernel: no license
See http://www.tux.org/lkml/#export-tainted for information about tainted modules
Module hello loaded, with warnings
出现警告,要加载的hello.o模块会影响内核,没有授权。没有出现期望的"test init ok"字样,也就是说test_init()函数里的代码根本没有执行。但是我用lsmod测试时,内核中已加载模块却有hello这个模块,测试结果如下:
ot@localhost hello]# lsmod- Module Size Used by Tainted: P
- hello 824 0 (unused)//这就是新增加的hello模块
- ide-cd 35196 0 (autoclean)
- cdrom 33472 0 (autoclean) [ide-cd]
- parport_pc 18756 1 (autoclean)
- lp 8868 0 (autoclean)
- parport 36480 1 (autoclean) [parport_pc lp]
- autofs 12948 0 (autoclean) (unused)
- pcnet32 18016 1
- mii 3944 0 [pcnet32]
- ipt_REJECT 3896 6 (autoclean)
- iptable_filter 2380 1 (autoclean)
- ip_tables 14648 2 [ipt_REJECT iptable_filter]
- keybdev 2880 0 (unused)
- mousedev 5428 1
- hid 21700 0 (unused)
- input 5792 0 [keybdev mousedev hid]
- usb-uhci 25868 0 (unused)
- usbcore 77696 1 [hid usb-uhci]
- ext3 69984 2
- jbd 51220 2 [ext3]
- BusLogic 99932 3
- sd_mod 13324 6
- scsi_mod 106168 2 [BusLogic sd_mod]
复制代码 而且,当我rmmod hello时,期望出现的“test exit ok”字样也没有出现,即test_exit()函数里的代码根本没有执行。
请教各位高手不吝赐教,谢谢! |
|