Linux内核代码,传参数

来源:互联网 发布:grub命令行启动ubuntu 编辑:程序博客网 时间:2024/06/05 05:24

一、源代码

1.1 temp_main.c

#include <linux/netfilter.h>#include <linux/netfilter_ipv4.h>#include <linux/module.h>#include <linux/moduleparam.h>#include <linux/kernel.h>#include <linux/inetdevice.h>#include <linux/string.h>#include <net/route.h>#include <linux/inet.h>#include <linux/netfilter.h>#include <linux/netfilter_ipv4.h>#include <linux/module.h>#include <linux/moduleparam.h>#include <linux/kernel.h>#include <linux/inetdevice.h>#include <linux/string.h>#include <net/route.h>#include <linux/inet.h>#include <linux/ip.h>#include <linux/tcp.h>#include <net/checksum.h>#include <net/tcp.h>#include <net/ip.h>#define LOG_INFO(fmt,args...) printk(KERN_INFO fmt, ##args) static int int_var = 0;static const char *str_var = "default";static int int_arr[6];int narr;module_param(int_var, int, 0644);MODULE_PARM_DESC(int_var, "A integer variable");module_param(str_var, charp, 0644);MODULE_PARM_DESC(str_var, "A string variable");module_param_array(int_arr, int, &narr, 0644);MODULE_PARM_DESC(int_arr, "A integer array");static int init_marker(void){    int i;    LOG_INFO("int_var: %d.\n", int_var);    LOG_INFO("str_var: %s.\n", str_var);    for (i=0; i<narr; ++i)    {        LOG_INFO("int_arr[%d]: %d.\n", i, int_arr[i]);    }    return 0;}static void exit_marker(void){}module_init(init_marker);module_exit(exit_marker);

1.2 Makefile

obj-m := temp.otemp-objs := temp_main.oKERNELDIR = /lib/modules/$(shell uname -r)/buildPWD := $(shell pwd)default:        $(MAKE) -C $(KERNELDIR) M=$(PWD) modulesclean:          $(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) cleaninstall:        cp temp.ko ../


二、运行

      insmod temp.ko int_var=123 str_var=helloworld int_arr=100,200,300


三、运行结果



参考资料:

      给内核模块传递参数:http://www.cnblogs.com/leaven/archive/2010/09/28/1837680.html

0 0
原创粉丝点击