EVENTHANDLER(9)

πŸ’– EVENTHANDLER(9) πŸ’– β€” kernel event handling functions

okay so bestie, you found EVENTHANDLER(9), straight out of the Kernel Developer's Manual πŸŽ€ and honestly? this one is lowkey iconic. it's the whole "hey kernel, tell me when stuff happens and I'll run your lil callback" system. no cap. let's get into it πŸ¦‹


✨ NAME ✨

EVENTHANDLER β€” kernel event handling functions 🌸


✨ 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 basically the kernel's group chat for events πŸ’¬. kernel subsystems can register that they're interested in a kernel event, and then when that event actually goes down, their callback functions get invoked. it's giving "notify me when it's my turn" energy and we love that for them.

Callback functions are invoked in order of priority. πŸ”₯ The relative priority of each callback among the other callbacks hanging out with the same event is given by the argument priority, which is an integer ranging from EVENTHANDLER_PRI_FIRST (the highest priority, main character) down to EVENTHANDLER_PRI_LAST (the lowest priority, just happy to be here). And if a handler genuinely 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. For events that are high frequency, it is suggested that you additionally use EVENTHANDLER_LIST_DEFINE() so that the event handlers can be invoked directly using EVENTHANDLER_DIRECT_INVOKE() (see below πŸ‘‡). Why? Because this saves the invoker from having to do a locked traversal of a global list of event handler lists. that's a whole lot of locking and traversing and honestly? not the vibe. πŸ’…

🧚 The Macros 🧚

  • EVENTHANDLER_DECLARE() β€” This macro declares an event handler named by the argument name with callback functions of type type. simple. clean. slay.
  • EVENTHANDLER_DEFINE() β€” This macro uses SYSINIT(9) to register a callback function func with event handler name. When invoked, the function func will be invoked with the argument arg as its first parameter, along with any additional parameters passed in via the macro EVENTHANDLER_INVOKE() (see below πŸ‘‡).
  • EVENTHANDLER_REGISTER() β€” This macro registers a callback function func with event handler name. When invoked, the function func will be invoked with the argument arg as its first parameter, along with any additional parameters passed in via the macro EVENTHANDLER_INVOKE() (see below πŸ‘‡). If registration is successful, EVENTHANDLER_REGISTER() returns a cookie of type eventhandler_tag. πŸͺ yes, an actual cookie.
  • EVENTHANDLER_DEREGISTER() β€” This macro removes a previously registered callback associated with the tag tag from the event handler named by the 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. 🫢 so polite. so patient.
  • EVENTHANDLER_DEREGISTER_NOWAIT() β€” This macro removes a previously registered callback associated with the tag tag from the event handler named by the argument name. Upon return, one or more threads could still be running the removed function(s), but no new calls will be made. To remove a handler function from inside that function, use this version of deregister, to avoid a deadlock. πŸ’€ yeah. use this one. don't be brave.
  • EVENTHANDLER_INVOKE() β€” This macro is used to invoke all the callbacks associated with event handler name. This macro is a variadic one. Additional arguments to the macro after the name parameter are passed as the second and subsequent arguments to each registered callback function. ✨
  • EVENTHANDLER_LIST_DEFINE() β€” This macro defines a reference to an event handler list named by the 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 the argument name. This is 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 the argument name. This macro can only be used if the list was defined with EVENTHANDLER_LIST_DEFINE(). The macro is variadic with the same semantics as EVENTHANDLER_INVOKE(). πŸ”₯

πŸ› οΈ The Functions Behind the Curtain πŸ› οΈ

The macros are implemented using the following functions:

  • eventhandler_register() β€” The eventhandler_register() function is used to register a callback with a given event. The arguments expected by this function are:
    • list β€” A pointer to an existing event handler list, or NULL. If list is NULL, the event handler list corresponding to the argument name is used.
    • name β€” The name of the event handler list.
    • func β€” A pointer to a callback function. The argument arg is passed to the callback function func as its first argument when it is 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_FIRST to EVENTHANDLER_PRI_LAST.

The eventhandler_register() function returns a tag that can later be used with eventhandler_deregister() to remove the particular callback function. πŸŽ€

  • eventhandler_deregister() β€” The eventhandler_deregister() function removes the callback associated with the 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).
⚠️ This function is not safe to call from inside an event handler callback. periodt. don't do it. πŸ’…
  • eventhandler_deregister_nowait() β€” The eventhandler_deregister_nowait() function removes the callback associated with the tag tag from the event handler list pointed to by list. This function is safe to call from inside an event handler callback. πŸ™Œ
  • eventhandler_find_list() β€” The eventhandler_find_list() function returns a pointer to the event handler list structure corresponding to event name. πŸ”
  • eventhandler_prune_list() β€” The eventhandler_prune_list() function removes all deregistered callbacks from the event list list. bye bestie 🧹

πŸ‘‘ Kernel Event Handlers πŸ‘‘

