xref: /freebsd/share/man/man9/EVENTHANDLER.9 (revision b2c76c41be32f904179efed29c0ca04d53f3996c)
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.\"
25*34aac98eSMitchell Horne.Dd October 7, 2022
26235b396aSJoseph Koshy.Dt EVENTHANDLER 9
27235b396aSJoseph Koshy.Os
28235b396aSJoseph Koshy.Sh NAME
29235b396aSJoseph Koshy.Nm EVENTHANDLER
30235b396aSJoseph Koshy.Nd kernel event handling functions
31235b396aSJoseph Koshy.Sh SYNOPSIS
32235b396aSJoseph Koshy.In sys/eventhandler.h
33235b396aSJoseph Koshy.Fn EVENTHANDLER_DECLARE name type
34b5111c46SEdward Tomasz Napierala.Fn EVENTHANDLER_DEFINE name func arg priority
35235b396aSJoseph Koshy.Fn EVENTHANDLER_INVOKE name ...
36235b396aSJoseph Koshy.Ft eventhandler_tag
37235b396aSJoseph Koshy.Fn EVENTHANDLER_REGISTER name func arg priority
38235b396aSJoseph Koshy.Fn EVENTHANDLER_DEREGISTER name tag
39fc091646SIan Lepore.Fn EVENTHANDLER_DEREGISTER_NOWAIT name tag
402ca45184SMatt Joras.Fn EVENTHANDLER_LIST_DECLARE name
412ca45184SMatt Joras.Fn EVENTHANDLER_LIST_DEFINE name
422ca45184SMatt Joras.Fn EVENTHANDLER_DIRECT_INVOKE name
43235b396aSJoseph Koshy.Ft eventhandler_tag
44235b396aSJoseph Koshy.Fo eventhandler_register
45235b396aSJoseph Koshy.Fa "struct eventhandler_list *list"
469df1bed3SJoseph Koshy.Fa "const char *name"
47235b396aSJoseph Koshy.Fa "void *func"
48235b396aSJoseph Koshy.Fa "void *arg"
49235b396aSJoseph Koshy.Fa "int priority"
50235b396aSJoseph Koshy.Fc
51235b396aSJoseph Koshy.Ft void
52235b396aSJoseph Koshy.Fo eventhandler_deregister
53235b396aSJoseph Koshy.Fa "struct eventhandler_list *list"
54235b396aSJoseph Koshy.Fa "eventhandler_tag tag"
55235b396aSJoseph Koshy.Fc
56fc091646SIan Lepore.Ft void
57fc091646SIan Lepore.Fo eventhandler_deregister_nowait
58fc091646SIan Lepore.Fa "struct eventhandler_list *list"
59fc091646SIan Lepore.Fa "eventhandler_tag tag"
60fc091646SIan Lepore.Fc
61235b396aSJoseph Koshy.Ft "struct eventhandler_list *"
629df1bed3SJoseph Koshy.Fn eventhandler_find_list "const char *name"
63235b396aSJoseph Koshy.Ft void
64235b396aSJoseph Koshy.Fn eventhandler_prune_list "struct eventhandler_list *list"
65235b396aSJoseph Koshy.Sh DESCRIPTION
66235b396aSJoseph KoshyThe
67235b396aSJoseph Koshy.Nm
68235b396aSJoseph Koshymechanism provides a way for kernel subsystems to register interest in
69235b396aSJoseph Koshykernel events and have their callback functions invoked when these
70235b396aSJoseph Koshyevents occur.
71235b396aSJoseph Koshy.Pp
72235b396aSJoseph KoshyCallback functions are invoked in order of priority.
7348ef2433SGiorgos KeramidasThe relative priority of each callback among other callbacks
7448ef2433SGiorgos Keramidasassociated with an event is given by argument
75235b396aSJoseph Koshy.Fa priority ,
76235b396aSJoseph Koshywhich is an integer ranging from
77235b396aSJoseph Koshy.Dv EVENTHANDLER_PRI_FIRST
78235b396aSJoseph Koshy(highest priority), to
79235b396aSJoseph Koshy.Dv EVENTHANDLER_PRI_LAST
80235b396aSJoseph Koshy(lowest priority).
81235b396aSJoseph KoshyThe symbol
82235b396aSJoseph Koshy.Dv EVENTHANDLER_PRI_ANY
83235b396aSJoseph Koshymay be used if the handler does not have a specific priority
84235b396aSJoseph Koshyassociated with it.
85b5111c46SEdward Tomasz Napierala.Pp
86b5111c46SEdward Tomasz NapieralaThe normal way to use this subsystem is via the macro interface.
872ca45184SMatt JorasFor events that are high frequency it is suggested that you additionally use
88f62b48f9SMatt Joras.Fn EVENTHANDLER_LIST_DEFINE
892ca45184SMatt Jorasso that the event handlers can be invoked directly using
902ca45184SMatt Joras.Fn EVENTHANDLER_DIRECT_INVOKE
912ca45184SMatt Joras(see below).
922ca45184SMatt JorasThis saves the invoker from having to do a locked traversal of a global
932ca45184SMatt Joraslist of event handler lists.
94b5111c46SEdward Tomasz Napierala.Bl -tag -width indent
95b5111c46SEdward Tomasz Napierala.It Fn EVENTHANDLER_DECLARE
96b5111c46SEdward Tomasz NapieralaThis macro declares an event handler named by argument
97b5111c46SEdward Tomasz Napierala.Fa name
98b5111c46SEdward Tomasz Napieralawith callback functions of type
99b5111c46SEdward Tomasz Napierala.Fa type .
100b5111c46SEdward Tomasz Napierala.It Fn EVENTHANDLER_DEFINE
101b5111c46SEdward Tomasz NapieralaThis macro uses
102b5111c46SEdward Tomasz Napierala.Xr SYSINIT 9
103b5111c46SEdward Tomasz Napieralato register a callback function
104b5111c46SEdward Tomasz Napierala.Fa func
105b5111c46SEdward Tomasz Napieralawith event handler
106b5111c46SEdward Tomasz Napierala.Fa name .
107b5111c46SEdward Tomasz NapieralaWhen invoked, function
108b5111c46SEdward Tomasz Napierala.Fa func
109b5111c46SEdward Tomasz Napieralawill be invoked with argument
110b5111c46SEdward Tomasz Napierala.Fa arg
111b5111c46SEdward Tomasz Napieralaas its first parameter along with any additional parameters passed in
112b5111c46SEdward Tomasz Napieralavia macro
113b5111c46SEdward Tomasz Napierala.Fn EVENTHANDLER_INVOKE
114b5111c46SEdward Tomasz Napierala(see below).
115b5111c46SEdward Tomasz Napierala.It Fn EVENTHANDLER_REGISTER
116b5111c46SEdward Tomasz NapieralaThis macro registers a callback function
117b5111c46SEdward Tomasz Napierala.Fa func
118b5111c46SEdward Tomasz Napieralawith event handler
119b5111c46SEdward Tomasz Napierala.Fa name .
120b5111c46SEdward Tomasz NapieralaWhen invoked, function
121b5111c46SEdward Tomasz Napierala.Fa func
122b5111c46SEdward Tomasz Napieralawill be invoked with argument
123b5111c46SEdward Tomasz Napierala.Fa arg
124b5111c46SEdward Tomasz Napieralaas its first parameter along with any additional parameters passed in
125b5111c46SEdward Tomasz Napieralavia macro
126b5111c46SEdward Tomasz Napierala.Fn EVENTHANDLER_INVOKE
127b5111c46SEdward Tomasz Napierala(see below).
128235b396aSJoseph KoshyIf registration is successful,
129235b396aSJoseph Koshy.Fn EVENTHANDLER_REGISTER
130235b396aSJoseph Koshyreturns a cookie of type
131235b396aSJoseph Koshy.Vt eventhandler_tag .
13248ef2433SGiorgos Keramidas.It Fn EVENTHANDLER_DEREGISTER
13348ef2433SGiorgos KeramidasThis macro removes a previously registered callback associated with tag
134235b396aSJoseph Koshy.Fa tag
135235b396aSJoseph Koshyfrom the event handler named by argument
136235b396aSJoseph Koshy.Fa name .
137fc091646SIan LeporeIt waits until no threads are running handlers for this event before
138fc091646SIan Leporereturning, making it safe to unload a module immediately upon return
139fc091646SIan Leporefrom this function.
140fc091646SIan Lepore.It Fn EVENTHANDLER_DEREGISTER_NOWAIT
141fc091646SIan LeporeThis macro removes a previously registered callback associated with tag
142fc091646SIan Lepore.Fa tag
143fc091646SIan Leporefrom the event handler named by argument
144fc091646SIan Lepore.Fa name .
145fc091646SIan LeporeUpon return, one or more threads could still be running the removed
146fc091646SIan Leporefunction(s), but no new calls will be made.
147fc091646SIan LeporeTo remove a handler function from within that function, use this
148fc091646SIan Leporeversion of deregister, to avoid a deadlock.
14948ef2433SGiorgos Keramidas.It Fn EVENTHANDLER_INVOKE
15048ef2433SGiorgos KeramidasThis macro is used to invoke all the callbacks associated with event
15148ef2433SGiorgos Keramidashandler
152235b396aSJoseph Koshy.Fa name .
153235b396aSJoseph KoshyThis macro is a variadic one.
154235b396aSJoseph KoshyAdditional arguments to the macro after the
155235b396aSJoseph Koshy.Fa name
156235b396aSJoseph Koshyparameter are passed as the second and subsequent arguments to each
157235b396aSJoseph Koshyregistered callback function.
1582ca45184SMatt Joras.It Fn EVENTHANDLER_LIST_DEFINE
1592ca45184SMatt JorasThis macro defines a reference to an event handler list named by
1602ca45184SMatt Jorasargument
1612ca45184SMatt Joras.Fa name .
1622ca45184SMatt JorasIt uses
1632ca45184SMatt Joras.Xr SYSINIT 9
1642ca45184SMatt Jorasto initialize the reference and the eventhandler list.
1652ca45184SMatt Joras.It Fn EVENTHANDLER_LIST_DECLARE
1662ca45184SMatt JorasThis macro declares an event handler list named by argument
1672ca45184SMatt Joras.Fa name .
1682ca45184SMatt JorasThis is only needed for users of
1692ca45184SMatt Joras.Fn EVENTHANDLER_DIRECT_INVOKE
1702ca45184SMatt Joraswhich are not in the same compilation unit of that list's definition.
1712ca45184SMatt Joras.It Fn EVENTHANDLER_DIRECT_INVOKE
1722ca45184SMatt JorasThis macro invokes the event handlers registered for the list named by
1732ca45184SMatt Jorasargument
1742ca45184SMatt Joras.Fa name .
1752ca45184SMatt JorasThis macro can only be used if the list was defined with
1762ca45184SMatt Joras.Fn EVENTHANDLER_LIST_DEFINE .
1772ca45184SMatt JorasThe macro is variadic with the same semantics as
1782ca45184SMatt Joras.Fn EVENTHANDLER_INVOKE .
17948ef2433SGiorgos Keramidas.El
180235b396aSJoseph Koshy.Pp
181235b396aSJoseph KoshyThe macros are implemented using the following functions:
18248ef2433SGiorgos Keramidas.Bl -tag -width indent
18348ef2433SGiorgos Keramidas.It Fn eventhandler_register
18448ef2433SGiorgos KeramidasThe
185235b396aSJoseph Koshy.Fn eventhandler_register
18648ef2433SGiorgos Keramidasfunction is used to register a callback with a given event.
18748ef2433SGiorgos KeramidasThe arguments expected by this function are:
18848ef2433SGiorgos Keramidas.Bl -tag -width ".Fa priority"
18948ef2433SGiorgos Keramidas.It Fa list
19048ef2433SGiorgos KeramidasA pointer to an existing event handler list, or
191235b396aSJoseph Koshy.Dv NULL .
19248ef2433SGiorgos KeramidasIf
193235b396aSJoseph Koshy.Fa list
194235b396aSJoseph Koshyis
195235b396aSJoseph Koshy.Dv NULL ,
196235b396aSJoseph Koshythe event handler list corresponding to argument
197235b396aSJoseph Koshy.Fa name
198235b396aSJoseph Koshyis used.
19948ef2433SGiorgos Keramidas.It Fa name
20048ef2433SGiorgos KeramidasThe name of the event handler list.
20148ef2433SGiorgos Keramidas.It Fa func
20248ef2433SGiorgos KeramidasA pointer to a callback function.
203235b396aSJoseph KoshyArgument
204235b396aSJoseph Koshy.Fa arg
205d3d90204SGiorgos Keramidasis passed to the callback function
206235b396aSJoseph Koshy.Fa func
207235b396aSJoseph Koshyas its first argument when it is invoked.
20848ef2433SGiorgos Keramidas.It Fa priority
20948ef2433SGiorgos KeramidasThe relative priority of this callback among all the callbacks
21048ef2433SGiorgos Keramidasregistered for this event.
21148ef2433SGiorgos KeramidasValid values are those in the range
212235b396aSJoseph Koshy.Dv EVENTHANDLER_PRI_FIRST
213235b396aSJoseph Koshyto
21448ef2433SGiorgos Keramidas.Dv EVENTHANDLER_PRI_LAST .
21548ef2433SGiorgos Keramidas.El
216235b396aSJoseph Koshy.Pp
21748ef2433SGiorgos KeramidasThe
21848ef2433SGiorgos Keramidas.Fn eventhandler_register
21948ef2433SGiorgos Keramidasfunction returns a
22048ef2433SGiorgos Keramidas.Fa tag
22148ef2433SGiorgos Keramidasthat can later be used with
222235b396aSJoseph Koshy.Fn eventhandler_deregister
22348ef2433SGiorgos Keramidasto remove the particular callback function.
22448ef2433SGiorgos Keramidas.It Fn eventhandler_deregister
22548ef2433SGiorgos KeramidasThe
22648ef2433SGiorgos Keramidas.Fn eventhandler_deregister
22748ef2433SGiorgos Keramidasfunction removes the callback associated with tag
228235b396aSJoseph Koshy.Fa tag
229235b396aSJoseph Koshyfrom the event handler list pointed to by
230235b396aSJoseph Koshy.Fa list .
231fc091646SIan LeporeIf
232fc091646SIan Lepore.Fa tag
233fc091646SIan Leporeis
234fc091646SIan Lepore.Va NULL ,
235fc091646SIan Leporeall callback functions for the event are removed.
236fc091646SIan LeporeThis function will not return until all threads have exited from the
237fc091646SIan Leporeremoved handler callback function(s).
238fc091646SIan LeporeThis function is not safe to call from inside an event handler callback.
239fc091646SIan Lepore.It Fn eventhandler_deregister_nowait
240fc091646SIan LeporeThe
241fc091646SIan Lepore.Fn eventhandler_deregister
242fc091646SIan Leporefunction removes the callback associated with tag
243fc091646SIan Lepore.Fa tag
244fc091646SIan Leporefrom the event handler list pointed to by
245fc091646SIan Lepore.Fa list .
246235b396aSJoseph KoshyThis function is safe to call from inside an event handler
247235b396aSJoseph Koshycallback.
24848ef2433SGiorgos Keramidas.It Fn eventhandler_find_list
24948ef2433SGiorgos KeramidasThe
250235b396aSJoseph Koshy.Fn eventhandler_find_list
25148ef2433SGiorgos Keramidasfunction returns a pointer to event handler list structure corresponding
25248ef2433SGiorgos Keramidasto event
253235b396aSJoseph Koshy.Fa name .
25448ef2433SGiorgos Keramidas.It Fn eventhandler_prune_list
25548ef2433SGiorgos KeramidasThe
256235b396aSJoseph Koshy.Fn eventhandler_prune_list
25748ef2433SGiorgos Keramidasfunction removes all deregistered callbacks from the event list
258235b396aSJoseph Koshy.Fa list .
25948ef2433SGiorgos Keramidas.El
260235b396aSJoseph Koshy.Sh RETURN VALUES
261235b396aSJoseph KoshyThe macro
262235b396aSJoseph Koshy.Fn EVENTHANDLER_REGISTER
263235b396aSJoseph Koshyand function
264235b396aSJoseph Koshy.Fn eventhandler_register
265235b396aSJoseph Koshyreturn a cookie of type
266235b396aSJoseph Koshy.Vt eventhandler_tag ,
267235b396aSJoseph Koshywhich may be used in a subsequent call to
268235b396aSJoseph Koshy.Fn EVENTHANDLER_DEREGISTER
269235b396aSJoseph Koshyor
270235b396aSJoseph Koshy.Fn eventhandler_deregister .
271235b396aSJoseph Koshy.Pp
27248ef2433SGiorgos KeramidasThe
273235b396aSJoseph Koshy.Fn eventhandler_find_list
27448ef2433SGiorgos Keramidasfunction
275235b396aSJoseph Koshyreturns a pointer to an event handler list corresponding to parameter
276235b396aSJoseph Koshy.Fa name ,
277235b396aSJoseph Koshyor
278235b396aSJoseph Koshy.Dv NULL
279235b396aSJoseph Koshyif no such list was found.
280235b396aSJoseph Koshy.Sh HISTORY
281235b396aSJoseph KoshyThe
282235b396aSJoseph Koshy.Nm
283235b396aSJoseph Koshyfacility first appeared in
284235b396aSJoseph Koshy.Fx 4.0 .
285235b396aSJoseph Koshy.Sh AUTHORS
286235b396aSJoseph KoshyThis manual page was written by
2872ca45184SMatt Joras.An Joseph Koshy Aq Mt jkoshy@FreeBSD.org
2882ca45184SMatt Jorasand
2892ca45184SMatt Joras.An Matt Joras Aq Mt mjoras@FreeBSD.org .
290