内核模块参数,验证小例子:
/** *Copyright (c) 2013.TianYuan *All rights reserved. * *文件名称: Modparma.c *文件标识: 模块参数的验证 * *当前版本:1.0 *作者:wuyq * *取代版本:xxx *原作者:xxx *完成日期:2013-11-18 */ #include <linux/init.h> #include <linux/module.h> /*内核提供的机制:在用户空间修改内核模块中全局变量的值*/ /*安装模块的时候,可以指定参数的值:\ 如insmod modparam.ko mpshort=100 mpint=200 mpstring="nihao" mparray=300,400*/ /*ls /sys/module/modparam/parameters*/ /*cat mparray; 有权限的能够写echo 55 > mpshort;cat mpshort*/ MODULE_LICENSE("GPL"); /* 定义全局变量*/ static short mpshort = 1; static int mpint = 10; static char *mpstring = "hello"; static int mparray[2] = {100, 200}; /*模块参数的声明*/ module_param(mpshort ,short, S_IRWXU); module_param(mpint, int , S_IRUSR); module_param(mpstring, charp, 00400);/*char* */ module_param_array(mparray, int, NULL, 00400); static int __init modparam_init(void) { printk("mpshort = %d\n", mpshort); printk("mpint = %d\n", mpint); printk("mpstring = %s\n", mpstring); printk("mparray = %d, %d\n", mparray[0], mparray[1]); return 0; } static void __exit modparam_exit(void) { printk("mpshort = %d\n", mpshort); printk("mpint = %d\n", mpint); printk("mpstring = %s\n", mpstring); printk("mparray = %d, %d\n", mparray[0], mparray[1]); } module_init(modparam_init); module_exit(modparam_exit);
时间: 2024-09-12 07:35:24