"Linux and Security" Curriculum Practice II

Source: Internet
Author: User
Tags function prototype

Compile and build a new kernel the principle of practice

Linux modules are a collection of functions and data types that can be compiled as standalone programs. The module mechanism is provided because Linux itself is a single core. Single core because all content is integrated and efficient, but scalability and maintainability is relatively poor, the module mechanism can compensate for this flaw.

Linux modules can be loaded into the kernel space by static or dynamic methods, and static loading refers to loading during kernel boot, and dynamic loading means loading at any time during kernel running.

When a module is loaded into the kernel, it becomes part of the kernel code. When the module is loaded into the system, the system modifies the symbol table in the kernel, adding the resources and symbols provided by the newly loaded module to the kernel symbol table for inter-module communication.

Second, the practice process

(a) Simple module--Name

1. Writing Module code

#include <linux/init.h>#include<linux/module.h>Module_license ("Dual BSD/GPL");//Claim PermissionStatic Char*name="zhangxin"; Static intnum=20135301; Static int__init Name_init (void) {PRINTK (Kern_alert"Name:%s\n", name);//Output NamePRINTK (Kern_alert"Num:%d\n", num);//Output Number    return 0; } Static void__exit Name_exit (void) {PRINTK (Kern_info"Name Module exit\n");} Module_init (Name_init); Module_exit (Name_exit); Module_param (num,int, S_irugo);//can pass parameters to numModule_param (Name,charp,s_irugo);//parameter can be passed to name//information statements such as authorsModule_author ("zhangxin"); Module_version ("v1.0"); Module_description ("A Simple module for testing PRINTK and module params");

2. Compiling the module

Next write Makefile.

obj-m:=myname.ocurrent_path:=$ (shell pwd) Linux_kernel_path:=/usr/src/linux-headers-3.13 . 0-genericall:    -C $ (Linux_kernel_path) m=$ (current_path) Modulesclean:    -C $ (Linux_kernel_path) m=$ (current_path) Clean

3. Load Test Unload module

(ii) process

1. Writing Module code

Module constructor: The initialization function that is called when the kernel module is loaded by executing a insmod or modprobe instruction. The function prototype must be Module_init (), the function pointer inside the parentheses

Module destructor: The function called when executing the rmmod instruction unload module. Function prototype is module_exit ()

Module License Statement: The function prototype is Module_license (), which tells the kernel which license the program uses, otherwise it will prompt the module to contaminate the kernel when it is loaded. The GPL is generally written.

Header file Module.h, this file must be included;

Header file Kernel.h, containing commonly used kernel functions;

The header file Init.h contains macros _init and _exit, which allow the kernel to free up memory.

Write a simple code that is used to output process information to the kernel.

#include <linux/kernel.h>#include<linux/module.h>#include<linux/init.h>#include<linux/sched.h>Static structTask_struct *pcurrent;Static int__init Print_init (void) {PRINTK (Kern_info"Print Current Task info\n"); PRINTK ("pid\ttgid\tprio\tstate\n"); For_each_process (pcurrent) {PRINTK ("%d\t",pcurrent->pid); PRINTK ("%d\t",pcurrent->Tgid); PRINTK ("%d\t",pcurrent->prio); PRINTK ("%ld\n",pcurrent->State ); }    return 0;}Static void__exit Print_exit (void) {PRINTK (Kern_info"finished\n");} Module_init (Print_init); Module_exit (print_exit);

2. Compiling the module

Next write Makefile.

(Where the "Enter +tab" key is used during all to make)

obj-m:=proclist.ocurrent_path:=$ (shell pwd) Linux_kernel_path:=/usr/src/linux-headers-3.13 . 0-genericall:    -C $ (Linux_kernel_path) m=$ (current_path) Modulesclean:    -C $ (Linux_kernel_path) m=$ (current_path) Clean

The first line: Write your own. C filename + ". O".

The third line of Linux_kernel_path after you write your own kernel version corresponding to the kernel source package address.

Explain the Make command:

Make-c $ (Linux_kernel_path) indicates jump to the kernel source directory to read the makefile there

m=$ (Current_path) indicates that returning to the current directory continues to perform the current makefile.

This is done after make:

A lot of files are generated:

3. Loading module

sudo insmod Proclist.ko

Once you have entered your password. The module has been loaded at this time.

4. Test module

DMESG: Look at the kernel information

Third, the problems encountered

1. Kernel version of Linux

Uname–r

Number of digits

2. Kernel location

"Linux and Security" Curriculum Practice II

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.