Today we’ll be discussing a basic c code, the famous hello.c, which will be loaded as a kernel module and discuss some of the basic aspects related to it.
MODULE_LICENSE (“GPL”); // included in module.h, tells about the license the code is having.
static int _init helloinit (void)
printk (KERN_ALERT “Hello, worldn”);
static void _exit helloexit (void)
printk (KERN_ALERT “Goodbyen”);
moduleinit (helloinit); // hello_init is the initialization function
moduleexit (helloexit); //hello_exit is the exit function
Now coming to the* _init* and *exit* terms used in the code. The _init in static int _init helloinit (void) specifies that the helloinit function is executed only at module load time. The module loader drops the initialization function for other uses once the module is loaded. The initialization function better be static since they are not meant to be visible outside the specific file ( not mandatory though). Similar explanation goes for _exit. This makes the exit function executed only at exit time.
The printk function, at a first glance, might look identical to printf of
How to compile the code and execute it. Let us assume that you named the source file above as hello.c. Use any suitable text editor to write a Makefile as below :
obj-m += hello.o
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
rm -f *~
I hope you have the kernel source in your system. If not you can get the kernel source from kernel.org. If you are using Fedora, you can do:
# yum install kernel-devel
To compile, in the current directory ( where there is hello.c and the Makefile) do :
Then change to super user by : $su
and do : # insmod ./hello.ko
If you do : # tail -f /var/log/messages (You can open this in a separate terminal to view the system logs)
then you will be able to see a line saying “Hello, world”. This is what your hello_init( ) was supposed to do at module load time. You can also see the module name “hello” among the list of loaded modules doing lsmod. Now you can unload the module do :
# rmmod hello
Now in /var/log/messages, you will see “Goodbye”. This is due to hello_exit( ).