Linux Kernel module programming-startup parameters
Source: Internet
Author: User
Linux Kernel module programming-startup parameters-general Linux technology-Linux programming and kernel information. The following is a detailed description. Startup parameters
In many examples above, we have to hard write something like a kernel module, such as a file name in/proc or the device's master device number, so we can use ioctl for it. This is in conflict with the flexible program spirit that can be customized by users who write Unix and Linux.
Before a program or kernel module can start to work, you can tell it something you need through command line parameters. In the case of kernel modules, we cannot get the replacement of argc and argv. We get better things. We can define global variables in the kernel module and insmod will fill them for us.
In this kernel module, we define two: str1 and str2. All you need to do is compile the kernel module and run it with insmod str1 = xxx str2 = yyy. When the init_module is called, str1 points to the string 'xxx', and str2 points to 'yyy '.
These parameters 2.0 are not checked for type in version 6.1. If the first character of str1 or str2 is a number, the kernel fills the variable with an integer instead of a string pointer. In actual situations, you need to check this.
On the other hand, in version 2.2, you use the macro MACRO_PARM to tell insmod that you expect a parameter, its name and type. This solves the type problem and allows the kernel module to receive strings starting with numbers.
Example: param. c
/* Param. c
*
* Receive command line parameters when the module is installed
*/
/* Copyright (C) 1998-99 by Ori Pomerantz */
/* Required header files */
/* Standard header file */
# Include/* kernel work */
# Include/* explicitly specify a module */
/* Process CONFIG_MODVERSIONS */
# If CONFIG_MODVERSIONS = 1
# Define MODVERSIONS
# Include
# Endif
# Include/* I need NULL */
/* Include this macro in version 2.2.3/usr/include/linux/version. h.
* Version 2.0.35 is not included.-therefore, this parameter must be added for later use */
# Ifndef KERNEL_VERSION
# Define KERNEL_VERSION (a, B, c) (a) x 65536 + (B) * 256 + (c ))
# Endif
/* Emmanuel Papirakis:
*
* Now (Version 2.2), the parameter name in the macro is now processed. The kernel cannot resolve the symbol name as if it had already been used.
*
* To transmit parameters to the module, you have to use a macro defined in include/linux/modules. h (row 176th.
* A macro requires two parameters. Parameter Name and its type. Type is a letter enclosed in double quotation marks. For example:
* "I" is an integer, and "s" is a string.
*/
Char * str1, * str2;
# If LINUX_VERSION_CODE> = KERNEL_VERSION (2, 2, 0)
MODULE_PARM (str1, "s ");
MODULE_PARM (str2, "s ");
# Endif
/* Initialize the module -- display parameters */
Int init_module ()
{
If (str1 = NULL | str2 = NULL ){
Printk ("Next time, do insmod param str1 = ");
Printk ("str2 = \ n ");
} Else
Printk ("Strings: % s and % s \ n", str1, str2 );
# If LINUX_VERSION_CODE> = KERNEL_VERSION (2, 2, 0)
Printk ("If you try to insmod this module twice ,");
Printk ("(without rmmod 'ing \ n ");
Printk ("it first), you might get the wrong ");
Printk ("error message: \ n ");
Printk ("'symbol for parameters str1 not found '. \ n ");
# Endif
Return 0;
}
/* Clear */
Void cleanup_module ()
{
}
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