免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2115 | 回复: 3
打印 上一主题 下一主题

求助:module_init()函数内代码无法执行问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-11-13 14:47 |只看该作者 |倒序浏览
小弟初学驱动编程,想熟悉下环境,就编了个小程序,但出现问题,求助各位大侠!
我虚拟机装的是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()函数里的代码根本没有执行。
小弟百思不得其解,请教各位高手不吝赐教,谢谢!

论坛徽章:
5
摩羯座
日期:2014-07-22 09:03:552015元宵节徽章
日期:2015-03-06 15:50:392015亚冠之大阪钢巴
日期:2015-06-12 16:01:352015年中国系统架构师大会
日期:2015-06-29 16:11:2815-16赛季CBA联赛之四川
日期:2018-12-17 14:10:21
2 [报告]
发表于 2009-11-13 15:07 |只看该作者
dmesg就能看到了
或者cat /proc/kmsg

论坛徽章:
1
天蝎座
日期:2013-10-23 21:11:03
3 [报告]
发表于 2009-11-13 15:36 |只看该作者
看看/var/log/messages
或者/var/log/syslog

论坛徽章:
0
4 [报告]
发表于 2009-11-14 23:41 |只看该作者
可能内核有什么版本检测啊
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP