主題:《Linux核心模組編程指南》(七)

來源:互聯網
上載者:User

主題:《Linux核心模組編程指南》(七)
發信人: kevintz()
整理人: kevintz(2000-06-24 00:41:53), 站內信件
《Linux核心模組編程指南》

《Linux Kernel Module Programming Guide》

作者:Ori Pomerantz 中譯者:譚志(lkmpg@21cn.com)

  

譯者註:

1、LKMPG是一本免費的書,英文版的發行和修改遵從GPL version 2的許可。為了
節省時間,我只翻譯了其中的大部分的大意,或者說這隻是我學習中的一些中文
筆記吧,不能算是嚴格上的翻譯,但我認為這已經足夠了。本文也允許免費發布
,但發布前請和我聯絡,但不要把本文用於商業目的。鑒於本人的水平,文章中
難免有錯誤,請大家不吝指正。

2、本文中的例子在Linux(kernel version 2.2.10)上調試通過。你用的Linux必
須支援核心模組的載入,如果不支援,請在編譯核心時選上核心模組的支援或升
級你的核心到一個支援核心模組的版本。

   

      

                         第七章  啟動參數

    在之前的許多例子,我們必須在核心模組裡硬性規定一些東西,例如產生/p
roc檔案的檔案名稱或主裝置號等。這違背了Unix和Linux的哲學--寫靈活的程式,
方便使用者定製。

    在程式或核心模組運作之前取得所需的參數是通過命令列參數。核心模組在
這種情況下,我們不會取得argc和argv,而是更好的方法。我們可以在核心模組
裡定義全域變數,insmod命令將會為我們填寫參數。

    在本章的例子,我們定義了兩個參數:str1和str2。我們要做的只是編譯內
核模組並用insmod str1=xxx str2=yyy命令運行。init_module被調用的時候,s
tr1和str2將分別指向"xxx"和"yyy"。

    警告:參數是沒有類型檢查的。如果xxx和yyy是數字,核心將用數字填充st
r1和str2,而不是字串"xxx"和"yyy"。實際應用中你要自己檢查。

(kevintz註:這似乎可以用MODULE_PARM來處理)

/* param.c 

 * 

 * Receive command line parameters at module installation

 */

/* Copyright (C) 1998-99 by Ori Pomerantz */

/* The necessary header files */

/* Standard in kernel modules */

#include <linux/kernel.h>   /* We're doing kernel work */

#include <linux/module.h>   /* Specifically, a module */

/* Deal with CONFIG_MODVERSIONS */

#if CONFIG_MODVERSIONS==1

#define MODVERSIONS

#include <linux/modversions.h>

#endif        

#include <stdio.h>  /* I need NULL */

/* In 2.2.3 /usr/include/linux/version.h includes a 

 * macro for this, but 2.0.35 doesn't - so I add it 

 * here if necessary. */

#ifndef KERNEL_VERSION

#define KERNEL_VERSION(a,b,c) ((a)*65536+(b)*256+(c))

#endif

/* Emmanuel Papirakis:

 *

 * Prameter names are now (2.2) handled in a macro.

 * The kernel doesn't resolve the symbol names

 * like it seems to have once did.

 *

 * To pass parameters to a module, you have to use a macro

 * defined in include/linux/modules.h (line 176).

 * The macro takes two parameters. The parameter's name and

 * it's type. The type is a letter in double quotes.

 * For example, "i" should be an integer and "s" should

 * be a string.

 */

char *str1, *str2;

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)

MODULE_PARM(str1, "s");

MODULE_PARM(str2, "s");

#endif

/* Initialize the module - show the parameters */

int init_module()

{

  if (str1 == NULL || str2 == NULL) {

    printk("Next time, do insmod param str1=<something>");

    printk("str2=<something>/n");

  } else

    printk("Strings:%s and %s/n", str1, str2);

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)

  printk("If you try to insmod this module twice,");

  printk("(without rmmod'ing/n");

  printk("it first), you might get the wrong"); 

  printk("error message:/n");

  printk("'symbol for parameters str1 not found'./n");

#endif

  return 0;

}

/* Cleanup */

void cleanup_module()

{

}  

本章的例子很簡單,相信大家自己都可以搞定:-)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.