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:

  1. first it's called with what set to MOD_QUIESCE. and listen β€” if the unload was not forced, a non-zero return here will prevent the unload from happening. like, hard no.
  2. 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 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_QUIESCE if it is currently in use.
  • but 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.

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:

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