Everytime I want to try something inside the kernel, I patch it’s code and recompile it whole, but this is not the nicest way when implementing new functionalities. The best way to do so is to implement it as a kernel module (LKM). The usual skeleton for a 2.6 kernel module is as follows:
hello.c:
#include <linux/kernel.h> // Needed for KERN_ALERT
#include <linux/init.h> // Needed for the macros
static int hello_init(void)
{
printk(KERN_ALERT “Hello, world \n”);
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT “Goodbye, world\n”);
}
module_init(hello_init);
module_exit(hello_exit);
Makefile:
obj-m := hello.o
else
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
endif
References:
Questions about loading modules.
Writing your own (2.6 kernel) device driver 101.
The Linux Kernel Module Programming Guide (for 2.4 Kernels).