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.
29 #include <json-c/json.h>
31 #include <afb/afb-req.h>
32 #include <afb/afb-event.h>
34 #include "afb-context.h"
36 #include "afb-session.h"
39 #include "afb-export.h"
45 * Definition of a hook for xreq
47 struct afb_hook_xreq {
48 struct afb_hook_xreq *next; /**< next hook */
49 unsigned refcount; /**< reference count */
50 unsigned flags; /**< hook flags */
51 char *api; /**< api hooked or NULL for any */
52 char *verb; /**< verb hooked or NULL for any */
53 struct afb_session *session; /**< session hooked or NULL if any */
54 struct afb_hook_xreq_itf *itf; /**< interface of hook */
55 void *closure; /**< closure for callbacks */
59 * Definition of a hook for export
61 struct afb_hook_ditf {
62 struct afb_hook_ditf *next; /**< next hook */
63 unsigned refcount; /**< reference count */
64 unsigned flags; /**< hook flags */
65 char *api; /**< api hooked or NULL for any */
66 struct afb_hook_ditf_itf *itf; /**< interface of hook */
67 void *closure; /**< closure for callbacks */
71 * Definition of a hook for export
74 struct afb_hook_svc *next; /**< next hook */
75 unsigned refcount; /**< reference count */
76 unsigned flags; /**< hook flags */
77 char *api; /**< api hooked or NULL for any */
78 struct afb_hook_svc_itf *itf; /**< interface of hook */
79 void *closure; /**< closure for callbacks */
83 * Definition of a hook for evt
86 struct afb_hook_evt *next; /**< next hook */
87 unsigned refcount; /**< reference count */
88 unsigned flags; /**< hook flags */
89 char *pattern; /**< event pattern name hooked or NULL for any */
90 struct afb_hook_evt_itf *itf; /**< interface of hook */
91 void *closure; /**< closure for callbacks */
95 * Definition of a hook for session
97 struct afb_hook_session {
98 struct afb_hook_session *next; /**< next hook */
99 unsigned refcount; /**< reference count */
100 unsigned flags; /**< hook flags */
101 char *pattern; /**< event pattern name hooked or NULL for any */
102 struct afb_hook_session_itf *itf; /**< interface of hook */
103 void *closure; /**< closure for callbacks */
107 * Definition of a hook for global
109 struct afb_hook_global {
110 struct afb_hook_global *next; /**< next hook */
111 unsigned refcount; /**< reference count */
112 unsigned flags; /**< hook flags */
113 struct afb_hook_global_itf *itf; /**< interface of hook */
114 void *closure; /**< closure for callbacks */
117 /* synchronisation across threads */
118 static pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
120 /* list of hooks for xreq */
121 static struct afb_hook_xreq *list_of_xreq_hooks = NULL;
123 /* list of hooks for export */
124 static struct afb_hook_ditf *list_of_ditf_hooks = NULL;
126 /* list of hooks for export */
127 static struct afb_hook_svc *list_of_svc_hooks = NULL;
129 /* list of hooks for evt */
130 static struct afb_hook_evt *list_of_evt_hooks = NULL;
132 /* list of hooks for session */
133 static struct afb_hook_session *list_of_session_hooks = NULL;
135 /* list of hooks for global */
136 static struct afb_hook_global *list_of_global_hooks = NULL;
139 static unsigned next_hookid = 0;
141 /******************************************************************************
143 *****************************************************************************/
144 static void init_hookid(struct afb_hookid *hookid)
146 hookid->id = __atomic_add_fetch(&next_hookid, 1, __ATOMIC_RELAXED);
147 clock_gettime(CLOCK_REALTIME, &hookid->time);
150 /******************************************************************************
151 * section: default callbacks for tracing requests
152 *****************************************************************************/
154 static char *_pbuf_(const char *fmt, va_list args, char **palloc, char *sbuf, size_t szsbuf, size_t *outlen)
161 rc = vsnprintf(sbuf, szsbuf, fmt, args);
162 if ((size_t)rc >= szsbuf) {
164 sbuf[szsbuf-2] = sbuf[szsbuf-3] = sbuf[szsbuf-4] = '.';
165 rc = vasprintf(palloc, fmt, cp);
170 if (rc >= 0 && outlen)
171 *outlen = (size_t)rc;
175 #if 0 /* old behaviour: use NOTICE */
176 static void _hook_(const char *fmt1, const char *fmt2, va_list arg2, ...)
178 char *tag, *data, *mem1, *mem2, buf1[256], buf2[2000];
181 data = _pbuf_(fmt2, arg2, &mem2, buf2, sizeof buf2, NULL);
183 va_start(arg1, arg2);
184 tag = _pbuf_(fmt1, arg1, &mem1, buf1, sizeof buf1, NULL);
187 NOTICE("[HOOK %s] %s", tag, data);
192 #else /* new behaviour: emits directly to stderr */
193 static void _hook_(const char *fmt1, const char *fmt2, va_list arg2, ...)
195 static const char chars[] = "HOOK: [] \n";
196 char *mem1, *mem2, buf1[256], buf2[2000];
200 iov[0].iov_base = (void*)&chars[0];
203 va_start(arg1, arg2);
204 iov[1].iov_base = _pbuf_(fmt1, arg1, &mem1, buf1, sizeof buf1, &iov[1].iov_len);
207 iov[2].iov_base = (void*)&chars[7];
210 iov[3].iov_base = _pbuf_(fmt2, arg2, &mem2, buf2, sizeof buf2, &iov[3].iov_len);
212 iov[4].iov_base = (void*)&chars[9];
222 static void _hook_xreq_(const struct afb_xreq *xreq, const char *format, ...)
225 va_start(ap, format);
226 _hook_("xreq-%06d:%s/%s", format, ap, xreq->hookindex, xreq->request.api, xreq->request.verb);
230 static void hook_xreq_begin_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
233 _hook_xreq_(xreq, "BEGIN");
235 _hook_xreq_(xreq, "BEGIN uid=%d=%s gid=%d pid=%d label=%s id=%s",
236 (int)xreq->cred->uid,
238 (int)xreq->cred->gid,
239 (int)xreq->cred->pid,
240 xreq->cred->label?:"(null)",
241 xreq->cred->id?:"(null)"
245 static void hook_xreq_end_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
247 _hook_xreq_(xreq, "END");
250 static void hook_xreq_json_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *obj)
252 _hook_xreq_(xreq, "json() -> %s", json_object_to_json_string(obj));
255 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)
257 _hook_xreq_(xreq, "get(%s) -> { name: %s, value: %s, path: %s }", name, arg.name, arg.value, arg.path);
260 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)
262 _hook_xreq_(xreq, "success(%s, %s)", json_object_to_json_string(obj), info);
265 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)
267 _hook_xreq_(xreq, "fail(%s, %s)", status, info);
270 static void hook_xreq_context_get_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, void *value)
272 _hook_xreq_(xreq, "context_get() -> %p", value);
275 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*))
277 _hook_xreq_(xreq, "context_set(%p, %p)", value, free_value);
280 static void hook_xreq_addref_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
282 _hook_xreq_(xreq, "addref()");
285 static void hook_xreq_unref_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
287 _hook_xreq_(xreq, "unref()");
290 static void hook_xreq_session_close_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
292 _hook_xreq_(xreq, "session_close()");
295 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)
297 _hook_xreq_(xreq, "session_set_LOA(%u) -> %d", level, result);
300 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)
302 _hook_xreq_(xreq, "subscribe(%s:%d) -> %d", afb_evt_eventid_fullname(eventid), afb_evt_eventid_id(eventid), result);
305 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)
307 _hook_xreq_(xreq, "unsubscribe(%s:%d) -> %d", afb_evt_eventid_fullname(eventid), afb_evt_eventid_id(eventid), result);
310 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)
312 _hook_xreq_(xreq, "subcall(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
315 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)
317 _hook_xreq_(xreq, " ...subcall... -> %d: %s", status, json_object_to_json_string(result));
320 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)
322 _hook_xreq_(xreq, "subcallsync(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
325 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)
327 _hook_xreq_(xreq, " ...subcallsync... -> %d: %s", status, json_object_to_json_string(result));
330 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)
337 len = vasprintf(&msg, fmt, ap);
341 _hook_xreq_(xreq, "vverbose(%d, %s, %d, %s) -> %s ? ? ?", level, file, line, func, fmt);
343 _hook_xreq_(xreq, "vverbose(%d, %s, %d, %s) -> %s", level, file, line, func, msg);
348 static void hook_xreq_store_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_stored_req *sreq)
350 _hook_xreq_(xreq, "store() -> %p", sreq);
353 static void hook_xreq_unstore_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
355 _hook_xreq_(xreq, "unstore()");
358 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)
360 _hook_xreq_(xreq, "subcall_req(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
363 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)
365 _hook_xreq_(xreq, " ...subcall_req... -> %d: %s", status, json_object_to_json_string(result));
368 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)
370 _hook_xreq_(xreq, "has_permission(%s) -> %d", permission, result);
373 static void hook_xreq_get_application_id_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, char *result)
375 _hook_xreq_(xreq, "get_application_id() -> %s", result);
378 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)
380 _hook_xreq_(xreq, "context_make(replace=%s, %p, %p, %p) -> %p", replace?"yes":"no", create_value, free_value, create_closure, result);
383 static void hook_xreq_get_uid_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int result)
385 _hook_xreq_(xreq, "get_uid() -> %d", result);
388 static struct afb_hook_xreq_itf hook_xreq_default_itf = {
389 .hook_xreq_begin = hook_xreq_begin_default_cb,
390 .hook_xreq_end = hook_xreq_end_default_cb,
391 .hook_xreq_json = hook_xreq_json_default_cb,
392 .hook_xreq_get = hook_xreq_get_default_cb,
393 .hook_xreq_success = hook_xreq_success_default_cb,
394 .hook_xreq_fail = hook_xreq_fail_default_cb,
395 .hook_xreq_context_get = hook_xreq_context_get_default_cb,
396 .hook_xreq_context_set = hook_xreq_context_set_default_cb,
397 .hook_xreq_addref = hook_xreq_addref_default_cb,
398 .hook_xreq_unref = hook_xreq_unref_default_cb,
399 .hook_xreq_session_close = hook_xreq_session_close_default_cb,
400 .hook_xreq_session_set_LOA = hook_xreq_session_set_LOA_default_cb,
401 .hook_xreq_subscribe = hook_xreq_subscribe_default_cb,
402 .hook_xreq_unsubscribe = hook_xreq_unsubscribe_default_cb,
403 .hook_xreq_subcall = hook_xreq_subcall_default_cb,
404 .hook_xreq_subcall_result = hook_xreq_subcall_result_default_cb,
405 .hook_xreq_subcallsync = hook_xreq_subcallsync_default_cb,
406 .hook_xreq_subcallsync_result = hook_xreq_subcallsync_result_default_cb,
407 .hook_xreq_vverbose = hook_xreq_vverbose_default_cb,
408 .hook_xreq_store = hook_xreq_store_default_cb,
409 .hook_xreq_unstore = hook_xreq_unstore_default_cb,
410 .hook_xreq_subcall_req = hook_xreq_subcall_req_default_cb,
411 .hook_xreq_subcall_req_result = hook_xreq_subcall_req_result_default_cb,
412 .hook_xreq_has_permission = hook_xreq_has_permission_default_cb,
413 .hook_xreq_get_application_id = hook_xreq_get_application_id_default_cb,
414 .hook_xreq_context_make = hook_xreq_context_make_default_cb,
415 .hook_xreq_get_uid = hook_xreq_get_uid_default_cb,
418 /******************************************************************************
419 * section: hooks for tracing requests
420 *****************************************************************************/
422 #define _HOOK_XREQ_(what,...) \
423 struct afb_hook_xreq *hook; \
424 struct afb_hookid hookid; \
425 pthread_rwlock_rdlock(&rwlock); \
426 init_hookid(&hookid); \
427 hook = list_of_xreq_hooks; \
429 if (hook->itf->hook_xreq_##what \
430 && (hook->flags & afb_hook_flag_req_##what) != 0 \
431 && (!hook->session || hook->session == xreq->context.session) \
432 && (!hook->api || !strcasecmp(hook->api, xreq->request.api)) \
433 && (!hook->verb || !strcasecmp(hook->verb, xreq->request.verb))) { \
434 hook->itf->hook_xreq_##what(hook->closure, &hookid, __VA_ARGS__); \
438 pthread_rwlock_unlock(&rwlock);
441 void afb_hook_xreq_begin(const struct afb_xreq *xreq)
443 _HOOK_XREQ_(begin, xreq);
446 void afb_hook_xreq_end(const struct afb_xreq *xreq)
448 _HOOK_XREQ_(end, xreq);
451 struct json_object *afb_hook_xreq_json(const struct afb_xreq *xreq, struct json_object *obj)
453 _HOOK_XREQ_(json, xreq, obj);
457 struct afb_arg afb_hook_xreq_get(const struct afb_xreq *xreq, const char *name, struct afb_arg arg)
459 _HOOK_XREQ_(get, xreq, name, arg);
463 void afb_hook_xreq_success(const struct afb_xreq *xreq, struct json_object *obj, const char *info)
465 _HOOK_XREQ_(success, xreq, obj, info);
468 void afb_hook_xreq_fail(const struct afb_xreq *xreq, const char *status, const char *info)
470 _HOOK_XREQ_(fail, xreq, status, info);
473 void *afb_hook_xreq_context_get(const struct afb_xreq *xreq, void *value)
475 _HOOK_XREQ_(context_get, xreq, value);
479 void afb_hook_xreq_context_set(const struct afb_xreq *xreq, void *value, void (*free_value)(void*))
481 _HOOK_XREQ_(context_set, xreq, value, free_value);
484 void afb_hook_xreq_addref(const struct afb_xreq *xreq)
486 _HOOK_XREQ_(addref, xreq);
489 void afb_hook_xreq_unref(const struct afb_xreq *xreq)
491 _HOOK_XREQ_(unref, xreq);
494 void afb_hook_xreq_session_close(const struct afb_xreq *xreq)
496 _HOOK_XREQ_(session_close, xreq);
499 int afb_hook_xreq_session_set_LOA(const struct afb_xreq *xreq, unsigned level, int result)
501 _HOOK_XREQ_(session_set_LOA, xreq, level, result);
505 int afb_hook_xreq_subscribe(const struct afb_xreq *xreq, struct afb_eventid *eventid, int result)
507 _HOOK_XREQ_(subscribe, xreq, eventid, result);
511 int afb_hook_xreq_unsubscribe(const struct afb_xreq *xreq, struct afb_eventid *eventid, int result)
513 _HOOK_XREQ_(unsubscribe, xreq, eventid, result);
517 void afb_hook_xreq_subcall(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
519 _HOOK_XREQ_(subcall, xreq, api, verb, args);
522 void afb_hook_xreq_subcall_result(const struct afb_xreq *xreq, int status, struct json_object *result)
524 _HOOK_XREQ_(subcall_result, xreq, status, result);
527 void afb_hook_xreq_subcallsync(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
529 _HOOK_XREQ_(subcallsync, xreq, api, verb, args);
532 int afb_hook_xreq_subcallsync_result(const struct afb_xreq *xreq, int status, struct json_object *result)
534 _HOOK_XREQ_(subcallsync_result, xreq, status, result);
538 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)
540 _HOOK_XREQ_(vverbose, xreq, level, file ?: "?", line, func ?: "?", fmt, args);
543 void afb_hook_xreq_store(const struct afb_xreq *xreq, struct afb_stored_req *sreq)
545 _HOOK_XREQ_(store, xreq, sreq);
548 void afb_hook_xreq_unstore(const struct afb_xreq *xreq)
550 _HOOK_XREQ_(unstore, xreq);
553 void afb_hook_xreq_subcall_req(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
555 _HOOK_XREQ_(subcall_req, xreq, api, verb, args);
558 void afb_hook_xreq_subcall_req_result(const struct afb_xreq *xreq, int status, struct json_object *result)
560 _HOOK_XREQ_(subcall_req_result, xreq, status, result);
563 int afb_hook_xreq_has_permission(const struct afb_xreq *xreq, const char *permission, int result)
565 _HOOK_XREQ_(has_permission, xreq, permission, result);
569 char *afb_hook_xreq_get_application_id(const struct afb_xreq *xreq, char *result)
571 _HOOK_XREQ_(get_application_id, xreq, result);
575 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)
577 _HOOK_XREQ_(context_make, xreq, replace, create_value, free_value, create_closure, result);
581 int afb_hook_xreq_get_uid(const struct afb_xreq *xreq, int result)
583 _HOOK_XREQ_(get_uid, xreq, result);
587 /******************************************************************************
588 * section: hooking xreqs
589 *****************************************************************************/
591 void afb_hook_init_xreq(struct afb_xreq *xreq)
597 struct afb_hook_xreq *hook;
599 /* scan hook list to get the expected flags */
601 if (afb_api_is_hookable(xreq->request.api)) {
602 pthread_rwlock_rdlock(&rwlock);
603 hook = list_of_xreq_hooks;
605 f = hook->flags & afb_hook_flags_req_all;
607 && (!hook->session || hook->session == xreq->context.session)
608 && (!hook->api || !strcasecmp(hook->api, xreq->request.api))
609 && (!hook->verb || !strcasecmp(hook->verb, xreq->request.verb));
614 pthread_rwlock_unlock(&rwlock);
617 /* store the hooking data */
618 xreq->hookflags = flags;
620 pthread_rwlock_wrlock(&rwlock);
623 xreq->hookindex = reqindex;
624 pthread_rwlock_unlock(&rwlock);
628 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)
630 struct afb_hook_xreq *hook;
632 /* alloc the result */
633 hook = calloc(1, sizeof *hook);
637 /* get a copy of the names */
638 hook->api = api ? strdup(api) : NULL;
639 hook->verb = verb ? strdup(verb) : NULL;
640 if ((api && !hook->api) || (verb && !hook->verb)) {
647 /* initialise the rest */
648 hook->session = session;
650 afb_session_addref(session);
653 hook->itf = itf ? itf : &hook_xreq_default_itf;
654 hook->closure = closure;
656 /* record the hook */
657 pthread_rwlock_wrlock(&rwlock);
658 hook->next = list_of_xreq_hooks;
659 list_of_xreq_hooks = hook;
660 pthread_rwlock_unlock(&rwlock);
666 struct afb_hook_xreq *afb_hook_addref_xreq(struct afb_hook_xreq *hook)
668 pthread_rwlock_wrlock(&rwlock);
670 pthread_rwlock_unlock(&rwlock);
674 void afb_hook_unref_xreq(struct afb_hook_xreq *hook)
676 struct afb_hook_xreq **prv;
679 pthread_rwlock_wrlock(&rwlock);
680 if (--hook->refcount)
684 prv = &list_of_xreq_hooks;
685 while (*prv && *prv != hook)
690 pthread_rwlock_unlock(&rwlock);
696 afb_session_unref(hook->session);
702 /******************************************************************************
703 * section: default callbacks for tracing daemon interface
704 *****************************************************************************/
706 static void _hook_ditf_(const struct afb_export *export, const char *format, ...)
709 va_start(ap, format);
710 _hook_("export-%s", format, ap, afb_export_apiname(export));
714 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)
716 _hook_ditf_(export, "event_broadcast.before(%s, %s)....", name, json_object_to_json_string(object));
719 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)
721 _hook_ditf_(export, "event_broadcast.after(%s, %s) -> %d", name, json_object_to_json_string(object), result);
724 static void hook_ditf_get_event_loop_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_event *result)
726 _hook_ditf_(export, "get_event_loop() -> %p", result);
729 static void hook_ditf_get_user_bus_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_bus *result)
731 _hook_ditf_(export, "get_user_bus() -> %p", result);
734 static void hook_ditf_get_system_bus_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_bus *result)
736 _hook_ditf_(export, "get_system_bus() -> %p", result);
739 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)
746 len = vasprintf(&msg, fmt, ap);
750 _hook_ditf_(export, "vverbose(%d, %s, %d, %s) -> %s ? ? ?", level, file, line, function, fmt);
752 _hook_ditf_(export, "vverbose(%d, %s, %d, %s) -> %s", level, file, line, function, msg);
757 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)
759 _hook_ditf_(export, "event_make(%s) -> %s:%d", name, afb_evt_eventid_fullname(result), afb_evt_eventid_id(result));
762 static void hook_ditf_rootdir_get_fd_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result)
766 _hook_ditf_(export, "rootdir_get_fd() -> %d, %m", result);
768 sprintf(path, "/proc/self/fd/%d", result);
769 readlink(path, path, sizeof path);
770 _hook_ditf_(export, "rootdir_get_fd() -> %d = %s", result, path);
774 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)
780 _hook_ditf_(export, "rootdir_open_locale(%s, %d, %s) -> %d, %m", filename, flags, locale, result);
782 sprintf(path, "/proc/self/fd/%d", result);
783 readlink(path, path, sizeof path);
784 _hook_ditf_(export, "rootdir_open_locale(%s, %d, %s) -> %d = %s", filename, flags, locale, result, path);
788 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)
790 _hook_ditf_(export, "queue_job(%p, %p, %p, %d) -> %d", callback, argument, group, timeout, result);
793 static void hook_ditf_unstore_req_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct afb_stored_req *sreq)
795 _hook_ditf_(export, "unstore_req(%p)", sreq);
798 static void hook_ditf_require_api_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, int initialized)
800 _hook_ditf_(export, "require_api(%s, %d)...", name, initialized);
803 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)
805 _hook_ditf_(export, "...require_api(%s, %d) -> %d", name, initialized, result);
808 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)
810 _hook_ditf_(export, "rename_api(%s -> %s) -> %d", oldname, newname, result);
813 static struct afb_hook_ditf_itf hook_ditf_default_itf = {
814 .hook_ditf_event_broadcast_before = hook_ditf_event_broadcast_before_cb,
815 .hook_ditf_event_broadcast_after = hook_ditf_event_broadcast_after_cb,
816 .hook_ditf_get_event_loop = hook_ditf_get_event_loop_cb,
817 .hook_ditf_get_user_bus = hook_ditf_get_user_bus_cb,
818 .hook_ditf_get_system_bus = hook_ditf_get_system_bus_cb,
819 .hook_ditf_vverbose = hook_ditf_vverbose_cb,
820 .hook_ditf_event_make = hook_ditf_event_make_cb,
821 .hook_ditf_rootdir_get_fd = hook_ditf_rootdir_get_fd_cb,
822 .hook_ditf_rootdir_open_locale = hook_ditf_rootdir_open_locale_cb,
823 .hook_ditf_queue_job = hook_ditf_queue_job_cb,
824 .hook_ditf_unstore_req = hook_ditf_unstore_req_cb,
825 .hook_ditf_require_api = hook_ditf_require_api_cb,
826 .hook_ditf_require_api_result = hook_ditf_require_api_result_cb,
827 .hook_ditf_rename_api = hook_ditf_rename_api_cb
830 /******************************************************************************
831 * section: hooks for tracing daemon interface (export)
832 *****************************************************************************/
834 #define _HOOK_DITF_(what,...) \
835 struct afb_hook_ditf *hook; \
836 struct afb_hookid hookid; \
837 const char *apiname = afb_export_apiname(export); \
838 pthread_rwlock_rdlock(&rwlock); \
839 init_hookid(&hookid); \
840 hook = list_of_ditf_hooks; \
842 if (hook->itf->hook_ditf_##what \
843 && (hook->flags & afb_hook_flag_ditf_##what) != 0 \
844 && (!hook->api || !strcasecmp(hook->api, apiname))) { \
845 hook->itf->hook_ditf_##what(hook->closure, &hookid, __VA_ARGS__); \
849 pthread_rwlock_unlock(&rwlock);
851 void afb_hook_ditf_event_broadcast_before(const struct afb_export *export, const char *name, struct json_object *object)
853 _HOOK_DITF_(event_broadcast_before, export, name, object);
856 int afb_hook_ditf_event_broadcast_after(const struct afb_export *export, const char *name, struct json_object *object, int result)
858 _HOOK_DITF_(event_broadcast_after, export, name, object, result);
862 struct sd_event *afb_hook_ditf_get_event_loop(const struct afb_export *export, struct sd_event *result)
864 _HOOK_DITF_(get_event_loop, export, result);
868 struct sd_bus *afb_hook_ditf_get_user_bus(const struct afb_export *export, struct sd_bus *result)
870 _HOOK_DITF_(get_user_bus, export, result);
874 struct sd_bus *afb_hook_ditf_get_system_bus(const struct afb_export *export, struct sd_bus *result)
876 _HOOK_DITF_(get_system_bus, export, result);
880 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)
882 _HOOK_DITF_(vverbose, export, level, file, line, function, fmt, args);
885 struct afb_eventid *afb_hook_ditf_event_make(const struct afb_export *export, const char *name, struct afb_eventid *result)
887 _HOOK_DITF_(event_make, export, name, result);
891 int afb_hook_ditf_rootdir_get_fd(const struct afb_export *export, int result)
893 _HOOK_DITF_(rootdir_get_fd, export, result);
897 int afb_hook_ditf_rootdir_open_locale(const struct afb_export *export, const char *filename, int flags, const char *locale, int result)
899 _HOOK_DITF_(rootdir_open_locale, export, filename, flags, locale, result);
903 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)
905 _HOOK_DITF_(queue_job, export, callback, argument, group, timeout, result);
909 void afb_hook_ditf_unstore_req(const struct afb_export *export, struct afb_stored_req *sreq)
911 _HOOK_DITF_(unstore_req, export, sreq);
914 void afb_hook_ditf_require_api(const struct afb_export *export, const char *name, int initialized)
916 _HOOK_DITF_(require_api, export, name, initialized);
919 int afb_hook_ditf_require_api_result(const struct afb_export *export, const char *name, int initialized, int result)
921 _HOOK_DITF_(require_api_result, export, name, initialized, result);
925 int afb_hook_ditf_rename_api(const struct afb_export *export, const char *oldname, const char *newname, int result)
927 _HOOK_DITF_(rename_api, export, oldname, newname, result);
931 /******************************************************************************
932 * section: hooking export
933 *****************************************************************************/
935 int afb_hook_flags_ditf(const char *api)
938 struct afb_hook_ditf *hook;
941 if (!api || afb_api_is_hookable(api)) {
942 pthread_rwlock_rdlock(&rwlock);
943 hook = list_of_ditf_hooks;
945 if (!api || !hook->api || !strcasecmp(hook->api, api))
946 flags |= hook->flags;
949 pthread_rwlock_unlock(&rwlock);
954 struct afb_hook_ditf *afb_hook_create_ditf(const char *api, int flags, struct afb_hook_ditf_itf *itf, void *closure)
956 struct afb_hook_ditf *hook;
958 /* alloc the result */
959 hook = calloc(1, sizeof *hook);
963 /* get a copy of the names */
964 hook->api = api ? strdup(api) : NULL;
965 if (api && !hook->api) {
970 /* initialise the rest */
973 hook->itf = itf ? itf : &hook_ditf_default_itf;
974 hook->closure = closure;
976 /* record the hook */
977 pthread_rwlock_wrlock(&rwlock);
978 hook->next = list_of_ditf_hooks;
979 list_of_ditf_hooks = hook;
980 pthread_rwlock_unlock(&rwlock);
986 struct afb_hook_ditf *afb_hook_addref_ditf(struct afb_hook_ditf *hook)
988 pthread_rwlock_wrlock(&rwlock);
990 pthread_rwlock_unlock(&rwlock);
994 void afb_hook_unref_ditf(struct afb_hook_ditf *hook)
996 struct afb_hook_ditf **prv;
999 pthread_rwlock_wrlock(&rwlock);
1000 if (--hook->refcount)
1004 prv = &list_of_ditf_hooks;
1005 while (*prv && *prv != hook)
1006 prv = &(*prv)->next;
1010 pthread_rwlock_unlock(&rwlock);
1019 /******************************************************************************
1020 * section: default callbacks for tracing service interface (export)
1021 *****************************************************************************/
1023 static void _hook_svc_(const struct afb_export *export, const char *format, ...)
1026 va_start(ap, format);
1027 _hook_("export-%s", format, ap, afb_export_apiname(export));
1031 static void hook_svc_start_before_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export)
1033 _hook_svc_(export, "start.before");
1036 static void hook_svc_start_after_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int status)
1038 _hook_svc_(export, "start.after -> %d", status);
1041 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)
1043 _hook_svc_(export, "on_event.before(%s, %d, %s)", event, eventid, json_object_to_json_string(object));
1046 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)
1048 _hook_svc_(export, "on_event.after(%s, %d, %s)", event, eventid, json_object_to_json_string(object));
1051 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)
1053 _hook_svc_(export, "call(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
1056 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)
1058 _hook_svc_(export, " ...call... -> %d: %s", status, json_object_to_json_string(result));
1061 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)
1063 _hook_svc_(export, "callsync(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
1066 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)
1068 _hook_svc_(export, " ...callsync... -> %d: %s", status, json_object_to_json_string(result));
1071 static struct afb_hook_svc_itf hook_svc_default_itf = {
1072 .hook_svc_start_before = hook_svc_start_before_default_cb,
1073 .hook_svc_start_after = hook_svc_start_after_default_cb,
1074 .hook_svc_on_event_before = hook_svc_on_event_before_default_cb,
1075 .hook_svc_on_event_after = hook_svc_on_event_after_default_cb,
1076 .hook_svc_call = hook_svc_call_default_cb,
1077 .hook_svc_call_result = hook_svc_call_result_default_cb,
1078 .hook_svc_callsync = hook_svc_callsync_default_cb,
1079 .hook_svc_callsync_result = hook_svc_callsync_result_default_cb
1082 /******************************************************************************
1083 * section: hooks for tracing service interface (export)
1084 *****************************************************************************/
1086 #define _HOOK_SVC_(what,...) \
1087 struct afb_hook_svc *hook; \
1088 struct afb_hookid hookid; \
1089 const char *apiname = afb_export_apiname(export); \
1090 pthread_rwlock_rdlock(&rwlock); \
1091 init_hookid(&hookid); \
1092 hook = list_of_svc_hooks; \
1094 if (hook->itf->hook_svc_##what \
1095 && (hook->flags & afb_hook_flag_svc_##what) != 0 \
1096 && (!hook->api || !strcasecmp(hook->api, apiname))) { \
1097 hook->itf->hook_svc_##what(hook->closure, &hookid, __VA_ARGS__); \
1099 hook = hook->next; \
1101 pthread_rwlock_unlock(&rwlock);
1103 void afb_hook_svc_start_before(const struct afb_export *export)
1105 _HOOK_SVC_(start_before, export);
1108 int afb_hook_svc_start_after(const struct afb_export *export, int status)
1110 _HOOK_SVC_(start_after, export, status);
1114 void afb_hook_svc_on_event_before(const struct afb_export *export, const char *event, int eventid, struct json_object *object)
1116 _HOOK_SVC_(on_event_before, export, event, eventid, object);
1119 void afb_hook_svc_on_event_after(const struct afb_export *export, const char *event, int eventid, struct json_object *object)
1121 _HOOK_SVC_(on_event_after, export, event, eventid, object);
1124 void afb_hook_svc_call(const struct afb_export *export, const char *api, const char *verb, struct json_object *args)
1126 _HOOK_SVC_(call, export, api, verb, args);
1129 void afb_hook_svc_call_result(const struct afb_export *export, int status, struct json_object *result)
1131 _HOOK_SVC_(call_result, export, status, result);
1134 void afb_hook_svc_callsync(const struct afb_export *export, const char *api, const char *verb, struct json_object *args)
1136 _HOOK_SVC_(callsync, export, api, verb, args);
1139 int afb_hook_svc_callsync_result(const struct afb_export *export, int status, struct json_object *result)
1141 _HOOK_SVC_(callsync_result, export, status, result);
1145 /******************************************************************************
1146 * section: hooking services (export)
1147 *****************************************************************************/
1149 int afb_hook_flags_svc(const char *api)
1152 struct afb_hook_svc *hook;
1155 if (!api || afb_api_is_hookable(api)) {
1156 pthread_rwlock_rdlock(&rwlock);
1157 hook = list_of_svc_hooks;
1159 if (!api || !hook->api || !strcasecmp(hook->api, api))
1160 flags |= hook->flags;
1163 pthread_rwlock_unlock(&rwlock);
1168 struct afb_hook_svc *afb_hook_create_svc(const char *api, int flags, struct afb_hook_svc_itf *itf, void *closure)
1170 struct afb_hook_svc *hook;
1172 /* alloc the result */
1173 hook = calloc(1, sizeof *hook);
1177 /* get a copy of the names */
1178 hook->api = api ? strdup(api) : NULL;
1179 if (api && !hook->api) {
1184 /* initialise the rest */
1186 hook->flags = flags;
1187 hook->itf = itf ? itf : &hook_svc_default_itf;
1188 hook->closure = closure;
1190 /* record the hook */
1191 pthread_rwlock_wrlock(&rwlock);
1192 hook->next = list_of_svc_hooks;
1193 list_of_svc_hooks = hook;
1194 pthread_rwlock_unlock(&rwlock);
1200 struct afb_hook_svc *afb_hook_addref_svc(struct afb_hook_svc *hook)
1202 pthread_rwlock_wrlock(&rwlock);
1204 pthread_rwlock_unlock(&rwlock);
1208 void afb_hook_unref_svc(struct afb_hook_svc *hook)
1210 struct afb_hook_svc **prv;
1213 pthread_rwlock_wrlock(&rwlock);
1214 if (--hook->refcount)
1218 prv = &list_of_svc_hooks;
1219 while (*prv && *prv != hook)
1220 prv = &(*prv)->next;
1224 pthread_rwlock_unlock(&rwlock);
1233 /******************************************************************************
1234 * section: default callbacks for tracing service interface (evt)
1235 *****************************************************************************/
1237 static void _hook_evt_(const char *evt, int id, const char *format, ...)
1240 va_start(ap, format);
1241 _hook_("evt-%s:%d", format, ap, evt, id);
1245 static void hook_evt_create_default_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
1247 _hook_evt_(evt, id, "create");
1250 static void hook_evt_push_before_default_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj)
1252 _hook_evt_(evt, id, "push.before(%s)", json_object_to_json_string(obj));
1256 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)
1258 _hook_evt_(evt, id, "push.after(%s) -> %d", json_object_to_json_string(obj), result);
1261 static void hook_evt_broadcast_before_default_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj)
1263 _hook_evt_(evt, id, "broadcast.before(%s)", json_object_to_json_string(obj));
1266 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)
1268 _hook_evt_(evt, id, "broadcast.after(%s) -> %d", json_object_to_json_string(obj), result);
1271 static void hook_evt_name_default_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, const char *result)
1273 _hook_evt_(evt, id, "name -> %s", result);
1276 static void hook_evt_addref_default_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
1278 _hook_evt_(evt, id, "addref");
1281 static void hook_evt_unref_default_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
1283 _hook_evt_(evt, id, "unref");
1286 static struct afb_hook_evt_itf hook_evt_default_itf = {
1287 .hook_evt_create = hook_evt_create_default_cb,
1288 .hook_evt_push_before = hook_evt_push_before_default_cb,
1289 .hook_evt_push_after = hook_evt_push_after_default_cb,
1290 .hook_evt_broadcast_before = hook_evt_broadcast_before_default_cb,
1291 .hook_evt_broadcast_after = hook_evt_broadcast_after_default_cb,
1292 .hook_evt_name = hook_evt_name_default_cb,
1293 .hook_evt_addref = hook_evt_addref_default_cb,
1294 .hook_evt_unref = hook_evt_unref_default_cb
1297 /******************************************************************************
1298 * section: hooks for tracing events interface (evt)
1299 *****************************************************************************/
1301 #define _HOOK_EVT_(what,...) \
1302 struct afb_hook_evt *hook; \
1303 struct afb_hookid hookid; \
1304 pthread_rwlock_rdlock(&rwlock); \
1305 init_hookid(&hookid); \
1306 hook = list_of_evt_hooks; \
1308 if (hook->itf->hook_evt_##what \
1309 && (hook->flags & afb_hook_flag_evt_##what) != 0 \
1310 && (!hook->pattern || !fnmatch(hook->pattern, evt, FNM_CASEFOLD))) { \
1311 hook->itf->hook_evt_##what(hook->closure, &hookid, __VA_ARGS__); \
1313 hook = hook->next; \
1315 pthread_rwlock_unlock(&rwlock);
1317 void afb_hook_evt_create(const char *evt, int id)
1319 _HOOK_EVT_(create, evt, id);
1322 void afb_hook_evt_push_before(const char *evt, int id, struct json_object *obj)
1324 _HOOK_EVT_(push_before, evt, id, obj);
1327 int afb_hook_evt_push_after(const char *evt, int id, struct json_object *obj, int result)
1329 _HOOK_EVT_(push_after, evt, id, obj, result);
1333 void afb_hook_evt_broadcast_before(const char *evt, int id, struct json_object *obj)
1335 _HOOK_EVT_(broadcast_before, evt, id, obj);
1338 int afb_hook_evt_broadcast_after(const char *evt, int id, struct json_object *obj, int result)
1340 _HOOK_EVT_(broadcast_after, evt, id, obj, result);
1344 void afb_hook_evt_name(const char *evt, int id, const char *result)
1346 _HOOK_EVT_(name, evt, id, result);
1349 void afb_hook_evt_addref(const char *evt, int id)
1351 _HOOK_EVT_(addref, evt, id);
1354 void afb_hook_evt_unref(const char *evt, int id)
1356 _HOOK_EVT_(unref, evt, id);
1359 /******************************************************************************
1360 * section: hooking services (evt)
1361 *****************************************************************************/
1363 int afb_hook_flags_evt(const char *name)
1366 struct afb_hook_evt *hook;
1369 if (!name || afb_api_is_hookable(name)) {
1370 pthread_rwlock_rdlock(&rwlock);
1371 hook = list_of_evt_hooks;
1373 if (!name || !hook->pattern || !fnmatch(hook->pattern, name, FNM_CASEFOLD))
1374 flags |= hook->flags;
1377 pthread_rwlock_unlock(&rwlock);
1382 struct afb_hook_evt *afb_hook_create_evt(const char *pattern, int flags, struct afb_hook_evt_itf *itf, void *closure)
1384 struct afb_hook_evt *hook;
1386 /* alloc the result */
1387 hook = calloc(1, sizeof *hook);
1391 /* get a copy of the names */
1392 hook->pattern = pattern ? strdup(pattern) : NULL;
1393 if (pattern && !hook->pattern) {
1398 /* initialise the rest */
1400 hook->flags = flags;
1401 hook->itf = itf ? itf : &hook_evt_default_itf;
1402 hook->closure = closure;
1404 /* record the hook */
1405 pthread_rwlock_wrlock(&rwlock);
1406 hook->next = list_of_evt_hooks;
1407 list_of_evt_hooks = hook;
1408 pthread_rwlock_unlock(&rwlock);
1414 struct afb_hook_evt *afb_hook_addref_evt(struct afb_hook_evt *hook)
1416 pthread_rwlock_wrlock(&rwlock);
1418 pthread_rwlock_unlock(&rwlock);
1422 void afb_hook_unref_evt(struct afb_hook_evt *hook)
1424 struct afb_hook_evt **prv;
1427 pthread_rwlock_wrlock(&rwlock);
1428 if (--hook->refcount)
1432 prv = &list_of_evt_hooks;
1433 while (*prv && *prv != hook)
1434 prv = &(*prv)->next;
1438 pthread_rwlock_unlock(&rwlock);
1441 free(hook->pattern);
1447 /******************************************************************************
1448 * section: default callbacks for sessions (session)
1449 *****************************************************************************/
1451 static void _hook_session_(struct afb_session *session, const char *format, ...)
1454 va_start(ap, format);
1455 _hook_("session-%s", format, ap, afb_session_uuid(session));
1459 static void hook_session_create_default_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1461 _hook_session_(session, "create -> token=%s", afb_session_token(session));
1464 static void hook_session_close_default_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1466 _hook_session_(session, "close");
1469 static void hook_session_destroy_default_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1471 _hook_session_(session, "destroy");
1474 static void hook_session_renew_default_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1476 _hook_session_(session, "renew -> token=%s", afb_session_token(session));
1479 static void hook_session_addref_default_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1481 _hook_session_(session, "addref");
1484 static void hook_session_unref_default_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1486 _hook_session_(session, "unref");
1489 static struct afb_hook_session_itf hook_session_default_itf = {
1490 .hook_session_create = hook_session_create_default_cb,
1491 .hook_session_close = hook_session_close_default_cb,
1492 .hook_session_destroy = hook_session_destroy_default_cb,
1493 .hook_session_renew = hook_session_renew_default_cb,
1494 .hook_session_addref = hook_session_addref_default_cb,
1495 .hook_session_unref = hook_session_unref_default_cb
1498 /******************************************************************************
1499 * section: hooks for tracing sessions (session)
1500 *****************************************************************************/
1502 #define _HOOK_SESSION_(what,...) \
1503 struct afb_hook_session *hook; \
1504 struct afb_hookid hookid; \
1505 const char *sessid = 0; \
1506 pthread_rwlock_rdlock(&rwlock); \
1507 init_hookid(&hookid); \
1508 hook = list_of_session_hooks; \
1510 if (hook->itf->hook_session_##what \
1511 && (hook->flags & afb_hook_flag_session_##what) != 0 \
1512 && (!hook->pattern || !fnmatch(hook->pattern, (sessid?:(sessid=afb_session_uuid(session))), FNM_CASEFOLD))) { \
1513 hook->itf->hook_session_##what(hook->closure, &hookid, __VA_ARGS__); \
1515 hook = hook->next; \
1517 pthread_rwlock_unlock(&rwlock);
1519 void afb_hook_session_create(struct afb_session *session)
1521 _HOOK_SESSION_(create, session);
1524 void afb_hook_session_close(struct afb_session *session)
1526 _HOOK_SESSION_(close, session);
1529 void afb_hook_session_destroy(struct afb_session *session)
1531 _HOOK_SESSION_(destroy, session);
1534 void afb_hook_session_renew(struct afb_session *session)
1536 _HOOK_SESSION_(renew, session);
1539 void afb_hook_session_addref(struct afb_session *session)
1541 _HOOK_SESSION_(addref, session);
1544 void afb_hook_session_unref(struct afb_session *session)
1546 _HOOK_SESSION_(unref, session);
1550 /******************************************************************************
1551 * section: hooking sessions (session)
1552 *****************************************************************************/
1554 struct afb_hook_session *afb_hook_create_session(const char *pattern, int flags, struct afb_hook_session_itf *itf, void *closure)
1556 struct afb_hook_session *hook;
1558 /* alloc the result */
1559 hook = calloc(1, sizeof *hook);
1563 /* get a copy of the names */
1564 hook->pattern = pattern ? strdup(pattern) : NULL;
1565 if (pattern && !hook->pattern) {
1570 /* initialise the rest */
1572 hook->flags = flags;
1573 hook->itf = itf ? itf : &hook_session_default_itf;
1574 hook->closure = closure;
1576 /* record the hook */
1577 pthread_rwlock_wrlock(&rwlock);
1578 hook->next = list_of_session_hooks;
1579 list_of_session_hooks = hook;
1580 pthread_rwlock_unlock(&rwlock);
1586 struct afb_hook_session *afb_hook_addref_session(struct afb_hook_session *hook)
1588 pthread_rwlock_wrlock(&rwlock);
1590 pthread_rwlock_unlock(&rwlock);
1594 void afb_hook_unref_session(struct afb_hook_session *hook)
1596 struct afb_hook_session **prv;
1599 pthread_rwlock_wrlock(&rwlock);
1600 if (--hook->refcount)
1604 prv = &list_of_session_hooks;
1605 while (*prv && *prv != hook)
1606 prv = &(*prv)->next;
1610 pthread_rwlock_unlock(&rwlock);
1613 free(hook->pattern);
1619 /******************************************************************************
1620 * section: default callbacks for globals (global)
1621 *****************************************************************************/
1623 static void _hook_global_(const char *format, ...)
1626 va_start(ap, format);
1627 _hook_("global", format, ap);
1631 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)
1638 len = vasprintf(&msg, fmt, ap);
1642 _hook_global_("vverbose(%d, %s, %d, %s) -> %s ? ? ?", level, file, line, func, fmt);
1644 _hook_global_("vverbose(%d, %s, %d, %s) -> %s", level, file, line, func, msg);
1649 static struct afb_hook_global_itf hook_global_default_itf = {
1650 .hook_global_vverbose = hook_global_vverbose_default_cb
1653 /******************************************************************************
1654 * section: hooks for tracing globals (global)
1655 *****************************************************************************/
1657 #define _HOOK_GLOBAL_(what,...) \
1658 struct afb_hook_global *hook; \
1659 struct afb_hookid hookid; \
1660 pthread_rwlock_rdlock(&rwlock); \
1661 init_hookid(&hookid); \
1662 hook = list_of_global_hooks; \
1664 if (hook->itf->hook_global_##what \
1665 && (hook->flags & afb_hook_flag_global_##what) != 0) { \
1666 hook->itf->hook_global_##what(hook->closure, &hookid, __VA_ARGS__); \
1668 hook = hook->next; \
1670 pthread_rwlock_unlock(&rwlock);
1672 static void afb_hook_global_vverbose(int level, const char *file, int line, const char *func, const char *fmt, va_list args)
1674 _HOOK_GLOBAL_(vverbose, level, file ?: "?", line, func ?: "?", fmt ?: "", args);
1677 /******************************************************************************
1678 * section: hooking globals (global)
1679 *****************************************************************************/
1681 static void update_global()
1683 struct afb_hook_global *hook;
1686 pthread_rwlock_rdlock(&rwlock);
1687 hook = list_of_global_hooks;
1689 flags = hook->flags;
1692 verbose_observer = (flags & afb_hook_flag_global_vverbose) ? afb_hook_global_vverbose : NULL;
1693 pthread_rwlock_unlock(&rwlock);
1696 struct afb_hook_global *afb_hook_create_global(int flags, struct afb_hook_global_itf *itf, void *closure)
1698 struct afb_hook_global *hook;
1700 /* alloc the result */
1701 hook = calloc(1, sizeof *hook);
1705 /* initialise the rest */
1707 hook->flags = flags;
1708 hook->itf = itf ? itf : &hook_global_default_itf;
1709 hook->closure = closure;
1711 /* record the hook */
1712 pthread_rwlock_wrlock(&rwlock);
1713 hook->next = list_of_global_hooks;
1714 list_of_global_hooks = hook;
1715 pthread_rwlock_unlock(&rwlock);
1717 /* update hooking */
1724 struct afb_hook_global *afb_hook_addref_global(struct afb_hook_global *hook)
1726 pthread_rwlock_wrlock(&rwlock);
1728 pthread_rwlock_unlock(&rwlock);
1732 void afb_hook_unref_global(struct afb_hook_global *hook)
1734 struct afb_hook_global **prv;
1737 pthread_rwlock_wrlock(&rwlock);
1738 if (--hook->refcount)
1742 prv = &list_of_global_hooks;
1743 while (*prv && *prv != hook)
1744 prv = &(*prv)->next;
1748 pthread_rwlock_unlock(&rwlock);
1753 /* update hooking */