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-x1.h>
31 #include <afb/afb-event-x2.h>
33 #include "afb-context.h"
35 #include "afb-session.h"
38 #include "afb-export.h"
41 #include "afb-msg-json.h"
45 #define MATCH(pattern,string) (\
47 ? !fnmatch((pattern),(string),FNM_CASEFOLD|FNM_EXTMATCH|FNM_PERIOD) \
50 #define MATCH_API(pattern,string) MATCH(pattern,string)
51 #define MATCH_VERB(pattern,string) MATCH(pattern,string)
52 #define MATCH_EVENT(pattern,string) MATCH(pattern,string)
53 #define MATCH_SESSION(pattern,string) MATCH(pattern,string)
56 * Definition of a hook for xreq
58 struct afb_hook_xreq {
59 struct afb_hook_xreq *next; /**< next hook */
60 unsigned refcount; /**< reference count */
61 unsigned flags; /**< hook flags */
62 char *api; /**< api hooked or NULL for any */
63 char *verb; /**< verb hooked or NULL for any */
64 struct afb_session *session; /**< session hooked or NULL if any */
65 struct afb_hook_xreq_itf *itf; /**< interface of hook */
66 void *closure; /**< closure for callbacks */
70 * Definition of a hook for export
73 struct afb_hook_api *next; /**< next hook */
74 unsigned refcount; /**< reference count */
75 unsigned flags; /**< hook flags */
76 char *api; /**< api hooked or NULL for any */
77 struct afb_hook_api_itf *itf; /**< interface of hook */
78 void *closure; /**< closure for callbacks */
82 * Definition of a hook for evt
85 struct afb_hook_evt *next; /**< next hook */
86 unsigned refcount; /**< reference count */
87 unsigned flags; /**< hook flags */
88 char *pattern; /**< event pattern name hooked or NULL for any */
89 struct afb_hook_evt_itf *itf; /**< interface of hook */
90 void *closure; /**< closure for callbacks */
94 * Definition of a hook for session
96 struct afb_hook_session {
97 struct afb_hook_session *next; /**< next hook */
98 unsigned refcount; /**< reference count */
99 unsigned flags; /**< hook flags */
100 char *pattern; /**< event pattern name hooked or NULL for any */
101 struct afb_hook_session_itf *itf; /**< interface of hook */
102 void *closure; /**< closure for callbacks */
106 * Definition of a hook for global
108 struct afb_hook_global {
109 struct afb_hook_global *next; /**< next hook */
110 unsigned refcount; /**< reference count */
111 unsigned flags; /**< hook flags */
112 struct afb_hook_global_itf *itf; /**< interface of hook */
113 void *closure; /**< closure for callbacks */
116 /* synchronisation across threads */
117 static pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
119 /* list of hooks for xreq */
120 static struct afb_hook_xreq *list_of_xreq_hooks = NULL;
122 /* list of hooks for export */
123 static struct afb_hook_api *list_of_api_hooks = NULL;
125 /* list of hooks for evt */
126 static struct afb_hook_evt *list_of_evt_hooks = NULL;
128 /* list of hooks for session */
129 static struct afb_hook_session *list_of_session_hooks = NULL;
131 /* list of hooks for global */
132 static struct afb_hook_global *list_of_global_hooks = NULL;
135 static unsigned next_hookid = 0;
137 /******************************************************************************
139 *****************************************************************************/
140 static void init_hookid(struct afb_hookid *hookid)
142 hookid->id = __atomic_add_fetch(&next_hookid, 1, __ATOMIC_RELAXED);
143 clock_gettime(CLOCK_REALTIME, &hookid->time);
146 /******************************************************************************
147 * section: default callbacks for tracing requests
148 *****************************************************************************/
150 static char *_pbuf_(const char *fmt, va_list args, char **palloc, char *sbuf, size_t szsbuf, size_t *outlen)
157 rc = vsnprintf(sbuf, szsbuf, fmt, args);
158 if ((size_t)rc >= szsbuf) {
160 sbuf[szsbuf-2] = sbuf[szsbuf-3] = sbuf[szsbuf-4] = '.';
161 rc = vasprintf(palloc, fmt, cp);
166 if (rc >= 0 && outlen)
167 *outlen = (size_t)rc;
171 #if 0 /* old behaviour: use NOTICE */
172 static void _hook_(const char *fmt1, const char *fmt2, va_list arg2, ...)
174 char *tag, *data, *mem1, *mem2, buf1[256], buf2[2000];
177 data = _pbuf_(fmt2, arg2, &mem2, buf2, sizeof buf2, NULL);
179 va_start(arg1, arg2);
180 tag = _pbuf_(fmt1, arg1, &mem1, buf1, sizeof buf1, NULL);
183 NOTICE("[HOOK %s] %s", tag, data);
188 #else /* new behaviour: emits directly to stderr */
189 static void _hook_(const char *fmt1, const char *fmt2, va_list arg2, ...)
191 static const char chars[] = "HOOK: [] \n";
192 char *mem1, *mem2, buf1[256], buf2[2000];
196 iov[0].iov_base = (void*)&chars[0];
199 va_start(arg1, arg2);
200 iov[1].iov_base = _pbuf_(fmt1, arg1, &mem1, buf1, sizeof buf1, &iov[1].iov_len);
203 iov[2].iov_base = (void*)&chars[7];
206 iov[3].iov_base = _pbuf_(fmt2, arg2, &mem2, buf2, sizeof buf2, &iov[3].iov_len);
208 iov[4].iov_base = (void*)&chars[9];
218 static void _hook_xreq_(const struct afb_xreq *xreq, const char *format, ...)
221 va_start(ap, format);
222 _hook_("xreq-%06d:%s/%s", format, ap, xreq->hookindex, xreq->request.called_api, xreq->request.called_verb);
226 static void hook_xreq_begin_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
229 _hook_xreq_(xreq, "BEGIN");
231 _hook_xreq_(xreq, "BEGIN uid=%d=%s gid=%d pid=%d label=%s id=%s",
232 (int)xreq->cred->uid,
234 (int)xreq->cred->gid,
235 (int)xreq->cred->pid,
236 xreq->cred->label?:"(null)",
237 xreq->cred->id?:"(null)"
241 static void hook_xreq_end_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
243 _hook_xreq_(xreq, "END");
246 static void hook_xreq_json_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *obj)
248 _hook_xreq_(xreq, "json() -> %s", json_object_to_json_string(obj));
251 static void hook_xreq_get_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *name, struct afb_arg arg)
253 _hook_xreq_(xreq, "get(%s) -> { name: %s, value: %s, path: %s }", name, arg.name, arg.value, arg.path);
256 static void hook_xreq_reply_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *obj, const char *error, const char *info)
258 _hook_xreq_(xreq, "reply[%s](%s, %s)", error?:"success", json_object_to_json_string(obj), info);
261 static void hook_xreq_legacy_context_get_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, void *value)
263 _hook_xreq_(xreq, "context_get() -> %p", value);
266 static void hook_xreq_legacy_context_set_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, void *value, void (*free_value)(void*))
268 _hook_xreq_(xreq, "context_set(%p, %p)", value, free_value);
271 static void hook_xreq_addref_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
273 _hook_xreq_(xreq, "addref()");
276 static void hook_xreq_unref_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
278 _hook_xreq_(xreq, "unref()");
281 static void hook_xreq_session_close_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
283 _hook_xreq_(xreq, "session_close()");
286 static void hook_xreq_session_set_LOA_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, unsigned level, int result)
288 _hook_xreq_(xreq, "session_set_LOA(%u) -> %d", level, result);
291 static void hook_xreq_subscribe_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_event_x2 *event_x2, int result)
293 _hook_xreq_(xreq, "subscribe(%s:%d) -> %d", afb_evt_event_x2_fullname(event_x2), afb_evt_event_x2_id(event_x2), result);
296 static void hook_xreq_unsubscribe_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_event_x2 *event_x2, int result)
298 _hook_xreq_(xreq, "unsubscribe(%s:%d) -> %d", afb_evt_event_x2_fullname(event_x2), afb_evt_event_x2_id(event_x2), result);
301 static void hook_xreq_subcall_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
303 _hook_xreq_(xreq, "subcall(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
306 static void hook_xreq_subcall_result_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *object, const char *error, const char *info)
308 _hook_xreq_(xreq, " ...subcall... [%s] -> %s (%s)", error?:"success", json_object_to_json_string(object), info?:"");
311 static void hook_xreq_subcallsync_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
313 _hook_xreq_(xreq, "subcallsync(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
316 static void hook_xreq_subcallsync_result_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int status, struct json_object *object, const char *error, const char *info)
318 _hook_xreq_(xreq, " ...subcallsync... %d [%s] -> %s (%s)", status, error?:"success", json_object_to_json_string(object), info?:"");
321 static void hook_xreq_vverbose_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)
328 len = vasprintf(&msg, fmt, ap);
332 _hook_xreq_(xreq, "vverbose(%d:%s, %s, %d, %s) -> %s ? ? ?", level, verbose_name_of_level(level), file, line, func, fmt);
334 _hook_xreq_(xreq, "vverbose(%d:%s, %s, %d, %s) -> %s", level, verbose_name_of_level(level), file, line, func, msg);
339 static void hook_xreq_legacy_store_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_stored_req *sreq)
341 _hook_xreq_(xreq, "store() -> %p", sreq);
344 static void hook_xreq_legacy_unstore_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
346 _hook_xreq_(xreq, "unstore()");
349 static void hook_xreq_has_permission_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *permission, int result)
351 _hook_xreq_(xreq, "has_permission(%s) -> %d", permission, result);
354 static void hook_xreq_get_application_id_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, char *result)
356 _hook_xreq_(xreq, "get_application_id() -> %s", result);
359 static void hook_xreq_context_make_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)
361 _hook_xreq_(xreq, "context_make(replace=%s, %p, %p, %p) -> %p", replace?"yes":"no", create_value, free_value, create_closure, result);
364 static void hook_xreq_get_uid_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int result)
366 _hook_xreq_(xreq, "get_uid() -> %d", result);
369 static void hook_xreq_get_client_info_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *result)
371 _hook_xreq_(xreq, "get_client_info() -> %s", json_object_to_json_string(result));
374 static struct afb_hook_xreq_itf hook_xreq_default_itf = {
375 .hook_xreq_begin = hook_xreq_begin_cb,
376 .hook_xreq_end = hook_xreq_end_cb,
377 .hook_xreq_json = hook_xreq_json_cb,
378 .hook_xreq_get = hook_xreq_get_cb,
379 .hook_xreq_reply = hook_xreq_reply_cb,
380 .hook_xreq_legacy_context_get = hook_xreq_legacy_context_get_cb,
381 .hook_xreq_legacy_context_set = hook_xreq_legacy_context_set_cb,
382 .hook_xreq_addref = hook_xreq_addref_cb,
383 .hook_xreq_unref = hook_xreq_unref_cb,
384 .hook_xreq_session_close = hook_xreq_session_close_cb,
385 .hook_xreq_session_set_LOA = hook_xreq_session_set_LOA_cb,
386 .hook_xreq_subscribe = hook_xreq_subscribe_cb,
387 .hook_xreq_unsubscribe = hook_xreq_unsubscribe_cb,
388 .hook_xreq_subcall = hook_xreq_subcall_cb,
389 .hook_xreq_subcall_result = hook_xreq_subcall_result_cb,
390 .hook_xreq_subcallsync = hook_xreq_subcallsync_cb,
391 .hook_xreq_subcallsync_result = hook_xreq_subcallsync_result_cb,
392 .hook_xreq_vverbose = hook_xreq_vverbose_cb,
393 .hook_xreq_legacy_store = hook_xreq_legacy_store_cb,
394 .hook_xreq_legacy_unstore = hook_xreq_legacy_unstore_cb,
395 .hook_xreq_has_permission = hook_xreq_has_permission_cb,
396 .hook_xreq_get_application_id = hook_xreq_get_application_id_cb,
397 .hook_xreq_context_make = hook_xreq_context_make_cb,
398 .hook_xreq_get_uid = hook_xreq_get_uid_cb,
399 .hook_xreq_get_client_info = hook_xreq_get_client_info_cb,
402 /******************************************************************************
403 * section: hooks for tracing requests
404 *****************************************************************************/
406 #define _HOOK_XREQ_2_(flag,func,...) \
407 struct afb_hook_xreq *hook; \
408 struct afb_hookid hookid; \
409 pthread_rwlock_rdlock(&rwlock); \
410 init_hookid(&hookid); \
411 hook = list_of_xreq_hooks; \
413 if (hook->itf->hook_xreq_##func \
414 && (hook->flags & afb_hook_flag_req_##flag) != 0 \
415 && (!hook->session || hook->session == xreq->context.session) \
416 && MATCH_API(hook->api, xreq->request.called_api) \
417 && MATCH_VERB(hook->verb, xreq->request.called_verb)) { \
418 hook->itf->hook_xreq_##func(hook->closure, &hookid, __VA_ARGS__); \
422 pthread_rwlock_unlock(&rwlock);
424 #define _HOOK_XREQ_(what,...) _HOOK_XREQ_2_(what,what,__VA_ARGS__)
426 void afb_hook_xreq_begin(const struct afb_xreq *xreq)
428 _HOOK_XREQ_(begin, xreq);
431 void afb_hook_xreq_end(const struct afb_xreq *xreq)
433 _HOOK_XREQ_(end, xreq);
436 struct json_object *afb_hook_xreq_json(const struct afb_xreq *xreq, struct json_object *obj)
438 _HOOK_XREQ_(json, xreq, obj);
442 struct afb_arg afb_hook_xreq_get(const struct afb_xreq *xreq, const char *name, struct afb_arg arg)
444 _HOOK_XREQ_(get, xreq, name, arg);
448 void afb_hook_xreq_reply(const struct afb_xreq *xreq, struct json_object *obj, const char *error, const char *info)
450 _HOOK_XREQ_(reply, xreq, obj, error, info);
453 void *afb_hook_xreq_legacy_context_get(const struct afb_xreq *xreq, void *value)
455 _HOOK_XREQ_(legacy_context_get, xreq, value);
459 void afb_hook_xreq_legacy_context_set(const struct afb_xreq *xreq, void *value, void (*free_value)(void*))
461 _HOOK_XREQ_(legacy_context_set, xreq, value, free_value);
464 void afb_hook_xreq_addref(const struct afb_xreq *xreq)
466 _HOOK_XREQ_(addref, xreq);
469 void afb_hook_xreq_unref(const struct afb_xreq *xreq)
471 _HOOK_XREQ_(unref, xreq);
474 void afb_hook_xreq_session_close(const struct afb_xreq *xreq)
476 _HOOK_XREQ_(session_close, xreq);
479 int afb_hook_xreq_session_set_LOA(const struct afb_xreq *xreq, unsigned level, int result)
481 _HOOK_XREQ_(session_set_LOA, xreq, level, result);
485 int afb_hook_xreq_subscribe(const struct afb_xreq *xreq, struct afb_event_x2 *event_x2, int result)
487 _HOOK_XREQ_(subscribe, xreq, event_x2, result);
491 int afb_hook_xreq_unsubscribe(const struct afb_xreq *xreq, struct afb_event_x2 *event_x2, int result)
493 _HOOK_XREQ_(unsubscribe, xreq, event_x2, result);
497 void afb_hook_xreq_subcall(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, int flags)
499 _HOOK_XREQ_(subcall, xreq, api, verb, args);
502 void afb_hook_xreq_subcall_result(const struct afb_xreq *xreq, struct json_object *object, const char *error, const char *info)
504 _HOOK_XREQ_(subcall_result, xreq, object, error, info);
507 void afb_hook_xreq_subcallsync(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, int flags)
509 _HOOK_XREQ_(subcallsync, xreq, api, verb, args);
512 int afb_hook_xreq_subcallsync_result(const struct afb_xreq *xreq, int status, struct json_object *object, const char *error, const char *info)
514 _HOOK_XREQ_(subcallsync_result, xreq, status, object, error, info);
518 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)
520 _HOOK_XREQ_(vverbose, xreq, level, file ?: "?", line, func ?: "?", fmt, args);
523 void afb_hook_xreq_legacy_store(const struct afb_xreq *xreq, struct afb_stored_req *sreq)
525 _HOOK_XREQ_(legacy_store, xreq, sreq);
528 void afb_hook_xreq_legacy_unstore(const struct afb_xreq *xreq)
530 _HOOK_XREQ_(legacy_unstore, xreq);
533 int afb_hook_xreq_has_permission(const struct afb_xreq *xreq, const char *permission, int result)
535 _HOOK_XREQ_(has_permission, xreq, permission, result);
539 char *afb_hook_xreq_get_application_id(const struct afb_xreq *xreq, char *result)
541 _HOOK_XREQ_(get_application_id, xreq, result);
545 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)
547 _HOOK_XREQ_(context_make, xreq, replace, create_value, free_value, create_closure, result);
551 int afb_hook_xreq_get_uid(const struct afb_xreq *xreq, int result)
553 _HOOK_XREQ_(get_uid, xreq, result);
557 struct json_object *afb_hook_xreq_get_client_info(const struct afb_xreq *xreq, struct json_object *result)
559 _HOOK_XREQ_(get_client_info, xreq, result);
563 /******************************************************************************
564 * section: hooking xreqs
565 *****************************************************************************/
567 void afb_hook_init_xreq(struct afb_xreq *xreq)
573 struct afb_hook_xreq *hook;
575 /* scan hook list to get the expected flags */
577 pthread_rwlock_rdlock(&rwlock);
578 hook = list_of_xreq_hooks;
580 f = hook->flags & afb_hook_flags_req_all;
582 && (!hook->session || hook->session == xreq->context.session)
583 && MATCH_API(hook->api, xreq->request.called_api)
584 && MATCH_VERB(hook->verb, xreq->request.called_verb);
589 pthread_rwlock_unlock(&rwlock);
591 /* store the hooking data */
592 xreq->hookflags = flags;
594 pthread_rwlock_wrlock(&rwlock);
597 xreq->hookindex = reqindex;
598 pthread_rwlock_unlock(&rwlock);
602 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)
604 struct afb_hook_xreq *hook;
606 /* alloc the result */
607 hook = calloc(1, sizeof *hook);
611 /* get a copy of the names */
612 hook->api = api ? strdup(api) : NULL;
613 hook->verb = verb ? strdup(verb) : NULL;
614 if ((api && !hook->api) || (verb && !hook->verb)) {
621 /* initialise the rest */
622 hook->session = session;
624 afb_session_addref(session);
627 hook->itf = itf ? itf : &hook_xreq_default_itf;
628 hook->closure = closure;
630 /* record the hook */
631 pthread_rwlock_wrlock(&rwlock);
632 hook->next = list_of_xreq_hooks;
633 list_of_xreq_hooks = hook;
634 pthread_rwlock_unlock(&rwlock);
640 struct afb_hook_xreq *afb_hook_addref_xreq(struct afb_hook_xreq *hook)
642 pthread_rwlock_wrlock(&rwlock);
644 pthread_rwlock_unlock(&rwlock);
648 void afb_hook_unref_xreq(struct afb_hook_xreq *hook)
650 struct afb_hook_xreq **prv;
653 pthread_rwlock_wrlock(&rwlock);
654 if (--hook->refcount)
658 prv = &list_of_xreq_hooks;
659 while (*prv && *prv != hook)
664 pthread_rwlock_unlock(&rwlock);
670 afb_session_unref(hook->session);
676 /******************************************************************************
677 * section: default callbacks for tracing daemon interface
678 *****************************************************************************/
680 static void _hook_api_(const struct afb_export *export, const char *format, ...)
683 va_start(ap, format);
684 _hook_("api-%s", format, ap, afb_export_apiname(export));
688 static void hook_api_event_broadcast_before_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, struct json_object *object)
690 _hook_api_(export, "event_broadcast.before(%s, %s)....", name, json_object_to_json_string(object));
693 static void hook_api_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)
695 _hook_api_(export, "event_broadcast.after(%s, %s) -> %d", name, json_object_to_json_string(object), result);
698 static void hook_api_get_event_loop_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_event *result)
700 _hook_api_(export, "get_event_loop() -> %p", result);
703 static void hook_api_get_user_bus_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_bus *result)
705 _hook_api_(export, "get_user_bus() -> %p", result);
708 static void hook_api_get_system_bus_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_bus *result)
710 _hook_api_(export, "get_system_bus() -> %p", result);
713 static void hook_api_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)
720 len = vasprintf(&msg, fmt, ap);
724 _hook_api_(export, "vverbose(%d:%s, %s, %d, %s) -> %s ? ? ?", level, verbose_name_of_level(level), file, line, function, fmt);
726 _hook_api_(export, "vverbose(%d:%s, %s, %d, %s) -> %s", level, verbose_name_of_level(level), file, line, function, msg);
731 static void hook_api_event_make_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, struct afb_event_x2 *result)
733 _hook_api_(export, "event_make(%s) -> %s:%d", name, afb_evt_event_x2_fullname(result), afb_evt_event_x2_id(result));
736 static void hook_api_rootdir_get_fd_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result)
738 char path[PATH_MAX], proc[100];
740 _hook_api_(export, "rootdir_get_fd() -> %d, %m", result);
742 snprintf(proc, sizeof proc, "/proc/self/fd/%d", result);
743 readlink(proc, path, sizeof path);
744 _hook_api_(export, "rootdir_get_fd() -> %d = %s", result, path);
748 static void hook_api_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)
750 char path[PATH_MAX], proc[100];
754 _hook_api_(export, "rootdir_open_locale(%s, %d, %s) -> %d, %m", filename, flags, locale, result);
756 snprintf(proc, sizeof proc, "/proc/self/fd/%d", result);
757 readlink(proc, path, sizeof path);
758 _hook_api_(export, "rootdir_open_locale(%s, %d, %s) -> %d = %s", filename, flags, locale, result, path);
762 static void hook_api_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)
764 _hook_api_(export, "queue_job(%p, %p, %p, %d) -> %d", callback, argument, group, timeout, result);
767 static void hook_api_unstore_req_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct afb_stored_req *sreq)
769 _hook_api_(export, "unstore_req(%p)", sreq);
772 static void hook_api_require_api_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, int initialized)
774 _hook_api_(export, "require_api(%s, %d)...", name, initialized);
777 static void hook_api_require_api_result_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, int initialized, int result)
779 _hook_api_(export, "...require_api(%s, %d) -> %d", name, initialized, result);
782 static void hook_api_add_alias_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *api, const char *alias, int result)
784 _hook_api_(export, "add_alias(%s -> %s) -> %d", api, alias?:"<null>", result);
787 static void hook_api_start_before_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export)
789 _hook_api_(export, "start.before");
792 static void hook_api_start_after_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int status)
794 _hook_api_(export, "start.after -> %d", status);
797 static void hook_api_on_event_before_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *event, int event_x2, struct json_object *object)
799 _hook_api_(export, "on_event.before(%s, %d, %s)", event, event_x2, json_object_to_json_string(object));
802 static void hook_api_on_event_after_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *event, int event_x2, struct json_object *object)
804 _hook_api_(export, "on_event.after(%s, %d, %s)", event, event_x2, json_object_to_json_string(object));
807 static void hook_api_call_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *api, const char *verb, struct json_object *args)
809 _hook_api_(export, "call(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
812 static void hook_api_call_result_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct json_object *object, const char *error, const char *info)
814 _hook_api_(export, " ...call... [%s] -> %s (%s)", error?:"success", json_object_to_json_string(object), info?:"");
817 static void hook_api_callsync_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *api, const char *verb, struct json_object *args)
819 _hook_api_(export, "callsync(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
822 static void hook_api_callsync_result_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int status, struct json_object *object, const char *error, const char *info)
824 _hook_api_(export, " ...callsync... %d [%s] -> %s (%s)", status, error?:"success", json_object_to_json_string(object), info?:"");
827 static void hook_api_new_api_before_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *api, const char *info, int noconcurrency)
829 _hook_api_(export, "new_api.before %s (%s)%s ...", api, info?:"", noconcurrency?" no-concurrency" : "");
832 static void hook_api_new_api_after_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *api)
834 _hook_api_(export, "... new_api.after %s -> %s (%d)", api, result >= 0 ? "OK" : "ERROR", result);
837 static void hook_api_api_set_verbs_v2_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const struct afb_verb_v2 *verbs)
839 _hook_api_(export, "set_verbs_v2 -> %s (%d)", result >= 0 ? "OK" : "ERROR", result);
842 static void hook_api_api_set_verbs_v3_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const struct afb_verb_v3 *verbs)
844 _hook_api_(export, "set_verbs_v3 -> %s (%d)", result >= 0 ? "OK" : "ERROR", result);
847 static void hook_api_api_add_verb_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *verb, const char *info, int glob)
849 _hook_api_(export, "add_verb(%s%s [%s]) -> %s (%d)", verb, glob?" (GLOB)":"", info?:"", result >= 0 ? "OK" : "ERROR", result);
852 static void hook_api_api_del_verb_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *verb)
854 _hook_api_(export, "del_verb(%s) -> %s (%d)", verb, result >= 0 ? "OK" : "ERROR", result);
857 static void hook_api_api_set_on_event_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result)
859 _hook_api_(export, "set_on_event -> %s (%d)", result >= 0 ? "OK" : "ERROR", result);
862 static void hook_api_api_set_on_init_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result)
864 _hook_api_(export, "set_on_init -> %s (%d)", result >= 0 ? "OK" : "ERROR", result);
867 static void hook_api_api_seal_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export)
869 _hook_api_(export, "seal");
872 static void hook_api_event_handler_add_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *pattern)
874 _hook_api_(export, "event_handler_add(%s) -> %s (%d)", pattern, result >= 0 ? "OK" : "ERROR", result);
877 static void hook_api_event_handler_del_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *pattern)
879 _hook_api_(export, "event_handler_del(%s) -> %s (%d)", pattern, result >= 0 ? "OK" : "ERROR", result);
882 static void hook_api_class_provide_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *name)
884 _hook_api_(export, "class_provide(%s) -> %s (%d)", name, result >= 0 ? "OK" : "ERROR", result);
887 static void hook_api_class_require_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *name)
889 _hook_api_(export, "class_require(%s) -> %s (%d)", name, result >= 0 ? "OK" : "ERROR", result);
892 static void hook_api_delete_api_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result)
894 _hook_api_(export, "delete_api -> %s (%d)", result >= 0 ? "OK" : "ERROR", result);
897 static void hook_api_on_event_handler_before_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *event, int event_x2, struct json_object *object, const char *pattern)
899 _hook_api_(export, "on_event_handler[%s].before(%s, %d, %s)", pattern, event, event_x2, json_object_to_json_string(object));
902 static void hook_api_on_event_handler_after_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *event, int event_x2, struct json_object *object, const char *pattern)
904 _hook_api_(export, "on_event_handler[%s].after(%s, %d, %s)", pattern, event, event_x2, json_object_to_json_string(object));
907 static struct afb_hook_api_itf hook_api_default_itf = {
908 .hook_api_event_broadcast_before = hook_api_event_broadcast_before_cb,
909 .hook_api_event_broadcast_after = hook_api_event_broadcast_after_cb,
910 .hook_api_get_event_loop = hook_api_get_event_loop_cb,
911 .hook_api_get_user_bus = hook_api_get_user_bus_cb,
912 .hook_api_get_system_bus = hook_api_get_system_bus_cb,
913 .hook_api_vverbose = hook_api_vverbose_cb,
914 .hook_api_event_make = hook_api_event_make_cb,
915 .hook_api_rootdir_get_fd = hook_api_rootdir_get_fd_cb,
916 .hook_api_rootdir_open_locale = hook_api_rootdir_open_locale_cb,
917 .hook_api_queue_job = hook_api_queue_job_cb,
918 .hook_api_legacy_unstore_req = hook_api_unstore_req_cb,
919 .hook_api_require_api = hook_api_require_api_cb,
920 .hook_api_require_api_result = hook_api_require_api_result_cb,
921 .hook_api_add_alias = hook_api_add_alias_cb,
922 .hook_api_start_before = hook_api_start_before_cb,
923 .hook_api_start_after = hook_api_start_after_cb,
924 .hook_api_on_event_before = hook_api_on_event_before_cb,
925 .hook_api_on_event_after = hook_api_on_event_after_cb,
926 .hook_api_call = hook_api_call_cb,
927 .hook_api_call_result = hook_api_call_result_cb,
928 .hook_api_callsync = hook_api_callsync_cb,
929 .hook_api_callsync_result = hook_api_callsync_result_cb,
930 .hook_api_new_api_before = hook_api_new_api_before_cb,
931 .hook_api_new_api_after = hook_api_new_api_after_cb,
932 .hook_api_api_set_verbs_v2 = hook_api_api_set_verbs_v2_cb,
933 .hook_api_api_set_verbs_v3 = hook_api_api_set_verbs_v3_cb,
934 .hook_api_api_add_verb = hook_api_api_add_verb_cb,
935 .hook_api_api_del_verb = hook_api_api_del_verb_cb,
936 .hook_api_api_set_on_event = hook_api_api_set_on_event_cb,
937 .hook_api_api_set_on_init = hook_api_api_set_on_init_cb,
938 .hook_api_api_seal = hook_api_api_seal_cb,
939 .hook_api_event_handler_add = hook_api_event_handler_add_cb,
940 .hook_api_event_handler_del = hook_api_event_handler_del_cb,
941 .hook_api_class_provide = hook_api_class_provide_cb,
942 .hook_api_class_require = hook_api_class_require_cb,
943 .hook_api_delete_api = hook_api_delete_api_cb,
944 .hook_api_on_event_handler_before = hook_api_on_event_handler_before_cb,
945 .hook_api_on_event_handler_after = hook_api_on_event_handler_after_cb,
948 /******************************************************************************
949 * section: hooks for tracing daemon interface (export)
950 *****************************************************************************/
952 #define _HOOK_API_2_(flag,func,...) \
953 struct afb_hook_api *hook; \
954 struct afb_hookid hookid; \
955 const char *apiname = afb_export_apiname(export); \
956 pthread_rwlock_rdlock(&rwlock); \
957 init_hookid(&hookid); \
958 hook = list_of_api_hooks; \
960 if (hook->itf->hook_api_##func \
961 && (hook->flags & afb_hook_flag_api_##flag) != 0 \
962 && MATCH_API(hook->api, apiname)) { \
963 hook->itf->hook_api_##func(hook->closure, &hookid, __VA_ARGS__); \
967 pthread_rwlock_unlock(&rwlock);
969 #define _HOOK_API_(what,...) _HOOK_API_2_(what,what,__VA_ARGS__)
971 void afb_hook_api_event_broadcast_before(const struct afb_export *export, const char *name, struct json_object *object)
973 _HOOK_API_2_(event_broadcast, event_broadcast_before, export, name, object);
976 int afb_hook_api_event_broadcast_after(const struct afb_export *export, const char *name, struct json_object *object, int result)
978 _HOOK_API_2_(event_broadcast, event_broadcast_after, export, name, object, result);
982 struct sd_event *afb_hook_api_get_event_loop(const struct afb_export *export, struct sd_event *result)
984 _HOOK_API_(get_event_loop, export, result);
988 struct sd_bus *afb_hook_api_get_user_bus(const struct afb_export *export, struct sd_bus *result)
990 _HOOK_API_(get_user_bus, export, result);
994 struct sd_bus *afb_hook_api_get_system_bus(const struct afb_export *export, struct sd_bus *result)
996 _HOOK_API_(get_system_bus, export, result);
1000 void afb_hook_api_vverbose(const struct afb_export *export, int level, const char *file, int line, const char *function, const char *fmt, va_list args)
1002 _HOOK_API_(vverbose, export, level, file, line, function, fmt, args);
1005 struct afb_event_x2 *afb_hook_api_event_make(const struct afb_export *export, const char *name, struct afb_event_x2 *result)
1007 _HOOK_API_(event_make, export, name, result);
1011 int afb_hook_api_rootdir_get_fd(const struct afb_export *export, int result)
1013 _HOOK_API_(rootdir_get_fd, export, result);
1017 int afb_hook_api_rootdir_open_locale(const struct afb_export *export, const char *filename, int flags, const char *locale, int result)
1019 _HOOK_API_(rootdir_open_locale, export, filename, flags, locale, result);
1023 int afb_hook_api_queue_job(const struct afb_export *export, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout, int result)
1025 _HOOK_API_(queue_job, export, callback, argument, group, timeout, result);
1029 void afb_hook_api_legacy_unstore_req(const struct afb_export *export, struct afb_stored_req *sreq)
1031 _HOOK_API_(legacy_unstore_req, export, sreq);
1034 void afb_hook_api_require_api(const struct afb_export *export, const char *name, int initialized)
1036 _HOOK_API_(require_api, export, name, initialized);
1039 int afb_hook_api_require_api_result(const struct afb_export *export, const char *name, int initialized, int result)
1041 _HOOK_API_2_(require_api, require_api_result, export, name, initialized, result);
1045 int afb_hook_api_add_alias(const struct afb_export *export, const char *api, const char *alias, int result)
1047 _HOOK_API_(add_alias, export, api, alias, result);
1051 void afb_hook_api_start_before(const struct afb_export *export)
1053 _HOOK_API_2_(start, start_before, export);
1056 int afb_hook_api_start_after(const struct afb_export *export, int status)
1058 _HOOK_API_2_(start, start_after, export, status);
1062 void afb_hook_api_on_event_before(const struct afb_export *export, const char *event, int event_x2, struct json_object *object)
1064 _HOOK_API_2_(on_event, on_event_before, export, event, event_x2, object);
1067 void afb_hook_api_on_event_after(const struct afb_export *export, const char *event, int event_x2, struct json_object *object)
1069 _HOOK_API_2_(on_event, on_event_after, export, event, event_x2, object);
1072 void afb_hook_api_call(const struct afb_export *export, const char *api, const char *verb, struct json_object *args)
1074 _HOOK_API_(call, export, api, verb, args);
1077 void afb_hook_api_call_result(const struct afb_export *export, struct json_object *object, const char*error, const char *info)
1079 _HOOK_API_2_(call, call_result, export, object, error, info);
1083 void afb_hook_api_callsync(const struct afb_export *export, const char *api, const char *verb, struct json_object *args)
1085 _HOOK_API_(callsync, export, api, verb, args);
1088 int afb_hook_api_callsync_result(const struct afb_export *export, int status, struct json_object *object, const char *error, const char *info)
1090 _HOOK_API_2_(callsync, callsync_result, export, status, object, error, info);
1094 void afb_hook_api_new_api_before(const struct afb_export *export, const char *api, const char *info, int noconcurrency)
1096 _HOOK_API_2_(new_api, new_api_before, export, api, info, noconcurrency);
1099 int afb_hook_api_new_api_after(const struct afb_export *export, int result, const char *api)
1101 _HOOK_API_2_(new_api, new_api_after, export, result, api);
1105 int afb_hook_api_api_set_verbs_v2(const struct afb_export *export, int result, const struct afb_verb_v2 *verbs)
1107 _HOOK_API_2_(api_set_verbs, api_set_verbs_v2, export, result, verbs);
1111 int afb_hook_api_api_set_verbs_v3(const struct afb_export *export, int result, const struct afb_verb_v3 *verbs)
1113 _HOOK_API_2_(api_set_verbs, api_set_verbs_v3, export, result, verbs);
1117 int afb_hook_api_api_add_verb(const struct afb_export *export, int result, const char *verb, const char *info, int glob)
1119 _HOOK_API_(api_add_verb, export, result, verb, info, glob);
1123 int afb_hook_api_api_del_verb(const struct afb_export *export, int result, const char *verb)
1125 _HOOK_API_(api_del_verb, export, result, verb);
1129 int afb_hook_api_api_set_on_event(const struct afb_export *export, int result)
1131 _HOOK_API_(api_set_on_event, export, result);
1135 int afb_hook_api_api_set_on_init(const struct afb_export *export, int result)
1137 _HOOK_API_(api_set_on_init, export, result);
1141 void afb_hook_api_api_seal(const struct afb_export *export)
1143 _HOOK_API_(api_seal, export);
1146 int afb_hook_api_event_handler_add(const struct afb_export *export, int result, const char *pattern)
1148 _HOOK_API_(event_handler_add, export, result, pattern);
1151 int afb_hook_api_event_handler_del(const struct afb_export *export, int result, const char *pattern)
1153 _HOOK_API_(event_handler_del, export, result, pattern);
1156 int afb_hook_api_class_provide(const struct afb_export *export, int result, const char *name)
1158 _HOOK_API_(class_provide, export, result, name);
1161 int afb_hook_api_class_require(const struct afb_export *export, int result, const char *name)
1163 _HOOK_API_(class_require, export, result, name);
1167 int afb_hook_api_delete_api(const struct afb_export *export, int result)
1169 _HOOK_API_(delete_api, export, result);
1173 void afb_hook_api_on_event_handler_before(const struct afb_export *export, const char *event, int event_x2, struct json_object *object, const char *pattern)
1175 _HOOK_API_2_(on_event_handler, on_event_handler_before, export, event, event_x2, object, pattern);
1178 void afb_hook_api_on_event_handler_after(const struct afb_export *export, const char *event, int event_x2, struct json_object *object, const char *pattern)
1180 _HOOK_API_2_(on_event_handler, on_event_handler_after, export, event, event_x2, object, pattern);
1183 /******************************************************************************
1184 * section: hooking export
1185 *****************************************************************************/
1187 int afb_hook_flags_api(const char *api)
1190 struct afb_hook_api *hook;
1193 pthread_rwlock_rdlock(&rwlock);
1194 hook = list_of_api_hooks;
1196 if (!api || MATCH_API(hook->api, api))
1197 flags |= hook->flags;
1200 pthread_rwlock_unlock(&rwlock);
1204 struct afb_hook_api *afb_hook_create_api(const char *api, int flags, struct afb_hook_api_itf *itf, void *closure)
1206 struct afb_hook_api *hook;
1208 /* alloc the result */
1209 hook = calloc(1, sizeof *hook);
1213 /* get a copy of the names */
1214 hook->api = api ? strdup(api) : NULL;
1215 if (api && !hook->api) {
1220 /* initialise the rest */
1222 hook->flags = flags;
1223 hook->itf = itf ? itf : &hook_api_default_itf;
1224 hook->closure = closure;
1226 /* record the hook */
1227 pthread_rwlock_wrlock(&rwlock);
1228 hook->next = list_of_api_hooks;
1229 list_of_api_hooks = hook;
1230 pthread_rwlock_unlock(&rwlock);
1236 struct afb_hook_api *afb_hook_addref_api(struct afb_hook_api *hook)
1238 pthread_rwlock_wrlock(&rwlock);
1240 pthread_rwlock_unlock(&rwlock);
1244 void afb_hook_unref_api(struct afb_hook_api *hook)
1246 struct afb_hook_api **prv;
1249 pthread_rwlock_wrlock(&rwlock);
1250 if (--hook->refcount)
1254 prv = &list_of_api_hooks;
1255 while (*prv && *prv != hook)
1256 prv = &(*prv)->next;
1260 pthread_rwlock_unlock(&rwlock);
1269 /******************************************************************************
1270 * section: default callbacks for tracing service interface (evt)
1271 *****************************************************************************/
1273 static void _hook_evt_(const char *evt, int id, const char *format, ...)
1276 va_start(ap, format);
1277 _hook_("evt-%s:%d", format, ap, evt, id);
1281 static void hook_evt_create_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
1283 _hook_evt_(evt, id, "create");
1286 static void hook_evt_push_before_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj)
1288 _hook_evt_(evt, id, "push.before(%s)", json_object_to_json_string(obj));
1292 static void hook_evt_push_after_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj, int result)
1294 _hook_evt_(evt, id, "push.after(%s) -> %d", json_object_to_json_string(obj), result);
1297 static void hook_evt_broadcast_before_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj)
1299 _hook_evt_(evt, id, "broadcast.before(%s)", json_object_to_json_string(obj));
1302 static void hook_evt_broadcast_after_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj, int result)
1304 _hook_evt_(evt, id, "broadcast.after(%s) -> %d", json_object_to_json_string(obj), result);
1307 static void hook_evt_name_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, const char *result)
1309 _hook_evt_(evt, id, "name -> %s", result);
1312 static void hook_evt_addref_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
1314 _hook_evt_(evt, id, "addref");
1317 static void hook_evt_unref_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
1319 _hook_evt_(evt, id, "unref");
1322 static struct afb_hook_evt_itf hook_evt_default_itf = {
1323 .hook_evt_create = hook_evt_create_cb,
1324 .hook_evt_push_before = hook_evt_push_before_cb,
1325 .hook_evt_push_after = hook_evt_push_after_cb,
1326 .hook_evt_broadcast_before = hook_evt_broadcast_before_cb,
1327 .hook_evt_broadcast_after = hook_evt_broadcast_after_cb,
1328 .hook_evt_name = hook_evt_name_cb,
1329 .hook_evt_addref = hook_evt_addref_cb,
1330 .hook_evt_unref = hook_evt_unref_cb
1333 /******************************************************************************
1334 * section: hooks for tracing events interface (evt)
1335 *****************************************************************************/
1337 #define _HOOK_EVT_(what,...) \
1338 struct afb_hook_evt *hook; \
1339 struct afb_hookid hookid; \
1340 pthread_rwlock_rdlock(&rwlock); \
1341 init_hookid(&hookid); \
1342 hook = list_of_evt_hooks; \
1344 if (hook->itf->hook_evt_##what \
1345 && (hook->flags & afb_hook_flag_evt_##what) != 0 \
1346 && MATCH_EVENT(hook->pattern, evt)) { \
1347 hook->itf->hook_evt_##what(hook->closure, &hookid, __VA_ARGS__); \
1349 hook = hook->next; \
1351 pthread_rwlock_unlock(&rwlock);
1353 void afb_hook_evt_create(const char *evt, int id)
1355 _HOOK_EVT_(create, evt, id);
1358 void afb_hook_evt_push_before(const char *evt, int id, struct json_object *obj)
1360 _HOOK_EVT_(push_before, evt, id, obj);
1363 int afb_hook_evt_push_after(const char *evt, int id, struct json_object *obj, int result)
1365 _HOOK_EVT_(push_after, evt, id, obj, result);
1369 void afb_hook_evt_broadcast_before(const char *evt, int id, struct json_object *obj)
1371 _HOOK_EVT_(broadcast_before, evt, id, obj);
1374 int afb_hook_evt_broadcast_after(const char *evt, int id, struct json_object *obj, int result)
1376 _HOOK_EVT_(broadcast_after, evt, id, obj, result);
1380 void afb_hook_evt_name(const char *evt, int id, const char *result)
1382 _HOOK_EVT_(name, evt, id, result);
1385 void afb_hook_evt_addref(const char *evt, int id)
1387 _HOOK_EVT_(addref, evt, id);
1390 void afb_hook_evt_unref(const char *evt, int id)
1392 _HOOK_EVT_(unref, evt, id);
1395 /******************************************************************************
1396 * section: hooking services (evt)
1397 *****************************************************************************/
1399 int afb_hook_flags_evt(const char *name)
1402 struct afb_hook_evt *hook;
1405 pthread_rwlock_rdlock(&rwlock);
1406 hook = list_of_evt_hooks;
1408 if (!name || MATCH_EVENT(hook->pattern, name))
1409 flags |= hook->flags;
1412 pthread_rwlock_unlock(&rwlock);
1416 struct afb_hook_evt *afb_hook_create_evt(const char *pattern, int flags, struct afb_hook_evt_itf *itf, void *closure)
1418 struct afb_hook_evt *hook;
1420 /* alloc the result */
1421 hook = calloc(1, sizeof *hook);
1425 /* get a copy of the names */
1426 hook->pattern = pattern ? strdup(pattern) : NULL;
1427 if (pattern && !hook->pattern) {
1432 /* initialise the rest */
1434 hook->flags = flags;
1435 hook->itf = itf ? itf : &hook_evt_default_itf;
1436 hook->closure = closure;
1438 /* record the hook */
1439 pthread_rwlock_wrlock(&rwlock);
1440 hook->next = list_of_evt_hooks;
1441 list_of_evt_hooks = hook;
1442 pthread_rwlock_unlock(&rwlock);
1448 struct afb_hook_evt *afb_hook_addref_evt(struct afb_hook_evt *hook)
1450 pthread_rwlock_wrlock(&rwlock);
1452 pthread_rwlock_unlock(&rwlock);
1456 void afb_hook_unref_evt(struct afb_hook_evt *hook)
1458 struct afb_hook_evt **prv;
1461 pthread_rwlock_wrlock(&rwlock);
1462 if (--hook->refcount)
1466 prv = &list_of_evt_hooks;
1467 while (*prv && *prv != hook)
1468 prv = &(*prv)->next;
1472 pthread_rwlock_unlock(&rwlock);
1475 free(hook->pattern);
1481 /******************************************************************************
1482 * section: default callbacks for sessions (session)
1483 *****************************************************************************/
1485 static void _hook_session_(struct afb_session *session, const char *format, ...)
1488 va_start(ap, format);
1489 _hook_("session-%s", format, ap, afb_session_uuid(session));
1493 static void hook_session_create_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1495 _hook_session_(session, "create -> token=%s", afb_session_token(session));
1498 static void hook_session_close_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1500 _hook_session_(session, "close");
1503 static void hook_session_destroy_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1505 _hook_session_(session, "destroy");
1508 static void hook_session_renew_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1510 _hook_session_(session, "renew -> token=%s", afb_session_token(session));
1513 static void hook_session_addref_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1515 _hook_session_(session, "addref");
1518 static void hook_session_unref_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1520 _hook_session_(session, "unref");
1523 static struct afb_hook_session_itf hook_session_default_itf = {
1524 .hook_session_create = hook_session_create_cb,
1525 .hook_session_close = hook_session_close_cb,
1526 .hook_session_destroy = hook_session_destroy_cb,
1527 .hook_session_renew = hook_session_renew_cb,
1528 .hook_session_addref = hook_session_addref_cb,
1529 .hook_session_unref = hook_session_unref_cb
1532 /******************************************************************************
1533 * section: hooks for tracing sessions (session)
1534 *****************************************************************************/
1536 #define _HOOK_SESSION_(what,...) \
1537 struct afb_hook_session *hook; \
1538 struct afb_hookid hookid; \
1539 const char *sessid = 0; \
1540 pthread_rwlock_rdlock(&rwlock); \
1541 init_hookid(&hookid); \
1542 hook = list_of_session_hooks; \
1544 if (hook->itf->hook_session_##what \
1545 && (hook->flags & afb_hook_flag_session_##what) != 0 \
1546 && MATCH_SESSION(hook->pattern, (sessid?:(sessid=afb_session_uuid(session))))) { \
1547 hook->itf->hook_session_##what(hook->closure, &hookid, __VA_ARGS__); \
1549 hook = hook->next; \
1551 pthread_rwlock_unlock(&rwlock);
1553 void afb_hook_session_create(struct afb_session *session)
1555 _HOOK_SESSION_(create, session);
1558 void afb_hook_session_close(struct afb_session *session)
1560 _HOOK_SESSION_(close, session);
1563 void afb_hook_session_destroy(struct afb_session *session)
1565 _HOOK_SESSION_(destroy, session);
1568 void afb_hook_session_renew(struct afb_session *session)
1570 _HOOK_SESSION_(renew, session);
1573 void afb_hook_session_addref(struct afb_session *session)
1575 _HOOK_SESSION_(addref, session);
1578 void afb_hook_session_unref(struct afb_session *session)
1580 _HOOK_SESSION_(unref, session);
1584 /******************************************************************************
1585 * section: hooking sessions (session)
1586 *****************************************************************************/
1588 struct afb_hook_session *afb_hook_create_session(const char *pattern, int flags, struct afb_hook_session_itf *itf, void *closure)
1590 struct afb_hook_session *hook;
1592 /* alloc the result */
1593 hook = calloc(1, sizeof *hook);
1597 /* get a copy of the names */
1598 hook->pattern = pattern ? strdup(pattern) : NULL;
1599 if (pattern && !hook->pattern) {
1604 /* initialise the rest */
1606 hook->flags = flags;
1607 hook->itf = itf ? itf : &hook_session_default_itf;
1608 hook->closure = closure;
1610 /* record the hook */
1611 pthread_rwlock_wrlock(&rwlock);
1612 hook->next = list_of_session_hooks;
1613 list_of_session_hooks = hook;
1614 pthread_rwlock_unlock(&rwlock);
1620 struct afb_hook_session *afb_hook_addref_session(struct afb_hook_session *hook)
1622 pthread_rwlock_wrlock(&rwlock);
1624 pthread_rwlock_unlock(&rwlock);
1628 void afb_hook_unref_session(struct afb_hook_session *hook)
1630 struct afb_hook_session **prv;
1633 pthread_rwlock_wrlock(&rwlock);
1634 if (--hook->refcount)
1638 prv = &list_of_session_hooks;
1639 while (*prv && *prv != hook)
1640 prv = &(*prv)->next;
1644 pthread_rwlock_unlock(&rwlock);
1647 free(hook->pattern);
1653 /******************************************************************************
1654 * section: default callbacks for globals (global)
1655 *****************************************************************************/
1657 static void _hook_global_(const char *format, ...)
1660 va_start(ap, format);
1661 _hook_("global", format, ap);
1665 static void hook_global_vverbose_cb(void *closure, const struct afb_hookid *hookid, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
1672 len = vasprintf(&msg, fmt, ap);
1676 _hook_global_("vverbose(%d:%s, %s, %d, %s) -> %s ? ? ?", level, verbose_name_of_level(level), file, line, func, fmt);
1678 _hook_global_("vverbose(%d:%s, %s, %d, %s) -> %s", level, verbose_name_of_level(level), file, line, func, msg);
1683 static struct afb_hook_global_itf hook_global_default_itf = {
1684 .hook_global_vverbose = hook_global_vverbose_cb
1687 /******************************************************************************
1688 * section: hooks for tracing globals (global)
1689 *****************************************************************************/
1691 #define _HOOK_GLOBAL_(what,...) \
1692 struct afb_hook_global *hook; \
1693 struct afb_hookid hookid; \
1694 pthread_rwlock_rdlock(&rwlock); \
1695 init_hookid(&hookid); \
1696 hook = list_of_global_hooks; \
1698 if (hook->itf->hook_global_##what \
1699 && (hook->flags & afb_hook_flag_global_##what) != 0) { \
1700 hook->itf->hook_global_##what(hook->closure, &hookid, __VA_ARGS__); \
1702 hook = hook->next; \
1704 pthread_rwlock_unlock(&rwlock);
1706 static void afb_hook_global_vverbose(int level, const char *file, int line, const char *func, const char *fmt, va_list args)
1708 _HOOK_GLOBAL_(vverbose, level, file ?: "?", line, func ?: "?", fmt ?: "", args);
1711 /******************************************************************************
1712 * section: hooking globals (global)
1713 *****************************************************************************/
1715 static void update_global()
1717 struct afb_hook_global *hook;
1720 pthread_rwlock_rdlock(&rwlock);
1721 hook = list_of_global_hooks;
1723 flags = hook->flags;
1726 verbose_observer = (flags & afb_hook_flag_global_vverbose) ? afb_hook_global_vverbose : NULL;
1727 pthread_rwlock_unlock(&rwlock);
1730 struct afb_hook_global *afb_hook_create_global(int flags, struct afb_hook_global_itf *itf, void *closure)
1732 struct afb_hook_global *hook;
1734 /* alloc the result */
1735 hook = calloc(1, sizeof *hook);
1739 /* initialise the rest */
1741 hook->flags = flags;
1742 hook->itf = itf ? itf : &hook_global_default_itf;
1743 hook->closure = closure;
1745 /* record the hook */
1746 pthread_rwlock_wrlock(&rwlock);
1747 hook->next = list_of_global_hooks;
1748 list_of_global_hooks = hook;
1749 pthread_rwlock_unlock(&rwlock);
1751 /* update hooking */
1758 struct afb_hook_global *afb_hook_addref_global(struct afb_hook_global *hook)
1760 pthread_rwlock_wrlock(&rwlock);
1762 pthread_rwlock_unlock(&rwlock);
1766 void afb_hook_unref_global(struct afb_hook_global *hook)
1768 struct afb_hook_global **prv;
1771 pthread_rwlock_wrlock(&rwlock);
1772 if (--hook->refcount)
1776 prv = &list_of_global_hooks;
1777 while (*prv && *prv != hook)
1778 prv = &(*prv)->next;
1782 pthread_rwlock_unlock(&rwlock);
1787 /* update hooking */