FreeBSD Kernel Module Example - Device Driver:
This is a slightly modified version of the module presented at:
http://freebsd.active-venture.com/arch-handbook/driverbasics-kld.html
Modify /etc/syslog.conf to get a kernel log file in /var/log:
kern.* /var/log/kernel.log
Don't forget to restart syslogd with either:
# killall -HUP syslogd
or
# /etc/rc.d/syslogd restart
Compile the module with "# make" and insert the module with:
# kldload -v ./skeleton.ko
and check the kernel log:
# tail -f /var/log/kernel.log
Remove the module with:
# kldunload skeleton
You should see something like this in the kernel.log:
Dec 5 10:47:36 muon kernel: Skeleton KLD loaded.
Dec 5 10:49:16 muon kernel: Skeleton KLD unloaded.
Tested on FreeBSD 5.3.
Example "skeleton.c"
/*
The kld interface is used through the following privileged commands:
* kldload - loads a new kernel module
* kldunload - unloads a kernel module
* kldstat - lists the currently loaded modules
# kldload -v ./skeleton.ko
# kldunload skeleton
*/
#include <sys/types.h>
#include <sys/module.h>
#include <sys/systm.h> /* uprintf */
#include <sys/errno.h>
#include <sys/param.h> /* defines used in kernel.h */
#include <sys/kernel.h> /* types used in module initialization */
/*
* Load handler that deals with the loading and unloading of a KLD.
*/
static int
skel_loader(struct module *m, int what, void *arg)
{
int err = 0;
switch (what) {
case MOD_LOAD: /* kldload */
printf("Skeleton KLD loaded.\n");
break;
case MOD_UNLOAD:
printf("Skeleton KLD unloaded.\n");
break;
default:
err = EINVAL;
break;
}
return(err);
}
/* Declare this module to the rest of the kernel */
static moduledata_t skel_mod = {
"skel",
skel_loader,
NULL
};
DECLARE_MODULE(skeleton, skel_mod, SI_SUB_KLD, SI_ORDER_ANY);
Makefile
SRCS=skeleton.c
KMOD=skeleton
.include <bsd.kmod.mk>
Last-Modified: Sat, 04 Feb 2006 16:12:34 GMT