]>
git.proxmox.com Git - mirror_frr.git/blob - lib/hook.h
2 * Copyright (c) 2016 David Lamparter, for NetDEF, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
31 /* type-safe subscribable hook points
33 * where "type-safe" applies to the function pointers used for subscriptions
40 * DECLARE_HOOK (some_update_event, (struct eventinfo *info), (info))
43 * DEFINE_HOOK (some_update_event, (struct eventinfo *info), (info))
45 * hook_call (some_update_event, info)
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).
52 * Do not use parameter names starting with "hook", these can collide with
53 * names used by the hook code itself.
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.
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).
63 * - to use a hook / create a handler:
65 * #include "mydaemon.h"
66 * int event_handler (struct eventinfo *info) { ... }
67 * hook_register (some_update_event, event_handler);
69 * or, if you need an argument to be passed along (addonptr will be added
70 * as first argument when calling the handler):
72 * #include "mydaemon.h"
73 * int event_handler (void *addonptr, struct eventinfo *info) { ... }
74 * hook_register_arg (some_update_event, event_handler, addonptr);
76 * (addonptr isn't typesafe, but that should be manageable.)
80 * - hook_unregister_all_module()
81 * - introspection / CLI / debug
84 * For loadable modules, the idea is that hooks could be automatically
85 * unregistered when a module is unloaded.
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.
94 void *hookfn
; /* actually a function pointer */
97 struct frrmod_runtime
*module
;
103 struct hookent
*entries
;
106 /* subscribe/add callback function to a hook
108 * always use hook_register(), which uses the static inline helper from
109 * DECLARE_HOOK in order to get type safety
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, _hook_typecheck_##hookname(func), \
116 NULL, false, THIS_MODULE, #func)
117 #define hook_register_arg(hookname, func, arg) \
118 _hook_register(&_hook_##hookname, \
119 _hook_typecheck_arg_##hookname(func), arg, true, \
122 extern void _hook_unregister(struct hook
*hook
, void *funcptr
, void *arg
,
124 #define hook_unregister(hookname, func) \
125 _hook_unregister(&_hook_##hookname, _hook_typecheck_##hookname(func), \
127 #define hook_unregister_arg(hookname, func, arg) \
128 _hook_unregister(&_hook_##hookname, \
129 _hook_typecheck_arg_##hookname(func), arg, true)
132 * this is private (static) to the file that has the DEFINE_HOOK statement
134 #define hook_call(hookname, ...) hook_call_##hookname(__VA_ARGS__)
136 /* helpers to add the void * arg */
137 #define HOOK_ADDDEF(...) (void *hookarg , ## __VA_ARGS__)
138 #define HOOK_ADDARG(...) (hookarg , ## __VA_ARGS__)
140 /* use in header file - declares the hook and its arguments
141 * usage: DECLARE_HOOK(my_hook, (int arg1, struct foo *arg2), (arg1, arg2))
142 * as above, "passlist" must use the same order and same names as "arglist"
144 * theoretically passlist is not neccessary, but let's keep things simple and
145 * use exact same args on DECLARE and DEFINE.
147 #define DECLARE_HOOK(hookname, arglist, passlist) \
148 extern struct hook _hook_##hookname; \
149 __attribute__((unused)) static void *_hook_typecheck_##hookname( \
150 int(*funcptr) arglist) \
152 return (void *)funcptr; \
154 __attribute__((unused)) static void *_hook_typecheck_arg_##hookname( \
155 int(*funcptr) HOOK_ADDDEF arglist) \
157 return (void *)funcptr; \
160 /* use in source file - contains hook-related definitions.
162 #define DEFINE_HOOK(hookname, arglist, passlist) \
163 struct hook _hook_##hookname = { \
164 .name = #hookname, .entries = NULL, \
166 static int hook_call_##hookname arglist \
169 struct hookent *he = _hook_##hookname.entries; \
173 int(*fptr) arglist; \
174 int(*farg) HOOK_ADDDEF arglist; \
176 for (; he; he = he->next) { \
177 hookarg = he->hookarg; \
178 hookp.voidptr = he->hookfn; \
180 hooksum += hookp.fptr passlist; \
182 hooksum += hookp.farg HOOK_ADDARG passlist; \
187 #endif /* _FRR_HOOK_H */