Kernel Event Handlers

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

acpi_sleep_event

Registered functions are called when the system is sent to sleep.

acpi_wakeup_event

Registered functions are called when the system is woken up.

dev_clone

Registered functions are called when a solicited item under /dev does not exist; in other words, these functions create device nodes on demand.

ifaddr_event

Registered functions are called when an address is set up on a network interface.

if_clone_event

Registered functions are called when a network interface is cloned.

ifnet_arrival_event

Registered functions are called when a new network interface appears.

ifnet_departure_event

Registered functions are called when a network interface is taken down.

power_profile_change

Registered functions are called when the system’s power profile changes.

process_exec

Registered functions are called when a process issues an exec operation.

process_exit

Registered functions are called when a process exits.

process_fork

Registered functions are called when a process forks.

shutdown_pre_sync

Registered functions are called when the system is shut down before any filesystems are synchronized.

shutdown_post_sync

Registered functions are called when the system is shut down after every filesystem is synchronized.

shutdown_final

Registered functions are called before halting the system.

vm_lowmem

Registered functions are called when virtual memory is low.

watchdog_list

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).

Note

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.

Note

We’ll walk through an example that uses event handlers in Chapter 6.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.203.172