MODULE(9)
π NAME π
okay bestie, so module β it's the structure that describes a kernel module. that's it, that's the vibe. lowkey iconic tho.
β¨ DESCRIPTION β¨
so every single module in the kernel? yeah, it's described by a module_t structure. and that structure is carrying:
- the name of the device
- a unique ID number
- a pointer to an event handler function
- a pointer to an argument, which gets handed straight to that event handler
- plus some kernel internal data π«Ά
and the DECLARE_MODULE(9) macro is the thing that actually registers the module with the system. no macro, no registration, periodt.
π¦ the event handler callbacks, aka the whole plot π¦
when the module gets loaded, the event handler function gets called with the what argument set to MOD_LOAD. simple. clean. slay.
on unload it's a whole two-step thing tho:
- first it's called with
whatset toMOD_QUIESCE. and listen β if the unload was not forced, a non-zero return here will prevent the unload from happening. like, hard no. - if the unload continues,
whatis set toMOD_UNLOAD. and if the module returns non-zero to this one, the unload will not happen either. π
π
the tea on MOD_QUIESCE vs MOD_UNLOAD π
the difference is literally the whole point, so pay attention bestie:
- the module should fail
MOD_QUIESCEif it is currently in use. - but
MOD_UNLOADshould only fail if it is impossible to unload the module β for instance because there are memory references to the module which cannot be revoked.
so one is "hey not right now, i'm busy" and the other is "okay this literally cannot happen". different energies entirely. π
π shutdown era π
when the system is shutting down, what contains the value of MOD_SHUTDOWN.
and real talk β for unsupported and unrecognized values of what, the module should return EOPNOTSUPP. don't guess, don't improvise, just return it. no cap.
π EXAMPLES π
here's the whole thing, verbatim, because i would never mess with your code:
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/module.h>
static int foo_handler(module_t mod, int /*modeventtype_t*/ what,
void *arg);
static moduledata_t mod_data= {
"foo",
foo_handler,
NULL
};
MODULE_VERSION(foo, 1);
MODULE_DEPEND(foo, bar, 1, 3, 4);
DECLARE_MODULE(foo, mod_data, SI_SUB_EXEC, SI_ORDER_ANY);
π SEE ALSO π
go read these next, they're the friend group:
DECLARE_MODULE(9)DEV_MODULE(9)DRIVER_MODULE(9)MODULE_DEPEND(9)MODULE_PNP_INFO(9)MODULE_VERSION(9)SYSCALL_MODULE(9)
and also this directory, which is full of examples and honestly kinda fun:
/usr/share/examples/kld
πΈ AUTHORS πΈ
this manual page was written by Alexander Langer <alex@FreeBSD.org>. shoutout to them fr fr, they ate. π
and that's the page bestie!! this one's from the Debian crew, dated <b>February 12, 2018</b> β old but gold, still serving. go forth and write your modules, you've got this. πβ¨π¦
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 2000 Alexander Langer
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms