]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sys/eventhandler.h
Partial merge of the SPDX changes
[FreeBSD/FreeBSD.git] / sys / sys / eventhandler.h
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1999 Michael Smith <msmith@freebsd.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30
31 #ifndef _SYS_EVENTHANDLER_H_
32 #define _SYS_EVENTHANDLER_H_
33
34 #include <sys/lock.h>
35 #include <sys/ktr.h>
36 #include <sys/mutex.h>
37 #include <sys/queue.h>
38
39 struct eventhandler_entry {
40         TAILQ_ENTRY(eventhandler_entry) ee_link;
41         int                             ee_priority;
42 #define EHE_DEAD_PRIORITY       (-1)
43         void                            *ee_arg;
44 };
45
46 #ifdef VIMAGE
47 struct eventhandler_entry_vimage {
48         void    (* func)(void);         /* Original function registered. */
49         void    *ee_arg;                /* Original argument registered. */
50         void    *sparep[2];
51 };
52 #endif
53
54 struct eventhandler_list {
55         char                            *el_name;
56         int                             el_flags;
57 #define EHL_INITTED     (1<<0)
58         u_int                           el_runcount;
59         struct mtx                      el_lock;
60         TAILQ_ENTRY(eventhandler_list)  el_link;
61         TAILQ_HEAD(,eventhandler_entry) el_entries;
62 };
63
64 typedef struct eventhandler_entry       *eventhandler_tag;
65
66 #define EHL_LOCK(p)             mtx_lock(&(p)->el_lock)
67 #define EHL_UNLOCK(p)           mtx_unlock(&(p)->el_lock)
68 #define EHL_LOCK_ASSERT(p, x)   mtx_assert(&(p)->el_lock, x)
69
70 /*
71  * Macro to invoke the handlers for a given event.
72  */
73 #define _EVENTHANDLER_INVOKE(name, list, ...) do {                      \
74         struct eventhandler_entry *_ep;                                 \
75         struct eventhandler_entry_ ## name *_t;                         \
76                                                                         \
77         KASSERT((list)->el_flags & EHL_INITTED,                         \
78            ("eventhandler_invoke: running non-inited list"));           \
79         EHL_LOCK_ASSERT((list), MA_OWNED);                              \
80         (list)->el_runcount++;                                          \
81         KASSERT((list)->el_runcount > 0,                                \
82             ("eventhandler_invoke: runcount overflow"));                \
83         CTR0(KTR_EVH, "eventhandler_invoke(\"" __STRING(name) "\")");   \
84         TAILQ_FOREACH(_ep, &((list)->el_entries), ee_link) {            \
85                 if (_ep->ee_priority != EHE_DEAD_PRIORITY) {            \
86                         EHL_UNLOCK((list));                             \
87                         _t = (struct eventhandler_entry_ ## name *)_ep; \
88                         CTR1(KTR_EVH, "eventhandler_invoke: executing %p", \
89                             (void *)_t->eh_func);                       \
90                         _t->eh_func(_ep->ee_arg , ## __VA_ARGS__);      \
91                         EHL_LOCK((list));                               \
92                 }                                                       \
93         }                                                               \
94         KASSERT((list)->el_runcount > 0,                                \
95             ("eventhandler_invoke: runcount underflow"));               \
96         (list)->el_runcount--;                                          \
97         if ((list)->el_runcount == 0)                                   \
98                 eventhandler_prune_list(list);                          \
99         EHL_UNLOCK((list));                                             \
100 } while (0)
101
102 /*
103  * Slow handlers are entirely dynamic; lists are created
104  * when entries are added to them, and thus have no concept of "owner",
105  *
106  * Slow handlers need to be declared, but do not need to be defined. The
107  * declaration must be in scope wherever the handler is to be invoked.
108  */
109 #define EVENTHANDLER_DECLARE(name, type)                                \
110 struct eventhandler_entry_ ## name                                      \
111 {                                                                       \
112         struct eventhandler_entry       ee;                             \
113         type                            eh_func;                        \
114 };                                                                      \
115 struct __hack
116
117 #define EVENTHANDLER_DEFINE(name, func, arg, priority)                  \
118         static eventhandler_tag name ## _tag;                           \
119         static void name ## _evh_init(void *ctx)                        \
120         {                                                               \
121                 name ## _tag = EVENTHANDLER_REGISTER(name, func, ctx,   \
122                     priority);                                          \
123         }                                                               \
124         SYSINIT(name ## _evh_init, SI_SUB_CONFIGURE, SI_ORDER_ANY,      \
125             name ## _evh_init, arg);                                    \
126         struct __hack
127
128 #define EVENTHANDLER_INVOKE(name, ...)                                  \
129 do {                                                                    \
130         struct eventhandler_list *_el;                                  \
131                                                                         \
132         if ((_el = eventhandler_find_list(#name)) != NULL)              \
133                 _EVENTHANDLER_INVOKE(name, _el , ## __VA_ARGS__);       \
134 } while (0)
135
136 #define EVENTHANDLER_REGISTER(name, func, arg, priority)                \
137         eventhandler_register(NULL, #name, func, arg, priority)
138
139 #define EVENTHANDLER_DEREGISTER(name, tag)                              \
140 do {                                                                    \
141         struct eventhandler_list *_el;                                  \
142                                                                         \
143         if ((_el = eventhandler_find_list(#name)) != NULL)              \
144                 eventhandler_deregister(_el, tag);                      \
145 } while(0)
146
147 #define EVENTHANDLER_DEREGISTER_NOWAIT(name, tag)                       \
148 do {                                                                    \
149         struct eventhandler_list *_el;                                  \
150                                                                         \
151         if ((_el = eventhandler_find_list(#name)) != NULL)              \
152                 eventhandler_deregister_nowait(_el, tag);               \
153 } while(0)
154
155 eventhandler_tag eventhandler_register(struct eventhandler_list *list, 
156             const char *name, void *func, void *arg, int priority);
157 void    eventhandler_deregister(struct eventhandler_list *list,
158             eventhandler_tag tag);
159 void    eventhandler_deregister_nowait(struct eventhandler_list *list,
160             eventhandler_tag tag);
161 struct eventhandler_list *eventhandler_find_list(const char *name);
162 void    eventhandler_prune_list(struct eventhandler_list *list);
163
164 #ifdef VIMAGE
165 typedef void (*vimage_iterator_func_t)(void *, ...);
166
167 eventhandler_tag vimage_eventhandler_register(struct eventhandler_list *list,
168             const char *name, void *func, void *arg, int priority,
169             vimage_iterator_func_t);
170 #endif
171
172 /*
173  * Standard system event queues.
174  */
175
176 /* Generic priority levels */
177 #define EVENTHANDLER_PRI_FIRST  0
178 #define EVENTHANDLER_PRI_ANY    10000
179 #define EVENTHANDLER_PRI_LAST   20000
180
181 /* Shutdown events */
182 typedef void (*shutdown_fn)(void *, int);
183
184 #define SHUTDOWN_PRI_FIRST      EVENTHANDLER_PRI_FIRST
185 #define SHUTDOWN_PRI_DEFAULT    EVENTHANDLER_PRI_ANY
186 #define SHUTDOWN_PRI_LAST       EVENTHANDLER_PRI_LAST
187
188 EVENTHANDLER_DECLARE(shutdown_pre_sync, shutdown_fn);   /* before fs sync */
189 EVENTHANDLER_DECLARE(shutdown_post_sync, shutdown_fn);  /* after fs sync */
190 EVENTHANDLER_DECLARE(shutdown_final, shutdown_fn);
191
192 /* Power state change events */
193 typedef void (*power_change_fn)(void *);
194 EVENTHANDLER_DECLARE(power_resume, power_change_fn);
195 EVENTHANDLER_DECLARE(power_suspend, power_change_fn);
196 EVENTHANDLER_DECLARE(power_suspend_early, power_change_fn);
197
198 /* Low memory event */
199 typedef void (*vm_lowmem_handler_t)(void *, int);
200 #define LOWMEM_PRI_DEFAULT      EVENTHANDLER_PRI_FIRST
201 EVENTHANDLER_DECLARE(vm_lowmem, vm_lowmem_handler_t);
202
203 /* Root mounted event */
204 typedef void (*mountroot_handler_t)(void *);
205 EVENTHANDLER_DECLARE(mountroot, mountroot_handler_t);
206
207 /* File system mount events */
208 struct mount;
209 struct vnode;
210 struct thread;
211 typedef void (*vfs_mounted_notify_fn)(void *, struct mount *, struct vnode *,
212     struct thread *);
213 typedef void (*vfs_unmounted_notify_fn)(void *, struct mount *,
214     struct thread *);
215 EVENTHANDLER_DECLARE(vfs_mounted, vfs_mounted_notify_fn);
216 EVENTHANDLER_DECLARE(vfs_unmounted, vfs_unmounted_notify_fn);
217
218 /*
219  * Process events
220  * process_fork and exit handlers are called without Giant.
221  * exec handlers are called with Giant, but that is by accident.
222  */
223 struct proc;
224 struct image_params;
225
226 typedef void (*exitlist_fn)(void *, struct proc *);
227 typedef void (*forklist_fn)(void *, struct proc *, struct proc *, int);
228 typedef void (*execlist_fn)(void *, struct proc *, struct image_params *);
229 typedef void (*proc_ctor_fn)(void *, struct proc *);
230 typedef void (*proc_dtor_fn)(void *, struct proc *);
231 typedef void (*proc_init_fn)(void *, struct proc *);
232 typedef void (*proc_fini_fn)(void *, struct proc *);
233 EVENTHANDLER_DECLARE(process_ctor, proc_ctor_fn);
234 EVENTHANDLER_DECLARE(process_dtor, proc_dtor_fn);
235 EVENTHANDLER_DECLARE(process_init, proc_init_fn);
236 EVENTHANDLER_DECLARE(process_fini, proc_fini_fn);
237 EVENTHANDLER_DECLARE(process_exit, exitlist_fn);
238 EVENTHANDLER_DECLARE(process_fork, forklist_fn);
239 EVENTHANDLER_DECLARE(process_exec, execlist_fn);
240
241 /*
242  * application dump event
243  */
244 typedef void (*app_coredump_start_fn)(void *, struct thread *, char *name);
245 typedef void (*app_coredump_progress_fn)(void *, struct thread *td, int byte_count);
246 typedef void (*app_coredump_finish_fn)(void *, struct thread *td);
247 typedef void (*app_coredump_error_fn)(void *, struct thread *td, char *msg, ...);
248
249 EVENTHANDLER_DECLARE(app_coredump_start, app_coredump_start_fn);
250 EVENTHANDLER_DECLARE(app_coredump_progress, app_coredump_progress_fn);
251 EVENTHANDLER_DECLARE(app_coredump_finish, app_coredump_finish_fn);
252 EVENTHANDLER_DECLARE(app_coredump_error, app_coredump_error_fn);
253
254 typedef void (*thread_ctor_fn)(void *, struct thread *);
255 typedef void (*thread_dtor_fn)(void *, struct thread *);
256 typedef void (*thread_fini_fn)(void *, struct thread *);
257 typedef void (*thread_init_fn)(void *, struct thread *);
258 EVENTHANDLER_DECLARE(thread_ctor, thread_ctor_fn);
259 EVENTHANDLER_DECLARE(thread_dtor, thread_dtor_fn);
260 EVENTHANDLER_DECLARE(thread_init, thread_init_fn);
261 EVENTHANDLER_DECLARE(thread_fini, thread_fini_fn);
262
263 typedef void (*uma_zone_chfn)(void *);
264 EVENTHANDLER_DECLARE(nmbclusters_change, uma_zone_chfn);
265 EVENTHANDLER_DECLARE(nmbufs_change, uma_zone_chfn);
266 EVENTHANDLER_DECLARE(maxsockets_change, uma_zone_chfn);
267
268 /* Kernel linker file load and unload events */
269 struct linker_file;
270 typedef void (*kld_load_fn)(void *, struct linker_file *);
271 typedef void (*kld_unload_fn)(void *, const char *, caddr_t, size_t);
272 typedef void (*kld_unload_try_fn)(void *, struct linker_file *, int *);
273 EVENTHANDLER_DECLARE(kld_load, kld_load_fn);
274 EVENTHANDLER_DECLARE(kld_unload, kld_unload_fn);
275 EVENTHANDLER_DECLARE(kld_unload_try, kld_unload_try_fn);
276
277 /* Generic graphics framebuffer interface */
278 struct fb_info;
279 typedef void (*register_framebuffer_fn)(void *, struct fb_info *);
280 typedef void (*unregister_framebuffer_fn)(void *, struct fb_info *);
281 EVENTHANDLER_DECLARE(register_framebuffer, register_framebuffer_fn);
282 EVENTHANDLER_DECLARE(unregister_framebuffer, unregister_framebuffer_fn);
283
284 /* Veto ada attachment */
285 struct cam_path;
286 struct ata_params;
287 typedef void (*ada_probe_veto_fn)(void *, struct cam_path *,
288     struct ata_params *, int *);
289 EVENTHANDLER_DECLARE(ada_probe_veto, ada_probe_veto_fn);
290
291 /* newbus device events */
292 enum evhdev_detach {
293         EVHDEV_DETACH_BEGIN,    /* Before detach() is called */
294         EVHDEV_DETACH_COMPLETE, /* After detach() returns 0 */
295         EVHDEV_DETACH_FAILED    /* After detach() returns err */
296 };
297 typedef void (*device_attach_fn)(void *, device_t);
298 typedef void (*device_detach_fn)(void *, device_t, enum evhdev_detach);
299 EVENTHANDLER_DECLARE(device_attach, device_attach_fn);
300 EVENTHANDLER_DECLARE(device_detach, device_detach_fn);
301
302 #endif /* _SYS_EVENTHANDLER_H_ */