Event handlers allow drivers to register one or more functions to be called when an event occurs. As an example, before halting the system, every function that is registered with the event handler shutdown_final
is called. Table 5-1 describes every event handler that is available.
Table 5-1. Kernel Event Handlers
Event Handler | Description |
---|---|
| Registered functions are called when the system is sent to sleep. |
| Registered functions are called when the system is woken up. |
| Registered functions are called when a solicited item under /dev does not exist; in other words, these functions create device nodes on demand. |
Registered functions are called when an address is set up on a network interface. | |
| Registered functions are called when a network interface is cloned. |
| Registered functions are called when a new network interface appears. |
| Registered functions are called when a network interface is taken down. |
| Registered functions are called when the system’s power profile changes. |
| Registered functions are called when a process issues an |
| Registered functions are called when a process exits. |
| Registered functions are called when a process forks. |
| Registered functions are called when the system is shut down before any filesystems are synchronized. |
| Registered functions are called when the system is shut down after every filesystem is synchronized. |
| Registered functions are called before halting the system. |
| Registered functions are called when virtual memory is low. |
| Registered functions are called when the watchdog timer is reinitialized. |
The FreeBSD kernel provides the following three macros for working with event handlers:
#include <sys/eventhandler.h> eventhandler_tag EVENTHANDLER_REGISTER(name, func, arg, priority); EVENTHANDLER_DEREGISTER(name, tag); EVENTHANDLER_INVOKE(name, ...);
The EVENTHANDLER_REGISTER
macro registers the function func
with the event handler name
. If successful, an eventhandler_tag
is returned. When func
is called, arg
will be its first argument. Functions registered with name
are called in order of priority
. priority
can be 0 (which is the highest priority) to 20000
(which is the lowest priority).
Generally, I use the constant EVENTHANDLER_PRI_ANY
, which equals 10000
, for priority
.
The EVENTHANDLER_DEREGISTER
macro deletes the function associated with tag
from the event handler name
(where tag
is an eventhandler_tag
).
The EVENTHANDLER_INVOKE
macro executes every function registered with the event handler name
. Note that you’ll never call EVENTHANDLER_INVOKE
, because each event handler has threads dedicated to do just that.
We’ll walk through an example that uses event handlers in Chapter 6.
18.217.203.172