Module_param () function learning in Linux

Source: Internet
Author: User

1. module_param
1. Why?
In user mode, you can use main () to pass command line parameters, while compile a kernel module to pass command line parameters through module_param.
2. The module_param macro is added to the Linux 2.6 kernel and is defined in the include/Linux/moduleparam. h file. The specific definition is as follows:
/* Helper functions: type is byte, short, ushort, Int, uint, long,


Ulong, CHARP, bool or invbool, or XXX If you define param_get_xxx,
Param_set_xxx and param_check_xxx.

 

*/
# Define module_param_named (name, value, type, Perm)
Param_check _ # type (name, & (value ));
Module_param_call (name, param_set _ # type, param_get _ # type, & Value, Perm );
_ Module_parm_type (name, # type)

# Define module_param (name, type, Perm)
Module_param_named (name, name, type, Perm)
We can see that module_param is implemented through module_param_named (name, name, type, Perm.
3. module_param uses three parameters: variable name, its type, and a permission mask for auxiliary sysfs entry.
This macro definition should be placed outside of any function, typically in front of the source file.
Eg: static char * Whom = "world"
Static int TIgE = 1;
Module_param (Tiger, Int, s_irugo );
Module_param (whom, CHARP, s_irugo );
4. The module parameters support many types:
Bool
Invbool
A boolean (true or false) value (the relevant variable should be of the int type). The invbool type reverses the value, so the true value is false, and vice versa.
CHARP: A character pointer value. The memory is allocated to strings provided by the user, so the pointer is set.
Int
Long
Short
Uint
Ulong
Ushort
Basic variable-length integer value. The value starting with u is an unsigned value.
5. array parameters. values provided by the list separated by commas (,) are also supported by the module loader.

Declare an array parameter, using:
Module_param_array (name, type, num, Perm );
Here name is the name of your array (also the parameter name ),
Type is the type of an array element,
Num is an integer variable,
Perm is a 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.

Tiger-John note:

What is the role of the perm parameter?
The final module_param field 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
For example:
# 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 s_irugo parameter 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.

Example 2:

After talking about this, let's look at a program to experience the following:

1. module_param.c

View plaincopy to clipboardprint?
/*
* File name: module_param.c
* Author: Tiger-John
*/
# Include <Linux/init. h>
# Include <Linux/module. h>
# Include <Linux/kernel. h>
Module_license ("GPL ");
Static char * WHO;
Static int times;
Module_param (WHO, CHARP, 0644 );
Module_param (times, Int, 0644 );
Static int _ init hello_init (void)
{
Int I;
For (I = 1; I <= times; I ++)
Printk ("% d % s! /N ", I, WHO );
Return 0;
}
Static void _ exit hello_exit (void)
{
Printk ("Goodbye, % s! /N ", WHO );
}
Module_init (hello_init );
Module_exit (hello_exit );
/*
* File name: module_param.c
* Author: Tiger-John
*/
# Include <Linux/init. h>
# Include <Linux/module. h>
# Include <Linux/kernel. h>
Module_license ("GPL ");
Static char * WHO;
Static int times;
Module_param (WHO, CHARP, 0644 );
Module_param (times, Int, 0644 );
Static int _ init hello_init (void)
{
Int I;
For (I = 1; I <= times; I ++)
Printk ("% d % s! /N ", I, WHO );
Return 0;
}
Static void _ exit hello_exit (void)
{
Printk ("Goodbye, % s! /N ", WHO );
}
Module_init (hello_init );
Module_exit (hello_exit );

2. Compile the MAKEFILE file

View plaincopy to clipboardprint?
1 obj-M: = module_param.o
2 current_path: = $ (shell PWD)
3 version_num: = $ (shell uname-R)
4 linux_path: =/usr/src/Linux-headers-$ (version_num)
5
6 All:
7 Make-C $ (linux_path) M = $ (current_path) Modules
8 clean:
9 make-C $ (linux_path) M = $ (current_path) clean
1 obj-M: = module_param.o
2 current_path: = $ (shell PWD)
3 version_num: = $ (shell uname-R)
4 linux_path: =/usr/src/Linux-headers-$ (version_num)
5
6 All:
7 Make-C $ (linux_path) M = $ (current_path) Modules
8 clean:
9 make-C $ (linux_path) M = $ (current_path) clean
 

3. Enter make on the terminal.

4. Loading module: sudo insmdo module_param.ko who = tiger times = 4

5. dmesg: view the result.

Process instance:

A. Enter make on the terminal.

Think @ Ubuntu :~ /Module_param $ make
Make-C/usr/src/linux-headers-2.6.32-25-generic M =/home/think/module_param modules
Make [1]: Entering directory '/usr/src/linux-headers-2.6.32-25-generic'
Building modules, stage 2.
Modpost 1 modules
Make [1]: Leaving directory '/usr/src/linux-headers-2.6.32-25-generic'
Think @ Ubuntu :~ /Module_param $

B. Enter sudo insmod module_param.ko who = tiger times = 4 on the terminal.

Think @ Ubuntu :~ /Module_param $ sudo insmod module_param.ko who = tiger times = 4

C input in the terminal: dmesg

[4297.711137] 1 tiger!
[4297.711139] 2 tiger!
[4297.711140] 3 tiger!
[4297.711141] 4 tiger!

 

 

From: http://www.linuxidc.com/Linux/2011-02/32131.htm

 

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.