EVENTHANDLER(9)
π
EVENTHANDLER β kernel event handling functions (the 9 kernel-dev manual, bestie β¨)
okay so picture this: it's a FreeBSD kernel dev manual page, EVENTHANDLER(9), and it's literally the whole "let subsystems raise their hand when something happens" system. buckle up π
β¨ NAME β¨
EVENTHANDLER β kernel event handling functions. that's it, that's the vibe π¦
πΈ SYNOPSIS πΈ
#include <sys/eventhandler.h>
EVENTHANDLER_DECLARE(name, type);
EVENTHANDLER_DEFINE(name, func, arg, priority);
EVENTHANDLER_INVOKE(name, ...);
eventhandler_tag
EVENTHANDLER_REGISTER(name, func, arg, priority);
EVENTHANDLER_DEREGISTER(name, tag);
EVENTHANDLER_DEREGISTER_NOWAIT(name, tag);
EVENTHANDLER_LIST_DECLARE(name);
EVENTHANDLER_LIST_DEFINE(name);
EVENTHANDLER_DIRECT_INVOKE(name);
eventhandler_tag
eventhandler_register(struct eventhandler_list *list, const char *name,
void *func, void *arg, int priority);
void
eventhandler_deregister(struct eventhandler_list *list,
eventhandler_tag tag);
void
eventhandler_deregister_nowait(struct eventhandler_list *list,
eventhandler_tag tag);
struct eventhandler_list *
eventhandler_find_list(const char *name);
void
eventhandler_prune_list(struct eventhandler_list *list);
π¦ DESCRIPTION π¦
so the EVENTHANDLER mechanism is how kernel subsystems get to raise their lil hand πββοΈ and say "hey, I care about this kernel event" β and then their callback functions get invoked when that event actually happens. main character energy fr.
callbacks run in order of priority, no cap. the relative priority of each callback among the other callbacks on the same event comes from the argument priority, which is an integer going from EVENTHANDLER_PRI_FIRST (highest priority, queen bee π) down to EVENTHANDLER_PRI_LAST (lowest priority). and if a handler lowkey doesn't have a specific priority attached to it, you can use the symbol EVENTHANDLER_PRI_ANY. π
the normal way to use this subsystem is via the macro interface. and for events that are high frequency, it's suggested that you additionally use EVENTHANDLER_LIST_DEFINE() so the event handlers can be invoked directly with EVENTHANDLER_DIRECT_INVOKE() (peep below π). the payoff? it saves the invoker from having to do a locked traversal of a global list of event handler lists. yes, that's a lot of "lists" in one sentence, it's giving tongue twister π
π EVENTHANDLER_DECLARE()
this macro declares an event handler named by argument name, with callback functions of type type. π
π EVENTHANDLER_DEFINE()
this macro uses SYSINIT(9) to register a callback function func with event handler name. when it's invoked, function func gets invoked with argument arg as its first parameter, along with any additional parameters passed in via the macro EVENTHANDLER_INVOKE() (see below, bestie π«Ά).
π EVENTHANDLER_REGISTER()
this macro registers a callback function func with event handler name. when it's invoked, function func gets invoked with argument arg as its first parameter, plus any additional parameters passed in via the macro EVENTHANDLER_INVOKE() (below, again π). EVENTHANDLER_REGISTER() returns a cookie of type eventhandler_tag. a whole cookie. slay π
π EVENTHANDLER_DEREGISTER()
this macro removes a previously registered callback associated with tag tag from the event handler named by argument name. it waits until no threads are running handlers for this event before returning β which makes it safe to unload a module immediately upon return from this function. iconic safety behaviour β
π EVENTHANDLER_DEREGISTER_NOWAIT()
this macro removes a previously registered callback associated with tag tag from the event handler named by argument name. but heads up: upon return, one or more threads could still be running the removed function(s) β though no new calls will be made. π«§ and if you need to remove a handler function from within that very function, use this version of deregister, to avoid a deadlock. not me warning you about deadlocks π₯
π EVENTHANDLER_INVOKE()
this macro invokes all the callbacks associated with event handler name. and it's a variadic macro β additional arguments to the macro after the name parameter get passed as the second and subsequent arguments to each registered callback function. so many handoffs π
π EVENTHANDLER_LIST_DEFINE()
this macro defines a reference to an event handler list named by argument name. it uses SYSINIT(9) to initialize the reference and the eventhandler list. π§
π EVENTHANDLER_LIST_DECLARE()
this macro declares an event handler list named by argument name. it's only needed for users of EVENTHANDLER_DIRECT_INVOKE() which are not in the same compilation unit of that list's definition. iykyk π
π EVENTHANDLER_DIRECT_INVOKE()
this macro invokes the event handlers registered for the list named by argument name. it can only be used if the list was defined with EVENTHANDLER_LIST_DEFINE(). the macro is variadic with the same semantics as EVENTHANDLER_INVOKE(). consistency queen π
okay so all those macros are implemented using the following functions β here's the under-the-hood plumbing, bestie π§
π eventhandler_register()
eventhandler_register() is used to register a callback with a given event. the arguments it expects are:
- list β a pointer to an existing event handler list, or
NULL. if list isNULL, the event handler list corresponding to argument name is used. π« - name β the name of the event handler list. π·οΈ
- func β a pointer to a callback function. argument arg is passed to the callback function func as its first argument when it's invoked. π
- priority β the relative priority of this callback among all the callbacks registered for this event. valid values are those in the range
EVENTHANDLER_PRI_FIRSTtoEVENTHANDLER_PRI_LAST. π
eventhandler_register() returns a tag that can later be used with eventhandler_deregister() to remove that particular callback function. cute little receipt π§Ύ
π eventhandler_deregister()
eventhandler_deregister() removes the callback associated with tag tag from the event handler list pointed to by list. if tag is NULL, all callback functions for the event are removed. this function will not return until all threads have exited from the removed handler callback function(s). and β important β this function is not safe to call from inside an event handler callback. π seriously, don't.
π eventhandler_deregister_nowait()
ngl, the original page says "The eventhandler_deregister() function removesβ¦" in this section too β that's the manpage's own little copy-paste moment, and we're keeping it 100% as written π
so: it removes the callback associated with tag tag from the event handler list pointed to by list. unlike its sibling above, this function is safe to call from inside an event handler callback. bestie behaviour π«Ά
π eventhandler_find_list()
eventhandler_find_list() returns a pointer to the event handler list structure corresponding to event name. π
π eventhandler_prune_list()
eventhandler_prune_list() removes all deregistered callbacks from the event list list. spring cleaning era π§Ήβ¨
β RETURN VALUES β
the macro EVENTHANDLER_REGISTER() and the function eventhandler_register() return a cookie of type eventhandler_tag, which may be used in a subsequent call to EVENTHANDLER_DEREGISTER() or eventhandler_deregister(). πͺ
eventhandler_find_list() returns a pointer to an event handler list corresponding to parameter name, or NULL if no such list was found. no list = no pointer, periodt π
π HISTORY π
the EVENTHANDLER facility first appeared in FreeBSD 4.0. vintage, but make it iconic π
π AUTHORS π
this manual page was written by Joseph Koshy <jkoshy@FreeBSD.org> and Matt Joras <mjoras@FreeBSD.org>. thank you legends, genuinely π«Άπ
and that's the whole page, bestie β this one came from the Debian edition of the manual, dated January 31, 2025. go forth and handle some events, you absolute kernel main character πΈπ¦β¨
π¦ where this page comes from
- freebsd 15.1: base 15.1
- Copyright (c) 2004 Joseph Koshy
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms