Add a directory hello to the/proc file system and add a file to this directory world, the content of the file is Hello World

Source: Internet
Author: User

First, the topic

Write a kernel module, add a directory hello to the/proc file system, and add a file in this directory to the world, the content of the file is Hello World. Kernel Version Requirements 2.6.18

?

Second, the experimental environment

Physical host: Win7 64bit, i5 dual core, 8G memory

Virtual machines: Vmware Workstation 10.0.2

Virtual Host: CentOs-5.11, kernel 2.6.18

?

Third, the experimental ideas

struct proc_dir_entry? *create_proc_entry(constchar? ) *name,mode_t mode,struct proc_dir_entry? *parent);

Name: File name

Mode: File Permissions

Parent: The file is in the/proc file system in the parental directory pointer.

The return value is the Proc_dir_entry pointer that was created (or NULL, indicating an error occurred during create). The returned pointer is then initialized with the other parameters of the file entry, such as the function that should be called when reading and writing to the file.

?

Iv. Concrete Implementation Steps

1. Preparatory work

    • View Kernel version

Use the uname-a command to view the current kernel version:

?

    • The Write kernel module requires root privileges to be upgraded to the root user

?

2. Writing the HELLOWORLD.C file

Create a folder and then create a file helloworld.c

?

3. Writing makefile

Next, write the makefile file, which is located in the same folder as the HELLOWORLD.C

?

4. Run make

After writing the makefile file, save the exit and run the make command in the directory where the makefile file is located.

    • An error appears, as shown in
    • Enter the error prompt folder, you can see the red build file, red table build is a link

    • Further using Ls–l to view its file attributes, build actually points to/usr/src/kernels/2.6.18-398.e15-i686

?

    • Solution: Install the kernel module, which may be due to the installation of the system when the installation is not complete, (refer to http://www.cnblogs.com/fsjohnhuang/p/3903091.html)

    • After installation, still error, and then view the folder that the build points to

    • Found on my own computer, the corresponding folder name is as follows (this makes me feel very strange, because the beginning of the experiment, with the UNAME-A command to view the kernel version of the time, the child version number is 398, but in the system, the kernel corresponding folder name is 402) so change 402 to 398

?

    • And then it's ready to be compiled.

?

    • Make will generate some module-related files, such as

?

5. View the. ko file properties

Use the Modinfo command to view the properties of the. ko file generated by make

    • An error occurred indicating that the command could not be found
    • Solution: You can view the path in the. Bash_profile in the home directory. if: path= $PATH: $HOME/bin needs to be added as follows:

Path= $PATH: $HOME/bin:/sbin:/usr/bin:/usr/sbin

(ref.: http://www.jb51.net/LINUXjishu/32192.html) This is also the case when the system is loaded with a lite mode, which causes some commands not to be used

    • Then restart the virtual machine and run the Modinfo command again to view the module information

?

6. Loading the module

Next, load the module with Insmod.

?

7. View all Modules

Once the module has been loaded successfully, you can use Lsmod to list all the modules, as you can see the HelloWorld module

?

8. View Kernel log information

With DMESG | Tail log information in the output kernel

You can see that the last line has printed the text set in the code

?

?

9. Open Open/proc/hello/world

When you finally open/proc/hello/world, the/proc/hello/world content is output

At this point, the module loading has been completed, and the following starts unloading the module

?

10. Uninstalling the module

Use Rmmod to unload the module

?

11. View Current Module

Check the current module again to see that no HelloWorld has been

?

12. View printing Information

To view print information when uninstalling

Uninstall succeeded, end of experiment

?

V. Summary

???? through this experiment, we have a certain understanding of the module programming of Linux System. A Linux kernel module consists mainly of the following parts.

(1) Module loading function

When a kernel module is loaded through the Insmod or Modprobe command, the module's load function is automatically executed by the kernel, completing the relevant initialization of this module.

(2) Module unload function

When a module is unloaded via the Rmmod command, the Unload function of the module is automatically executed by the kernel, and the function opposite to the module unload function is completed.

(3) Module License Statement

The license (LICENSE) statement describes the license permissions of the kernel module, and if LICENSE is not declared, the module is loaded and receives a warning that the kernel is contaminated (kernel tainted).

In the field of Linux kernel modules, acceptable license include "GPL", "GPL v2", "GPL and additional rights", "Dual BSD/GPL", "Dual MPL/GPL" and "proprietary" (There is a controversy in academia and in the legal profession as to whether a module can adopt non-GPL license rights such as "proprietary").

In most cases, the kernel module should follow the GPL-compatible license. The most common Linux kernel module is the Module_license ("GPL v2") statement that declares the module with the GPL v2.

???? In the compilation, if you use the makefile way, you should pay special attention to the makefile file, where space is required to use the TAB key, otherwise there will be an error. At the same time, you need to pass the directory where the kernel source code is located as a parameter to the make command.

Add a directory hello to the/proc file system and add a file to this directory world, the content of the file is Hello 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.