内核模块编程实验-HelloWorld

1、编写一个linux的Helloworld.c文件

#include<linux/module.h>
#include<linux/kernel.h>
int init_hello_module(void)
{
 printk("hello world, i started! ..! it is a kernelmodule in 703\n");
 printf("And our teacher,Mr. YangLiang, is guiding us in how to write a kernel module\n");
 
 return 0;
}
void cleanup_hello_module(void)
{
 printf("goodbye from zhongshan institute\n");
}
MODULE_LICENSE("Dual BSD/GPL");
module_init(init_hello_module);
module_exit(cleanup_hello_module);

2、编写了一个Makefile文件,代码如下:


# To build modules outside of the kernel tree, we run "make"
# in the kernel source tree; the Makefile these then includes this
# Makefile once again.
# This conditional selects whether we are being included from the
# kernel Makefile or not.
# called from kernel build system: just declare what our modules are
obj-m := YesOrNo.o
CROSS_COMPILE =
CC            = gcc

    # Assume the source tree is where the running kernel was built
    # You should set KERNELDIR in the environment if it's elsewhere
    KERNELDIR ?=  /usr/src/linux-headers-2.6.18-6-686
    # The current directory is passed to sub-makes as argument
    PWD := $(shell pwd)
all:  modules

modules:
 $(MAKE) -C $(KERNELDIR) M=$(PWD) modules

clean:
 rm -rf *.o *~ core .depend *.symvers .*.cmd *.ko *.mod.c .tmp_versions $(TARGET)


在Makefile中直接make是出错的,因为makefile中有2个错误的地方,需要改正过来

1、obj-m := YesOrNo.o 中的YesOrNo.o应该改成对应的.C文件名 ,obj-m := Helloworld.o,当然,也可以把Helloworld.c文件改成YesOrNo.c
2、KERNELDIR ?=  /usr/src/linux-headers-2.6.18-6-686 这个地方出错了,读者需要把kerneldir路径改成你所安装linux的版本,查看本机版本号用uname -a命令,在/usr/src/目录下找到相应的版本号的文件路径,然后把它复制到KERNELDIR后面
以下是我的示例

所以我就要改成 KERNELDIR ?=  /usr/src/linux-headers-4.10.0-42-generic

完成这两步骤,就可以使用make命令了。

u  编译

a)   写好了Makefile,直接输入make,即可完成编译

b)   检查当前目录下,是否出现helloworld.ko文件,如果生成了.ko文件,则表示内核模块生成成功

u  运行

a)   insmod helloworld.ko

b)   lsmod 检查是否helloworld已经出现在列表中
c)   tail –f /var/log/syslog 查看是否有打印,修改helloworld.c中的打印,重新编译后,看是否打印有所不同


▲强化 Hack你的操作系统

 修改helloworld.c文件,尝试让你的操作系统无响应。
#include<linux/module.h>
#include<linux/kernel.h>
int init_hello_module(void)
{
 printk("hello world, i started! ..! it is a kernelmodule in 703\n");
 printf("And our teacher,Mr. YangLiang, is guiding us in how to write a kernel module\n");
  while(1)
        {
            ;    
        }
 return 0;
}
void cleanup_hello_module(void)
{
 printf("goodbye from zhongshan institute\n");
}
MODULE_LICENSE("Dual BSD/GPL");
module_init(init_hello_module);
module_exit(cleanup_hello_module);
在文件中加了一个死循环,然后再按着刚才的步骤make编译helloworld.C,然后加载到内核里,系统马上崩溃掉   insmod helloworld.ko  当你敲完这一句回车,系统马上死掉。


强化建议在虚拟机里实验,系统挂掉可以重新启动虚拟机。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

脑电信号要分类

你的鼓励是我创作的前进动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值