xref: /freebsd/share/man/man9/EVENTHANDLER.9 (revision 34aac98e3511d90df9209204df6772935a2b7617)
1235b396aSJoseph Koshy.\" Copyright (c) 2004 Joseph Koshy
2235b396aSJoseph Koshy.\" All rights reserved.
3235b396aSJoseph Koshy.\"
4235b396aSJoseph Koshy.\" Redistribution and use in source and binary forms, with or without
5235b396aSJoseph Koshy.\" modification, are permitted provided that the following conditions
6235b396aSJoseph Koshy.\" are met:
7235b396aSJoseph Koshy.\" 1. Redistributions of source code must retain the above copyright
8235b396aSJoseph Koshy.\"    notice, this list of conditions and the following disclaimer.
9235b396aSJoseph Koshy.\" 2. Redistributions in binary form must reproduce the above copyright
10235b396aSJoseph Koshy.\"    notice, this list of conditions and the following disclaimer in the
11235b396aSJoseph Koshy.\"    documentation and/or other materials provided with the distribution.
12235b396aSJoseph Koshy.\"
13235b396aSJoseph Koshy.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14235b396aSJoseph Koshy.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15235b396aSJoseph Koshy.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16235b396aSJoseph Koshy.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17235b396aSJoseph Koshy.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18235b396aSJoseph Koshy.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19235b396aSJoseph Koshy.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20235b396aSJoseph Koshy.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21235b396aSJoseph Koshy.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22235b396aSJoseph Koshy.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23235b396aSJoseph Koshy.\" SUCH DAMAGE.
24235b396aSJoseph Koshy.\" $FreeBSD$
25235b396aSJoseph Koshy.\"
26*34aac98eSMitchell Horne.Dd October 7, 2022
27235b396aSJoseph Koshy.Dt EVENTHANDLER 9
28235b396aSJoseph Koshy.Os
29235b396aSJoseph Koshy.Sh NAME
30235b396aSJoseph Koshy.Nm EVENTHANDLER
31235b396aSJoseph Koshy.Nd kernel event handling functions
32235b396aSJoseph Koshy.Sh SYNOPSIS
33235b396aSJoseph Koshy.In sys/eventhandler.h
34235b396aSJoseph Koshy.Fn EVENTHANDLER_DECLARE name type
35b5111c46SEdward Tomasz Napierala.Fn EVENTHANDLER_DEFINE name func arg priority
36235b396aSJoseph Koshy.Fn EVENTHANDLER_INVOKE name ...
37235b396aSJoseph Koshy.Ft eventhandler_tag
38235b396aSJoseph Koshy.Fn EVENTHANDLER_REGISTER name func arg priority
39235b396aSJoseph Koshy.Fn EVENTHANDLER_DEREGISTER name tag
40fc091646SIan Lepore.Fn EVENTHANDLER_DEREGISTER_NOWAIT name tag
412ca45184SMatt Joras.Fn EVENTHANDLER_LIST_DECLARE name
422ca45184SMatt Joras.Fn EVENTHANDLER_LIST_DEFINE name
432ca45184SMatt Joras.Fn EVENTHANDLER_DIRECT_INVOKE name
44235b396aSJoseph Koshy.Ft eventhandler_tag
45235b396aSJoseph Koshy.Fo eventhandler_register
46235b396aSJoseph Koshy.Fa "struct eventhandler_list *list"
479df1bed3SJoseph Koshy.Fa "const char *name"
48235b396aSJoseph Koshy.Fa "void *func"
49235b396aSJoseph Koshy.Fa "void *arg"
50235b396aSJoseph Koshy.Fa "int priority"
51235b396aSJoseph Koshy.Fc
52235b396aSJoseph Koshy.Ft void
53235b396aSJoseph Koshy.Fo eventhandler_deregister
54235b396aSJoseph Koshy.Fa "struct eventhandler_list *list"
55235b396aSJoseph Koshy.Fa "eventhandler_tag tag"
56235b396aSJoseph Koshy.Fc
57fc091646SIan Lepore.Ft void
58fc091646SIan Lepore.Fo eventhandler_deregister_nowait
59fc091646SIan Lepore.Fa "struct eventhandler_list *list"
60fc091646SIan Lepore.Fa "eventhandler_tag tag"
61fc091646SIan Lepore.Fc
62235b396aSJoseph Koshy.Ft "struct eventhandler_list *"
639df1bed3SJoseph Koshy.Fn eventhandler_find_list "const char *name"
64235b396aSJoseph Koshy.Ft void
65235b396aSJoseph Koshy.Fn eventhandler_prune_list "struct eventhandler_list *list"
66235b396aSJoseph Koshy.Sh DESCRIPTION
67235b396aSJoseph KoshyThe
68235b396aSJoseph Koshy.Nm
69235b396aSJoseph Koshymechanism provides a way for kernel subsystems to register interest in
70235b396aSJoseph Koshykernel events and have their callback functions invoked when these
71235b396aSJoseph Koshyevents occur.
72235b396aSJoseph Koshy.Pp
73235b396aSJoseph KoshyCallback functions are invoked in order of priority.
7448ef2433SGiorgos KeramidasThe relative priority of each callback among other callbacks
7548ef2433SGiorgos Keramidasassociated with an event is given by argument
76235b396aSJoseph Koshy.Fa priority ,
77235b396aSJoseph Koshywhich is an integer ranging from
78235b396aSJoseph Koshy.Dv EVENTHANDLER_PRI_FIRST
79235b396aSJoseph Koshy(highest priority), to
80235b396aSJoseph Koshy.Dv EVENTHANDLER_PRI_LAST
81235b396aSJoseph Koshy(lowest priority).
82235b396aSJoseph KoshyThe symbol
83235b396aSJoseph Koshy.Dv EVENTHANDLER_PRI_ANY
84235b396aSJoseph Koshymay be used if the handler does not have a specific priority
85235b396aSJoseph Koshyassociated with it.
86b5111c46SEdward Tomasz Napierala.Pp
87b5111c46SEdward Tomasz NapieralaThe normal way to use this subsystem is via the macro interface.
882ca45184SMatt JorasFor events that are high frequency it is suggested that you additionally use
89f62b48f9SMatt Joras.Fn EVENTHANDLER_LIST_DEFINE
902ca45184SMatt Jorasso that the event handlers can be invoked directly using
912ca45184SMatt Joras.Fn EVENTHANDLER_DIRECT_INVOKE
922ca45184SMatt Joras(see below).
932ca45184SMatt JorasThis saves the invoker from having to do a locked traversal of a global
942ca45184SMatt Joraslist of event handler lists.
95b5111c46SEdward Tomasz Napierala.Bl -tag -width indent
96b5111c46SEdward Tomasz Napierala.It Fn EVENTHANDLER_DECLARE
97b5111c46SEdward Tomasz NapieralaThis macro declares an event handler named by argument
98b5111c46SEdward Tomasz Napierala.Fa name
99b5111c46SEdward Tomasz Napieralawith callback functions of type
100b5111c46SEdward Tomasz Napierala.Fa type .
101b5111c46SEdward Tomasz Napierala.It Fn EVENTHANDLER_DEFINE
102b5111c46SEdward Tomasz NapieralaThis macro uses
103b5111c46SEdward Tomasz Napierala.Xr SYSINIT 9
104b5111c46SEdward Tomasz Napieralato register a callback function
105b5111c46SEdward Tomasz Napierala.Fa func
106b5111c46SEdward Tomasz Napieralawith event handler
107b5111c46SEdward Tomasz Napierala.Fa name .
108b5111c46SEdward Tomasz NapieralaWhen invoked, function
109b5111c46SEdward Tomasz Napierala.Fa func
110b5111c46SEdward Tomasz Napieralawill be invoked with argument
111b5111c46SEdward Tomasz Napierala.Fa arg
112b5111c46SEdward Tomasz Napieralaas its first parameter along with any additional parameters passed in
113b5111c46SEdward Tomasz Napieralavia macro
114b5111c46SEdward Tomasz Napierala.Fn EVENTHANDLER_INVOKE
115b5111c46SEdward Tomasz Napierala(see below).
116b5111c46SEdward Tomasz Napierala.It Fn EVENTHANDLER_REGISTER
117b5111c46SEdward Tomasz NapieralaThis macro registers a callback function
118b5111c46SEdward Tomasz Napierala.Fa func
119b5111c46SEdward Tomasz Napieralawith event handler
120b5111c46SEdward Tomasz Napierala.Fa name .
121b5111c46SEdward Tomasz NapieralaWhen invoked, function
122b5111c46SEdward Tomasz Napierala.Fa func
123b5111c46SEdward Tomasz Napieralawill be invoked with argument
124b5111c46SEdward Tomasz Napierala.Fa arg
125b5111c46SEdward Tomasz Napieralaas its first parameter along with any additional parameters passed in
126b5111c46SEdward Tomasz Napieralavia macro
127b5111c46SEdward Tomasz Napierala.Fn EVENTHANDLER_INVOKE
128b5111c46SEdward Tomasz Napierala(see below).
129235b396aSJoseph KoshyIf registration is successful,
130235b396aSJoseph Koshy.Fn EVENTHANDLER_REGISTER
131235b396aSJoseph Koshyreturns a cookie of type
132235b396aSJoseph Koshy.Vt eventhandler_tag .
13348ef2433SGiorgos Keramidas.It Fn EVENTHANDLER_DEREGISTER
13448ef2433SGiorgos KeramidasThis macro removes a previously registered callback associated with tag
135235b396aSJoseph Koshy.Fa tag
136235b396aSJoseph Koshyfrom the event handler named by argument
137235b396aSJoseph Koshy.Fa name .
138fc091646SIan LeporeIt waits until no threads are running handlers for this event before
139fc091646SIan Leporereturning, making it safe to unload a module immediately upon return
140fc091646SIan Leporefrom this function.
141fc091646SIan Lepore.It Fn EVENTHANDLER_DEREGISTER_NOWAIT
142fc091646SIan LeporeThis macro removes a previously registered callback associated with tag
143fc091646SIan Lepore.Fa tag
144fc091646SIan Leporefrom the event handler named by argument
145fc091646SIan Lepore.Fa name .
146fc091646SIan LeporeUpon return, one or more threads could still be running the removed
147fc091646SIan Leporefunction(s), but no new calls will be made.
148fc091646SIan LeporeTo remove a handler function from within that function, use this
149fc091646SIan Leporeversion of deregister, to avoid a deadlock.
15048ef2433SGiorgos Keramidas.It Fn EVENTHANDLER_INVOKE
15148ef2433SGiorgos KeramidasThis macro is used to invoke all the callbacks associated with event
15248ef2433SGiorgos Keramidashandler
153235b396aSJoseph Koshy.Fa name .
154235b396aSJoseph KoshyThis macro is a variadic one.
155235b396aSJoseph KoshyAdditional arguments to the macro after the
156235b396aSJoseph Koshy.Fa name
157235b396aSJoseph Koshyparameter are passed as the second and subsequent arguments to each
158235b396aSJoseph Koshyregistered callback function.
1592ca45184SMatt Joras.It Fn EVENTHANDLER_LIST_DEFINE
1602ca45184SMatt JorasThis macro defines a reference to an event handler list named by
1612ca45184SMatt Jorasargument
1622ca45184SMatt Joras.Fa name .
1632ca45184SMatt JorasIt uses
1642ca45184SMatt Joras.Xr SYSINIT 9
1652ca45184SMatt Jorasto initialize the reference and the eventhandler list.
1662ca45184SMatt Joras.It Fn EVENTHANDLER_LIST_DECLARE
1672ca45184SMatt JorasThis macro declares an event handler list named by argument
1682ca45184SMatt Joras.Fa name .
1692ca45184SMatt JorasThis is only needed for users of
1702ca45184SMatt Joras.Fn EVENTHANDLER_DIRECT_INVOKE
1712ca45184SMatt Joraswhich are not in the same compilation unit of that list's definition.
1722ca45184SMatt Joras.It Fn EVENTHANDLER_DIRECT_INVOKE
1732ca45184SMatt JorasThis macro invokes the event handlers registered for the list named by
1742ca45184SMatt Jorasargument
1752ca45184SMatt Joras.Fa name .
1762ca45184SMatt JorasThis macro can only be used if the list was defined with
1772ca45184SMatt Joras.Fn EVENTHANDLER_LIST_DEFINE .
1782ca45184SMatt JorasThe macro is variadic with the same semantics as
1792ca45184SMatt Joras.Fn EVENTHANDLER_INVOKE .
18048ef2433SGiorgos Keramidas.El
181235b396aSJoseph Koshy.Pp
182235b396aSJoseph KoshyThe macros are implemented using the following functions:
18348ef2433SGiorgos Keramidas.Bl -tag -width indent
18448ef2433SGiorgos Keramidas.It Fn eventhandler_register
18548ef2433SGiorgos KeramidasThe
186235b396aSJoseph Koshy.Fn eventhandler_register
18748ef2433SGiorgos Keramidasfunction is used to register a callback with a given event.
18848ef2433SGiorgos KeramidasThe arguments expected by this function are:
18948ef2433SGiorgos Keramidas.Bl -tag -width ".Fa priority"
19048ef2433SGiorgos Keramidas.It Fa list
19148ef2433SGiorgos KeramidasA pointer to an existing event handler list, or
192235b396aSJoseph Koshy.Dv NULL .
19348ef2433SGiorgos KeramidasIf
194235b396aSJoseph Koshy.Fa list
195235b396aSJoseph Koshyis
196235b396aSJoseph Koshy.Dv NULL ,
197235b396aSJoseph Koshythe event handler list corresponding to argument
198235b396aSJoseph Koshy.Fa name
199235b396aSJoseph Koshyis used.
20048ef2433SGiorgos Keramidas.It Fa name
20148ef2433SGiorgos KeramidasThe name of the event handler list.
20248ef2433SGiorgos Keramidas.It Fa func
20348ef2433SGiorgos KeramidasA pointer to a callback function.
204235b396aSJoseph KoshyArgument
205235b396aSJoseph Koshy.Fa arg
206d3d90204SGiorgos Keramidasis passed to the callback function
207235b396aSJoseph Koshy.Fa func
208235b396aSJoseph Koshyas its first argument when it is invoked.
20948ef2433SGiorgos Keramidas.It Fa priority
21048ef2433SGiorgos KeramidasThe relative priority of this callback among all the callbacks
21148ef2433SGiorgos Keramidasregistered for this event.
21248ef2433SGiorgos KeramidasValid values are those in the range
213235b396aSJoseph Koshy.Dv EVENTHANDLER_PRI_FIRST
214235b396aSJoseph Koshyto
21548ef2433SGiorgos Keramidas.Dv EVENTHANDLER_PRI_LAST .
21648ef2433SGiorgos Keramidas.El
217235b396aSJoseph Koshy.Pp
21848ef2433SGiorgos KeramidasThe
21948ef2433SGiorgos Keramidas.Fn eventhandler_register
22048ef2433SGiorgos Keramidasfunction returns a
22148ef2433SGiorgos Keramidas.Fa tag
22248ef2433SGiorgos Keramidasthat can later be used with
223235b396aSJoseph Koshy.Fn eventhandler_deregister
22448ef2433SGiorgos Keramidasto remove the particular callback function.
22548ef2433SGiorgos Keramidas.It Fn eventhandler_deregister
22648ef2433SGiorgos KeramidasThe
22748ef2433SGiorgos Keramidas.Fn eventhandler_deregister
22848ef2433SGiorgos Keramidasfunction removes the callback associated with tag
229235b396aSJoseph Koshy.Fa tag
230235b396aSJoseph Koshyfrom the event handler list pointed to by
231235b396aSJoseph Koshy.Fa list .
232fc091646SIan LeporeIf
233fc091646SIan Lepore.Fa tag
234fc091646SIan Leporeis
235fc091646SIan Lepore.Va NULL ,
236fc091646SIan Leporeall callback functions for the event are removed.
237fc091646SIan LeporeThis function will not return until all threads have exited from the
238fc091646SIan Leporeremoved handler callback function(s).
239fc091646SIan LeporeThis function is not safe to call from inside an event handler callback.
240fc091646SIan Lepore.It Fn eventhandler_deregister_nowait
241fc091646SIan LeporeThe
242fc091646SIan Lepore.Fn eventhandler_deregister
243fc091646SIan Leporefunction removes the callback associated with tag
244fc091646SIan Lepore.Fa tag
245fc091646SIan Leporefrom the event handler list pointed to by
246fc091646SIan Lepore.Fa list .
247235b396aSJoseph KoshyThis function is safe to call from inside an event handler
248235b396aSJoseph Koshycallback.
24948ef2433SGiorgos Keramidas.It Fn eventhandler_find_list
25048ef2433SGiorgos KeramidasThe
251235b396aSJoseph Koshy.Fn eventhandler_find_list
25248ef2433SGiorgos Keramidasfunction returns a pointer to event handler list structure corresponding
25348ef2433SGiorgos Keramidasto event
254235b396aSJoseph Koshy.Fa name .
25548ef2433SGiorgos Keramidas.It Fn eventhandler_prune_list
25648ef2433SGiorgos KeramidasThe
257235b396aSJoseph Koshy.Fn eventhandler_prune_list
25848ef2433SGiorgos Keramidasfunction removes all deregistered callbacks from the event list
259235b396aSJoseph Koshy.Fa list .
26048ef2433SGiorgos Keramidas.El
261235b396aSJoseph Koshy.Sh RETURN VALUES
262235b396aSJoseph KoshyThe macro
263235b396aSJoseph Koshy.Fn EVENTHANDLER_REGISTER
264235b396aSJoseph Koshyand function
265235b396aSJoseph Koshy.Fn eventhandler_register
266235b396aSJoseph Koshyreturn a cookie of type
267235b396aSJoseph Koshy.Vt eventhandler_tag ,
268235b396aSJoseph Koshywhich may be used in a subsequent call to
269235b396aSJoseph Koshy.Fn EVENTHANDLER_DEREGISTER
270235b396aSJoseph Koshyor
271235b396aSJoseph Koshy.Fn eventhandler_deregister .
272235b396aSJoseph Koshy.Pp
27348ef2433SGiorgos KeramidasThe
274235b396aSJoseph Koshy.Fn eventhandler_find_list
27548ef2433SGiorgos Keramidasfunction
276235b396aSJoseph Koshyreturns a pointer to an event handler list corresponding to parameter
277235b396aSJoseph Koshy.Fa name ,
278235b396aSJoseph Koshyor
279235b396aSJoseph Koshy.Dv NULL
280235b396aSJoseph Koshyif no such list was found.
281235b396aSJoseph Koshy.Sh HISTORY
282235b396aSJoseph KoshyThe
283235b396aSJoseph Koshy.Nm
284235b396aSJoseph Koshyfacility first appeared in
285235b396aSJoseph Koshy.Fx 4.0 .
286235b396aSJoseph Koshy.Sh AUTHORS
287235b396aSJoseph KoshyThis manual page was written by
2882ca45184SMatt Joras.An Joseph Koshy Aq Mt jkoshy@FreeBSD.org
2892ca45184SMatt Jorasand
2902ca45184SMatt Joras.An Matt Joras Aq Mt mjoras@FreeBSD.org .
291