The following event handlers are present in the kernel:

  • acpi_sleep_event β€” Callbacks invoked when the system is being sent to sleep. 😴
  • acpi_wakeup_event β€” Callbacks invoked when the system is being woken up. β˜€οΈ
  • app_coredump_start β€” Callbacks invoked at start of application core dump.
  • app_coredump_progress β€” Callbacks invoked during progress of application core dump.
  • app_coredump_finish β€” Callbacks invoked at finish of application core dump.
  • app_coredump_error β€” Callbacks invoked on error of application core dump. 😭
  • bpf_track β€” Callbacks invoked when a BPF listener attaches to/detaches from network interface.
  • cpufreq_levels_changed β€” Callback invoked when cpu frequency levels have changed.
  • cpufreq_post_change β€” Callback invoked after cpu frequency has changed.
  • cpufreq_pre_change β€” Callback invoked before cpu frequency has changed.
  • dcons_poll β€” Callback invoked to poll for dcons changes.
  • device_attach β€” Callback invoked after a device has attached. πŸ”Œ
  • device_detach β€” Callbacks invoked before and after a device has detached.
  • dev_clone β€” Callbacks invoked when a new entry is created under /dev. ✨
  • group_attach_event β€” Callback invoked when an interface has been added to an interface group.
  • group_change_event β€” Callback invoked when a change has been made to an interface group.
  • group_detach_event β€” Callback invoked when an interface has been removed from an interface group.
  • ifaddr_event β€” Callbacks invoked when an address is set up on a network interface.
  • ifaddr_event_ext β€” Callback invoked when an address has been added or removed from an interface.
  • if_clone_event β€” Callbacks invoked when an interface is cloned.
  • iflladdr_event β€” Callback invoked when an if link layer address event has happened.
  • ifnet_arrival_event β€” Callbacks invoked when a new network interface appears. 🌈
  • ifnet_departure_event β€” Callbacks invoked when a network interface is taken down.
  • ifnet_link_event β€” Callback invoked when an interface link event has happened.
  • kld_load β€” Callbacks invoked after a linker file has been loaded.
  • kld_unload β€” Callbacks invoked after a linker file has been successfully unloaded.
  • kld_unload_try β€” Callbacks invoked before a linker file is about to be unloaded. These callbacks may be used to return an error and prevent the unload from proceeding. πŸ›‘
  • lle_event β€” Callback invoked when a link layer event has happened.
  • nmbclusters_change β€” Callback invoked when the number of mbuf clusters has changed.
  • nmbufs_change β€” Callback invoked when the number of mbufs has changed.
  • maxsockets_change β€” Callback invoked when the maximum number of sockets has changed.
  • mountroot β€” Callback invoked when root has been mounted. 🏠
  • power_profile_change β€” Callbacks invoked when the power profile of the system changes.
  • power_resume β€” Callback invoked when the system has resumed.
  • power_suspend β€” Callback invoked just before the system is suspended.
  • process_ctor β€” Callback invoked when a process is created. 🐣
  • process_dtor β€” Callback invoked when a process is destroyed.
  • process_exec β€” Callbacks invoked when a process performs an exec() operation.
  • process_exit β€” Callbacks invoked when a process exits.
  • process_fini β€” Callback invoked when a process memory is destroyed. This is never called. πŸ’€ (yes really. it's a whole thing.)
  • process_fork β€” Callbacks that fire when a process forks a child. πŸ‘Ά
  • process_init β€” Callback that fires when a process gets initialized.
  • random_adaptor_attach β€” Callback that fires when a new random module has been loaded. 🎲
  • register_framebuffer β€” Callback that fires when a new frame buffer is registered.
  • route_redirect_event β€” Callback that fires when a route gets redirected to a new location. 🚦
  • shutdown_pre_sync β€” Callbacks that fire at shutdown time, before file systems are synchronized.
  • shutdown_post_sync β€” Callbacks that fire at shutdown time, after all file systems are synchronized.
  • shutdown_final β€” Callbacks that fire just before the system halts. bye bestie πŸ‘‹
  • tcp_offload_listen_start β€” Callback for TCP Offload to start listening for new connections.
  • tcp_offload_listen_stop β€” Callback for TCP Offload to stop listening for new connections.
  • thread_ctor β€” Callback that fires when a thread object is created.
  • thread_dtor β€” Callback that fires when a thread object is destroyed. πŸ’€
  • thread_init β€” Callback that fires when a thread object is initialized.
  • thread_fini β€” Callback that fires when a thread object is deinitialized.
  • usb_dev_configured β€” Callback that fires when a USB device is configured. πŸ”Œ
  • unregister_framebuffer β€” Callback that fires when a frame buffer is deregistered.
  • vfs_mounted β€” Callback that fires when a file system is mounted.
  • vfs_unmounted β€” Callback that fires when a file system is unmounted.
  • vlan_config β€” Callback that fires when the vlan configuration has changed.
  • vlan_unconfig β€” Callback that fires when a vlan is destroyed.
  • vm_lowmem β€” Callbacks that fire when virtual memory is low. 😰
  • watchdog_list β€” Callbacks that fire when the system watchdog timer is reinitialized. πŸ•

✨ RETURN VALUES ✨

So like, what do you actually get back from all this registering, bestie? Good question, love that for you. πŸ’…

The macro EVENTHANDLER_REGISTER() and the function eventhandler_register() both hand you back a cookie of type eventhandler_tag. Keep it! You'll need it later when you call EVENTHANDLER_DEREGISTER() or eventhandler_deregister() β€” that cookie is your ticket to unsubscribing. πŸͺ

And then there's eventhandler_find_list(), which returns a pointer to an event handler list matching the parameter name. But if no such list exists? It gives you NULL. Not found, no list, nothing to see here. πŸ™ˆ


🌸 HISTORY 🌸

The EVENTHANDLER facility first appeared in FreeBSD 4.0. an icon, a legend, the moment. πŸ‘‘


🫢 AUTHORS 🫢

This manual page was written by:

  • Joseph Koshy <jkoshy@FreeBSD.org>
  • Matt Joras <mjoras@FreeBSD.org>

And that's a wrap on the whole page, bestie!! This came from the Debian crew, dated October 21, 2018, and it lives in section EVENTHANDLER(9) of the manual. Go register something. Slay. πŸ’–βœ¨πŸŽ€
πŸ“¦ where this page comes from