Linux内核模块编程之Helloworld(初级)
来源:互联网 发布:python 函数是对象 编辑:程序博客网 时间:2024/05/22 11:58
注意printk那里,KERN_ALERT和打印消息之间是没有逗号的,搞得劳资查了半天才发现一直没有提示信息的原因
#include <linux/init.h>#include <linux/module.h>MODULE_LICENSE("Dual BSD/GPL");//MODULE_LICENSE()指明认证方式,现在支持的有:“GPL” “GPL v2" "GPL and additional rights" "Dual BSD/GPL" "Dual MIT/GPL" "Dual MPL/GPL" "Proprietary",这是内核2.6里新添加的,实验发现它不是必需的。static int hello_init(void){ printk(KERN_ALERT "Hello, World\n");//printk是内核级别的打印函数,KERN_ALERT是指该条信息是警告信息 return 0;}static int hello_exit(void){ printk(KERN_ALERT "Goodbye, cruel world\n");}module_init(hello_init);//模块入口module_exit(hello_exit);//模块出口
下面我们来写Makefile(命名为makefile我的会提示错误),-C 去内核源码目录下读取Makefile,m=返回当前路径执行当前目录下的Makefile
。我觉得Makefile这个文件挺6的,该文件会根据xx.o查找相应的xx.c文件
TARGET=helloKDIR=/usr/src/kernels/3.10.0-514.el7.x86_64 //找到内核文件所在路径,系统不同路径也不同,可以使用find / -name kernel查找PWD=$(shell pwd) //这个是指执行shell命令pwd,即用PWD记录当前路径obj-m=$(TARGET).odefault: make -C $(KDIR) M=$(PWD) modules
那么如何运行呢,首先make
[04:21:42] makemake -C /usr/src/kernels/3.10.0-514.el7.x86_64 M=/root/kernel modulesmake[1]: Entering directory `/usr/src/kernels/3.10.0-514.el7.x86_64' CC [M] /root/kernel/hello.o/root/kernel/hello.c: In function ‘hello_exit’:/root/kernel/hello.c:14:1: warning: no return statement in function returning non-void [-Wreturn-type] } ^In file included from /root/kernel/hello.c:1:0:/root/kernel/hello.c: In function ‘__exittest’:include/linux/init.h:305:4: warning: return from incompatible pointer type [enabled by default] { return exitfn; } \ ^/root/kernel/hello.c:17:1: note: in expansion of macro ‘module_exit’ module_exit(hello_exit); ^ Building modules, stage 2. MODPOST 1 modules CC /root/kernel/hello.mod.o LD [M] /root/kernel/hello.komake[1]: Leaving directory `/usr/src/kernels/3.10.0-514.el7.x86_64'[04:21:45] lshello.c hello.ko hello.mod.c hello.mod.o hello.o Makefile modules.order Module.symvers
现在进行加载模块,首先在当前终端
[04:21:47] tail -f /var/log/messagesMay 22 04:01:01 bogon systemd: Started Session 727 of user root.May 22 04:01:01 bogon systemd: Starting Session 727 of user root.May 22 04:10:01 bogon systemd: Started Session 728 of user root.May 22 04:10:01 bogon systemd: Starting Session 728 of user root.May 22 04:18:35 bogon kernel: Hello, World//在另一个终端执行insmod ./hello.ko才会出现这个May 22 04:18:49 bogon kernel: Goodbye, cruel world//在另一个终端执行rmmod hello才会出现这个May 22 04:20:01 bogon systemd: Started Session 729 of user root.May 22 04:20:01 bogon systemd: Starting Session 729 of user root.May 22 04:20:01 bogon kernel: Hello, WorldMay 22 04:20:11 bogon kernel: Goodbye, cruel worldMay 22 04:22:06 bogon kernel: Hello, World
我在另一个终端的执行过程就是进入该目录,然后执行insmod ./hello.ko 然后第一个终端就会显示Hello,world。在执行rmmod hello 就会显示Goodbye, cruel world
但是!根据系统版本的不同还是什么其他原因,有时候信息在/var/log/messages 里看到,使用dmesg可以看到
[root@bogon kernel]# dmesg|tail -5[ 123.690748] test: module verification failed: signature and/or required key missing - tainting kernel[ 876.865300] Hello, World[ 908.638904] Goodbye, cruel world[ 1235.101965] e1000: ens33 NIC Link is Down[ 1241.118672] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None[root@bogon kernel]#
或者是在加载或者移除模块这里看到
也可以像下面这么写,注意,uname -r那里是使用的反引号就是tab键上面的那个键,$(PWD)当前工作目录,clean清除,打印信息是显示在另一个终端的,例如我就是在物理机上使用ssh链接虚拟机centos7,然后执行下面的命令,而在虚拟机的终端上则会显示打印的那几条信息
[root@bogon modules]# cat first.c#include<linux/kernel.h>#include<linux/module.h>int init_module(void){ printk(KERN_ALERT "hi,this is bp\n"); return 0;}void cleanup_module(void){ printk(KERN_ALERT "goobye bp\n");}[root@bogon modules]# cat Makefile obj-m=first.odefault: make -C /usr/src/kernels/`uname -r` M=$(PWD) modulesclean: make -C /usr/src/kernels/`uname -r` M=$(PWD) clean[root@bogon modules]# makemake -C /usr/src/kernels/`uname -r` M=/root/modules modulesmake[1]: Entering directory `/usr/src/kernels/3.10.0-514.el7.x86_64' Building modules, stage 2. MODPOST 1 modulesmake[1]: Leaving directory `/usr/src/kernels/3.10.0-514.el7.x86_64'[root@bogon modules]# modinfo first.ko//查看模块信息filename: /root/modules/first.korhelversion: 7.3srcversion: 2523BB278E7311D9141E7F4depends: vermagic: 3.10.0-514.el7.x86_64 SMP mod_unload modversions [root@bogon modules]# insmod first.ko[root@bogon modules]# rmmod first[root@bogon modules]# lsa.c a.mod.o first.ko first.o hello.mod.c Makefilea.ko a.o first.mod.c hello.c hello.mod.o modules.ordera.mod.c first.c first.mod.o hello.ko hello.o Module.symvers[root@bogon modules]# make cleanmake -C /usr/src/kernels/`uname -r` M=/root/modules cleanmake[1]: Entering directory `/usr/src/kernels/3.10.0-514.el7.x86_64' CLEAN /root/modules/.tmp_versions CLEAN /root/modules/Module.symversmake[1]: Leaving directory `/usr/src/kernels/3.10.0-514.el7.x86_64'[root@bogon modules]# lsa.c first.c hello.c Makefile[root@bogon modules]#
阅读全文
0 0
- Linux内核模块编程之Helloworld(初级)
- Linux内核模块编程之Helloworld!
- Linux内核模块编程之helloworld
- Linux内核模块编程-HelloWorld
- Linux v2.6内核编程之最简单的内核模块helloworld
- Linux 内核模块 helloworld
- linux内核模块编程之入门(一) 模块编程
- Linux之内核模块编程
- Ubuntu下内核编程之第一个模块helloworld
- 【转载】Ubuntu下内核编程之第一个模块helloworld
- linux 内核模块编程之模块参数(四)
- linux 内核模块编程之内核符号导出(五)
- linux内核编程helloworld(中级)
- 内核模块编程---helloworld简单入门程序(1)
- linux内核模块编程之进阶(三)
- linux 内核模块编程之环境搭建(一)
- linux 内核模块编程之hello word(二)
- linux 内核模块编程之LED驱动程序(六)
- Android 颜色渐变(gradient)的实现总结
- 大数的加减乘除基本思想(拆开来算)
- java的数据类型(引用数据类型)
- hibernate save和update以及saveOrUpdate区别
- PHP基础语法
- Linux内核模块编程之Helloworld(初级)
- linux下测试网速
- C json实战引擎 一 , 实现解析部分,json实战
- 常用HTTP状态码
- Android程序员看世界--人工智能AI(1)
- Java中异常处理示例
- 在struts2中获取请求action名称或参数
- dbm数据库操作
- Python 边做边学 8.1 工具类--HTTP工具