官术网_书友最值得收藏!

How it works...

Once compiled as before, a new file, module_par.ko, should be ready to be loaded into our ESPRESSObin. However, before doing it, let's use the modinfo utility on it, as follows:

# modinfo module_par.ko 
filename: /root/module_par.ko
version: 0.1
description: Module with parameters
author: Rodolfo Giometti
license: GPL
srcversion: 21315B65C307ABE9769814F
depends:
name: module_par
vermagic: 4.18.0 SMP preempt mod_unload aarch64
parm: var:an integer value (int)
parm: str:a string value (charp)
parm: arr:an array of 8 values (array of int)
The modinfo command is also included in the kmod package as insmod.

As we can see in the last three lines (all prefixed by the parm: string), we have a list of module's parameters defined in the code by the module_param() and module_param_array() macros and described with MODULE_PARM_DESC().

Now, if we simply insert the module as before, we get default values, as shown in the following code block:

# insmod module_par.ko 
[ 6021.345064] module_par:module_par_init: loaded
[ 6021.347028] module_par:module_par_init: var = 0x3f
[ 6021.351810] module_par:module_par_init: str = "default string"
[ 6021.357904] module_par:module_par_init: arr = 0 0 0 0 0 0 0 0

But if we use the next command line, we force new values:

# insmod module_par.ko var=0x01 str=\"new value\" arr='1,2,3' 
[ 6074.175964] module_par:module_par_init: loaded
[ 6074.177915] module_par:module_par_init: var = 0x01
[ 6074.184932] module_par:module_par_init: str = "new value"
[ 6074.189765] module_par:module_par_init: arr = 1 2 3 0 0 0 0 0
Don't forget to remove the module_par module by using the rmmod module_par command before trying to reload it with new values!

As a final note, let me suggest taking a closer look at the following module parameter definition:

static int var = 0x3f;
module_param(var, int, S_IRUSR | S_IWUSR);
MODULE_PARM_DESC(var, "an integer value");

First, we have the declaration of the variable that represents the parameter, then we have the real module parameter definition (where we specify the type and the file access permissions), and then we have the description.

The modinfo command is able to display all of the preceding information, except the file access permissions, which refer to the file related to this parameter within the sysfs filesystem! In fact, if we take a look at the /sys/module/module_par/parameters/ directory, we get the following:

# ls -l /sys/module/module_par/parameters/
total 0
-rw------- 1 root root 4096 Feb 1 12:46 arr
-rw------- 1 root root 4096 Feb 1 12:46 str
-rw------- 1 root root 4096 Feb 1 12:46 var

Now, it should be clear what parameters S_IRUSR and S_IWUSR means; they allow the module user (that is, the root user) to write into these files and then read from them the corresponding parameters.

Defines S_IRUSR and related function are defined in the following file: linux/include/uapi/linux/stat.h.
主站蜘蛛池模板: 金华市| 东明县| 镇康县| 贡山| 丽江市| 沂水县| 梅河口市| 宣化县| 德阳市| 新河县| 永定县| 蓬安县| 安远县| 八宿县| 全椒县| 四川省| 江阴市| 增城市| 科技| 建宁县| 资源县| 华宁县| 新竹市| 东乡| 浦城县| 高青县| 万盛区| 隆安县| 五指山市| 蓝山县| 固镇县| 温州市| 嘉祥县| 多伦县| 墨竹工卡县| 法库县| 乐东| 富顺县| 龙门县| 邵东县| 铁岭县|