+ hook->itf = itf ? itf : &hook_api_default_itf;
+ hook->closure = closure;
+
+ /* record the hook */
+ pthread_mutex_lock(&mutex);
+ hook->next = list_of_api_hooks;
+ list_of_api_hooks = hook;
+ pthread_mutex_unlock(&mutex);
+
+ /* returns it */
+ return hook;
+}
+
+struct afb_hook_api *afb_hook_addref_api(struct afb_hook_api *hook)
+{
+ __atomic_add_fetch(&hook->refcount, 1, __ATOMIC_RELAXED);
+ return hook;
+}
+
+static void hook_clean_api()
+{
+ struct afb_hook_api **prv, *hook, *head;
+
+ if (pthread_rwlock_trywrlock(&rwlock) == 0) {
+ /* unlink under mutex */
+ head = NULL;
+ pthread_mutex_lock(&mutex);
+ prv = &list_of_api_hooks;
+ while ((hook = *prv)) {
+ if (hook->refcount)
+ prv = &(*prv)->next;
+ else {
+ *prv = hook->next;
+ hook->next = head;
+ head = hook;
+ }
+ }
+ pthread_mutex_unlock(&mutex);
+ pthread_rwlock_unlock(&rwlock);
+
+ /* free found hooks */
+ while((hook = head)) {
+ head = hook->next;
+ free(hook->api);
+ free(hook);
+ }
+ }
+}
+
+void afb_hook_unref_api(struct afb_hook_api *hook)
+{
+ if (hook && !__atomic_sub_fetch(&hook->refcount, 1, __ATOMIC_RELAXED))
+ hook_clean_api();
+}
+
+/******************************************************************************
+ * section: default callbacks for tracing service interface (evt)
+ *****************************************************************************/
+
+static void _hook_evt_(const char *evt, int id, const char *format, ...)
+{
+ va_list ap;
+ va_start(ap, format);
+ _hook_("evt-%s:%d", format, ap, evt, id);
+ va_end(ap);
+}
+
+static void hook_evt_create_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
+{
+ _hook_evt_(evt, id, "create");
+}
+
+static void hook_evt_push_before_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj)
+{
+ _hook_evt_(evt, id, "push.before(%s)", json_object_to_json_string_ext(obj, JSON_C_TO_STRING_NOSLASHESCAPE));
+}
+
+
+static void hook_evt_push_after_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj, int result)
+{
+ _hook_evt_(evt, id, "push.after(%s) -> %d", json_object_to_json_string_ext(obj, JSON_C_TO_STRING_NOSLASHESCAPE), result);
+}
+
+static void hook_evt_broadcast_before_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj)
+{
+ _hook_evt_(evt, id, "broadcast.before(%s)", json_object_to_json_string_ext(obj, JSON_C_TO_STRING_NOSLASHESCAPE));
+}
+
+static void hook_evt_broadcast_after_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj, int result)
+{
+ _hook_evt_(evt, id, "broadcast.after(%s) -> %d", json_object_to_json_string_ext(obj, JSON_C_TO_STRING_NOSLASHESCAPE), result);
+}
+
+static void hook_evt_name_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, const char *result)
+{
+ _hook_evt_(evt, id, "name -> %s", result);
+}
+
+static void hook_evt_addref_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
+{
+ _hook_evt_(evt, id, "addref");
+}
+
+static void hook_evt_unref_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
+{
+ _hook_evt_(evt, id, "unref");
+}
+
+static struct afb_hook_evt_itf hook_evt_default_itf = {
+ .hook_evt_create = hook_evt_create_cb,
+ .hook_evt_push_before = hook_evt_push_before_cb,
+ .hook_evt_push_after = hook_evt_push_after_cb,
+ .hook_evt_broadcast_before = hook_evt_broadcast_before_cb,
+ .hook_evt_broadcast_after = hook_evt_broadcast_after_cb,
+ .hook_evt_name = hook_evt_name_cb,
+ .hook_evt_addref = hook_evt_addref_cb,
+ .hook_evt_unref = hook_evt_unref_cb
+};
+
+/******************************************************************************
+ * section: hooks for tracing events interface (evt)
+ *****************************************************************************/
+
+#define _HOOK_EVT_(what,...) \
+ struct afb_hook_evt *hook; \
+ struct afb_hookid hookid; \
+ pthread_rwlock_rdlock(&rwlock); \
+ init_hookid(&hookid); \
+ hook = list_of_evt_hooks; \
+ while (hook) { \
+ if (hook->refcount \
+ && hook->itf->hook_evt_##what \
+ && (hook->flags & afb_hook_flag_evt_##what) != 0 \
+ && MATCH_EVENT(hook->pattern, evt)) { \
+ hook->itf->hook_evt_##what(hook->closure, &hookid, __VA_ARGS__); \
+ } \
+ hook = hook->next; \
+ } \
+ pthread_rwlock_unlock(&rwlock);
+
+void afb_hook_evt_create(const char *evt, int id)
+{
+ _HOOK_EVT_(create, evt, id);
+}
+
+void afb_hook_evt_push_before(const char *evt, int id, struct json_object *obj)
+{
+ _HOOK_EVT_(push_before, evt, id, obj);
+}
+
+int afb_hook_evt_push_after(const char *evt, int id, struct json_object *obj, int result)
+{
+ _HOOK_EVT_(push_after, evt, id, obj, result);
+ return result;
+}
+
+void afb_hook_evt_broadcast_before(const char *evt, int id, struct json_object *obj)
+{
+ _HOOK_EVT_(broadcast_before, evt, id, obj);
+}
+
+int afb_hook_evt_broadcast_after(const char *evt, int id, struct json_object *obj, int result)
+{
+ _HOOK_EVT_(broadcast_after, evt, id, obj, result);
+ return result;
+}
+
+void afb_hook_evt_name(const char *evt, int id, const char *result)
+{
+ _HOOK_EVT_(name, evt, id, result);
+}
+
+void afb_hook_evt_addref(const char *evt, int id)
+{
+ _HOOK_EVT_(addref, evt, id);
+}
+
+void afb_hook_evt_unref(const char *evt, int id)
+{
+ _HOOK_EVT_(unref, evt, id);
+}
+
+/******************************************************************************
+ * section: hooking services (evt)
+ *****************************************************************************/
+
+int afb_hook_flags_evt(const char *name)
+{
+ int flags;
+ struct afb_hook_evt *hook;
+
+ flags = 0;
+ pthread_rwlock_rdlock(&rwlock);
+ hook = list_of_evt_hooks;
+ while (hook) {
+ if (!name || MATCH_EVENT(hook->pattern, name))
+ flags |= hook->flags;
+ hook = hook->next;
+ }
+ pthread_rwlock_unlock(&rwlock);
+ return flags;
+}
+
+struct afb_hook_evt *afb_hook_create_evt(const char *pattern, int flags, struct afb_hook_evt_itf *itf, void *closure)
+{
+ struct afb_hook_evt *hook;
+
+ /* alloc the result */
+ hook = calloc(1, sizeof *hook);
+ if (hook == NULL)
+ return NULL;
+
+ /* get a copy of the names */
+ hook->pattern = pattern ? strdup(pattern) : NULL;
+ if (pattern && !hook->pattern) {
+ free(hook);
+ return NULL;
+ }
+
+ /* initialise the rest */
+ hook->refcount = 1;
+ hook->flags = flags;
+ hook->itf = itf ? itf : &hook_evt_default_itf;