您的位置:首页 > 运维架构 > Linux

linux内核模块编程三

2013-10-07 17:37 288 查看
一、内核模块参数解析

模块可以接受命令行参数,但不是像我们以前使用的argc/argv那样。为了向你的模块传递参数,需要声明一个全局变量来保存命令行参数,然后使用module_param()宏(/include/linux/moduleparam.h)使其生效。

module_param()宏有三个参数:变量的名字,在sysfs中相应的文件类型和权限。如果参数是整型数组或者字符串的时,可以使用module_param_array()宏

还可以使用MODULE_PARM_DESC()对参数进行描述

#define module_param(name, type, perm)

#define module_param_array(name, type, nump, perm)

#define MODULE_PARM_DESC(_parm, desc)

/*
*  hello-5.c - Demonstrates command line argument passing to a module.
*/
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/stat.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Peter Jay Salzman");

static short int myshort = 1;
static int myint = 420;
static long int mylong = 9999;
static char *mystring = "blah";
static int myintArray[2] = { -1, -1 };
static int arr_argc = 0;

/*
* module_param(foo, int, 0000)
* The first param is the parameters name
* The second param is it's data type
* The final argument is the permissions bits,
* for exposing parameters in sysfs (if non-zero) at a later stage.
*/

module_param(myshort, short, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
MODULE_PARM_DESC(myshort, "A short integer");
module_param(myint, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
MODULE_PARM_DESC(myint, "An integer");
module_param(mylong, long, S_IRUSR);
MODULE_PARM_DESC(mylong, "A long integer");
module_param(mystring, charp, 0000);
MODULE_PARM_DESC(mystring, "A character string");

/*
* module_param_array(name, type, num, perm);
* The first param is the parameter's (in this case the array's) name
* The second param is the data type of the elements of the array
* The third argument is a pointer to the variable that will store the number
* of elements of the array initialized by the user at module loading time
* The fourth argument is the permission bits
*/
module_param_array(myintArray, int, &arr_argc, 0000);
MODULE_PARM_DESC(myintArray, "An array of integers");

static int __init hello_5_init(void)
{
int i;
printk(KERN_INFO "Hello, world 5\n=============\n");
printk(KERN_INFO "myshort is a short integer: %hd\n", myshort);
printk(KERN_INFO "myint is an integer: %d\n", myint);
printk(KERN_INFO "mylong is a long integer: %ld\n", mylong);
printk(KERN_INFO "mystring is a string: %s\n", mystring);
for (i = 0; i < (sizeof myintArray / sizeof (int)); i++)
{
printk(KERN_INFO "myintArray[%d] = %d\n", i, myintArray[i]);
}
printk(KERN_INFO "got %d arguments for myintArray.\n", arr_argc);
return 0;
}

static void __exit hello_5_exit(void)
{
printk(KERN_INFO "Goodbye, world 5\n");
}

module_init(hello_5_init);
module_exit(hello_5_exit);


执行insmod加载模块

[root@localhost modules_programming]# insmod hello-5.ko mystring="wph" myshort=255 myintArray=-1,8

[root@localhost modules_programming]# rmmod hello_5

[root@localhost modules_programming]# dmesg | tail -10

[ 867.412255] Hello, world 5

=============

[ 867.412319] myshort is a short integer: 255

[ 867.412408] myint is an integer: 420

[ 867.412433] mylong is a long integer: 9999

[ 867.412456] mystring is a string: wph

[ 867.412481] myintArray[0] = -1

[ 867.412504] myintArray[1] = 8

[ 867.412527] got 2 arguments for myintArray.

[ 989.226025] Goodbye, world 5

二、模块代码分割成多个文件

在很多时候,我们的模块代码量会很大,如果都所有代码都放在一个文件中这样不仅不方面对代码review,对后期的维护也是非常不利的,所以我们要对代码进行分块整理,对不同功能的模块用单独一个文件来存放,这样也便于管理

/*
*  start.c - Illustration of multi filed modules
*/

#include <linux/kernel.h>	/* We're doing kernel work */
#include <linux/module.h>	/* Specifically, a module */

int init_module(void)
{
printk(KERN_INFO "Hello, world - this is the kernel speaking\n");
return 0;
}


/*
*  stop.c - Illustration of multi filed modules
*/

#include <linux/kernel.h>	/* We're doing kernel work */
#include <linux/module.h>	/* Specifically, a module  */

void cleanup_module()
{
printk(KERN_INFO "Short is the life of a kernel module\n");
}

Makefile文件

obj-m += hello-1.o
obj-m += hello-2.o
obj-m += hello-4.o
obj-m += hello-5.o
obj-m += startstop.o
startstop-objs := start.o stop.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean


[root@localhost modules_programming]# make

make -C /lib/modules/3.6.10-4.fc18.i686/build M=/myfile/modules_programming modules

make[1]: Entering directory `/usr/src/kernels/3.6.10-4.fc18.i686'

CC [M] /myfile/modules_programming/start.o

CC [M] /myfile/modules_programming/stop.o

LD [M] /myfile/modules_programming/startstop.o

Building modules, stage 2.

MODPOST 5 modules

CC /myfile/modules_programming/startstop.mod.o

LD [M] /myfile/modules_programming/startstop.ko

make[1]: Leaving directory `/usr/src/kernels/3.6.10-4.fc18.i686'

[root@localhost modules_programming]# ls

hello-1.c hello-2.mod.c hello-4.o modules.order startstop.o

hello-1.ko hello-2.mod.o hello-5.c Module.symvers stop.c

hello-1.mod.c hello-2.o hello-5.ko start.c stop.o

hello-1.mod.o hello-4.c hello-5.mod.c start.o

hello-1.o hello-4.ko hello-5.mod.o startstop.ko

hello-2.c hello-4.mod.c hello-5.o startstop.mod.c

hello-2.ko hello-4.mod.o Makefile startstop.mod.o
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: