一、源代码
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.o
temp-objs := temp_main.o
KERNELDIR = /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
$(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
install:
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