Parameter passing in the Linux kernel module

Source: Internet
Author: User

 Module_param () comprehension
In user mode, you can pass command line parameters through main (), while compile a kernel module through module_param ()
The module_param () macro is added in the Linux 2.6 kernel and is defined in the include/Linux/moduleparam. h file. The specific definition is as follows:
# Define module_param (name, type, Perm)
Module_param_named (name, name, type, Perm)
Three parameters are used: the name of the parameter to be passed, the Data Type of the variable, and the permission to access the parameter.

What is the role of the perm parameter?
The perm parameter is a permission value, indicating the attributes of this parameter in the file node of the sysfs file system. You should use <Linux/STAT. h>. this value controls who can access the representation of these module parameters in sysfs. when the perm value is 0, this parameter does not exist in the file node of the sysfs file system. Otherwise, the directory named after this module name will appear under the/sys/module/directory, with the given permissions ..
Permission defined in include/Linux/STAT. h
# Deprecision 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

The parameter s_irugo can be read by everyone, but cannot be changed. s_irugo | s_iwusr allows root to change the parameter. note: If a parameter is modified by sysfs, the parameter value you see in your module also changes, but your module has no other notifications. you should not make module parameters writable unless you are ready to detect this change and respond accordingly.

Module_param () should be placed outside any function, typically before the source file. The definition is as follows:
Static char * Whom = "world ";
Static int howtasks = 1;
Module_param (howmany, Int, s_irugo );
Module_param (whom, CHARP, s_irugo );

The module parameters support many types:
Boolean (true or false) value (the relevant variable should be of the int type ).
The invbool type is reversed, so the true value is false, and vice versa.
CHARP is a character pointer value. The memory is allocated to strings provided by the user, so the pointer is set.

Array parameter, the value provided by the list separated by commas (,). The module loader also supports. Declare an array parameter, using:
Module_param_array (name, type, num, Perm );
Name of the name array (also the parameter name ),
The type of the type array element,
Num is an integer variable,
Perm's common permission value.
If the array parameter is set during loading, num is set to the number of provided numbers. The module loader rejects more values than the array can put down.

Hello. c
# Include <Linux/init. h>
# Include <Linux/module. h>
# Include <Linux/moduleparam. h>
Module_license ("dual BSD/GPL ");

Static char * Who = "world ";
Static int times = 1;
Module_param (times, Int, s_irusr );
Module_param (WHO, CHARP, s_irusr );

Static int hello_init (void)
Int I;
For (I = 0; I <times; I ++)
Printk (kern_alert "(% d) Hello, % s! \ N ", I, WHO );
Return 0;

Static void hello_exit (void)
Printk (kern_alert "Goodbye, % s! \ N ", WHO );

Module_init (hello_init );
Module_exit (hello_exit );

Compile and generate the executable file hello

# Insmod hello. Ko
WHO = "world" Times = 5
# (1) Hello, world!
# (2) Hello, world!
# (3) Hello, world!
# (4) Hello, world!
# (5) Hello, world!
# Rmmod hello
# Goodbye, world!

NOTE: If no parameter is input when the module hello is loadedThe initial value of who is "world" and the initial value of times is 1. When a string is passed to the pointer, such a string cannot be passed.

WHO = "Hello world! ". That is, the string cannot contain spaces.


Related Article

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: 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.