linux driver 简单实例

来源:互联网 发布:数据透视图 总数 编辑:程序博客网 时间:2024/06/06 03:03

helloworld.c 代码:


#include <linux/module.h>

#include <linux/kernel.h>
#include <linux/init.h>

static int __init hellokernel_init(void)
{
    printk(KERN_INFO "hello kernel!\n");
    return 0;
}

static void __exit hellokernel_exit(void)
{
    printk(KERN_INFO "exit kernel!\n");
}

module_init(hellokernel_init);
module_exit(hellokernel_exit);

MODULE_LICENSE("GPL");

MODULE_AUTHOR("****");


Makefile 代码:

hailonxi@hailonxi-VirtualBox:~$ cat Makefile

obj-m := helloworld.o

PWD    := $(shell pwd)

all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
    rm -rf *.o *~core .*.cmd *.mod.c ./tmp_version


编译make:

hailonxi@hailonxi-VirtualBox:~$ make
make -C /lib/modules/3.11.0-15-generic/build M=/home/hailonxi modules
make[1]: Entering directory `/usr/src/linux-headers-3.11.0-15-generic'
  Building modules, stage 2.
  MODPOST 1 modules
make[1]: Leaving directory `/usr/src/linux-headers-3.11.0-15-generic'

目录生成文件如下:

hailonxi@hailonxi-VirtualBox:~$ ls
Desktop           helloworld.c      helloworld.o    Music      Videos
Documents         helloworld.ko     Makefile        Pictures
Downloads         helloworld.mod.c  modules.order   Public
examples.desktop  helloworld.mod.o  Module.symvers  Templates


执行:

hailonxi@hailonxi-VirtualBox:~$
hailonxi@hailonxi-VirtualBox:~$ insmod helloworld.ko  //插入模块
insmod: error inserting 'helloworld.ko': -1 Operation not permitted===要root权限
hailonxi@hailonxi-VirtualBox:~$ sudo insmod helloworld.ko  ===插入模块
[sudo] password for hailonxi:
hailonxi@hailonxi-VirtualBox:~$ sudo rmmod helloworld ==删除模块
hailonxi@hailonxi-VirtualBox:~$ dmesg | tail -8  ===显示驱动的打印信息
[    8.684696] vesafb: framebuffer at 0xe0000000, mapped to 0xffffc90000680000, using 1216k, total 1216k
[    8.685904] Console: switching to colour frame buffer device 80x30
[    8.687450] fb0: VESA VGA frame buffer device
[   45.864889] audit_printk_skb: 171 callbacks suppressed
[   45.864892] type=1400 audit(1438051706.759:69): apparmor="DENIED" operation="open" parent=1 profile="/usr/lib/telepathy/mission-control-5" name="/usr/share/gvfs/remote-volume-monitors/" pid=1914 comm="mission-control" requested_mask="r" denied_mask="r" fsuid=1000 ouid=0
[  897.462531] type=1400 audit(1438052558.359:70): apparmor="DENIED" operation="capable" parent=1 profile="/usr/sbin/cupsd" pid=671 comm="cupsd" pid=671 comm="cupsd" capability=36  capname="block_suspend"
[ 1597.010173] hello kernel!
[ 1726.934708] exit kernel!

谢谢!!









0 0
原创粉丝点击