カーネルパラメータとインタラクティブなインスタンス

2836 ワード

カーネルモジュールとパラメータをインタラクティブにする方法は、2.6カーネル(include/linux/moduleparam.h)で関数定義を行います.1、module_param(name, type, perm); nameは、ユーザが見たパラメータ名であり、モジュール内でパラメータを受け入れる変数でもある.typeはパラメータのデータ型を表し、byte、short、ushort、int、uint、long、ulong、charp、bool、invboolの1つである.permはsysfs内の対応するファイルへのアクセス権を指定します.アクセス権は、0777などのlinuxファイルアクセス権と同じか、include/linux/stat.hで定義したマクロを使用して設定します.2、module_param_named(name, value, type, perm); nameは外部可視パラメータ名、valueはソースファイル内部のグローバル変数名、すなわち外部nameから受信されたパラメータである.3、module_param_string(name, string, len, perm); nameは外部のパラメータ名であり、stringは内部の変数名である.lenはstringで命名されたbufferサイズ(bufferのサイズより小さくても意味がない).permはsysfsのアクセス権を表します(またはpermはゼロで、対応するsysfsアイテムを完全に閉じることを示します).     4、module_param_array(name, type, nump, perm); nameは外部モジュールのパラメータ名であり、プログラム内部の変数名でもある.typeはデータ型です.permはsysfsのアクセス権限です.ポインタnumpは、配列nameに格納されるパラメータの数を示す整数を指します.name配列は静的に割り当てられなければならないことに注意してください.もちろん、5、int param_などの他のパラメータインタラクティブ関数もあります.set_byte(const char *val, struct kernel_param *kp); 6、int param_get_byte(char *buffer, struct kernel_param *kp); 7、......
他の人の例を転載すると以下のようになります.
#include <linux/module.h>
#include <linux/moduleparam.h>    /* Optional, to include module_param() macros */
#include <linux/kernel.h>    /* Optional, to include prink() prototype */
#include <linux/init.h>        /* Optional, to include module_init() macros */
#include <linux/stat.h>        /* Optional, to include S_IRUSR ... */

static int myint = -99;
static char *mystring = "i'm hungry";

static int myintary[]= {1,2,3,4};
static char *mystrary[] = {"apple", "orange", "banana"};
static int nstrs = 3;

module_param(myint, int, S_IRUSR|S_IWUSR);
MODULE_PARM_DESC(myint, "A trial integer");

module_param(mystring, charp, 0);
module_param_array(myintary, int, NULL, 0444);
module_param_array(mystrary, charp, &nstrs, 0664);

static int __init hello_init(void)
{
    int i;

    printk(KERN_INFO "myint is %d
", myint); printk(KERN_INFO "mystring is %s
", mystring); printk(KERN_INFO "myintary are"); for(i = 0; i < sizeof(myintary)/sizeof(int); i++) printk(" %d", myintary[i]); printk("
"); printk(KERN_INFO "mystrary are"); for(i=0; i < nstrs; i++) printk(" %s", mystrary[i]); printk("
"); return 0; } static void __exit hello_exit(void) { printk(KERN_INFO "hello_exit
"); } MODULE_LICENSE("Dual BSD/GPL"); module_init(hello_init); module_exit(hello_exit);

#insmod param.ko myint=100 mystring="abc" myintary=-1,-2 mystrary="a","b"

    :
myint is 100
mystring is abc
myintary are -1 -2 3 4
mystrary are a b