Kernel_module_param

module_param 内核传参

用户态C语言中, 函数传参使用main(int argc, char ** argv), 而内核态传参数使用:

  • 在内核函数中使用 module_param指定模块参数。
  • 加载内核时传递参数给模块。


module_param 函数

$itop_kernel/include/linux/moduleparam.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* module_param - typesafe helper for a module/cmdline parameter
* @value: the variable to alter, and exposed parameter name.
* @type: the type of the parameter
* @perm: visibility in sysfs.
*
* @value becomes the module parameter, or (prefixed by KBUILD_MODNAME and a
* ".") the kernel commandline parameter. Note that - is changed to _, so
* the user can use "foo-bar=1" even for variable "foo_bar".
*
* @perm is 0 if the the variable is not to appear in sysfs, or 0444
* for world-readable, 0644 for root-writable, etc. Note that if it
* is writable, you may need to use kparam_block_sysfs_write() around
* accesses (esp. charp, which can be kfreed when it changes).
*
* The @type is simply pasted to refer to a param_ops_##type and a
* param_check_##type: for convenience many standard types are provided but
* you can create your own by defining those variables.
*
* 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)
/**
* name: 内核中参数的名称
* type: 数据类型, byte, short, int, long, ulong, bool, charp(字符指针)
* perm: 内核参数权限。 可以查看 include/linux/stat.h
**/

#define S_ISUID 0004000
#define S_ISGID 0002000
#define S_ISVTX 0001000

#define S_IRWXU 00700
#define S_IRUSR 00400
#define S_IWUSR 00200
#define S_IXUSR 00100

#define S_IRWXG 00070
#define S_IRGRP 00040
#define S_IWGRP 00020
#define S_IXGRP 00010

#define S_IRWXO 00007
#define S_IROTH 00004
#define S_IWOTH 00002
#define S_IXOTH 00001

#endif

#ifdef __KERNEL__
#define S_IRWXUGO (S_IRWXU|S_IRWXG|S_IRWXO)
#define S_IALLUGO (S_ISUID|S_ISGID|S_ISVTX|S_IRWXUGO)
#define S_IRUGO (S_IRUSR|S_IRGRP|S_IROTH)
#define S_IWUGO (S_IWUSR|S_IWGRP|S_IWOTH)
#define S_IXUGO (S_IXUSR|S_IXGRP|S_IXOTH)

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <linux/init.h>
#include <linux/module.h>
#include <linux/stat.h>
#include <linux/moduleparam.h>

static int age = 25;
static char *name = "Li Gang";

static int __init hello_init(void)
{
printk("age = %d, name = [ %s ]\n", age, name);
return 0;
}


static void __exit hello_exit(void)
{
printk("module: exit hello module\n");
}

module_init(hello_init);
module_exit(hello_exit);

module_param(age, int, 0754);
module_param(name, charp, 0754);


MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("rocky");

运行

1
2
3
4
5
$ sudo insmod module_param.ko age=100
Dec 22 00:11:53 ubuntu kernel: [54928.625291] age = 100, name = [ Li Gang ]

$ sudo insmod module_param.ko
Dec 22 00:12:19 ubuntu kernel: [54954.592554] age = 25, name = [ Li Gang ]

问题

module_param err

1
2
/home/Postgres/driver/module_param/module_param.c:24:1: note: in expansion of macro ‘module_param’
module_param(age, int, 0666);

最后发现是module_param声明中有关权限的问题,

内核态的所属用户为root 即赋予的 user权限 可以使 r w x 但是group other 的权限最多为 读r, x

不能是可写的.

欣赏此文? 求鼓励,求支持!