Linux Kernel programming (startup parameters)

Source: Internet
Author: User
Linux Kernel programming (startup parameters)-General Linux technology-Linux programming and kernel information. The following is a detailed description. 6. startup parameters
In many previous examples, we have to forcibly write something into the kernel module, such as the/proc file name or the device master code, so that we can use ioctl's to process it. This statement violates the Unix and Linux principles: Write flexible programs that users can set freely.
Before a program or kernel module is started, some messages are notified through command line parameters. In the case of kernel modules, we don't have the argc and argv parameters, but there is something better. We can define global variables in the kernel module. insmod will assign values to us.
In this kernel module, we define two variables: str1 and str2. All you need to do is compile the kernel module and run str1 = xxx str2 = yyy. When init_module is called, str1 points to the string xxx, and str2 points to the string yyy.
No type check is available for these parameters in version 2.0. If the first character of str1 and str2 is a number, the kernel assigns these variables as integers instead of pointers to strings. In actual situations, you must check the type.
On the other hand, in version 2.2, you can use the macro MACRO_PARM to tell insmod that you need a parameter, its name and type. This solves the type problem and allows the kernel module to receive strings starting with numbers.
Ex param. c

/* Param. c
*
* Receive command line parameters at module installation
*/

/* Copyright (C) 1998-99 by Ori Pomerantz */





/* The necessary header files */

/* Standard in kernel modules */
# Include/* Were doing kernel work */
# Include/* Specifically, a module */

/* Deal with CONFIG_MODVERSIONS */
# If CONFIG_MODVERSIONS = 1
# Define MODVERSIONS
# Include
# Endif


# Include/* I need NULL */


/* In 2.2.3/usr/include/linux/version. h between des
* Macro for this, but 2.0.35 doesnt-so I add it
* Here if necessary .*/
# Ifndef KERNEL_VERSION
# Define KERNEL_VERSION (a, B, c) (a) x 65536 + (B) * 256 + (c ))
# Endif



/* Emmanuel Papirakis:
*
* Prameter names are now (2.2) handled in a macro.
* The kernel doesnt resolve the symbol names
* Like it seems to have once did.
*
* To pass parameters to a module, you have to use a macro
* Defined in include/linux/modules. h (line 176 ).
* The macro takes two parameters. The parameters name and
* Its type. The type is a letter in double quotes.
* For example, "" I "" shocould be an integer and "" s "" shocould
* Be 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-show the parameters */
Int init_module ()
{
If (str1 = NULL | str2 = NULL ){
Printk ("" Next time, do insmod param str1 = "");
Printk ("" str2 = "");
} Else
Printk ("" Strings: % s and % s "", str1, str2 );

# If LINUX_VERSION_CODE> = KERNEL_VERSION (2, 2, 0)
Printk ("" If you try to insmod this module twice ,"");
Printk ("" (without rmmoding "");
Printk ("" it first), you might get the wrong "");
Printk ("" error message :"");
Printk ("symbol for parameters str1 not found ."");
# Endif

Return 0;
}


/* Cleanup */
Void cleanup_module ()
{
}
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: 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.