]> git.proxmox.com Git - mirror_frr.git/blob - lib/hook.h
Merge branch 'master'
[mirror_frr.git] / lib / hook.h
1 /*
2 * Copyright (c) 2016 David Lamparter, for NetDEF, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22
23 #ifndef _FRR_HOOK_H
24 #define _FRR_HOOK_H
25
26 #include <stdbool.h>
27
28 #include "module.h"
29 #include "memory.h"
30
31 /* type-safe subscribable hook points
32 *
33 * where "type-safe" applies to the function pointers used for subscriptions
34 *
35 * overall usage:
36 * - to create a hook:
37 *
38 * mydaemon.h:
39 * #include "hook.h"
40 * DECLARE_HOOK (some_update_event, (struct eventinfo *info), (info))
41 *
42 * mydaemon.c:
43 * DEFINE_HOOK (some_update_event, (struct eventinfo *info), (info))
44 * ...
45 * hook_call (some_update_event, info)
46 *
47 * Note: the second and third macro args must be the hook function's
48 * parameter list, with the same names for each parameter. The second
49 * macro arg is with types (used for defining things), the third arg is
50 * just the names (used for passing along parameters).
51 *
52 * Do not use parameter names starting with "hook", these can collide with
53 * names used by the hook code itself.
54 *
55 * The return value is always "int" for now; hook_call will sum up the
56 * return values from each registered user. Default is 0.
57 *
58 * There are no pre-defined semantics for the value, in most cases it is
59 * ignored. For success/failure indication, 0 should be success, and
60 * handlers should make sure to only return 0 or 1 (not -1 or other values).
61 *
62 *
63 * - to use a hook / create a handler:
64 *
65 * #include "mydaemon.h"
66 * int event_handler (struct eventinfo *info) { ... }
67 * hook_register (some_update_event, event_handler);
68 *
69 * or, if you need an argument to be passed along (addonptr will be added
70 * as first argument when calling the handler):
71 *
72 * #include "mydaemon.h"
73 * int event_handler (void *addonptr, struct eventinfo *info) { ... }
74 * hook_register_arg (some_update_event, event_handler, addonptr);
75 *
76 * (addonptr isn't typesafe, but that should be manageable.)
77 */
78
79 /* TODO:
80 * - hook_unregister_all_module()
81 * - introspection / CLI / debug
82 * - testcases ;)
83 *
84 * For loadable modules, the idea is that hooks could be automatically
85 * unregistered when a module is unloaded.
86 *
87 * It's also possible to add a constructor (MTYPE style) to DEFINE_HOOK,
88 * which would make it possible for the CLI to show all hooks and all
89 * registered handlers.
90 */
91
92 struct hookent {
93 struct hookent *next;
94 void *hookfn; /* actually a function pointer */
95 void *hookarg;
96 bool has_arg;
97 struct frrmod_runtime *module;
98 const char *fnname;
99 };
100
101 struct hook {
102 const char *name;
103 struct hookent *entries;
104 };
105
106 /* subscribe/add callback function to a hook
107 *
108 * always use hook_register(), which uses the static inline helper from
109 * DECLARE_HOOK in order to get type safety
110 */
111 extern void _hook_register(struct hook *hook, void *funcptr, void *arg,
112 bool has_arg, struct frrmod_runtime *module,
113 const char *funcname);
114 #define hook_register(hookname, func) \
115 _hook_register(&_hook_ ## hookname, \
116 _hook_typecheck_ ## hookname (func), \
117 NULL, false, THIS_MODULE, #func)
118 #define hook_register_arg(hookname, func, arg) \
119 _hook_register(&_hook_ ## hookname, \
120 _hook_typecheck_arg_ ## hookname (func), \
121 arg, true, THIS_MODULE, #func)
122
123 extern void _hook_unregister(struct hook *hook, void *funcptr, void *arg,
124 bool has_arg);
125 #define hook_unregister(hookname, func) \
126 _hook_unregister(&_hook_ ## hookname, \
127 _hook_typecheck_ ## hookname (func), NULL, false)
128 #define hook_unregister_arg(hookname, func, arg) \
129 _hook_unregister(&_hook_ ## hookname, \
130 _hook_typecheck_arg_ ## hookname (func), arg, true)
131
132 /* invoke hooks
133 * this is private (static) to the file that has the DEFINE_HOOK statement
134 */
135 #define hook_call(hookname, ...) \
136 hook_call_ ## hookname (__VA_ARGS__)
137
138 /* helpers to add the void * arg */
139 #define HOOK_ADDDEF(...) (void *hookarg , ## __VA_ARGS__)
140 #define HOOK_ADDARG(...) (hookarg , ## __VA_ARGS__)
141
142 /* use in header file - declares the hook and its arguments
143 * usage: DECLARE_HOOK(my_hook, (int arg1, struct foo *arg2), (arg1, arg2))
144 * as above, "passlist" must use the same order and same names as "arglist"
145 *
146 * theoretically passlist is not neccessary, but let's keep things simple and
147 * use exact same args on DECLARE and DEFINE.
148 */
149 #define DECLARE_HOOK(hookname, arglist, passlist) \
150 extern struct hook _hook_ ## hookname; \
151 __attribute__((unused)) \
152 static void *_hook_typecheck_ ## hookname ( \
153 int (*funcptr) arglist) { \
154 return (void *)funcptr; } \
155 __attribute__((unused)) \
156 static void *_hook_typecheck_arg_ ## hookname ( \
157 int (*funcptr) HOOK_ADDDEF arglist) { \
158 return (void *)funcptr; }
159
160 /* use in source file - contains hook-related definitions.
161 */
162 #define DEFINE_HOOK(hookname, arglist, passlist) \
163 struct hook _hook_ ## hookname = { \
164 .name = #hookname, \
165 .entries = NULL, \
166 }; \
167 static int hook_call_ ## hookname arglist { \
168 int hooksum = 0; \
169 struct hookent *he = _hook_ ## hookname .entries; \
170 void *hookarg; \
171 union { \
172 void *voidptr; \
173 int (*fptr) arglist; \
174 int (*farg) HOOK_ADDDEF arglist; \
175 } hookp; \
176 for (; he; he = he->next) { \
177 hookarg = he->hookarg; \
178 hookp.voidptr = he->hookfn; \
179 if (!he->has_arg) \
180 hooksum += hookp.fptr passlist; \
181 else \
182 hooksum += hookp.farg HOOK_ADDARG passlist; \
183 } \
184 return hooksum; \
185 }
186
187 #endif /* _FRR_HOOK_H */