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

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.
主站蜘蛛池模板: 八宿县| 顺义区| 东乡族自治县| 娱乐| 灵宝市| 永吉县| 永泰县| 利川市| 宁远县| 普格县| 富民县| 凉山| 香河县| 铁岭县| 辽宁省| 扎赉特旗| 崇义县| 蒙阴县| 咸丰县| 兴仁县| 图片| 永平县| 平利县| 库尔勒市| 清水县| 郯城县| 青阳县| 德兴市| 宣武区| 万山特区| 遂平县| 清原| 双峰县| 安图县| 易门县| 梁山县| 定安县| 阿克陶县| 应城市| 开远市| 石渠县|