MODULE(9)

πŸ“¦
version 15.1 Β· 2 versions
, in freebsd 14.5, freebsd 15.1, nixpkgs unstable

✨ MODULE(9) ✨

okay bestie, this is the section 9 page about module β€” the struct that describes a kernel module. it's from the Kernel Developer's Manual, so grab your iced coffee and let's get into it 🫢


πŸ’… NAME πŸ’…

module β€” the structure that describes a kernel module ✨


🌸 DESCRIPTION 🌸

so every module in the kernel is described by a module_t structure. what's in it? the name of the device, a unique ID number, a pointer to an event handler function, and a pointer to an argument that gets handed to that event handler β€” plus some kernel internal data. ngl there's a lot going on in there.

and if the event handler function is NULL? no problem, the module just uses a no-operation function handler instead. easy. πŸ’…

the DECLARE_MODULE(9) macro is what registers the module with the system. that's the main character move. πŸ‘‘

when the module is loaded, the event handler function gets called with the what argument set to MOD_LOAD. okay so far so good.

on unload it's first called with what set to MOD_QUIESCE. here's the tea: if the unload wasn't forced, a non-zero return will prevent the unload from happening. so return non-zero if you're not ready to leave, periodt. βœ‹

if the unload continues, what is set to MOD_UNLOAD. and if the module returns non-zero to this one? the unload will not happen. simple as that.

now this is the part everyone gets confused about, so listen up πŸ“ β€” the difference between MOD_QUIESCE and MOD_UNLOAD:

  • the module should fail MOD_QUIESCE if it is currently in use
  • whereas MOD_UNLOAD should only fail if it is impossible to unload the module β€” for instance, because there are memory references to the module which cannot be revoked

like, in-use vs. literally-cannot-let-go. different vibes entirely. πŸ’€

when the system is shutting down, what contains the value of MOD_SHUTDOWN. yeah, that happens too.

and for unsupported and unrecognized values of what? the module should return EOPNOTSUPP. that's it, that's the answer. πŸŽ€


πŸ¦‹ EXAMPLES πŸ¦‹

here's the whole thing in action, and yes you can copy it exactly:

#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);

iconic. ate. no notes. πŸ”₯


🌈 SEE ALSO 🌈


πŸ’œ AUTHORS πŸ’œ

This manual page was written by Alexander Langer <alex@FreeBSD.org>.


that's the whole page bestie, go forth and load some modules πŸ’–πŸ¦‹βœ¨

β€” this one's from the Debian crew, dated November 11, 2021 (the version is right there in the footer, iykyk) πŸŽ€

πŸ“¦ where this page comes from