Topic: Linux kernel module programming guide (7)

Source: Internet
Author: User

Topic: Linux kernel module programming guide (7)
Sender: kevintz ()
Prepared by: kevintz (00:41:53), internal letter
Linux Kernel module programming guide

Linux Kernel module programming guide

Translated by Ori pomerantz: tan Zhi (lkmpg@21cn.com)



Note:

1. lkmpg is a free book. The release and modification of the English version follows the GPL version 2 license. To
Saving time, I only translated most of the things, or this is just some Chinese in my learning.
Note-taking is not a strict translation, but I think it is enough. This article also allows free Publishing
But please contact me before release, but do not use this article for commercial purposes. In view of my own level
Errors are inevitable. Please correct them.

2. The example in this article is successfully debugged on Linux (kernel version 2.2.10. You must use Linux
The kernel module must be loaded. If not, select or upgrade the kernel module during kernel compilation.
Level your kernel to a version that supports the kernel module.



Chapter 7 startup parameters

In many previous examples, we have to specify something in the kernel module, such as generating/P
Name of the ROC file or the master device number. This violates the philosophy of UNIX and Linux-writing flexible programs,
Easy to customize.

The required parameter obtained before the program or kernel module operates is a command line parameter. The kernel module
In this case, we will not obtain argc and argv, but a better method. We can go to the kernel module
And the insmod command will fill in the parameters for us.

In this chapter, we define two parameters: str1 and str2. All we need to do is compile
Run the kernel module with the insmod str1 = xxx str2 = YYY command. When init_module is called
Tr1 and str2 point to "XXX" and "yyy" respectively ".

Warning: There is no type check for the parameter. If XXX and YYY are numbers, the kernel will fill the st with numbers
R1 and str2, instead of strings "XXX" and "yyy ". In actual application, you need to check it yourself.

(Kevintz Note: It seems that module_parm can be used for processing)

/* 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 <Linux/kernel. h>/* We're doing kernel work */

# Include <Linux/module. h>/* specifically, a module */

/* Deal with config_modversions */

# If config_modversions = 1

# Define modversions

# Include <Linux/modversions. h>

# Endif

# Include <stdio. h>/* I need null */

/* In 2.2.3/usr/include/Linux/version. h between des

* Macro for this, but 2.0.35 doesn't-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 doesn' t 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 parameter's name and

* It's type. The type is a letter in double quotes.

* For example, "I" shocould be an integer and "S" showould

* 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 = <something> ");

Printk ("str2 = <something>/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;

}

/* Cleanup */

Void cleanup_module ()

{

}

The example in this chapter is very simple. I believe everyone can handle it by themselves :-)

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.