Hello World Linux Module

Hello World Module

#include <linux/init.h>
#include <linux/module.h>
 
static int init_module(void)
{
    printk(KERN_ALERT "Hello, world!n");
    return 0;
}
 
static void clean_module()
{
    printk(KERN_ALERT "Goodbye, world!n");
}

Compile – Makefile

ifneq ($(KERNELRELEASE),)
	obj-m := hello.o
else
	KERNELDIR ?= /lib/modules/$(shell uname -r)/build
	PWD := $(shell pwd)
default:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
endif

Load module

insmod hello.ko

Unload module

remod hello

To view kernel messages printed from printk function execute the following command in a terminal window:

dmesg | tail

For more: Linux Kernel Module Programming

Categories: C, Drivers, Linux, Rootkits Tags: , , , ,
%d bloggers like this: