It is a Dynamic Kernel Linker facility unique to FreeBSD >= 4.0 (similar to Linux's and NetBSD/OpenBSD's lkm). It allows system administrators to dynamically add and remove functionality from a running system. This allows device driver writers to load their new changes into a running kernel without constantly rebooting to test changes.
The KLD interface is used through the following commands:
- kldload - loads a new kernel module
- kldunload - (guess what..) unloads a kernel module
- kldstat - lists the currently loaded modules
Here is the skeleton layout of a kernel module:
/*
* KLD Skeleton
* Inspired by Andrew Reiter's Daemonnews article
*/
#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 */
uprintf("Skeleton KLD loaded.\n");
break;
case MOD_UNLOAD:
uprintf("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);
In order to compile a module from source, a Makefile is needed:
SRCS=skeleton.c
KMOD=skeleton
.include <bsd.kmod.mk>
Simply running make with this Makefile will create a file skeleton.ko that can be loaded into your system by typing:
# kldload -v ./skeleton.ko