linux内核模块参数函数module_param测试

测试环境CPU架构:x86_64

操作系统版本:openEuler 22.03 LTS

内核版本:5.10

测试代码

// module_param_test.c

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>

MODULE_AUTHOR("HYQ");
MODULE_DESCRIPTION("module param test!");
MODULE_LICENSE("GPL");

static bool bool_param = true;
module_param(bool_param, bool, 0644);
MODULE_PARM_DESC(bool_param, "this is bool test!");

static u_char byte_param = 'a';
module_param(byte_param, byte, 0644);
MODULE_PARM_DESC(byte_param, "this is byte test!");

static uint hexint_param = 0xffffffff;
module_param(hexint_param, hexint, 0644);
MODULE_PARM_DESC(hexint_param, "this is hexint test!");

static short short_param = -15;
module_param(short_param, short, 0644);
MODULE_PARM_DESC(short_param, "this is short test!");

static unsigned short ushort_param = 15;
module_param(ushort_param, ushort, 0644);
MODULE_PARM_DESC(ushort_param, "this is ushort test!");

static int int_param = -150;
module_param(int_param, int, 0644);
MODULE_PARM_DESC(int_param, "this is int test!");

static unsigned int uint_param = 150;
module_param(uint_param, uint, 0644);
MODULE_PARM_DESC(uint_param, "this is uint test!");

static long long_param = -1500;
module_param(long_param, long, 0644);
MODULE_PARM_DESC(long_param, "this is long test!");

static unsigned long ulong_param = 1500;
module_param(ulong_param, ulong, 0644);
MODULE_PARM_DESC(ulong_param, "this is ulong test!");

static int __init module_param_test_init(void)
{
	printk(KERN_INFO "bool_param = %d\n", bool_param);
	printk(KERN_INFO "byte_param = %d\n", byte_param);
	printk(KERN_INFO "hexint_param = %d\n", hexint_param);
	printk(KERN_INFO "short_param = %d\n", short_param);
	printk(KERN_INFO "ushort_param = %d\n", ushort_param);
	printk(KERN_INFO "int_param = %d\n", int_param);
	printk(KERN_INFO "uint_param = %d\n", uint_param);
	printk(KERN_INFO "long_param = %ld\n", long_param);
 	printk(KERN_INFO "ulong_param = %ld\n", ulong_param);
	return 0;
}

static void __exit module_param_test_exit(void)
{
	printk(KERN_INFO "module exit!");
}

module_init(module_param_test_init);
module_exit(module_param_test_exit);

Makefile:

obj-m+=module_param_test.o
CONFIG_MODULE_SIG=n
all:
	make -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) modules
clean:
	make -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) clean

加载模块

 insmod module_param_test.ko

内核日志输出:

 查看内核模块信息

modinfo module_param_test.ko

卸载模块

rmmod module_param_test.ko

 加载内核模块时传入参数int_param

 从内核日志输出可以看到该参数的值已经被设置为1000,而不是原来默认的-150。如下图所示:

传递多个参数只要在加载模块后用"参数名=参数值"的方式添加即可,如下图所示:

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值