• HelloWorld 模块


    helloworld.c 代码

     1 #include <linux/init.h>
     2 #include <linux/module.h>
     3 
     4 MODULE_LICENSE("Dual BSD/GPL");
     5 
     6 static int hello_init(void)
     7 {
     8 printk(KERN_ALERT "Hello world
    ");
     9 return 0;
    10 }
    11 
    12 static void hello_exit(void)
    13 {
    14 printk(KERN_ALERT "goodbye,cruel world
    ");
    15 }
    16 
    17 module_init(hello_init);
    18 module_exit(hello_exit);

    Makefile 代码

    obj-m := helloworld.o
    
    CURRENT_DIR :=$(shell pwd)
    
    KERNEL_DIR := /usr/src/linux-headers-$(shell uname -r)
    
    all:
            $(MAKE) -C $(KERNEL_DIR) M=$(CURRENT_DIR) modules
    
    clean:
            rm -rf %.o

    执行make

    ryan@Ryan-pc:/data1/Ryan/demo/helloworld$ make 
    make -C /usr/src/linux-headers-3.13.0-32-generic  M=/data1/Ryan/demo/helloworld modules
    make[1]: Entering directory `/usr/src/linux-headers-3.13.0-32-generic'
      Building modules, stage 2.
      MODPOST 1 modules
    make[1]: Leaving directory `/usr/src/linux-headers-3.13.0-32-generic'

    加载模块

    1 ryan@Ryan-pc:/data1/Ryan/demo/helloworld$ sudo insmod helloworld.ko 
    2 ryan@Ryan-pc:/data1/Ryan/demo/helloworld$ 

    dmesg查看

    1 ryan@Ryan-pc:/data1/Ryan/demo/helloworld$ dmesg
    [178401.813566] sr 0:0:0:0: [sr0] Device not ready
    [178401.813570] sr 0:0:0:0: [sr0]  
    [178401.813571] Result: hostbyte=DID_OK driverbyte=DRIVER_SENSE
    [178401.813572] sr 0:0:0:0: [sr0]  
    [178401.813573] Sense Key : Not Ready [current] 
    [178401.813575] sr 0:0:0:0: [sr0]  
    [178401.813577] Add. Sense: Medium not present - tray closed
    [178401.813579] sr 0:0:0:0: [sr0] CDB: 
    [178401.813580] Read(10): 28 00 00 00 02 2f 00 00 01 00
    [178401.813584] end_request: I/O error, dev sr0, sector 2236
    [179614.794066] helloworld: module verification failed: signature and/or  required key missing - tainting kernel
    [179614.794552] Hello world
    [180458.268623] goodbye,cruel world
    [180514.941519] Hello world
    [180549.136795] goodbye,cruel world
    [181244.193514] Hello world

    卸载模块

    1 ryan@Ryan-pc:/data1/Ryan/demo/helloworld$ sudo rmmod helloworld 
    2 ryan@Ryan-pc:/data1/Ryan/demo/helloworld$ 
     
  • 相关阅读:
    关联模型(1:n)
    关联模型 (1对1)
    cheerio api
    二维数组去重
    Fiddler 模拟post 提交
    DataReader 转为实体
    在做一些复杂的类型转换之前(比如将一个数据转换成一个属性的类型,属性可能为可空类型)先判断该类型是否为可空类型,否则会报如下错误:
    WebClient 文件下载
    利用iTextSharp组件给PDF文档添加图片水印,文字水印
    TList、DataTable To Json
  • 原文地址:https://www.cnblogs.com/xiaoxiaodewo/p/5621613.html
Copyright © 2020-2023  润新知