driver hello

 

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
//必选
//模块许可声明
MODULE_LICENSE("GPL");
//模块加载函数
static int hello_init(void)
{
        printk(KERN_ALERT "hello,I am edsionte\n");
        return 0;
}
//模块卸载函数
static void hello_exit(void)
{
        printk(KERN_ALERT "goodbye,kernel\n");
}
//模块注册
module_init(hello_init);
module_exit(hello_exit);
//可选
MODULE_AUTHOR("edsionte Wu");
MODULE_DESCRIPTION("This is a simple example!\n");
MODULE_ALIAS("A simplest example");

通常一个模块程序的中,模块加载函数,模块卸载函数以及模块许可声明是必须有的,而象模块参数,模块导出符号以及模块作者信息声明等都是可选的。

我们编写了模块加载函数后,还必须用module_init(mode_name);的形式注册这个函数。因为当我们接下来用insmod加载模块 时,内核会自动去寻找并执行内核加载函数,完成一些初始化工作。类似的当我们使用rmmod命令时,内核会自动去执行内核卸载函数。

请注意这里的printk函数,可以简单的理解为它是内核中的printf函数,初次使用很容易将其打成printf。

obj-m += hello.o
#generate the path
CURRENT_PATH:=$(shell pwd)
#the current kernel version number
LINUX_KERNEL:=$(shell uname -r)
#the absolute path
LINUX_KERNEL_PATH:=/usr/src/linux-headers-$(LINUX_KERNEL)
#complie object
all:
        make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules
#clean
clean:
        make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) clean

obj-m obj-m表示需要编绎成模块的目标文件名集合,意思是将后面跟的东东编译成内核模块
obj-y 编译进内核
obj-n 不编译

当make的目标为all时,-C $(KDIR) 指明跳转到内核源码目录下读取那里的Makefile;M=$(PWD) 表明然后返回到当前目录继续读入、执行当前的Makefile。

mutian@mutian:~/test$ make
make -C /usr/src/linux-headers-3.5.0-54-generic M=/home/mutian/test modules
make[1]: Entering directory `/usr/src/linux-headers-3.5.0-54-generic'
  CC [M]  /home/mutian/test/hello.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/mutian/test/hello.mod.o
  LD [M]  /home/mutian/test/hello.ko
make[1]: Leaving directory `/usr/src/linux-headers-3.5.0-54-generic'
mutian@mutian:~/test$ ls
hello.c  hello.ko  hello.mod.c  hello.mod.o  hello.o  Makefile  modules.order  Module.symvers
mutian@mutian:~/test$ sudo insmod hello.ko
mutian@mutian:~/test$ sudo rmmod hello.ko
mutian@mutian:~/test$ dmesg
............
[   21.022456] 00:00:00.000220 main     OS Version: #81~precise1-Ubuntu SMP Tue Jul 15 04:02:22 UTC 2014
[   21.022474] 00:00:00.000233 main     Executable: /opt/VBoxGuestAdditions-5.0.10/sbin/VBoxService
[   21.022474] 00:00:00.000234 main     Process ID: 1177
[   21.022474] 00:00:00.000235 main     Package type: LINUX_64BITS_GENERIC
[   21.023263] 00:00:00.001017 main     5.0.10 r104061 started. Verbose level = 0
[   21.079170] 00:00:00.056889 automount vbsvcAutoMountWorker: Shared folder 'vm-share' was mounted to '/media/sf_vm-share'
[  343.793132] audit_printk_skb: 54 callbacks suppressed
[  343.793141] type=1400 audit(1462263564.088:30): apparmor="DENIED" operation="capable" parent=1 profile="/usr/sbin/cupsd" pid=951 comm="cupsd" pid=951 comm="cupsd" capability=36  capname="block_suspend"
[ 1203.772307] type=1400 audit(1462264423.952:31): apparmor="DENIED" operation="capable" parent=1 profile="/usr/sbin/cupsd" pid=951 comm="cupsd" pid=951 comm="cupsd" capability=36  capname="block_suspend"
[87389.124192] hello,I am edsionte
[87414.307776] goodbye,kernel

 

posted @ 2016-05-04 16:47  牧 天  阅读(219)  评论(0)    收藏  举报