- header files:
+ linux/module.h: for kernel module
+ linux/kernel.h: for printk/KERN_ALERT
+ linux/init.h: for macros
- functions and macros:
+ init_module(): for initializing/loading module
+ cleanup_module(): for removing module
+ module_init(), module_exit()
+ __initdata, __init, __exit
+ MODULE_LICENSE(), MODULE_DESCRIPTION(), MODULE_AUTHOR(), MODULE_SUPPORTED_DEVICE(): for licensing
+ MODULE_PARM(): for passing arguments
+ printk(), KERN_ALERT: print messages to console or log file
TARGET := hello
WARN := -W -Wall -Wstrict-prototypes -Wmissing-prototypes
INCLUDE := -isystem /lib/modules/`uname -r`/build/include
CFLAGS := -O2 -DMODULE -D__KERNEL__ ${WARN} ${INCLUDE}
CC := gcc
${TARGET}.o: ${TARGET}.c
.PHONY: clean
clean:
rm -rf {TARGET}.o
|
[user@hostname src]$ make gcc -O2 -DMODULE -D__KERNEL__ -W -Wall -Wstrict-prototypes -Wmissing-prototypes -isystem /lib/modules/`uname -r`/build/include -c -o hello.o hello.c hello.c:5: warning: no previous prototype for `init_module' hello.c:12: warning: no previous prototype for `cleanup_module' /lib/modules/2.4.26/build/include/asm/processor.h: In function `copy_segments': /lib/modules/2.4.26/build/include/asm/processor.h:433: warning: unused parameter `p' /lib/modules/2.4.26/build/include/asm/processor.h:433: warning: unused parameter `mm' /lib/modules/2.4.26/build/include/asm/processor.h: In function `release_segments': /lib/modules/2.4.26/build/include/asm/processor.h:434: warning: unused parameter `mm' /lib/modules/2.4.26/build/include/linux/prefetch.h: In function `prefetch': /lib/modules/2.4.26/build/include/linux/prefetch.h:43: warning: unused parameter `x' /lib/modules/2.4.26/build/include/linux/prefetch.h: In function `prefetchw': /lib/modules/2.4.26/build/include/linux/prefetch.h:48: warning: unused parameter `x' [user@hostname src]$
[user@hostname src]$ gcc -D__KERNEL__ -DMODULE -Wall -O2 -isystem /lib/modules/`uname -r`/build/include -c hello.c
- Switch to super user (root)
- [user@hostname src]$ insmod hello.o
Warning: loading hello.o will taint the kernel: no license
See http://www.tux.org/lkml/#export-tainted for information about tainted modules
Module hello loaded, with warnings
[user@hostname src]$ lsmod
Module Size Used by Tainted: P
hello 796 0 (unused)
soundcore 6404 0 (autoclean)
...
[user@hostname src]$ rmmod hello
- Also you can use commands like these (including /sbin/):
[user@hostname src]$ /sbin/insmod
[user@hostname src]$ /sbin/lsmod
[user@hostname src]$ /sbin/rmmod
[user@hostname src]$ tail /var/log/messages
...
Jan 25 02:49:57 {hostname} kernel: module inserted
Jan 25 02:52:14 {hostname} kernel: module deleted
[user@hostname src]$
Those "module inserted"/"module deleted" messages above are hard-coded in the source code with "printk" function