zoukankan      html  css  js  c++  java
  • First insmod a module

    不得不说网上坑爹的文章比虱子还多,参考这位仁兄调试成功

    喜欢C的人却靠着Java产业吃饭,人艰不拆...

    对于未知的东西,有个习惯,run success first,then research

    environment

    [root@vohst etc]# uname -a
    Linux vohst 3.10.0-123.el7.x86_64 #1 SMP Mon Jun 30 12:09:22 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

    [root@vohst bin]# make
    make -C /lib/modules/3.10.0-123.el7.x86_64/build/ M=/home/voh/bin modules
    make[1]: Entering directory `/usr/src/kernels/3.10.0-123.el7.x86_64'
      CC [M]  /home/voh/bin/hello.o
      Building modules, stage 2.
      MODPOST 1 modules
      CC      /home/voh/bin/hello.mod.o
      LD [M]  /home/voh/bin/hello.ko
    make[1]: Leaving directory `/usr/src/kernels/3.10.0-123.el7.x86_64'
    [root@vohst bin]#

    command(root):

    make

    insmod

    lsmod | head

    rmmod

    [17842.192368] Hello World!
    [18340.765263] Good bye, ubuntu
    [18413.168837] Hello World!
    [18832.134751] Good bye, ubuntu  //i like centos and suse,but suse is allways not purl
    不会打印到屏幕,不重要,logdir=/var/log/dmesg,不是每次都能找到呢...

    c & Makefile

     1 //Begin---hello.c
     2 //my kernel path
     3 //[root@vohst bin]# ls /usr/src/kernels/3.10.0-123.el7.x86_64/include/linux/module.h 
     4 #include</usr/src/kernels/3.10.0-123.el7.x86_64/include/linux/init.h>
     5 #include</usr/src/kernels/3.10.0-123.el7.x86_64/include/linux/module.h>
     6 
     7 MODULE_LICENSE("GPL");
     8 //printk(KERN_ALERT "Begin
    ");
     9 static int hello_init(void)
    10 {
    11   printk(KERN_ALERT "Hello World!
    ");
    12   return 0;
    13 }
    14 
    15 static void hello_exit(void)
    16 {
    17   printk(KERN_ALERT "Good bye, ubuntu
    ");
    18   // return 0;
    19 }
    20 
    21 module_init(hello_init);
    22 module_exit(hello_exit);
    23 //End---hello.c
    #Begin---Makefile
    KERNELDIR=/lib/modules/3.10.0-123.el7.x86_64/build/
    PWD:=$(shell pwd)
    INSTALLDIR=/home/voh/bin
    obj-m:= hello.o
    modules:
    #this line should begins with "#"
        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
    modules_install:
    ##########
        cp hello.ko $(INSTALLDIR)
    clean:
    ##########
        rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
    .PHONY: modules modules_install clean
    #End---Makefile

    ---------------------------------------------------------------------------------------------

    http://blog.csdn.net/fareast8612/article/details/7470386

    【0】笔者的配置环境
    XP->VMWare 7.1->Ubuntu 9.04

    【1】有必要查询下Linux内核
    # uname -r
    2.6.28-11-generic

    # ls /usr/src/
    linux-headers-2.6.28-11 linux-headers-2.6.28-11-generic

    由此可见内核版本和内核头文件版本是一致的,都是2.6.28-11。(如果不一致的话在insmod一步必定出错:
    Error inserting './hello.ko': -1 Invalid module format
    网上有纠正这个错误的方法,但是感觉是在投机——躲避内核的版本检查;笔者在安装Ubuntu 8.04的时候出现过header头文件和内核版本不匹配的问题,后来通过重装Ubuntu为9.04解决之)。

    【2】编写hello.c
    新建自己的工作目录,如:
    # mkdir /home/wk/hello

    编写hello.c
    # cd /home/wk/hello
    # gedit hello.c
    加入以下内容:
    //Begin---hello.c
    #include</usr/src/linux-headers-2.6.28-11/include/linux/init.h>
    #include</usr/src/linux-headers-2.6.28-11/include/linux/module.h>

    MODULE_LICENSE("GPL");
    //printk(KERN_ALERT "Begin ");
    static int hello_init(void)
    {
    printk(KERN_ALERT "Hello World! ");
    return 0;
    }

    static void hello_exit(void)
    {
    printk(KERN_ALERT "Good bye, ubuntu ");
    // return 0;
    }

    module_init(hello_init);
    module_exit(hello_exit);
    //End---hello.c
    注意第一行
    #include</usr/src/linux-headers-2.6.28-11/include/linux/init.h>
    位置要正确;或者你只要写成
    #include<linux/init.h>
    保存退出(Ctrl+Q)。

    【3】编写Makefile
    # cd /home/wk/hello
    # gedit Makefile
    注意大小写。
    #Begin---Makefile
    KERNELDIR=/lib/modules/2.6.28-11-generic/build
    PWD:=$(shell pwd)
    INSTALLDIR=/home/wk/hello/install
    obj-m:= hello.o
    modules:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
    modules_install:
    cp hello.ko $(INSTALLDIR)
    clean:
    rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
    .PHONY: modules modules_install clean
    #End---Makefile

  • 相关阅读:
    20155313 杨瀚 《网络对抗技术》实验九 Web安全基础
    20155313 杨瀚 《网络对抗技术》实验八 Web基础
    20155313 杨瀚 《网络对抗技术》实验七 网络欺诈防范
    20155313 杨瀚 《网络对抗技术》实验六 信息搜集与漏洞扫描
    20155313 杨瀚 《网络对抗技术》实验五 MSF基础应用
    20155313 杨瀚 《网络对抗技术》实验四 恶意代码分析
    20155313 杨瀚 《网络对抗技术》实验三 免杀原理与实践
    20155313 杨瀚 《网络对抗技术》实验二 后门原理与实践
    20155313 杨瀚 《网络对抗技术》实验一 PC平台逆向破解(5)M
    20155313 2017-2018-1 《信息安全系统设计基础》课程总结
  • 原文地址:https://www.cnblogs.com/eiguleo/p/4131030.html
Copyright © 2011-2022 走看看