Libfrr provides type-safe subscribable hook points where other pieces of code can add one or more callback functions. “type-safe” in this case applies to the function pointers used for subscriptions. The implementations checks (at compile-time) whether a callback to be added has the appropriate function signature (parameters) for the hook.
#include "hook.h" DECLARE_HOOK(some_update_event, (struct eventinfo *info), (info));
#include "mydaemon.h" DEFINE_HOOK(some_update_event, (struct eventinfo *info), (info)); ... hook_call(some_update_event, info);
#include "mydaemon.h" static int event_handler(struct eventinfo *info); ... hook_register(some_update_event, event_handler);
Do not use parameter names starting with “hook”, these can collide with names used by the hook code itself.
Callbacks to be placed on hooks always return “int” for now; hook_call will sum up the return values from each called function. (The default is 0 if no callbacks are registered.)
There are no pre-defined semantics for the value, in most cases it is ignored. For success/failure indication, 0 should be success, and handlers should make sure to only return 0 or 1 (not -1 or other values).
There is no built-in way to abort executing a chain after a failure of one
of the callbacks. If this is needed, the hook can use an extra
bool *aborted argument.
Hooks support a “priority” value for ordering registered calls relative to each other. The priority is a signed integer where lower values are called earlier. There are also “Koohs”, which is hooks with reverse priority ordering (for cleanup/deinit hooks, so you can use the same priority value).
Recommended priority value ranges are:
|-999 … 0 … 999||main executable / daemon, or library|
|-1999 … -1000||modules registering calls that should run before the daemon’s bits|
|1000 … 1999||modules’ calls that should run after daemon’s (includes default value: 1000)|
Note: the default value is 1000, based on the following 2 expectations:
- most hook_register() usage will be in loadable modules
- usage of hook_register() in the daemon itself may need relative ordering to itself, making an explicit value the expected case
The priority value is passed as extra argument on hook_register_prio() / hook_register_arg_prio(). Whether a hook runs in reverse is determined solely by the code defining / calling the hook. (DECLARE_KOOH is actually the same thing as DECLARE_HOOK, it’s just there to make it obvious.)
DECLARE_HOOK(name, arglist, passlist)¶
DECLARE_KOOH(name, arglist, passlist)¶
- name – Name of the hook to be defined
- arglist – Function definition style parameter list in braces.
- passlist – List of the same parameters without their types.
Note: the second and third macro args must be the hook function’s parameter list, with the same names for each parameter. The second macro arg is with types (used for defining things), the third arg is just the names (used for passing along parameters).
This macro must be placed in a header file; this header file must be included to register a callback on the hook.
DECLARE_HOOK(foo, (), ()); DECLARE_HOOK(bar, (int arg), (arg)); DECLARE_HOOK(baz, (const void *x, in_addr_t y), (x, y));
DEFINE_HOOK(name, arglist, passlist)¶
Implements an hook. Each
DECLARE_HOOKmust have be accompanied by exactly one
DEFINE_HOOK, which needs to be placed in a source file. The hook can only be called from this source file. This is intentional to avoid overloading and/or misusing hooks for distinct purposes.
The compiled source file will include a global symbol with the name of the hook prefixed by _hook_. Trying to register a callback for a hook that doesn’t exist will therefore result in a linker error, or a module load-time error for dynamic modules.
DEFINE_KOOH(name, arglist, passlist)¶
DEFINE_HOOK, but the sense of priorities / order of callbacks is reversed. This should be used for cleanup hooks.
Calls the specified named hook. Parameters to the hook are passed right after the hook name, e.g.:
hook_call(foo); hook_call(bar, 0); hook_call(baz, NULL, INADDR_ANY);
Returns the sum of return values from all callbacks. The
DEFINE_HOOKstatement for the hook must be placed in the file before any
hook_calluse of the hook.
hook_register(name, int (*callback)(...))¶
hook_register_prio(name, int priority, int (*callback)(...))¶
hook_register_arg(name, int (*callback)(void *arg, ...), void *arg)¶
hook_register_arg_prio(name, int priority, int (*callback)(void *arg, ...), void *arg)¶
Register a callback with an hook. If the caller needs to pass an extra argument to the callback, the _arg variant can be used and the extra parameter will be passed as first argument to the callback. There is no typechecking for this argument.
The priority value is used as described above. The variants without a priority parameter use 1000 as priority value.
hook_unregister(name, int (*callback)(...))¶
hook_unregister_arg(name, int (*callback)(void *arg, ...), void *arg)¶
Removes a previously registered callback from a hook. Note that there is no _prio variant of these calls. The priority value is only used during registration.