2 * Copyright (C) 2016, 2017, 2018 "IoT.bzh"
3 * Author José Bollo <jose.bollo@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
28 #include <json-c/json.h>
30 #include <afb/afb-req.h>
31 #include <afb/afb-event.h>
33 #include "afb-context.h"
35 #include "afb-session.h"
38 #include "afb-export.h"
44 #define MATCH(pattern,string) (!fnmatch((pattern),(string),FNM_CASEFOLD|FNM_EXTMATCH))
47 * Definition of a hook for xreq
49 struct afb_hook_xreq {
50 struct afb_hook_xreq *next; /**< next hook */
51 unsigned refcount; /**< reference count */
52 unsigned flags; /**< hook flags */
53 char *api; /**< api hooked or NULL for any */
54 char *verb; /**< verb hooked or NULL for any */
55 struct afb_session *session; /**< session hooked or NULL if any */
56 struct afb_hook_xreq_itf *itf; /**< interface of hook */
57 void *closure; /**< closure for callbacks */
61 * Definition of a hook for export
63 struct afb_hook_ditf {
64 struct afb_hook_ditf *next; /**< next hook */
65 unsigned refcount; /**< reference count */
66 unsigned flags; /**< hook flags */
67 char *api; /**< api hooked or NULL for any */
68 struct afb_hook_ditf_itf *itf; /**< interface of hook */
69 void *closure; /**< closure for callbacks */
73 * Definition of a hook for export
76 struct afb_hook_svc *next; /**< next hook */
77 unsigned refcount; /**< reference count */
78 unsigned flags; /**< hook flags */
79 char *api; /**< api hooked or NULL for any */
80 struct afb_hook_svc_itf *itf; /**< interface of hook */
81 void *closure; /**< closure for callbacks */
85 * Definition of a hook for evt
88 struct afb_hook_evt *next; /**< next hook */
89 unsigned refcount; /**< reference count */
90 unsigned flags; /**< hook flags */
91 char *pattern; /**< event pattern name hooked or NULL for any */
92 struct afb_hook_evt_itf *itf; /**< interface of hook */
93 void *closure; /**< closure for callbacks */
97 * Definition of a hook for session
99 struct afb_hook_session {
100 struct afb_hook_session *next; /**< next hook */
101 unsigned refcount; /**< reference count */
102 unsigned flags; /**< hook flags */
103 char *pattern; /**< event pattern name hooked or NULL for any */
104 struct afb_hook_session_itf *itf; /**< interface of hook */
105 void *closure; /**< closure for callbacks */
109 * Definition of a hook for global
111 struct afb_hook_global {
112 struct afb_hook_global *next; /**< next hook */
113 unsigned refcount; /**< reference count */
114 unsigned flags; /**< hook flags */
115 struct afb_hook_global_itf *itf; /**< interface of hook */
116 void *closure; /**< closure for callbacks */
119 /* synchronisation across threads */
120 static pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
122 /* list of hooks for xreq */
123 static struct afb_hook_xreq *list_of_xreq_hooks = NULL;
125 /* list of hooks for export */
126 static struct afb_hook_ditf *list_of_ditf_hooks = NULL;
128 /* list of hooks for export */
129 static struct afb_hook_svc *list_of_svc_hooks = NULL;
131 /* list of hooks for evt */
132 static struct afb_hook_evt *list_of_evt_hooks = NULL;
134 /* list of hooks for session */
135 static struct afb_hook_session *list_of_session_hooks = NULL;
137 /* list of hooks for global */
138 static struct afb_hook_global *list_of_global_hooks = NULL;
141 static unsigned next_hookid = 0;
143 /******************************************************************************
145 *****************************************************************************/
146 static void init_hookid(struct afb_hookid *hookid)
148 hookid->id = __atomic_add_fetch(&next_hookid, 1, __ATOMIC_RELAXED);
149 clock_gettime(CLOCK_REALTIME, &hookid->time);
152 /******************************************************************************
153 * section: default callbacks for tracing requests
154 *****************************************************************************/
156 static char *_pbuf_(const char *fmt, va_list args, char **palloc, char *sbuf, size_t szsbuf, size_t *outlen)
163 rc = vsnprintf(sbuf, szsbuf, fmt, args);
164 if ((size_t)rc >= szsbuf) {
166 sbuf[szsbuf-2] = sbuf[szsbuf-3] = sbuf[szsbuf-4] = '.';
167 rc = vasprintf(palloc, fmt, cp);
172 if (rc >= 0 && outlen)
173 *outlen = (size_t)rc;
177 #if 0 /* old behaviour: use NOTICE */
178 static void _hook_(const char *fmt1, const char *fmt2, va_list arg2, ...)
180 char *tag, *data, *mem1, *mem2, buf1[256], buf2[2000];
183 data = _pbuf_(fmt2, arg2, &mem2, buf2, sizeof buf2, NULL);
185 va_start(arg1, arg2);
186 tag = _pbuf_(fmt1, arg1, &mem1, buf1, sizeof buf1, NULL);
189 NOTICE("[HOOK %s] %s", tag, data);
194 #else /* new behaviour: emits directly to stderr */
195 static void _hook_(const char *fmt1, const char *fmt2, va_list arg2, ...)
197 static const char chars[] = "HOOK: [] \n";
198 char *mem1, *mem2, buf1[256], buf2[2000];
202 iov[0].iov_base = (void*)&chars[0];
205 va_start(arg1, arg2);
206 iov[1].iov_base = _pbuf_(fmt1, arg1, &mem1, buf1, sizeof buf1, &iov[1].iov_len);
209 iov[2].iov_base = (void*)&chars[7];
212 iov[3].iov_base = _pbuf_(fmt2, arg2, &mem2, buf2, sizeof buf2, &iov[3].iov_len);
214 iov[4].iov_base = (void*)&chars[9];
224 static void _hook_xreq_(const struct afb_xreq *xreq, const char *format, ...)
227 va_start(ap, format);
228 _hook_("xreq-%06d:%s/%s", format, ap, xreq->hookindex, xreq->request.api, xreq->request.verb);
232 static void hook_xreq_begin_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
235 _hook_xreq_(xreq, "BEGIN");
237 _hook_xreq_(xreq, "BEGIN uid=%d=%s gid=%d pid=%d label=%s id=%s",
238 (int)xreq->cred->uid,
240 (int)xreq->cred->gid,
241 (int)xreq->cred->pid,
242 xreq->cred->label?:"(null)",
243 xreq->cred->id?:"(null)"
247 static void hook_xreq_end_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
249 _hook_xreq_(xreq, "END");
252 static void hook_xreq_json_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *obj)
254 _hook_xreq_(xreq, "json() -> %s", json_object_to_json_string(obj));
257 static void hook_xreq_get_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *name, struct afb_arg arg)
259 _hook_xreq_(xreq, "get(%s) -> { name: %s, value: %s, path: %s }", name, arg.name, arg.value, arg.path);
262 static void hook_xreq_success_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *obj, const char *info)
264 _hook_xreq_(xreq, "success(%s, %s)", json_object_to_json_string(obj), info);
267 static void hook_xreq_fail_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *status, const char *info)
269 _hook_xreq_(xreq, "fail(%s, %s)", status, info);
272 static void hook_xreq_context_get_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, void *value)
274 _hook_xreq_(xreq, "context_get() -> %p", value);
277 static void hook_xreq_context_set_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, void *value, void (*free_value)(void*))
279 _hook_xreq_(xreq, "context_set(%p, %p)", value, free_value);
282 static void hook_xreq_addref_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
284 _hook_xreq_(xreq, "addref()");
287 static void hook_xreq_unref_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
289 _hook_xreq_(xreq, "unref()");
292 static void hook_xreq_session_close_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
294 _hook_xreq_(xreq, "session_close()");
297 static void hook_xreq_session_set_LOA_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, unsigned level, int result)
299 _hook_xreq_(xreq, "session_set_LOA(%u) -> %d", level, result);
302 static void hook_xreq_subscribe_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_eventid *eventid, int result)
304 _hook_xreq_(xreq, "subscribe(%s:%d) -> %d", afb_evt_eventid_fullname(eventid), afb_evt_eventid_id(eventid), result);
307 static void hook_xreq_unsubscribe_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_eventid *eventid, int result)
309 _hook_xreq_(xreq, "unsubscribe(%s:%d) -> %d", afb_evt_eventid_fullname(eventid), afb_evt_eventid_id(eventid), result);
312 static void hook_xreq_subcall_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
314 _hook_xreq_(xreq, "subcall(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
317 static void hook_xreq_subcall_result_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int status, struct json_object *result)
319 _hook_xreq_(xreq, " ...subcall... -> %d: %s", status, json_object_to_json_string(result));
322 static void hook_xreq_subcallsync_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
324 _hook_xreq_(xreq, "subcallsync(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
327 static void hook_xreq_subcallsync_result_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int status, struct json_object *result)
329 _hook_xreq_(xreq, " ...subcallsync... -> %d: %s", status, json_object_to_json_string(result));
332 static void hook_xreq_vverbose_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
339 len = vasprintf(&msg, fmt, ap);
343 _hook_xreq_(xreq, "vverbose(%d, %s, %d, %s) -> %s ? ? ?", level, file, line, func, fmt);
345 _hook_xreq_(xreq, "vverbose(%d, %s, %d, %s) -> %s", level, file, line, func, msg);
350 static void hook_xreq_store_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_stored_req *sreq)
352 _hook_xreq_(xreq, "store() -> %p", sreq);
355 static void hook_xreq_unstore_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
357 _hook_xreq_(xreq, "unstore()");
360 static void hook_xreq_subcall_req_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
362 _hook_xreq_(xreq, "subcall_req(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
365 static void hook_xreq_subcall_req_result_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int status, struct json_object *result)
367 _hook_xreq_(xreq, " ...subcall_req... -> %d: %s", status, json_object_to_json_string(result));
370 static void hook_xreq_has_permission_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *permission, int result)
372 _hook_xreq_(xreq, "has_permission(%s) -> %d", permission, result);
375 static void hook_xreq_get_application_id_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, char *result)
377 _hook_xreq_(xreq, "get_application_id() -> %s", result);
380 static void hook_xreq_context_make_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int replace, void *(*create_value)(void*), void (*free_value)(void*), void *create_closure, void *result)
382 _hook_xreq_(xreq, "context_make(replace=%s, %p, %p, %p) -> %p", replace?"yes":"no", create_value, free_value, create_closure, result);
385 static void hook_xreq_get_uid_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int result)
387 _hook_xreq_(xreq, "get_uid() -> %d", result);
390 static struct afb_hook_xreq_itf hook_xreq_default_itf = {
391 .hook_xreq_begin = hook_xreq_begin_default_cb,
392 .hook_xreq_end = hook_xreq_end_default_cb,
393 .hook_xreq_json = hook_xreq_json_default_cb,
394 .hook_xreq_get = hook_xreq_get_default_cb,
395 .hook_xreq_success = hook_xreq_success_default_cb,
396 .hook_xreq_fail = hook_xreq_fail_default_cb,
397 .hook_xreq_context_get = hook_xreq_context_get_default_cb,
398 .hook_xreq_context_set = hook_xreq_context_set_default_cb,
399 .hook_xreq_addref = hook_xreq_addref_default_cb,
400 .hook_xreq_unref = hook_xreq_unref_default_cb,
401 .hook_xreq_session_close = hook_xreq_session_close_default_cb,
402 .hook_xreq_session_set_LOA = hook_xreq_session_set_LOA_default_cb,
403 .hook_xreq_subscribe = hook_xreq_subscribe_default_cb,
404 .hook_xreq_unsubscribe = hook_xreq_unsubscribe_default_cb,
405 .hook_xreq_subcall = hook_xreq_subcall_default_cb,
406 .hook_xreq_subcall_result = hook_xreq_subcall_result_default_cb,
407 .hook_xreq_subcallsync = hook_xreq_subcallsync_default_cb,
408 .hook_xreq_subcallsync_result = hook_xreq_subcallsync_result_default_cb,
409 .hook_xreq_vverbose = hook_xreq_vverbose_default_cb,
410 .hook_xreq_store = hook_xreq_store_default_cb,
411 .hook_xreq_unstore = hook_xreq_unstore_default_cb,
412 .hook_xreq_subcall_req = hook_xreq_subcall_req_default_cb,
413 .hook_xreq_subcall_req_result = hook_xreq_subcall_req_result_default_cb,
414 .hook_xreq_has_permission = hook_xreq_has_permission_default_cb,
415 .hook_xreq_get_application_id = hook_xreq_get_application_id_default_cb,
416 .hook_xreq_context_make = hook_xreq_context_make_default_cb,
417 .hook_xreq_get_uid = hook_xreq_get_uid_default_cb,
420 /******************************************************************************
421 * section: hooks for tracing requests
422 *****************************************************************************/
424 #define _HOOK_XREQ_(what,...) \
425 struct afb_hook_xreq *hook; \
426 struct afb_hookid hookid; \
427 pthread_rwlock_rdlock(&rwlock); \
428 init_hookid(&hookid); \
429 hook = list_of_xreq_hooks; \
431 if (hook->itf->hook_xreq_##what \
432 && (hook->flags & afb_hook_flag_req_##what) != 0 \
433 && (!hook->session || hook->session == xreq->context.session) \
434 && (!hook->api || !strcasecmp(hook->api, xreq->request.api)) \
435 && (!hook->verb || !strcasecmp(hook->verb, xreq->request.verb))) { \
436 hook->itf->hook_xreq_##what(hook->closure, &hookid, __VA_ARGS__); \
440 pthread_rwlock_unlock(&rwlock);
443 void afb_hook_xreq_begin(const struct afb_xreq *xreq)
445 _HOOK_XREQ_(begin, xreq);
448 void afb_hook_xreq_end(const struct afb_xreq *xreq)
450 _HOOK_XREQ_(end, xreq);
453 struct json_object *afb_hook_xreq_json(const struct afb_xreq *xreq, struct json_object *obj)
455 _HOOK_XREQ_(json, xreq, obj);
459 struct afb_arg afb_hook_xreq_get(const struct afb_xreq *xreq, const char *name, struct afb_arg arg)
461 _HOOK_XREQ_(get, xreq, name, arg);
465 void afb_hook_xreq_success(const struct afb_xreq *xreq, struct json_object *obj, const char *info)
467 _HOOK_XREQ_(success, xreq, obj, info);
470 void afb_hook_xreq_fail(const struct afb_xreq *xreq, const char *status, const char *info)
472 _HOOK_XREQ_(fail, xreq, status, info);
475 void *afb_hook_xreq_context_get(const struct afb_xreq *xreq, void *value)
477 _HOOK_XREQ_(context_get, xreq, value);
481 void afb_hook_xreq_context_set(const struct afb_xreq *xreq, void *value, void (*free_value)(void*))
483 _HOOK_XREQ_(context_set, xreq, value, free_value);
486 void afb_hook_xreq_addref(const struct afb_xreq *xreq)
488 _HOOK_XREQ_(addref, xreq);
491 void afb_hook_xreq_unref(const struct afb_xreq *xreq)
493 _HOOK_XREQ_(unref, xreq);
496 void afb_hook_xreq_session_close(const struct afb_xreq *xreq)
498 _HOOK_XREQ_(session_close, xreq);
501 int afb_hook_xreq_session_set_LOA(const struct afb_xreq *xreq, unsigned level, int result)
503 _HOOK_XREQ_(session_set_LOA, xreq, level, result);
507 int afb_hook_xreq_subscribe(const struct afb_xreq *xreq, struct afb_eventid *eventid, int result)
509 _HOOK_XREQ_(subscribe, xreq, eventid, result);
513 int afb_hook_xreq_unsubscribe(const struct afb_xreq *xreq, struct afb_eventid *eventid, int result)
515 _HOOK_XREQ_(unsubscribe, xreq, eventid, result);
519 void afb_hook_xreq_subcall(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
521 _HOOK_XREQ_(subcall, xreq, api, verb, args);
524 void afb_hook_xreq_subcall_result(const struct afb_xreq *xreq, int status, struct json_object *result)
526 _HOOK_XREQ_(subcall_result, xreq, status, result);
529 void afb_hook_xreq_subcallsync(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
531 _HOOK_XREQ_(subcallsync, xreq, api, verb, args);
534 int afb_hook_xreq_subcallsync_result(const struct afb_xreq *xreq, int status, struct json_object *result)
536 _HOOK_XREQ_(subcallsync_result, xreq, status, result);
540 void afb_hook_xreq_vverbose(const struct afb_xreq *xreq, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
542 _HOOK_XREQ_(vverbose, xreq, level, file ?: "?", line, func ?: "?", fmt, args);
545 void afb_hook_xreq_store(const struct afb_xreq *xreq, struct afb_stored_req *sreq)
547 _HOOK_XREQ_(store, xreq, sreq);
550 void afb_hook_xreq_unstore(const struct afb_xreq *xreq)
552 _HOOK_XREQ_(unstore, xreq);
555 void afb_hook_xreq_subcall_req(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
557 _HOOK_XREQ_(subcall_req, xreq, api, verb, args);
560 void afb_hook_xreq_subcall_req_result(const struct afb_xreq *xreq, int status, struct json_object *result)
562 _HOOK_XREQ_(subcall_req_result, xreq, status, result);
565 int afb_hook_xreq_has_permission(const struct afb_xreq *xreq, const char *permission, int result)
567 _HOOK_XREQ_(has_permission, xreq, permission, result);
571 char *afb_hook_xreq_get_application_id(const struct afb_xreq *xreq, char *result)
573 _HOOK_XREQ_(get_application_id, xreq, result);
577 void *afb_hook_xreq_context_make(const struct afb_xreq *xreq, int replace, void *(*create_value)(void*), void (*free_value)(void*), void *create_closure, void *result)
579 _HOOK_XREQ_(context_make, xreq, replace, create_value, free_value, create_closure, result);
583 int afb_hook_xreq_get_uid(const struct afb_xreq *xreq, int result)
585 _HOOK_XREQ_(get_uid, xreq, result);
589 /******************************************************************************
590 * section: hooking xreqs
591 *****************************************************************************/
593 void afb_hook_init_xreq(struct afb_xreq *xreq)
599 struct afb_hook_xreq *hook;
601 /* scan hook list to get the expected flags */
603 if (afb_api_is_hookable(xreq->request.api)) {
604 pthread_rwlock_rdlock(&rwlock);
605 hook = list_of_xreq_hooks;
607 f = hook->flags & afb_hook_flags_req_all;
609 && (!hook->session || hook->session == xreq->context.session)
610 && (!hook->api || !strcasecmp(hook->api, xreq->request.api))
611 && (!hook->verb || !strcasecmp(hook->verb, xreq->request.verb));
616 pthread_rwlock_unlock(&rwlock);
619 /* store the hooking data */
620 xreq->hookflags = flags;
622 pthread_rwlock_wrlock(&rwlock);
625 xreq->hookindex = reqindex;
626 pthread_rwlock_unlock(&rwlock);
630 struct afb_hook_xreq *afb_hook_create_xreq(const char *api, const char *verb, struct afb_session *session, int flags, struct afb_hook_xreq_itf *itf, void *closure)
632 struct afb_hook_xreq *hook;
634 /* alloc the result */
635 hook = calloc(1, sizeof *hook);
639 /* get a copy of the names */
640 hook->api = api ? strdup(api) : NULL;
641 hook->verb = verb ? strdup(verb) : NULL;
642 if ((api && !hook->api) || (verb && !hook->verb)) {
649 /* initialise the rest */
650 hook->session = session;
652 afb_session_addref(session);
655 hook->itf = itf ? itf : &hook_xreq_default_itf;
656 hook->closure = closure;
658 /* record the hook */
659 pthread_rwlock_wrlock(&rwlock);
660 hook->next = list_of_xreq_hooks;
661 list_of_xreq_hooks = hook;
662 pthread_rwlock_unlock(&rwlock);
668 struct afb_hook_xreq *afb_hook_addref_xreq(struct afb_hook_xreq *hook)
670 pthread_rwlock_wrlock(&rwlock);
672 pthread_rwlock_unlock(&rwlock);
676 void afb_hook_unref_xreq(struct afb_hook_xreq *hook)
678 struct afb_hook_xreq **prv;
681 pthread_rwlock_wrlock(&rwlock);
682 if (--hook->refcount)
686 prv = &list_of_xreq_hooks;
687 while (*prv && *prv != hook)
692 pthread_rwlock_unlock(&rwlock);
698 afb_session_unref(hook->session);
704 /******************************************************************************
705 * section: default callbacks for tracing daemon interface
706 *****************************************************************************/
708 static void _hook_ditf_(const struct afb_export *export, const char *format, ...)
711 va_start(ap, format);
712 _hook_("export-%s", format, ap, afb_export_apiname(export));
716 static void hook_ditf_event_broadcast_before_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, struct json_object *object)
718 _hook_ditf_(export, "event_broadcast.before(%s, %s)....", name, json_object_to_json_string(object));
721 static void hook_ditf_event_broadcast_after_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, struct json_object *object, int result)
723 _hook_ditf_(export, "event_broadcast.after(%s, %s) -> %d", name, json_object_to_json_string(object), result);
726 static void hook_ditf_get_event_loop_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_event *result)
728 _hook_ditf_(export, "get_event_loop() -> %p", result);
731 static void hook_ditf_get_user_bus_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_bus *result)
733 _hook_ditf_(export, "get_user_bus() -> %p", result);
736 static void hook_ditf_get_system_bus_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_bus *result)
738 _hook_ditf_(export, "get_system_bus() -> %p", result);
741 static void hook_ditf_vverbose_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int level, const char *file, int line, const char *function, const char *fmt, va_list args)
748 len = vasprintf(&msg, fmt, ap);
752 _hook_ditf_(export, "vverbose(%d, %s, %d, %s) -> %s ? ? ?", level, file, line, function, fmt);
754 _hook_ditf_(export, "vverbose(%d, %s, %d, %s) -> %s", level, file, line, function, msg);
759 static void hook_ditf_event_make_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, struct afb_eventid *result)
761 _hook_ditf_(export, "event_make(%s) -> %s:%d", name, afb_evt_eventid_fullname(result), afb_evt_eventid_id(result));
764 static void hook_ditf_rootdir_get_fd_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result)
768 _hook_ditf_(export, "rootdir_get_fd() -> %d, %m", result);
770 sprintf(path, "/proc/self/fd/%d", result);
771 readlink(path, path, sizeof path);
772 _hook_ditf_(export, "rootdir_get_fd() -> %d = %s", result, path);
776 static void hook_ditf_rootdir_open_locale_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *filename, int flags, const char *locale, int result)
782 _hook_ditf_(export, "rootdir_open_locale(%s, %d, %s) -> %d, %m", filename, flags, locale, result);
784 sprintf(path, "/proc/self/fd/%d", result);
785 readlink(path, path, sizeof path);
786 _hook_ditf_(export, "rootdir_open_locale(%s, %d, %s) -> %d = %s", filename, flags, locale, result, path);
790 static void hook_ditf_queue_job_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout, int result)
792 _hook_ditf_(export, "queue_job(%p, %p, %p, %d) -> %d", callback, argument, group, timeout, result);
795 static void hook_ditf_unstore_req_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct afb_stored_req *sreq)
797 _hook_ditf_(export, "unstore_req(%p)", sreq);
800 static void hook_ditf_require_api_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, int initialized)
802 _hook_ditf_(export, "require_api(%s, %d)...", name, initialized);
805 static void hook_ditf_require_api_result_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, int initialized, int result)
807 _hook_ditf_(export, "...require_api(%s, %d) -> %d", name, initialized, result);
810 static void hook_ditf_rename_api_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *oldname, const char *newname, int result)
812 _hook_ditf_(export, "rename_api(%s -> %s) -> %d", oldname, newname, result);
815 static struct afb_hook_ditf_itf hook_ditf_default_itf = {
816 .hook_ditf_event_broadcast_before = hook_ditf_event_broadcast_before_cb,
817 .hook_ditf_event_broadcast_after = hook_ditf_event_broadcast_after_cb,
818 .hook_ditf_get_event_loop = hook_ditf_get_event_loop_cb,
819 .hook_ditf_get_user_bus = hook_ditf_get_user_bus_cb,
820 .hook_ditf_get_system_bus = hook_ditf_get_system_bus_cb,
821 .hook_ditf_vverbose = hook_ditf_vverbose_cb,
822 .hook_ditf_event_make = hook_ditf_event_make_cb,
823 .hook_ditf_rootdir_get_fd = hook_ditf_rootdir_get_fd_cb,
824 .hook_ditf_rootdir_open_locale = hook_ditf_rootdir_open_locale_cb,
825 .hook_ditf_queue_job = hook_ditf_queue_job_cb,
826 .hook_ditf_unstore_req = hook_ditf_unstore_req_cb,
827 .hook_ditf_require_api = hook_ditf_require_api_cb,
828 .hook_ditf_require_api_result = hook_ditf_require_api_result_cb,
829 .hook_ditf_rename_api = hook_ditf_rename_api_cb
832 /******************************************************************************
833 * section: hooks for tracing daemon interface (export)
834 *****************************************************************************/
836 #define _HOOK_DITF_(what,...) \
837 struct afb_hook_ditf *hook; \
838 struct afb_hookid hookid; \
839 const char *apiname = afb_export_apiname(export); \
840 pthread_rwlock_rdlock(&rwlock); \
841 init_hookid(&hookid); \
842 hook = list_of_ditf_hooks; \
844 if (hook->itf->hook_ditf_##what \
845 && (hook->flags & afb_hook_flag_ditf_##what) != 0 \
846 && (!hook->api || !strcasecmp(hook->api, apiname))) { \
847 hook->itf->hook_ditf_##what(hook->closure, &hookid, __VA_ARGS__); \
851 pthread_rwlock_unlock(&rwlock);
853 void afb_hook_ditf_event_broadcast_before(const struct afb_export *export, const char *name, struct json_object *object)
855 _HOOK_DITF_(event_broadcast_before, export, name, object);
858 int afb_hook_ditf_event_broadcast_after(const struct afb_export *export, const char *name, struct json_object *object, int result)
860 _HOOK_DITF_(event_broadcast_after, export, name, object, result);
864 struct sd_event *afb_hook_ditf_get_event_loop(const struct afb_export *export, struct sd_event *result)
866 _HOOK_DITF_(get_event_loop, export, result);
870 struct sd_bus *afb_hook_ditf_get_user_bus(const struct afb_export *export, struct sd_bus *result)
872 _HOOK_DITF_(get_user_bus, export, result);
876 struct sd_bus *afb_hook_ditf_get_system_bus(const struct afb_export *export, struct sd_bus *result)
878 _HOOK_DITF_(get_system_bus, export, result);
882 void afb_hook_ditf_vverbose(const struct afb_export *export, int level, const char *file, int line, const char *function, const char *fmt, va_list args)
884 _HOOK_DITF_(vverbose, export, level, file, line, function, fmt, args);
887 struct afb_eventid *afb_hook_ditf_event_make(const struct afb_export *export, const char *name, struct afb_eventid *result)
889 _HOOK_DITF_(event_make, export, name, result);
893 int afb_hook_ditf_rootdir_get_fd(const struct afb_export *export, int result)
895 _HOOK_DITF_(rootdir_get_fd, export, result);
899 int afb_hook_ditf_rootdir_open_locale(const struct afb_export *export, const char *filename, int flags, const char *locale, int result)
901 _HOOK_DITF_(rootdir_open_locale, export, filename, flags, locale, result);
905 int afb_hook_ditf_queue_job(const struct afb_export *export, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout, int result)
907 _HOOK_DITF_(queue_job, export, callback, argument, group, timeout, result);
911 void afb_hook_ditf_unstore_req(const struct afb_export *export, struct afb_stored_req *sreq)
913 _HOOK_DITF_(unstore_req, export, sreq);
916 void afb_hook_ditf_require_api(const struct afb_export *export, const char *name, int initialized)
918 _HOOK_DITF_(require_api, export, name, initialized);
921 int afb_hook_ditf_require_api_result(const struct afb_export *export, const char *name, int initialized, int result)
923 _HOOK_DITF_(require_api_result, export, name, initialized, result);
927 int afb_hook_ditf_rename_api(const struct afb_export *export, const char *oldname, const char *newname, int result)
929 _HOOK_DITF_(rename_api, export, oldname, newname, result);
933 /******************************************************************************
934 * section: hooking export
935 *****************************************************************************/
937 int afb_hook_flags_ditf(const char *api)
940 struct afb_hook_ditf *hook;
943 if (!api || afb_api_is_hookable(api)) {
944 pthread_rwlock_rdlock(&rwlock);
945 hook = list_of_ditf_hooks;
947 if (!api || !hook->api || !strcasecmp(hook->api, api))
948 flags |= hook->flags;
951 pthread_rwlock_unlock(&rwlock);
956 struct afb_hook_ditf *afb_hook_create_ditf(const char *api, int flags, struct afb_hook_ditf_itf *itf, void *closure)
958 struct afb_hook_ditf *hook;
960 /* alloc the result */
961 hook = calloc(1, sizeof *hook);
965 /* get a copy of the names */
966 hook->api = api ? strdup(api) : NULL;
967 if (api && !hook->api) {
972 /* initialise the rest */
975 hook->itf = itf ? itf : &hook_ditf_default_itf;
976 hook->closure = closure;
978 /* record the hook */
979 pthread_rwlock_wrlock(&rwlock);
980 hook->next = list_of_ditf_hooks;
981 list_of_ditf_hooks = hook;
982 pthread_rwlock_unlock(&rwlock);
988 struct afb_hook_ditf *afb_hook_addref_ditf(struct afb_hook_ditf *hook)
990 pthread_rwlock_wrlock(&rwlock);
992 pthread_rwlock_unlock(&rwlock);
996 void afb_hook_unref_ditf(struct afb_hook_ditf *hook)
998 struct afb_hook_ditf **prv;
1001 pthread_rwlock_wrlock(&rwlock);
1002 if (--hook->refcount)
1006 prv = &list_of_ditf_hooks;
1007 while (*prv && *prv != hook)
1008 prv = &(*prv)->next;
1012 pthread_rwlock_unlock(&rwlock);
1021 /******************************************************************************
1022 * section: default callbacks for tracing service interface (export)
1023 *****************************************************************************/
1025 static void _hook_svc_(const struct afb_export *export, const char *format, ...)
1028 va_start(ap, format);
1029 _hook_("export-%s", format, ap, afb_export_apiname(export));
1033 static void hook_svc_start_before_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export)
1035 _hook_svc_(export, "start.before");
1038 static void hook_svc_start_after_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int status)
1040 _hook_svc_(export, "start.after -> %d", status);
1043 static void hook_svc_on_event_before_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *event, int eventid, struct json_object *object)
1045 _hook_svc_(export, "on_event.before(%s, %d, %s)", event, eventid, json_object_to_json_string(object));
1048 static void hook_svc_on_event_after_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *event, int eventid, struct json_object *object)
1050 _hook_svc_(export, "on_event.after(%s, %d, %s)", event, eventid, json_object_to_json_string(object));
1053 static void hook_svc_call_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *api, const char *verb, struct json_object *args)
1055 _hook_svc_(export, "call(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
1058 static void hook_svc_call_result_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int status, struct json_object *result)
1060 _hook_svc_(export, " ...call... -> %d: %s", status, json_object_to_json_string(result));
1063 static void hook_svc_callsync_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *api, const char *verb, struct json_object *args)
1065 _hook_svc_(export, "callsync(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
1068 static void hook_svc_callsync_result_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int status, struct json_object *result)
1070 _hook_svc_(export, " ...callsync... -> %d: %s", status, json_object_to_json_string(result));
1073 static struct afb_hook_svc_itf hook_svc_default_itf = {
1074 .hook_svc_start_before = hook_svc_start_before_default_cb,
1075 .hook_svc_start_after = hook_svc_start_after_default_cb,
1076 .hook_svc_on_event_before = hook_svc_on_event_before_default_cb,
1077 .hook_svc_on_event_after = hook_svc_on_event_after_default_cb,
1078 .hook_svc_call = hook_svc_call_default_cb,
1079 .hook_svc_call_result = hook_svc_call_result_default_cb,
1080 .hook_svc_callsync = hook_svc_callsync_default_cb,
1081 .hook_svc_callsync_result = hook_svc_callsync_result_default_cb
1084 /******************************************************************************
1085 * section: hooks for tracing service interface (export)
1086 *****************************************************************************/
1088 #define _HOOK_SVC_(what,...) \
1089 struct afb_hook_svc *hook; \
1090 struct afb_hookid hookid; \
1091 const char *apiname = afb_export_apiname(export); \
1092 pthread_rwlock_rdlock(&rwlock); \
1093 init_hookid(&hookid); \
1094 hook = list_of_svc_hooks; \
1096 if (hook->itf->hook_svc_##what \
1097 && (hook->flags & afb_hook_flag_svc_##what) != 0 \
1098 && (!hook->api || !strcasecmp(hook->api, apiname))) { \
1099 hook->itf->hook_svc_##what(hook->closure, &hookid, __VA_ARGS__); \
1101 hook = hook->next; \
1103 pthread_rwlock_unlock(&rwlock);
1105 void afb_hook_svc_start_before(const struct afb_export *export)
1107 _HOOK_SVC_(start_before, export);
1110 int afb_hook_svc_start_after(const struct afb_export *export, int status)
1112 _HOOK_SVC_(start_after, export, status);
1116 void afb_hook_svc_on_event_before(const struct afb_export *export, const char *event, int eventid, struct json_object *object)
1118 _HOOK_SVC_(on_event_before, export, event, eventid, object);
1121 void afb_hook_svc_on_event_after(const struct afb_export *export, const char *event, int eventid, struct json_object *object)
1123 _HOOK_SVC_(on_event_after, export, event, eventid, object);
1126 void afb_hook_svc_call(const struct afb_export *export, const char *api, const char *verb, struct json_object *args)
1128 _HOOK_SVC_(call, export, api, verb, args);
1131 void afb_hook_svc_call_result(const struct afb_export *export, int status, struct json_object *result)
1133 _HOOK_SVC_(call_result, export, status, result);
1136 void afb_hook_svc_callsync(const struct afb_export *export, const char *api, const char *verb, struct json_object *args)
1138 _HOOK_SVC_(callsync, export, api, verb, args);
1141 int afb_hook_svc_callsync_result(const struct afb_export *export, int status, struct json_object *result)
1143 _HOOK_SVC_(callsync_result, export, status, result);
1147 /******************************************************************************
1148 * section: hooking services (export)
1149 *****************************************************************************/
1151 int afb_hook_flags_svc(const char *api)
1154 struct afb_hook_svc *hook;
1157 if (!api || afb_api_is_hookable(api)) {
1158 pthread_rwlock_rdlock(&rwlock);
1159 hook = list_of_svc_hooks;
1161 if (!api || !hook->api || !strcasecmp(hook->api, api))
1162 flags |= hook->flags;
1165 pthread_rwlock_unlock(&rwlock);
1170 struct afb_hook_svc *afb_hook_create_svc(const char *api, int flags, struct afb_hook_svc_itf *itf, void *closure)
1172 struct afb_hook_svc *hook;
1174 /* alloc the result */
1175 hook = calloc(1, sizeof *hook);
1179 /* get a copy of the names */
1180 hook->api = api ? strdup(api) : NULL;
1181 if (api && !hook->api) {
1186 /* initialise the rest */
1188 hook->flags = flags;
1189 hook->itf = itf ? itf : &hook_svc_default_itf;
1190 hook->closure = closure;
1192 /* record the hook */
1193 pthread_rwlock_wrlock(&rwlock);
1194 hook->next = list_of_svc_hooks;
1195 list_of_svc_hooks = hook;
1196 pthread_rwlock_unlock(&rwlock);
1202 struct afb_hook_svc *afb_hook_addref_svc(struct afb_hook_svc *hook)
1204 pthread_rwlock_wrlock(&rwlock);
1206 pthread_rwlock_unlock(&rwlock);
1210 void afb_hook_unref_svc(struct afb_hook_svc *hook)
1212 struct afb_hook_svc **prv;
1215 pthread_rwlock_wrlock(&rwlock);
1216 if (--hook->refcount)
1220 prv = &list_of_svc_hooks;
1221 while (*prv && *prv != hook)
1222 prv = &(*prv)->next;
1226 pthread_rwlock_unlock(&rwlock);
1235 /******************************************************************************
1236 * section: default callbacks for tracing service interface (evt)
1237 *****************************************************************************/
1239 static void _hook_evt_(const char *evt, int id, const char *format, ...)
1242 va_start(ap, format);
1243 _hook_("evt-%s:%d", format, ap, evt, id);
1247 static void hook_evt_create_default_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
1249 _hook_evt_(evt, id, "create");
1252 static void hook_evt_push_before_default_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj)
1254 _hook_evt_(evt, id, "push.before(%s)", json_object_to_json_string(obj));
1258 static void hook_evt_push_after_default_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj, int result)
1260 _hook_evt_(evt, id, "push.after(%s) -> %d", json_object_to_json_string(obj), result);
1263 static void hook_evt_broadcast_before_default_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj)
1265 _hook_evt_(evt, id, "broadcast.before(%s)", json_object_to_json_string(obj));
1268 static void hook_evt_broadcast_after_default_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj, int result)
1270 _hook_evt_(evt, id, "broadcast.after(%s) -> %d", json_object_to_json_string(obj), result);
1273 static void hook_evt_name_default_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, const char *result)
1275 _hook_evt_(evt, id, "name -> %s", result);
1278 static void hook_evt_addref_default_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
1280 _hook_evt_(evt, id, "addref");
1283 static void hook_evt_unref_default_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
1285 _hook_evt_(evt, id, "unref");
1288 static struct afb_hook_evt_itf hook_evt_default_itf = {
1289 .hook_evt_create = hook_evt_create_default_cb,
1290 .hook_evt_push_before = hook_evt_push_before_default_cb,
1291 .hook_evt_push_after = hook_evt_push_after_default_cb,
1292 .hook_evt_broadcast_before = hook_evt_broadcast_before_default_cb,
1293 .hook_evt_broadcast_after = hook_evt_broadcast_after_default_cb,
1294 .hook_evt_name = hook_evt_name_default_cb,
1295 .hook_evt_addref = hook_evt_addref_default_cb,
1296 .hook_evt_unref = hook_evt_unref_default_cb
1299 /******************************************************************************
1300 * section: hooks for tracing events interface (evt)
1301 *****************************************************************************/
1303 #define _HOOK_EVT_(what,...) \
1304 struct afb_hook_evt *hook; \
1305 struct afb_hookid hookid; \
1306 pthread_rwlock_rdlock(&rwlock); \
1307 init_hookid(&hookid); \
1308 hook = list_of_evt_hooks; \
1310 if (hook->itf->hook_evt_##what \
1311 && (hook->flags & afb_hook_flag_evt_##what) != 0 \
1312 && (!hook->pattern || MATCH(hook->pattern, evt))) { \
1313 hook->itf->hook_evt_##what(hook->closure, &hookid, __VA_ARGS__); \
1315 hook = hook->next; \
1317 pthread_rwlock_unlock(&rwlock);
1319 void afb_hook_evt_create(const char *evt, int id)
1321 _HOOK_EVT_(create, evt, id);
1324 void afb_hook_evt_push_before(const char *evt, int id, struct json_object *obj)
1326 _HOOK_EVT_(push_before, evt, id, obj);
1329 int afb_hook_evt_push_after(const char *evt, int id, struct json_object *obj, int result)
1331 _HOOK_EVT_(push_after, evt, id, obj, result);
1335 void afb_hook_evt_broadcast_before(const char *evt, int id, struct json_object *obj)
1337 _HOOK_EVT_(broadcast_before, evt, id, obj);
1340 int afb_hook_evt_broadcast_after(const char *evt, int id, struct json_object *obj, int result)
1342 _HOOK_EVT_(broadcast_after, evt, id, obj, result);
1346 void afb_hook_evt_name(const char *evt, int id, const char *result)
1348 _HOOK_EVT_(name, evt, id, result);
1351 void afb_hook_evt_addref(const char *evt, int id)
1353 _HOOK_EVT_(addref, evt, id);
1356 void afb_hook_evt_unref(const char *evt, int id)
1358 _HOOK_EVT_(unref, evt, id);
1361 /******************************************************************************
1362 * section: hooking services (evt)
1363 *****************************************************************************/
1365 int afb_hook_flags_evt(const char *name)
1368 struct afb_hook_evt *hook;
1371 if (!name || afb_api_is_hookable(name)) {
1372 pthread_rwlock_rdlock(&rwlock);
1373 hook = list_of_evt_hooks;
1375 if (!name || !hook->pattern || MATCH(hook->pattern, name))
1376 flags |= hook->flags;
1379 pthread_rwlock_unlock(&rwlock);
1384 struct afb_hook_evt *afb_hook_create_evt(const char *pattern, int flags, struct afb_hook_evt_itf *itf, void *closure)
1386 struct afb_hook_evt *hook;
1388 /* alloc the result */
1389 hook = calloc(1, sizeof *hook);
1393 /* get a copy of the names */
1394 hook->pattern = pattern ? strdup(pattern) : NULL;
1395 if (pattern && !hook->pattern) {
1400 /* initialise the rest */
1402 hook->flags = flags;
1403 hook->itf = itf ? itf : &hook_evt_default_itf;
1404 hook->closure = closure;
1406 /* record the hook */
1407 pthread_rwlock_wrlock(&rwlock);
1408 hook->next = list_of_evt_hooks;
1409 list_of_evt_hooks = hook;
1410 pthread_rwlock_unlock(&rwlock);
1416 struct afb_hook_evt *afb_hook_addref_evt(struct afb_hook_evt *hook)
1418 pthread_rwlock_wrlock(&rwlock);
1420 pthread_rwlock_unlock(&rwlock);
1424 void afb_hook_unref_evt(struct afb_hook_evt *hook)
1426 struct afb_hook_evt **prv;
1429 pthread_rwlock_wrlock(&rwlock);
1430 if (--hook->refcount)
1434 prv = &list_of_evt_hooks;
1435 while (*prv && *prv != hook)
1436 prv = &(*prv)->next;
1440 pthread_rwlock_unlock(&rwlock);
1443 free(hook->pattern);
1449 /******************************************************************************
1450 * section: default callbacks for sessions (session)
1451 *****************************************************************************/
1453 static void _hook_session_(struct afb_session *session, const char *format, ...)
1456 va_start(ap, format);
1457 _hook_("session-%s", format, ap, afb_session_uuid(session));
1461 static void hook_session_create_default_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1463 _hook_session_(session, "create -> token=%s", afb_session_token(session));
1466 static void hook_session_close_default_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1468 _hook_session_(session, "close");
1471 static void hook_session_destroy_default_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1473 _hook_session_(session, "destroy");
1476 static void hook_session_renew_default_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1478 _hook_session_(session, "renew -> token=%s", afb_session_token(session));
1481 static void hook_session_addref_default_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1483 _hook_session_(session, "addref");
1486 static void hook_session_unref_default_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1488 _hook_session_(session, "unref");
1491 static struct afb_hook_session_itf hook_session_default_itf = {
1492 .hook_session_create = hook_session_create_default_cb,
1493 .hook_session_close = hook_session_close_default_cb,
1494 .hook_session_destroy = hook_session_destroy_default_cb,
1495 .hook_session_renew = hook_session_renew_default_cb,
1496 .hook_session_addref = hook_session_addref_default_cb,
1497 .hook_session_unref = hook_session_unref_default_cb
1500 /******************************************************************************
1501 * section: hooks for tracing sessions (session)
1502 *****************************************************************************/
1504 #define _HOOK_SESSION_(what,...) \
1505 struct afb_hook_session *hook; \
1506 struct afb_hookid hookid; \
1507 const char *sessid = 0; \
1508 pthread_rwlock_rdlock(&rwlock); \
1509 init_hookid(&hookid); \
1510 hook = list_of_session_hooks; \
1512 if (hook->itf->hook_session_##what \
1513 && (hook->flags & afb_hook_flag_session_##what) != 0 \
1514 && (!hook->pattern || MATCH(hook->pattern, (sessid?:(sessid=afb_session_uuid(session)))))) { \
1515 hook->itf->hook_session_##what(hook->closure, &hookid, __VA_ARGS__); \
1517 hook = hook->next; \
1519 pthread_rwlock_unlock(&rwlock);
1521 void afb_hook_session_create(struct afb_session *session)
1523 _HOOK_SESSION_(create, session);
1526 void afb_hook_session_close(struct afb_session *session)
1528 _HOOK_SESSION_(close, session);
1531 void afb_hook_session_destroy(struct afb_session *session)
1533 _HOOK_SESSION_(destroy, session);
1536 void afb_hook_session_renew(struct afb_session *session)
1538 _HOOK_SESSION_(renew, session);
1541 void afb_hook_session_addref(struct afb_session *session)
1543 _HOOK_SESSION_(addref, session);
1546 void afb_hook_session_unref(struct afb_session *session)
1548 _HOOK_SESSION_(unref, session);
1552 /******************************************************************************
1553 * section: hooking sessions (session)
1554 *****************************************************************************/
1556 struct afb_hook_session *afb_hook_create_session(const char *pattern, int flags, struct afb_hook_session_itf *itf, void *closure)
1558 struct afb_hook_session *hook;
1560 /* alloc the result */
1561 hook = calloc(1, sizeof *hook);
1565 /* get a copy of the names */
1566 hook->pattern = pattern ? strdup(pattern) : NULL;
1567 if (pattern && !hook->pattern) {
1572 /* initialise the rest */
1574 hook->flags = flags;
1575 hook->itf = itf ? itf : &hook_session_default_itf;
1576 hook->closure = closure;
1578 /* record the hook */
1579 pthread_rwlock_wrlock(&rwlock);
1580 hook->next = list_of_session_hooks;
1581 list_of_session_hooks = hook;
1582 pthread_rwlock_unlock(&rwlock);
1588 struct afb_hook_session *afb_hook_addref_session(struct afb_hook_session *hook)
1590 pthread_rwlock_wrlock(&rwlock);
1592 pthread_rwlock_unlock(&rwlock);
1596 void afb_hook_unref_session(struct afb_hook_session *hook)
1598 struct afb_hook_session **prv;
1601 pthread_rwlock_wrlock(&rwlock);
1602 if (--hook->refcount)
1606 prv = &list_of_session_hooks;
1607 while (*prv && *prv != hook)
1608 prv = &(*prv)->next;
1612 pthread_rwlock_unlock(&rwlock);
1615 free(hook->pattern);
1621 /******************************************************************************
1622 * section: default callbacks for globals (global)
1623 *****************************************************************************/
1625 static void _hook_global_(const char *format, ...)
1628 va_start(ap, format);
1629 _hook_("global", format, ap);
1633 static void hook_global_vverbose_default_cb(void *closure, const struct afb_hookid *hookid, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
1640 len = vasprintf(&msg, fmt, ap);
1644 _hook_global_("vverbose(%d, %s, %d, %s) -> %s ? ? ?", level, file, line, func, fmt);
1646 _hook_global_("vverbose(%d, %s, %d, %s) -> %s", level, file, line, func, msg);
1651 static struct afb_hook_global_itf hook_global_default_itf = {
1652 .hook_global_vverbose = hook_global_vverbose_default_cb
1655 /******************************************************************************
1656 * section: hooks for tracing globals (global)
1657 *****************************************************************************/
1659 #define _HOOK_GLOBAL_(what,...) \
1660 struct afb_hook_global *hook; \
1661 struct afb_hookid hookid; \
1662 pthread_rwlock_rdlock(&rwlock); \
1663 init_hookid(&hookid); \
1664 hook = list_of_global_hooks; \
1666 if (hook->itf->hook_global_##what \
1667 && (hook->flags & afb_hook_flag_global_##what) != 0) { \
1668 hook->itf->hook_global_##what(hook->closure, &hookid, __VA_ARGS__); \
1670 hook = hook->next; \
1672 pthread_rwlock_unlock(&rwlock);
1674 static void afb_hook_global_vverbose(int level, const char *file, int line, const char *func, const char *fmt, va_list args)
1676 _HOOK_GLOBAL_(vverbose, level, file ?: "?", line, func ?: "?", fmt ?: "", args);
1679 /******************************************************************************
1680 * section: hooking globals (global)
1681 *****************************************************************************/
1683 static void update_global()
1685 struct afb_hook_global *hook;
1688 pthread_rwlock_rdlock(&rwlock);
1689 hook = list_of_global_hooks;
1691 flags = hook->flags;
1694 verbose_observer = (flags & afb_hook_flag_global_vverbose) ? afb_hook_global_vverbose : NULL;
1695 pthread_rwlock_unlock(&rwlock);
1698 struct afb_hook_global *afb_hook_create_global(int flags, struct afb_hook_global_itf *itf, void *closure)
1700 struct afb_hook_global *hook;
1702 /* alloc the result */
1703 hook = calloc(1, sizeof *hook);
1707 /* initialise the rest */
1709 hook->flags = flags;
1710 hook->itf = itf ? itf : &hook_global_default_itf;
1711 hook->closure = closure;
1713 /* record the hook */
1714 pthread_rwlock_wrlock(&rwlock);
1715 hook->next = list_of_global_hooks;
1716 list_of_global_hooks = hook;
1717 pthread_rwlock_unlock(&rwlock);
1719 /* update hooking */
1726 struct afb_hook_global *afb_hook_addref_global(struct afb_hook_global *hook)
1728 pthread_rwlock_wrlock(&rwlock);
1730 pthread_rwlock_unlock(&rwlock);
1734 void afb_hook_unref_global(struct afb_hook_global *hook)
1736 struct afb_hook_global **prv;
1739 pthread_rwlock_wrlock(&rwlock);
1740 if (--hook->refcount)
1744 prv = &list_of_global_hooks;
1745 while (*prv && *prv != hook)
1746 prv = &(*prv)->next;
1750 pthread_rwlock_unlock(&rwlock);
1755 /* update hooking */