本文主要是介绍内核ko模块传参-module_param()函数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.使用module_param()函数传递参数
hello.c
#include <linux/init.h> #include <linux/module.h> #include <linux/moduleparam.h>MODULE_LICENSE("GPL");static char *who="world"; static int times=1; //先声明变量(static型),然后调用module_param module_param(times,int,S_IRUGO); module_param(who,charp,S_IRUGO); static int hello_init(void) {int i;for(i=0;i<times;i++){printk(KERN_ALERT "(%d) hello ,%s \r\n",i,who);}return 0; }static void hello_exit(void){printk(KERN_ALERT "Goodbye,%s!\n",who);}module_init(hello_init);module_exit(hello_exit);
makefile
#General Purpose Makefile for cross compile Linux Kernel module ifneq ($(KERNELRELEASE),) obj-m := hello.o #+=是连接字符串 elseARCH := arm CROSS_COMPILE := arm-linux-gnueabihf- KERN_DIR := /home/zqh/lichee/linux-zero-4.14.y #选择内核路径 PWD :=$(shell pwd) #当前路径all:make ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) -C $(KERN_DIR) M=$(PWD) modules clean: make -C $(KERN_DIR) M=$(shell pwd) modules cleanrm -rf modules.order endif
2.现象
# insmod hello.ko who="mom" times=5 # dmesg | tail -10 [ 6.057104] RTL8723BS: set pairwise key camid:4, addr:e6:02:9b:c8:c8:41, kid:0, type:AES [ 6.068026] RTL8723BS: set group key camid:5, addr:e6:02:9b:c8:c8:41, kid:2, type:AES [ 129.211272] hello: loading out-of-tree module taints kernel. [ 129.217970] (0) hello ,world [ 151.453028] Goodbye,world! [ 383.426642] (0) hello ,mom [ 383.429541] (1) hello ,mom [ 383.432419] (2) hello ,mom [ 383.435295] (3) hello ,mom [ 383.438304] (4) hello ,mom
这篇关于内核ko模块传参-module_param()函数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!