+/******************************************************************************
+ * section: default callbacks for sessions (session)
+ *****************************************************************************/
+
+static void _hook_session_(struct afb_session *session, const char *format, ...)
+{
+ va_list ap;
+ va_start(ap, format);
+ _hook_("session-%s", format, ap, afb_session_uuid(session));
+ va_end(ap);
+}
+
+static void hook_session_create_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
+{
+ _hook_session_(session, "create -> token=%s", afb_session_token(session));
+}
+
+static void hook_session_close_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
+{
+ _hook_session_(session, "close");
+}
+
+static void hook_session_destroy_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
+{
+ _hook_session_(session, "destroy");
+}
+
+static void hook_session_renew_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
+{
+ _hook_session_(session, "renew -> token=%s", afb_session_token(session));
+}
+
+static void hook_session_addref_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
+{
+ _hook_session_(session, "addref");
+}
+
+static void hook_session_unref_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
+{
+ _hook_session_(session, "unref");
+}
+
+static struct afb_hook_session_itf hook_session_default_itf = {
+ .hook_session_create = hook_session_create_cb,
+ .hook_session_close = hook_session_close_cb,
+ .hook_session_destroy = hook_session_destroy_cb,
+ .hook_session_renew = hook_session_renew_cb,
+ .hook_session_addref = hook_session_addref_cb,
+ .hook_session_unref = hook_session_unref_cb
+};
+
+/******************************************************************************
+ * section: hooks for tracing sessions (session)
+ *****************************************************************************/
+
+#define _HOOK_SESSION_(what,...) \
+ struct afb_hook_session *hook; \
+ struct afb_hookid hookid; \
+ const char *sessid = 0; \
+ pthread_rwlock_rdlock(&rwlock); \
+ init_hookid(&hookid); \
+ hook = list_of_session_hooks; \
+ while (hook) { \
+ if (hook->refcount \
+ && hook->itf->hook_session_##what \
+ && (hook->flags & afb_hook_flag_session_##what) != 0 \
+ && MATCH_SESSION(hook->pattern, (sessid?:(sessid=afb_session_uuid(session))))) { \
+ hook->itf->hook_session_##what(hook->closure, &hookid, __VA_ARGS__); \
+ } \
+ hook = hook->next; \
+ } \
+ pthread_rwlock_unlock(&rwlock);
+
+void afb_hook_session_create(struct afb_session *session)
+{
+ _HOOK_SESSION_(create, session);
+}
+
+void afb_hook_session_close(struct afb_session *session)
+{
+ _HOOK_SESSION_(close, session);
+}
+
+void afb_hook_session_destroy(struct afb_session *session)
+{
+ _HOOK_SESSION_(destroy, session);
+}
+
+void afb_hook_session_renew(struct afb_session *session)
+{
+ _HOOK_SESSION_(renew, session);
+}
+
+void afb_hook_session_addref(struct afb_session *session)
+{
+ _HOOK_SESSION_(addref, session);
+}
+
+void afb_hook_session_unref(struct afb_session *session)
+{
+ _HOOK_SESSION_(unref, session);
+}
+
+
+/******************************************************************************
+ * section: hooking sessions (session)
+ *****************************************************************************/
+
+struct afb_hook_session *afb_hook_create_session(const char *pattern, int flags, struct afb_hook_session_itf *itf, void *closure)
+{
+ struct afb_hook_session *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_session_default_itf;
+ hook->closure = closure;
+
+ /* record the hook */
+ pthread_mutex_lock(&mutex);
+ hook->next = list_of_session_hooks;
+ list_of_session_hooks = hook;
+ pthread_mutex_unlock(&mutex);
+
+ /* returns it */
+ return hook;
+}
+
+struct afb_hook_session *afb_hook_addref_session(struct afb_hook_session *hook)
+{
+ __atomic_add_fetch(&hook->refcount, 1, __ATOMIC_RELAXED);
+ return hook;
+}
+
+static void hook_clean_session()
+{
+ struct afb_hook_session **prv, *hook, *head;
+
+ if (pthread_rwlock_trywrlock(&rwlock) == 0) {
+ /* unlink under mutex */
+ head = NULL;
+ pthread_mutex_lock(&mutex);
+ prv = &list_of_session_hooks;
+ while ((hook = *prv)) {
+ if (hook->refcount)