xref: /freebsd/share/man/man9/EVENTHANDLER.9 (revision eda14cbc264d6969b02f2b1994cef11148e914f1)
1.\" Copyright (c) 2004 Joseph Koshy
2.\" All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\"
13.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23.\" SUCH DAMAGE.
24.\" $FreeBSD$
25.\"
26.Dd October 21, 2018
27.Dt EVENTHANDLER 9
28.Os
29.Sh NAME
30.Nm EVENTHANDLER
31.Nd kernel event handling functions
32.Sh SYNOPSIS
33.In sys/eventhandler.h
34.Fn EVENTHANDLER_DECLARE name type
35.Fn EVENTHANDLER_DEFINE name func arg priority
36.Fn EVENTHANDLER_INVOKE name ...
37.Ft eventhandler_tag
38.Fn EVENTHANDLER_REGISTER name func arg priority
39.Fn EVENTHANDLER_DEREGISTER name tag
40.Fn EVENTHANDLER_DEREGISTER_NOWAIT name tag
41.Fn EVENTHANDLER_LIST_DECLARE name
42.Fn EVENTHANDLER_LIST_DEFINE name
43.Fn EVENTHANDLER_DIRECT_INVOKE name
44.Ft eventhandler_tag
45.Fo eventhandler_register
46.Fa "struct eventhandler_list *list"
47.Fa "const char *name"
48.Fa "void *func"
49.Fa "void *arg"
50.Fa "int priority"
51.Fc
52.Ft void
53.Fo eventhandler_deregister
54.Fa "struct eventhandler_list *list"
55.Fa "eventhandler_tag tag"
56.Fc
57.Ft void
58.Fo eventhandler_deregister_nowait
59.Fa "struct eventhandler_list *list"
60.Fa "eventhandler_tag tag"
61.Fc
62.Ft "struct eventhandler_list *"
63.Fn eventhandler_find_list "const char *name"
64.Ft void
65.Fn eventhandler_prune_list "struct eventhandler_list *list"
66.Sh DESCRIPTION
67The
68.Nm
69mechanism provides a way for kernel subsystems to register interest in
70kernel events and have their callback functions invoked when these
71events occur.
72.Pp
73Callback functions are invoked in order of priority.
74The relative priority of each callback among other callbacks
75associated with an event is given by argument
76.Fa priority ,
77which is an integer ranging from
78.Dv EVENTHANDLER_PRI_FIRST
79(highest priority), to
80.Dv EVENTHANDLER_PRI_LAST
81(lowest priority).
82The symbol
83.Dv EVENTHANDLER_PRI_ANY
84may be used if the handler does not have a specific priority
85associated with it.
86.Pp
87The normal way to use this subsystem is via the macro interface.
88For events that are high frequency it is suggested that you additionally use
89.Fn EVENTHANDLER_LIST_DEFINE
90so that the event handlers can be invoked directly using
91.Fn EVENTHANDLER_DIRECT_INVOKE
92(see below).
93This saves the invoker from having to do a locked traversal of a global
94list of event handler lists.
95.Bl -tag -width indent
96.It Fn EVENTHANDLER_DECLARE
97This macro declares an event handler named by argument
98.Fa name
99with callback functions of type
100.Fa type .
101.It Fn EVENTHANDLER_DEFINE
102This macro uses
103.Xr SYSINIT 9
104to register a callback function
105.Fa func
106with event handler
107.Fa name .
108When invoked, function
109.Fa func
110will be invoked with argument
111.Fa arg
112as its first parameter along with any additional parameters passed in
113via macro
114.Fn EVENTHANDLER_INVOKE
115(see below).
116.It Fn EVENTHANDLER_REGISTER
117This macro registers a callback function
118.Fa func
119with event handler
120.Fa name .
121When invoked, function
122.Fa func
123will be invoked with argument
124.Fa arg
125as its first parameter along with any additional parameters passed in
126via macro
127.Fn EVENTHANDLER_INVOKE
128(see below).
129If registration is successful,
130.Fn EVENTHANDLER_REGISTER
131returns a cookie of type
132.Vt eventhandler_tag .
133.It Fn EVENTHANDLER_DEREGISTER
134This macro removes a previously registered callback associated with tag
135.Fa tag
136from the event handler named by argument
137.Fa name .
138It waits until no threads are running handlers for this event before
139returning, making it safe to unload a module immediately upon return
140from this function.
141.It Fn EVENTHANDLER_DEREGISTER_NOWAIT
142This macro removes a previously registered callback associated with tag
143.Fa tag
144from the event handler named by argument
145.Fa name .
146Upon return, one or more threads could still be running the removed
147function(s), but no new calls will be made.
148To remove a handler function from within that function, use this
149version of deregister, to avoid a deadlock.
150.It Fn EVENTHANDLER_INVOKE
151This macro is used to invoke all the callbacks associated with event
152handler
153.Fa name .
154This macro is a variadic one.
155Additional arguments to the macro after the
156.Fa name
157parameter are passed as the second and subsequent arguments to each
158registered callback function.
159.It Fn EVENTHANDLER_LIST_DEFINE
160This macro defines a reference to an event handler list named by
161argument
162.Fa name .
163It uses
164.Xr SYSINIT 9
165to initialize the reference and the eventhandler list.
166.It Fn EVENTHANDLER_LIST_DECLARE
167This macro declares an event handler list named by argument
168.Fa name .
169This is only needed for users of
170.Fn EVENTHANDLER_DIRECT_INVOKE
171which are not in the same compilation unit of that list's definition.
172.It Fn EVENTHANDLER_DIRECT_INVOKE
173This macro invokes the event handlers registered for the list named by
174argument
175.Fa name .
176This macro can only be used if the list was defined with
177.Fn EVENTHANDLER_LIST_DEFINE .
178The macro is variadic with the same semantics as
179.Fn EVENTHANDLER_INVOKE .
180.El
181.Pp
182The macros are implemented using the following functions:
183.Bl -tag -width indent
184.It Fn eventhandler_register
185The
186.Fn eventhandler_register
187function is used to register a callback with a given event.
188The arguments expected by this function are:
189.Bl -tag -width ".Fa priority"
190.It Fa list
191A pointer to an existing event handler list, or
192.Dv NULL .
193If
194.Fa list
195is
196.Dv NULL ,
197the event handler list corresponding to argument
198.Fa name
199is used.
200.It Fa name
201The name of the event handler list.
202.It Fa func
203A pointer to a callback function.
204Argument
205.Fa arg
206is passed to the callback function
207.Fa func
208as its first argument when it is invoked.
209.It Fa priority
210The relative priority of this callback among all the callbacks
211registered for this event.
212Valid values are those in the range
213.Dv EVENTHANDLER_PRI_FIRST
214to
215.Dv EVENTHANDLER_PRI_LAST .
216.El
217.Pp
218The
219.Fn eventhandler_register
220function returns a
221.Fa tag
222that can later be used with
223.Fn eventhandler_deregister
224to remove the particular callback function.
225.It Fn eventhandler_deregister
226The
227.Fn eventhandler_deregister
228function removes the callback associated with tag
229.Fa tag
230from the event handler list pointed to by
231.Fa list .
232If
233.Fa tag
234is
235.Va NULL ,
236all callback functions for the event are removed.
237This function will not return until all threads have exited from the
238removed handler callback function(s).
239This function is not safe to call from inside an event handler callback.
240.It Fn eventhandler_deregister_nowait
241The
242.Fn eventhandler_deregister
243function removes the callback associated with tag
244.Fa tag
245from the event handler list pointed to by
246.Fa list .
247This function is safe to call from inside an event handler
248callback.
249.It Fn eventhandler_find_list
250The
251.Fn eventhandler_find_list
252function returns a pointer to event handler list structure corresponding
253to event
254.Fa name .
255.It Fn eventhandler_prune_list
256The
257.Fn eventhandler_prune_list
258function removes all deregistered callbacks from the event list
259.Fa list .
260.El
261.Ss Kernel Event Handlers
262The following event handlers are present in the kernel:
263.Bl -tag -width indent
264.It Vt acpi_sleep_event
265Callbacks invoked when the system is being sent to sleep.
266.It Vt acpi_wakeup_event
267Callbacks invoked when the system is being woken up.
268.It Vt app_coredump_start
269Callbacks invoked at start of application core dump.
270.It Vt app_coredump_progress
271Callbacks invoked during progress of application core dump.
272.It Vt app_coredump_finish
273Callbacks invoked at finish of application core dump.
274.It Vt app_coredump_error
275Callbacks invoked on error of application core dump.
276.It Vt bpf_track
277Callbacks invoked when a BPF listener attaches to/detaches from network interface.
278.It Vt cpufreq_levels_changed
279Callback invoked when cpu frequency levels have changed.
280.It Vt cpufreq_post_change
281Callback invoked after cpu frequency has changed.
282.It Vt cpufreq_pre_change
283Callback invoked before cpu frequency has changed.
284.It Vt dcons_poll
285Callback invoked to poll for dcons changes.
286.It Vt device_attach
287Callback invoked after a device has attached.
288.It Vt device_detach
289Callbacks invoked before and after a device has detached.
290.It Vt dev_clone
291Callbacks invoked when a new entry is created under
292.Pa /dev .
293.It Vt group_attach_event
294Callback invoked when an interfance has been added to an interface group.
295.It Vt group_change_event
296Callback invoked when an change has been made to an interface group.
297.It Vt group_detach_event
298Callback invoked when an interfance has been removed from an interface group.
299.It Vt ifaddr_event
300Callbacks invoked when an address is set up on a network interface.
301.It Vt ifaddr_event_ext
302Callback invoked when an address has been added or removed from an interface.
303.It Vt if_clone_event
304Callbacks invoked when an interface is cloned.
305.It Vt iflladdr_event
306Callback invoked when an if link layer address event has happened.
307.It Vt ifnet_arrival_event
308Callbacks invoked when a new network interface appears.
309.It Vt ifnet_departure_event
310Callbacks invoked when a network interface is taken down.
311.It Vt ifnet_link_event
312Callback invoked when an interfance link event has happened.
313.It Vt kld_load
314Callbacks invoked after a linker file has been loaded.
315.It Vt kld_unload
316Callbacks invoked after a linker file has been successfully unloaded.
317.It Vt kld_unload_try
318Callbacks invoked before a linker file is about to be unloaded.
319These callbacks may be used to return an error and prevent the unload from
320proceeding.
321.It Vt lle_event
322Callback invoked when an link layer event has happened.
323.It Vt nmbclusters_change
324Callback invoked when the number of mbuf clusters has changed.
325.It Vt nmbufs_change
326Callback invoked when the number of mbufs has changed.
327.It Vt maxsockets_change
328Callback invoked when the maximum number of sockets has changed.
329.It Vt mountroot
330Callback invoked when root has been mounted.
331.It Vt power_profile_change
332Callbacks invoked when the power profile of the system changes.
333.It Vt power_resume
334Callback invoked when the system has resumed.
335.It Vt power_suspend
336Callback invoked just before the system is suspended.
337.It Vt process_ctor
338Callback invoked when a process is created.
339.It Vt process_dtor
340Callback invoked when a process is destroyed.
341.It Vt process_exec
342Callbacks invoked when a process performs an
343.Fn exec
344operation.
345.It Vt process_exit
346Callbacks invoked when a process exits.
347.It Vt process_fini
348Callback invoked when a process memory is destroyed.
349This is never called.
350.It Vt process_fork
351Callbacks invoked when a process forks a child.
352.It Vt process_init
353Callback invoked when a process is initialized.
354.It Vt random_adaptor_attach
355Callback invoked when a new random module has been loaded.
356.It Vt register_framebuffer
357Callback invoked when a new frame buffer is registered.
358.It Vt route_redirect_event
359Callback invoked when a route gets redirected to a new location.
360.It Vt shutdown_pre_sync
361Callbacks invoked at shutdown time, before file systems are synchronized.
362.It Vt shutdown_post_sync
363Callbacks invoked at shutdown time, after all file systems are synchronized.
364.It Vt shutdown_final
365Callbacks invoked just before halting the system.
366.It Vt tcp_offload_listen_start
367Callback invoked for TCP Offload to start listening for new connections.
368.It Vt tcp_offload_listen_stop
369Callback invoked ror TCP Offload to stop listening for new connections.
370.It Vt thread_ctor
371Callback invoked when a thread object is created.
372.It Vt thread_dtor
373Callback invoked when a thread object is destroyed.
374.It Vt thread_init
375Callback invoked when a thread object is initialized.
376.It Vt thread_fini
377Callback invoked when a thread object is deinitalized.
378.It Vt usb_dev_configured
379Callback invoked when a USB device is configured
380.It Vt unregister_framebuffer
381Callback invoked when a frame buffer is deregistered.
382.It Vt vfs_mounted
383Callback invoked when a file system is mounted.
384.It Vt vfs_unmounted
385Callback invoked when a file system is unmounted.
386.It Vt vlan_config
387Callback invoked when the vlan configuration has changed.
388.It Vt vlan_unconfig
389Callback invoked when a vlan is destroyed.
390.It Vt vm_lowmem
391Callbacks invoked when virtual memory is low.
392.It Vt watchdog_list
393Callbacks invoked when the system watchdog timer is reinitialized.
394.El
395.Sh RETURN VALUES
396The macro
397.Fn EVENTHANDLER_REGISTER
398and function
399.Fn eventhandler_register
400return a cookie of type
401.Vt eventhandler_tag ,
402which may be used in a subsequent call to
403.Fn EVENTHANDLER_DEREGISTER
404or
405.Fn eventhandler_deregister .
406.Pp
407The
408.Fn eventhandler_find_list
409function
410returns a pointer to an event handler list corresponding to parameter
411.Fa name ,
412or
413.Dv NULL
414if no such list was found.
415.Sh HISTORY
416The
417.Nm
418facility first appeared in
419.Fx 4.0 .
420.Sh AUTHORS
421This manual page was written by
422.An Joseph Koshy Aq Mt jkoshy@FreeBSD.org
423and
424.An Matt Joras Aq Mt mjoras@FreeBSD.org .
425