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

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.
主站蜘蛛池模板: 南川市| 古交市| 水富县| 肥城市| 桦甸市| 揭西县| 凌源市| 甘孜| 临沧市| 桂阳县| 永仁县| 镇康县| 宁德市| 惠东县| 鞍山市| 房产| 舞钢市| 商城县| 叙永县| 独山县| 茌平县| 山东省| 绥德县| 星座| 德兴市| 达尔| 兖州市| 仁怀市| 蕲春县| 开阳县| 三原县| 通城县| 全南县| 绥阳县| 华坪县| 遵义县| 林西县| 松滋市| 泸定县| 阿巴嘎旗| 泸西县|