Insmod kernel module parameter transfer

Source: Internet
Author: User

Linux Kernel provides a simple framework for passing parameters to modules. It allows the driver to declare parameters, and the user specifies the corresponding value for parameters during system startup or module loading. In the driver, the parameter usage is like a global variable.

Use macro module_param () to define a module parameter:

Module_param (name, type, Perm );

Name is both the parameter name you see and the variable that accepts the parameter in the module. Type indicates the Data Type of the parameter.

For example:

Byte, short, ushort, Int, uint, long, ulong, CHARP, bool, invbool;

Perm specifies the access permission for the corresponding file in sysfs. Access Permissions are managed in the same way as Linux Files love your access permissions, such as 0644, or use Macros in stat. H, such as s_irugo. 0 indicates that the corresponding items in sysfs are completely disabled.


These macros do not declare variables. Therefore, variables must be declared before using macros.

Usage:

Static unsigned int int_var = 0;

Module_param (int_var, uint, s_irugo );

 

These must be written at the beginning of the module source file. That is, int_var is global. The variable name in the module source file can also be different from that in the External Parameter Name.

Module_param_named () Definition.

Module_param_named (name, variable, type, Perm); where name is an externally visible parameter name, variable is the global variable name in the source file, while module_param is implemented through module_param_named, except that the name and variable are the same.

For example:

Static unsigned int max_test = 9;

Module_param_name (maximum_line_test, max_test, Int, 0 );

 

If the module parameter is a string, the module parameter is usually defined using the char type. The kernel copies the user-supplied string to the memory, and the corresponding variable points to this string.

For example:

Static char * Name;

Module_param (name, CHARP, 0 );

 

Another method is to use the macro module_param_string () to allow the kernel to directly copy the string to the character array in the program.

Module_param_string (name, String, Len, Perm );

Here, name is the external parameter name, string is the internal variable name, Len is the buffer size (which can be smaller than the buffer size, but it does not make sense ), perm indicates the access permission of sysfs (or if perm is zero, it indicates that the corresponding sysfs item is completely disabled ).

For example:

Static char species [buf_len];

Module_param_string (specifies, species, buf_len, 0 );

 

To pass multiple parameters, you can use the macro module_param_array.
Module_param_array (name, type, nump, Perm );

Here, name is both the parameter name of the external module and the variable name inside the program, type is the data type, and perm is the access permission of sysfs. The pointer nump points to an integer. Its value indicates how many parameters are stored in the array name. It is worth noting that the name array must be statically allocated.

For example:

Static int finsh [max_fish];

Static int nr_fish;
Module_param_array (fish, Int, & nr_fish, 0444 );
Final Transfer

The number of elements in the array is stored in nr_fish. The internal array name is different from the external parameter name through the macro module_param_array_named.

For example:

Module_param_array_named (name, array, type, nump, Perm );

The macro module_parm_desc () is used to describe the parameters:

Static unsigned short size = 1;

Module_param (size, ushort, 0644 );

Module_parm_desc (size, "The size in inches of the fishing pole connected to this computer .");

 

Test:

The following small module specifically operates on how to pass parameters, although very small, dirty.

It can also be tested on a PC and verified.

1 # include <Linux/module. h> 2 # include <Linux/moduleparam. h> 3 # include <Linux/kernel. h> 4 5 6 # define max_array 6 7 8 Static int_var = 0; 9 static char * str_var = "default"; 10 static int_array [6]; 11 int narr; 12 // The module transmits the variables and descriptions 13 module_param (int_var, Int, 0644); 14 module_parm_desc (int_var, "a integer variable "); 15 16 // The module transmits the variable and description 17 module_param (str_var, CHARP, 0644); 18 module_parm_desc (str_var, "a string variable "); 19 // description of the module Transfer Quantity combination 20 module_param_array (int_array, Int, & narr, 0644); 21 module_parm_desc (int_array, "a integer array "); 22 // when the array is passed, an additional parameter int type address 23 24 25 static int _ init hello_init (void) 26 {27 int I; 28 printk (kern_alert "hello, my lkm. \ n "); 29 printk (kern_alert" int_var % d. \ n ", int_var); 30 printk (kern_alert" str_var % S. \ n ", str_var); // This array parameter does not know how to use 31 for (I = 0; I <narr; I ++) {32 printk ("int_array [% d] = % d \ n", I, int_array [I]); 33} 34 return 0; 35} 36 37 static void _ exit hello_exit (void) 38 {39 printk (kern_alert "bye, my lkm. \ n "); 40} 41 module_init (hello_init); 42 module_exit (hello_exit); 43 module_license (" GPL "); 44 module_author (" Edward "); 45 module_deion ("this module is a example. ");
View code

 

Insmod hello. Ko int_var = 1 str_var = "Edward" int_array = 1, 2, 4, 5

To view the output content of printk, run the dmesg command.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.