Tutorial Linux Kernel Module Tutorial: Hello World

Hexui Undetected CSGO Cheats PUBG Accounts

sucsucwonwon

Dank Tier Donator
Jul 14, 2019
1
218
0
Game Name
N/A
Anticheat
N/A
Coding Language
C
What you need
C Programming, Linux basics
Hello! This is my first post, and first tutorial.

This write-up will explain how to develop, compile and load your first Linux kernel module.

Step 0: Prerequisites

This tutorial assumes you are running Linux (duh.), and you have the Linux headers installed. (linux-headers for Arch)
You should also have make and gcc installed.
You should use C, not C++. I believe it's possible to do so, but highly advised against.

Step 1: Create the project files

The first thing you will want to when creating any sort of project is setup the project directory and add any necessary files.

We will need a Makefile, and a C file. Name the C file what you want your file kernel object to be named, in this case hello-world.c

Step 2: The Code (I'll explain it after this section)

Makefile:
obj-m += hello-world.o

all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
hello-world.c:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>

static int __init helloworld_init(void)
{
        printk(KERN_INFO "Hello, World!\n");

        return 0; // Non zero means init has failed, and the module can't be loaded
}

static void __exit helloworld_exit(void)
{
        printk(KERN_INFO "Goodbye, World!\n");
}

module_init(helloworld_init);
module_exit(helloworld_exit);
Step 3: Compiling

Simply run make from the project's base directory and if all went well you will have a kernel object(.ko) name hello-world.ko, this is the file we will load into the kernel's memory

Step 4: Loading and unloading

To load the module simply run: sudo insmod hello-world.ko
And nothing should happen.

To read what your kernel module has printed to the screen run: dmesg
You might have to scroll up a bit to see what your module has printed.

hw.png


To unload the module run: sudo rmmod hello-world

gw.png


Step 5: This is cool and all but... How does it work?

static int __init helloworld_init(void)
This is the module init function, equivalent to a normal programs main() function, __init tells the kernel to free this function from memory once it has loaded.

printk(KERN_INFO "Hello, World!\n");
This is equivalent to printf() or std::cout, although instead of printing to /dev/console it prints to /proc/kmsg.
KERN_INFO tells it to print at that level, there are many different levels each specifying a certain level of urgency. (See Wikipedia | printk)

static void __exit helloworld_exit(void)
This is the exit function called when we unload the module with rmmod, or when the system is shutdown.
__exit works very similarly to __init.

module_init(helloworld_init);
module_exit(helloworld_exit);

These lines just tell the kernel which function does what.

Further Reading

The Linux driver implementer’s API guide
 

Attachments

You can download 0 Attachments

Rake

I'm not your friend
Administrator
Jan 21, 2014
13,306
79,068
2,484
Very nice, thank you for sharing
 
Community Mods