2014年7月2日 星期三

[Kernel Module] Make own kernel module

Code 1


#include <linux/init.h> 
#include <linux/module.h> 
MODULE_LICENSE("Dual BSD/GPL"); static char *book_name = "dissecting Linux Device Driver"; static int num = 4000; static int param[8] = {1, 2, 3, 4, 5, 6, 7, 8}; static int param_len = 8; static int book_init(void) { 
    int i; 
    printk(KERN_ALERT " book name: %s\n", book_name); 
    printk(KERN_ALERT " book num: %d\n", num); 
    for(i = 0; i < 8; i++)  
    { 
        printk(KERN_ALERT "param[%d] = %d \n", i, param[i]); 
    } 
    return 0; } 
static void book_exit(void) { 
    printk(KERN_ALERT " Book module exit\n"); } 

module_init(book_init); 
module_exit(book_exit); 
module_param(num, int, S_IRUGO); 
module_param(book_name, charp, S_IRUGO); 
module_param_array(param, int, &param_len, S_IRUGO); 


MODULE_AUTHOR("Jimmy, fightingjimmy@gmail.com"); 
MODULE_DESCRIPTION("A simple Module for testing module params"); 
MODULE_VERSION("v1.0");

Compile


make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules
                         |
                         V
make -C /lib/modules/3.8.0-33-generic/build M=`pwd` test.ko

insmod


sudo insmod test.ko book_name='GoodBook' num=5000

Result


Jul  3 07:48:16 happy-Desktop kernel: [10874.385886]  book num: 5000
Jul  3 07:48:16 happy-Desktop kernel: [10874.385891] param[0] = 1 Jul  3 07:48:16 happy-Desktop kernel: [10874.385897] param[1] = 2 Jul  3 07:48:16 happy-Desktop kernel: [10874.385901] param[2] = 3 Jul  3 07:48:16 happy-Desktop kernel: [10874.385933] param[3] = 4 Jul  3 07:48:16 happy-Desktop kernel: [10874.385938] param[4] = 5 Jul  3 07:48:16 happy-Desktop kernel: [10874.385943] param[5] = 6 Jul  3 07:48:16 happy-Desktop kernel: [10874.385947] param[6] = 7 Jul  3 07:48:16 happy-Desktop kernel: [10874.385951] param[7] = 8 

Reference

  1. l​i​n​u​x​ ​模​块​层​叠​技​术​和​多​个​k​o​的​编​译

0 意見:

張貼留言