The simplest kernel driver module of Linux2.6

Source: Internet
Author: User
Article title: The simplest Linux2.6 kernel driver module. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

/* Hello. c */

# Include /* Needed by all modules */

# Include /* Needed for KERN_ALERT */

# Include /* Needed for the module-macros */

Static int _ init hello_init (void) // Module entry function specified by module_init ()

{

Printk (KERN_ALERT "Hello, world! \ N ");

Return 0;

}

Static void _ exit hello_exit (void) // Module exit function specified by module_exit ()

{

Printk (KERN_ALERT "Goodbye, cruel world! \ N ");

}

Module_init (hello_init );

Module_exit (hello_exit );

MODULE_LICENSE ("Dual BSD/GPL"); // shocould always exist or you'll get a warning

MODULE_AUTHOR ("BENSON"); // optional

MODULE_DESCRIPTION ("STUDY_MODULE"); // optional

/* Makefile */

# Makefile 2.6

Obj-m + = hello. o

KDIR: =/lib/modules/$ (shell uname-r)/build

# PWD = $ (shell pwd)

All:

Make-C $ (KDIR) M = $ (PWD) modules

Clean:

Make-C $ (KDIR) M = $ (PWD) clean

 

Obj-m: = hello. o indicates that the hello. o module is generated after compilation.
$ (KDIR) specifies the path of the kernel source code. "M =" indicates that this is an external module, and M = $ (PWD) specifies the path of the module file.

Note: makefile predefines the $ (PWD) variable. you do not need to define it again here.

Run # make

Load Module

# Insmod hello. ko

# Lsmod outputs the kernel loaded module information. you can view the loaded hello module.

......

Module Size Used

Hello 5632 0

You can view the module loading information in the log.

# Vi/var/log/messages

......

Sep 27 13:25:21 localhost kernel: Hello, world!

Uninstall module

# Rmmod hello. ko

# Lsmod found that the hello module has been uninstalled

View log information

# Vi/var/log/messages ......

............

Sep 27 13:26:58 localhost kernel: Goodbye, cruel world!

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.