给内核模块传参 并导出符号

 module_param(name, type, perm)这个宏用来给内核模块传参数,其中参数类型有byte, short, ushort, int, uint, long, ulong, charp, bool, invbool

* Standard types are:
 *	byte, short, ushort, int, uint, long, ulong
 *	charp: a character pointer
 *	bool: a bool, values 0/1, y/n, Y/N.
 *	invbool: the above, only sense-reversed (N = true).
 */
#define module_param(name, type, perm)				\
	module_param_named(name, name, type, perm)


#define module_param_named(name, value, type, perm)			   \
	param_check_##type(name, &(value));				   \
	module_param_cb(name, &param_ops_##type, &value, perm);		   \
	__MODULE_PARM_TYPE(name, #type)
//calculation.h

#ifndef _CALCULATION_H
#define _CALCULATION_H

//对导出符号的声明,不能加static修饰
extern int itype;
int addFunc(int a, int b);
int subFunc(int a, int b);

#endif
//calculation.c

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


static int __init cal_init(void)
{
    printk(KERN_ALERT "calculation init()\n");

    printk(KERN_ALERT "itype+1=%d, itype-1=%d\n", addFunc(itype, 1), subFunc(itype, 1));
    
    return 0;
}

static void __exit cal_exit(void)
{
    printk(KERN_ALERT "calculation exit()\n");
}

module_init(cal_init);
module_exit(cal_exit);
MODULE_LICENSE("GPL2");

 

 

//param.c

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

static int itype = 0;
module_param(itype, int, 0);

static bool btype = 0;
module_param(btype, bool, 0644);

static char ctype = 0;
module_param(ctype, byte, 0);

static char* stype = 0;
module_param(stype, charp, 0644);


static int addFunc(int a, int b)
{
    return a+b;
}

static int subFunc(int a, int b)
{
    return a-b;
}

EXPORT_SYMBOL(itype);
EXPORT_SYMBOL(addFunc);
EXPORT_SYMBOL(subFunc);


static int __init param_init(void)
{
    printk(KERN_ALERT "param_init(void)\n");
    printk(KERN_ALERT "itype=%d\n", itype);
    printk(KERN_ALERT "btype=%d\n", btype);
    printk(KERN_ALERT "ctype=%d\n", ctype);
    printk(KERN_ALERT "stype=%s\n", stype);

    return 0;
}

static void __exit param_exit(void)
{
    printk(KERN_ALERT "param_exit(void)\n");
}

module_init(param_init);
module_exit(param_exit);
MODULE_LICENSE("GPL2");

输出:加载模块时,并给定参数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值