+ -
当前位置:首页 → 问答吧 → module_init()函数内代码不能执行

module_init()函数内代码不能执行

时间:2010-07-25

来源:互联网

我虚拟机装的是redhat9
hello.c代码如下:
  1. #ifndef _KERNEL_
  2.         #define _KERNEL_
  3. #endif
  4. #ifndef MODULE
  5.         #define MODULE
  6. #endif

  7. #include<linux/module.h>
  8. //#include<linux/sched.h>
  9. #include<linux/kernel.h>
  10. #include<linux/init.h>
  11. #include<linux/errno.h>
  12. int test_init();
  13. void test_exit();
  14. module_init(test_init);
  15. module_exit(test_exit);
  16. int test_init()
  17. {
  18.         printk("test init ok\n");
  19.         return 0;
  20. }

  21. void test_exit()
  22. {
  23.         printk("test release ok\n");

  24. }
复制代码
Makefile代码如下:
  1. CC=gcc
  2. INCLUDE=/usr/src/linux-2.4.20-8/
  3. OBJS=hello.o
  4. SRC=hello.c
  5. FLAGS=-D_KERNEL_ -DMODULE -I $(INCLUDE)

  6. hello.o:hello.c
  7.         $(CC) $(FLAGS) -c $< -o $@
  8. clean:
  9.         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
  1. Module                  Size  Used by    Tainted: P
  2. hello                    824   0  (unused)//这就是新增加的hello模块
  3. ide-cd                 35196   0  (autoclean)
  4. cdrom                  33472   0  (autoclean) [ide-cd]
  5. parport_pc             18756   1  (autoclean)
  6. lp                      8868   0  (autoclean)
  7. parport                36480   1  (autoclean) [parport_pc lp]
  8. autofs                 12948   0  (autoclean) (unused)
  9. pcnet32                18016   1
  10. mii                     3944   0  [pcnet32]
  11. ipt_REJECT              3896   6  (autoclean)
  12. iptable_filter          2380   1  (autoclean)
  13. ip_tables              14648   2  [ipt_REJECT iptable_filter]
  14. keybdev                 2880   0  (unused)
  15. mousedev                5428   1
  16. hid                    21700   0  (unused)
  17. input                   5792   0  [keybdev mousedev hid]
  18. usb-uhci               25868   0  (unused)
  19. usbcore                77696   1  [hid usb-uhci]
  20. ext3                   69984   2
  21. jbd                    51220   2  [ext3]
  22. BusLogic               99932   3
  23. sd_mod                 13324   6
  24. scsi_mod              106168   2  [BusLogic sd_mod]
复制代码
而且,当我rmmod hello时,期望出现的“test exit ok”字样也没有出现,即test_exit()函数里的代码根本没有执行。
请教各位高手不吝赐教,谢谢!

作者: 0vk0   发布时间: 2010-07-25

本帖最后由 wmmy2008 于 2010-07-25 10:56 编辑

Linux module should be compiled to  *.ko

作者: wmmy2008   发布时间: 2010-07-25