Chapter:Linux Loadable Kernel Module

From Juneday education
Jump to: navigation, search

Pre req

  • OS
  • Kernel
  • FOSS

Kernel

Linux tools and other useful stuff

  • dmesg
  • /proc
  • kernlog
  • lsmod, rmmod, insmod, modprobe
  • /sys

Device Driver

Linux Loadable Kernel Module

Don't mess things up

Kernel header files

Some settings

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Rikard and Henrik");
MODULE_DESCRIPTION("Them stupid idiots at Juneday");
MODULE_VERSION("0.1")

Module parameters

module_param(name, charp, S_IRUGO); MODULE_PARM_DESC

init

module_init

exit

module_exit

Hello example

hello.c

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
 
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Juneday");
MODULE_DESCRIPTION("Stupid LKM saying hello");
MODULE_VERSION("0.0.1");
 
static char *name = "Hello Cleveland";
module_param(name, charp, S_IRUGO); 
MODULE_PARM_DESC(name, "");
 
static int __init hello_init(void){
   printk(KERN_INFO "Juneday's LKM (%s) says hello\n", name);
   return 0;
}
 
static void __exit hello_exit(void){
   printk(KERN_INFO "Juneday's LKM (%s) says goodbye\n", name);
}
 
module_init(hello_init);
module_exit(hello_exit);

hello.c

obj-m+=hello.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

insmod:
	sudo insmod hello.ko

rmmod:
	sudo rmmod hello.ko

Links