2 * Copyright (C) 2015-2020 "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.
18 #if WITH_AFB_HOOK /***********************************************************/
30 #include <json-c/json.h>
31 #if !defined(JSON_C_TO_STRING_NOSLASHESCAPE)
32 #define JSON_C_TO_STRING_NOSLASHESCAPE 0
35 #include <afb/afb-req-x1.h>
36 #include <afb/afb-event-x2.h>
38 #include "afb-context.h"
40 #include "afb-session.h"
43 #include "afb-export.h"
46 #include "afb-msg-json.h"
50 #define MATCH(pattern,string) (\
52 ? !fnmatch((pattern),(string),FNM_CASEFOLD|FNM_EXTMATCH|FNM_PERIOD) \
53 : afb_api_is_public(string))
55 #define MATCH_API(pattern,string) MATCH(pattern,string)
56 #define MATCH_VERB(pattern,string) MATCH(pattern,string)
57 #define MATCH_EVENT(pattern,string) MATCH(pattern,string)
58 #define MATCH_SESSION(pattern,string) MATCH(pattern,string)
61 * Definition of a hook for xreq
63 struct afb_hook_xreq {
64 struct afb_hook_xreq *next; /**< next hook */
65 unsigned refcount; /**< reference count */
66 unsigned flags; /**< hook flags */
67 char *api; /**< api hooked or NULL for any */
68 char *verb; /**< verb hooked or NULL for any */
69 struct afb_session *session; /**< session hooked or NULL if any */
70 struct afb_hook_xreq_itf *itf; /**< interface of hook */
71 void *closure; /**< closure for callbacks */
75 * Definition of a hook for export
78 struct afb_hook_api *next; /**< next hook */
79 unsigned refcount; /**< reference count */
80 unsigned flags; /**< hook flags */
81 char *api; /**< api hooked or NULL for any */
82 struct afb_hook_api_itf *itf; /**< interface of hook */
83 void *closure; /**< closure for callbacks */
87 * Definition of a hook for evt
90 struct afb_hook_evt *next; /**< next hook */
91 unsigned refcount; /**< reference count */
92 unsigned flags; /**< hook flags */
93 char *pattern; /**< event pattern name hooked or NULL for any */
94 struct afb_hook_evt_itf *itf; /**< interface of hook */
95 void *closure; /**< closure for callbacks */
99 * Definition of a hook for session
101 struct afb_hook_session {
102 struct afb_hook_session *next; /**< next hook */
103 unsigned refcount; /**< reference count */
104 unsigned flags; /**< hook flags */
105 char *pattern; /**< event pattern name hooked or NULL for any */
106 struct afb_hook_session_itf *itf; /**< interface of hook */
107 void *closure; /**< closure for callbacks */
111 * Definition of a hook for global
113 struct afb_hook_global {
114 struct afb_hook_global *next; /**< next hook */
115 unsigned refcount; /**< reference count */
116 unsigned flags; /**< hook flags */
117 struct afb_hook_global_itf *itf; /**< interface of hook */
118 void *closure; /**< closure for callbacks */
121 /* synchronization across threads */
122 static pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
123 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
125 /* list of hooks for xreq */
126 static struct afb_hook_xreq *list_of_xreq_hooks = NULL;
128 /* list of hooks for export */
129 static struct afb_hook_api *list_of_api_hooks = NULL;
131 /* list of hooks for evt */
132 static struct afb_hook_evt *list_of_evt_hooks = NULL;
134 /* list of hooks for session */
135 static struct afb_hook_session *list_of_session_hooks = NULL;
137 /* list of hooks for global */
138 static struct afb_hook_global *list_of_global_hooks = NULL;
141 static unsigned next_hookid = 0;
143 /******************************************************************************
145 *****************************************************************************/
146 static void init_hookid(struct afb_hookid *hookid)
148 hookid->id = __atomic_add_fetch(&next_hookid, 1, __ATOMIC_RELAXED);
149 clock_gettime(CLOCK_REALTIME, &hookid->time);
152 /******************************************************************************
153 * section: default callbacks for tracing requests
154 *****************************************************************************/
156 static char *_pbuf_(const char *fmt, va_list args, char **palloc, char *sbuf, size_t szsbuf, size_t *outlen)
163 rc = vsnprintf(sbuf, szsbuf, fmt, args);
164 if ((size_t)rc >= szsbuf) {
166 sbuf[szsbuf-2] = sbuf[szsbuf-3] = sbuf[szsbuf-4] = '.';
167 rc = vasprintf(palloc, fmt, cp);
172 if (rc >= 0 && outlen)
173 *outlen = (size_t)rc;
177 static void _hook_(const char *fmt1, const char *fmt2, va_list arg2, ...)
179 static const char chars[] = "HOOK: [] \n";
180 char *mem1, *mem2, buf1[256], buf2[2000];
185 iov[0].iov_base = (void*)&chars[0];
189 va_start(arg1, arg2);
190 iov[1].iov_base = _pbuf_(fmt1, arg1, &mem1, buf1, sizeof buf1, &iov[1].iov_len);
194 iov[2].iov_base = (void*)&chars[7];
198 iov[3].iov_base = _pbuf_(fmt2, arg2, &mem2, buf2, sizeof buf2, &iov[3].iov_len);
201 iov[4].iov_base = (void*)&chars[9];
204 (void)writev(2, iov, 5);
210 static void _hook_xreq_(const struct afb_xreq *xreq, const char *format, ...)
213 va_start(ap, format);
214 _hook_("xreq-%06d:%s/%s", format, ap, xreq->hookindex, xreq->request.called_api, xreq->request.called_verb);
218 static void hook_xreq_begin_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
220 struct afb_cred *cred = xreq->context.credentials;
223 _hook_xreq_(xreq, "BEGIN");
225 _hook_xreq_(xreq, "BEGIN uid=%d=%s gid=%d pid=%d label=%s id=%s",
230 cred->label?:"(null)",
235 static void hook_xreq_end_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
237 _hook_xreq_(xreq, "END");
240 static void hook_xreq_json_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *obj)
242 _hook_xreq_(xreq, "json() -> %s", json_object_to_json_string_ext(obj, JSON_C_TO_STRING_NOSLASHESCAPE));
245 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)
247 _hook_xreq_(xreq, "get(%s) -> { name: %s, value: %s, path: %s }", name, arg.name, arg.value, arg.path);
250 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)
252 _hook_xreq_(xreq, "reply[%s](%s, %s)", error?:"success", json_object_to_json_string_ext(obj, JSON_C_TO_STRING_NOSLASHESCAPE), info);
255 static void hook_xreq_legacy_context_get_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, void *value)
257 _hook_xreq_(xreq, "context_get() -> %p", value);
260 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*))
262 _hook_xreq_(xreq, "context_set(%p, %p)", value, free_value);
265 static void hook_xreq_addref_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
267 _hook_xreq_(xreq, "addref()");
270 static void hook_xreq_unref_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
272 _hook_xreq_(xreq, "unref()");
275 static void hook_xreq_session_close_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
277 _hook_xreq_(xreq, "session_close()");
280 static void hook_xreq_session_set_LOA_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, unsigned level, int result)
282 _hook_xreq_(xreq, "session_set_LOA(%u) -> %d", level, result);
285 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)
287 _hook_xreq_(xreq, "subscribe(%s:%d) -> %d", afb_evt_event_x2_fullname(event_x2), afb_evt_event_x2_id(event_x2), result);
290 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)
292 _hook_xreq_(xreq, "unsubscribe(%s:%d) -> %d", afb_evt_event_x2_fullname(event_x2), afb_evt_event_x2_id(event_x2), result);
295 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)
297 _hook_xreq_(xreq, "subcall(%s/%s, %s) ...", api, verb, json_object_to_json_string_ext(args, JSON_C_TO_STRING_NOSLASHESCAPE));
300 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)
302 _hook_xreq_(xreq, " ...subcall... [%s] -> %s (%s)", error?:"success", json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE), info?:"");
305 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)
307 _hook_xreq_(xreq, "subcallsync(%s/%s, %s) ...", api, verb, json_object_to_json_string_ext(args, JSON_C_TO_STRING_NOSLASHESCAPE));
310 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)
312 _hook_xreq_(xreq, " ...subcallsync... %d [%s] -> %s (%s)", status, error?:"success", json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE), info?:"");
315 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)
322 len = vasprintf(&msg, fmt, ap);
326 _hook_xreq_(xreq, "vverbose(%d:%s, %s, %d, %s) -> %s ? ? ?", level, verbose_name_of_level(level), file, line, func, fmt);
328 _hook_xreq_(xreq, "vverbose(%d:%s, %s, %d, %s) -> %s", level, verbose_name_of_level(level), file, line, func, msg);
333 static void hook_xreq_legacy_store_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_stored_req *sreq)
335 _hook_xreq_(xreq, "store() -> %p", sreq);
338 static void hook_xreq_legacy_unstore_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
340 _hook_xreq_(xreq, "unstore()");
343 static void hook_xreq_has_permission_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *permission, int result)
345 _hook_xreq_(xreq, "has_permission(%s) -> %d", permission, result);
348 static void hook_xreq_get_application_id_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, char *result)
350 _hook_xreq_(xreq, "get_application_id() -> %s", result);
353 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)
355 _hook_xreq_(xreq, "context_make(replace=%s, %p, %p, %p) -> %p", replace?"yes":"no", create_value, free_value, create_closure, result);
358 static void hook_xreq_get_uid_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int result)
360 _hook_xreq_(xreq, "get_uid() -> %d", result);
363 static void hook_xreq_get_client_info_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *result)
365 _hook_xreq_(xreq, "get_client_info() -> %s", json_object_to_json_string_ext(result, JSON_C_TO_STRING_NOSLASHESCAPE));
368 static struct afb_hook_xreq_itf hook_xreq_default_itf = {
369 .hook_xreq_begin = hook_xreq_begin_cb,
370 .hook_xreq_end = hook_xreq_end_cb,
371 .hook_xreq_json = hook_xreq_json_cb,
372 .hook_xreq_get = hook_xreq_get_cb,
373 .hook_xreq_reply = hook_xreq_reply_cb,
374 .hook_xreq_legacy_context_get = hook_xreq_legacy_context_get_cb,
375 .hook_xreq_legacy_context_set = hook_xreq_legacy_context_set_cb,
376 .hook_xreq_addref = hook_xreq_addref_cb,
377 .hook_xreq_unref = hook_xreq_unref_cb,
378 .hook_xreq_session_close = hook_xreq_session_close_cb,
379 .hook_xreq_session_set_LOA = hook_xreq_session_set_LOA_cb,
380 .hook_xreq_subscribe = hook_xreq_subscribe_cb,
381 .hook_xreq_unsubscribe = hook_xreq_unsubscribe_cb,
382 .hook_xreq_subcall = hook_xreq_subcall_cb,
383 .hook_xreq_subcall_result = hook_xreq_subcall_result_cb,
384 .hook_xreq_subcallsync = hook_xreq_subcallsync_cb,
385 .hook_xreq_subcallsync_result = hook_xreq_subcallsync_result_cb,
386 .hook_xreq_vverbose = hook_xreq_vverbose_cb,
387 .hook_xreq_legacy_store = hook_xreq_legacy_store_cb,
388 .hook_xreq_legacy_unstore = hook_xreq_legacy_unstore_cb,
389 .hook_xreq_has_permission = hook_xreq_has_permission_cb,
390 .hook_xreq_get_application_id = hook_xreq_get_application_id_cb,
391 .hook_xreq_context_make = hook_xreq_context_make_cb,
392 .hook_xreq_get_uid = hook_xreq_get_uid_cb,
393 .hook_xreq_get_client_info = hook_xreq_get_client_info_cb,
396 /******************************************************************************
397 * section: hooks for tracing requests
398 *****************************************************************************/
400 #define _HOOK_XREQ_2_(flag,func,...) \
401 struct afb_hook_xreq *hook; \
402 struct afb_hookid hookid; \
403 pthread_rwlock_rdlock(&rwlock); \
404 init_hookid(&hookid); \
405 hook = list_of_xreq_hooks; \
408 && hook->itf->hook_xreq_##func \
409 && (hook->flags & afb_hook_flag_req_##flag) != 0 \
410 && (!hook->session || hook->session == xreq->context.session) \
411 && MATCH_API(hook->api, xreq->request.called_api) \
412 && MATCH_VERB(hook->verb, xreq->request.called_verb)) { \
413 hook->itf->hook_xreq_##func(hook->closure, &hookid, __VA_ARGS__); \
417 pthread_rwlock_unlock(&rwlock);
419 #define _HOOK_XREQ_(what,...) _HOOK_XREQ_2_(what,what,__VA_ARGS__)
421 void afb_hook_xreq_begin(const struct afb_xreq *xreq)
423 _HOOK_XREQ_(begin, xreq);
426 void afb_hook_xreq_end(const struct afb_xreq *xreq)
428 _HOOK_XREQ_(end, xreq);
431 struct json_object *afb_hook_xreq_json(const struct afb_xreq *xreq, struct json_object *obj)
433 _HOOK_XREQ_(json, xreq, obj);
437 struct afb_arg afb_hook_xreq_get(const struct afb_xreq *xreq, const char *name, struct afb_arg arg)
439 _HOOK_XREQ_(get, xreq, name, arg);
443 void afb_hook_xreq_reply(const struct afb_xreq *xreq, struct json_object *obj, const char *error, const char *info)
445 _HOOK_XREQ_(reply, xreq, obj, error, info);
448 void *afb_hook_xreq_legacy_context_get(const struct afb_xreq *xreq, void *value)
450 _HOOK_XREQ_(legacy_context_get, xreq, value);
454 void afb_hook_xreq_legacy_context_set(const struct afb_xreq *xreq, void *value, void (*free_value)(void*))
456 _HOOK_XREQ_(legacy_context_set, xreq, value, free_value);
459 void afb_hook_xreq_addref(const struct afb_xreq *xreq)
461 _HOOK_XREQ_(addref, xreq);
464 void afb_hook_xreq_unref(const struct afb_xreq *xreq)
466 _HOOK_XREQ_(unref, xreq);
469 void afb_hook_xreq_session_close(const struct afb_xreq *xreq)
471 _HOOK_XREQ_(session_close, xreq);
474 int afb_hook_xreq_session_set_LOA(const struct afb_xreq *xreq, unsigned level, int result)
476 _HOOK_XREQ_(session_set_LOA, xreq, level, result);
480 int afb_hook_xreq_subscribe(const struct afb_xreq *xreq, struct afb_event_x2 *event_x2, int result)
482 _HOOK_XREQ_(subscribe, xreq, event_x2, result);
486 int afb_hook_xreq_unsubscribe(const struct afb_xreq *xreq, struct afb_event_x2 *event_x2, int result)
488 _HOOK_XREQ_(unsubscribe, xreq, event_x2, result);
492 void afb_hook_xreq_subcall(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, int flags)
494 _HOOK_XREQ_(subcall, xreq, api, verb, args);
497 void afb_hook_xreq_subcall_result(const struct afb_xreq *xreq, struct json_object *object, const char *error, const char *info)
499 _HOOK_XREQ_(subcall_result, xreq, object, error, info);
502 void afb_hook_xreq_subcallsync(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, int flags)
504 _HOOK_XREQ_(subcallsync, xreq, api, verb, args);
507 int afb_hook_xreq_subcallsync_result(const struct afb_xreq *xreq, int status, struct json_object *object, const char *error, const char *info)
509 _HOOK_XREQ_(subcallsync_result, xreq, status, object, error, info);
513 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)
515 _HOOK_XREQ_(vverbose, xreq, level, file ?: "?", line, func ?: "?", fmt, args);
518 void afb_hook_xreq_legacy_store(const struct afb_xreq *xreq, struct afb_stored_req *sreq)
520 _HOOK_XREQ_(legacy_store, xreq, sreq);
523 void afb_hook_xreq_legacy_unstore(const struct afb_xreq *xreq)
525 _HOOK_XREQ_(legacy_unstore, xreq);
528 int afb_hook_xreq_has_permission(const struct afb_xreq *xreq, const char *permission, int result)
530 _HOOK_XREQ_(has_permission, xreq, permission, result);
534 char *afb_hook_xreq_get_application_id(const struct afb_xreq *xreq, char *result)
536 _HOOK_XREQ_(get_application_id, xreq, result);
540 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)
542 _HOOK_XREQ_(context_make, xreq, replace, create_value, free_value, create_closure, result);
546 int afb_hook_xreq_get_uid(const struct afb_xreq *xreq, int result)
548 _HOOK_XREQ_(get_uid, xreq, result);
552 struct json_object *afb_hook_xreq_get_client_info(const struct afb_xreq *xreq, struct json_object *result)
554 _HOOK_XREQ_(get_client_info, xreq, result);
558 /******************************************************************************
559 * section: hooking xreqs
560 *****************************************************************************/
562 void afb_hook_init_xreq(struct afb_xreq *xreq)
564 static int reqindex = 0;
568 struct afb_hook_xreq *hook;
570 /* scan hook list to get the expected flags */
572 pthread_rwlock_rdlock(&rwlock);
573 hook = list_of_xreq_hooks;
575 f = hook->flags & afb_hook_flags_req_all;
577 && (!hook->session || hook->session == xreq->context.session)
578 && MATCH_API(hook->api, xreq->request.called_api)
579 && MATCH_VERB(hook->verb, xreq->request.called_verb);
584 pthread_rwlock_unlock(&rwlock);
586 /* store the hooking data */
587 xreq->hookflags = flags;
590 x = __atomic_load_n(&reqindex, __ATOMIC_RELAXED);
591 xreq->hookindex = (x + 1) % 1000000 ?: 1;
592 } while (x != __atomic_exchange_n(&reqindex, xreq->hookindex, __ATOMIC_RELAXED));
596 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)
598 struct afb_hook_xreq *hook;
600 /* alloc the result */
601 hook = calloc(1, sizeof *hook);
605 /* get a copy of the names */
606 hook->api = api ? strdup(api) : NULL;
607 hook->verb = verb ? strdup(verb) : NULL;
608 if ((api && !hook->api) || (verb && !hook->verb)) {
615 /* initialise the rest */
616 hook->session = session;
618 afb_session_addref(session);
621 hook->itf = itf ? itf : &hook_xreq_default_itf;
622 hook->closure = closure;
624 /* record the hook */
625 pthread_mutex_lock(&mutex);
626 hook->next = list_of_xreq_hooks;
627 list_of_xreq_hooks = hook;
628 pthread_mutex_unlock(&mutex);
634 struct afb_hook_xreq *afb_hook_addref_xreq(struct afb_hook_xreq *hook)
636 __atomic_add_fetch(&hook->refcount, 1, __ATOMIC_RELAXED);
640 static void hook_clean_xreq()
642 struct afb_hook_xreq **prv, *hook, *head;
644 if (pthread_rwlock_trywrlock(&rwlock) == 0) {
645 /* unlink under mutex */
647 pthread_mutex_lock(&mutex);
648 prv = &list_of_xreq_hooks;
649 while ((hook = *prv)) {
658 pthread_mutex_unlock(&mutex);
659 pthread_rwlock_unlock(&rwlock);
661 /* free found hooks */
662 while((hook = head)) {
667 afb_session_unref(hook->session);
673 void afb_hook_unref_xreq(struct afb_hook_xreq *hook)
675 if (hook && !__atomic_sub_fetch(&hook->refcount, 1, __ATOMIC_RELAXED))
679 /******************************************************************************
680 * section: default callbacks for tracing daemon interface
681 *****************************************************************************/
683 static void _hook_api_(const struct afb_export *export, const char *format, ...)
686 va_start(ap, format);
687 _hook_("api-%s", format, ap, afb_export_apiname(export));
691 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)
693 _hook_api_(export, "event_broadcast.before(%s, %s)....", name, json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE));
696 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)
698 _hook_api_(export, "event_broadcast.after(%s, %s) -> %d", name, json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE), result);
701 static void hook_api_get_event_loop_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_event *result)
703 _hook_api_(export, "get_event_loop() -> %p", result);
706 static void hook_api_get_user_bus_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_bus *result)
708 _hook_api_(export, "get_user_bus() -> %p", result);
711 static void hook_api_get_system_bus_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_bus *result)
713 _hook_api_(export, "get_system_bus() -> %p", result);
716 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)
723 len = vasprintf(&msg, fmt, ap);
727 _hook_api_(export, "vverbose(%d:%s, %s, %d, %s) -> %s ? ? ?", level, verbose_name_of_level(level), file, line, function, fmt);
729 _hook_api_(export, "vverbose(%d:%s, %s, %d, %s) -> %s", level, verbose_name_of_level(level), file, line, function, msg);
734 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)
736 _hook_api_(export, "event_make(%s) -> %s:%d", name, afb_evt_event_x2_fullname(result), afb_evt_event_x2_id(result));
739 static void hook_api_rootdir_get_fd_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result)
741 char path[PATH_MAX], proc[100];
745 _hook_api_(export, "rootdir_get_fd() -> %d, %m", result);
747 snprintf(proc, sizeof proc, "/proc/self/fd/%d", result);
748 s = readlink(proc, path, sizeof path);
749 path[s < 0 ? 0 : s >= sizeof path ? sizeof path - 1 : s] = 0;
750 _hook_api_(export, "rootdir_get_fd() -> %d = %s", result, path);
754 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)
756 char path[PATH_MAX], proc[100];
762 _hook_api_(export, "rootdir_open_locale(%s, %d, %s) -> %d, %m", filename, flags, locale, result);
764 snprintf(proc, sizeof proc, "/proc/self/fd/%d", result);
765 s = readlink(proc, path, sizeof path);
766 path[s < 0 ? 0 : s >= sizeof path ? sizeof path - 1 : s] = 0;
767 _hook_api_(export, "rootdir_open_locale(%s, %d, %s) -> %d = %s", filename, flags, locale, result, path);
771 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)
773 _hook_api_(export, "queue_job(%p, %p, %p, %d) -> %d", callback, argument, group, timeout, result);
776 static void hook_api_unstore_req_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct afb_stored_req *sreq)
778 _hook_api_(export, "unstore_req(%p)", sreq);
781 static void hook_api_require_api_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, int initialized)
783 _hook_api_(export, "require_api(%s, %d)...", name, initialized);
786 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)
788 _hook_api_(export, "...require_api(%s, %d) -> %d", name, initialized, result);
791 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)
793 _hook_api_(export, "add_alias(%s -> %s) -> %d", api, alias?:"<null>", result);
796 static void hook_api_start_before_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export)
798 _hook_api_(export, "start.before");
801 static void hook_api_start_after_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int status)
803 _hook_api_(export, "start.after -> %d", status);
806 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)
808 _hook_api_(export, "on_event.before(%s, %d, %s)", event, event_x2, json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE));
811 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)
813 _hook_api_(export, "on_event.after(%s, %d, %s)", event, event_x2, json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE));
816 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)
818 _hook_api_(export, "call(%s/%s, %s) ...", api, verb, json_object_to_json_string_ext(args, JSON_C_TO_STRING_NOSLASHESCAPE));
821 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)
823 _hook_api_(export, " ...call... [%s] -> %s (%s)", error?:"success", json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE), info?:"");
826 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)
828 _hook_api_(export, "callsync(%s/%s, %s) ...", api, verb, json_object_to_json_string_ext(args, JSON_C_TO_STRING_NOSLASHESCAPE));
831 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)
833 _hook_api_(export, " ...callsync... %d [%s] -> %s (%s)", status, error?:"success", json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE), info?:"");
836 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)
838 _hook_api_(export, "new_api.before %s (%s)%s ...", api, info?:"", noconcurrency?" no-concurrency" : "");
841 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)
843 _hook_api_(export, "... new_api.after %s -> %s (%d)", api, result >= 0 ? "OK" : "ERROR", result);
846 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)
848 _hook_api_(export, "set_verbs_v2 -> %s (%d)", result >= 0 ? "OK" : "ERROR", result);
851 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)
853 _hook_api_(export, "set_verbs_v3 -> %s (%d)", result >= 0 ? "OK" : "ERROR", result);
856 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)
858 _hook_api_(export, "add_verb(%s%s [%s]) -> %s (%d)", verb, glob?" (GLOB)":"", info?:"", result >= 0 ? "OK" : "ERROR", result);
861 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)
863 _hook_api_(export, "del_verb(%s) -> %s (%d)", verb, result >= 0 ? "OK" : "ERROR", result);
866 static void hook_api_api_set_on_event_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result)
868 _hook_api_(export, "set_on_event -> %s (%d)", result >= 0 ? "OK" : "ERROR", result);
871 static void hook_api_api_set_on_init_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result)
873 _hook_api_(export, "set_on_init -> %s (%d)", result >= 0 ? "OK" : "ERROR", result);
876 static void hook_api_api_seal_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export)
878 _hook_api_(export, "seal");
881 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)
883 _hook_api_(export, "event_handler_add(%s) -> %s (%d)", pattern, result >= 0 ? "OK" : "ERROR", result);
886 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)
888 _hook_api_(export, "event_handler_del(%s) -> %s (%d)", pattern, result >= 0 ? "OK" : "ERROR", result);
891 static void hook_api_class_provide_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *name)
893 _hook_api_(export, "class_provide(%s) -> %s (%d)", name, result >= 0 ? "OK" : "ERROR", result);
896 static void hook_api_class_require_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *name)
898 _hook_api_(export, "class_require(%s) -> %s (%d)", name, result >= 0 ? "OK" : "ERROR", result);
901 static void hook_api_delete_api_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result)
903 _hook_api_(export, "delete_api -> %s (%d)", result >= 0 ? "OK" : "ERROR", result);
906 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)
908 _hook_api_(export, "on_event_handler[%s].before(%s, %d, %s)", pattern, event, event_x2, json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE));
911 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)
913 _hook_api_(export, "on_event_handler[%s].after(%s, %d, %s)", pattern, event, event_x2, json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE));
916 static void hook_api_settings_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct json_object *object)
918 _hook_api_(export, "settings -> %s", json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE));
921 static struct afb_hook_api_itf hook_api_default_itf = {
922 .hook_api_event_broadcast_before = hook_api_event_broadcast_before_cb,
923 .hook_api_event_broadcast_after = hook_api_event_broadcast_after_cb,
924 .hook_api_get_event_loop = hook_api_get_event_loop_cb,
925 .hook_api_get_user_bus = hook_api_get_user_bus_cb,
926 .hook_api_get_system_bus = hook_api_get_system_bus_cb,
927 .hook_api_vverbose = hook_api_vverbose_cb,
928 .hook_api_event_make = hook_api_event_make_cb,
929 .hook_api_rootdir_get_fd = hook_api_rootdir_get_fd_cb,
930 .hook_api_rootdir_open_locale = hook_api_rootdir_open_locale_cb,
931 .hook_api_queue_job = hook_api_queue_job_cb,
932 .hook_api_legacy_unstore_req = hook_api_unstore_req_cb,
933 .hook_api_require_api = hook_api_require_api_cb,
934 .hook_api_require_api_result = hook_api_require_api_result_cb,
935 .hook_api_add_alias = hook_api_add_alias_cb,
936 .hook_api_start_before = hook_api_start_before_cb,
937 .hook_api_start_after = hook_api_start_after_cb,
938 .hook_api_on_event_before = hook_api_on_event_before_cb,
939 .hook_api_on_event_after = hook_api_on_event_after_cb,
940 .hook_api_call = hook_api_call_cb,
941 .hook_api_call_result = hook_api_call_result_cb,
942 .hook_api_callsync = hook_api_callsync_cb,
943 .hook_api_callsync_result = hook_api_callsync_result_cb,
944 .hook_api_new_api_before = hook_api_new_api_before_cb,
945 .hook_api_new_api_after = hook_api_new_api_after_cb,
946 .hook_api_api_set_verbs_v2 = hook_api_api_set_verbs_v2_cb,
947 .hook_api_api_set_verbs_v3 = hook_api_api_set_verbs_v3_cb,
948 .hook_api_api_add_verb = hook_api_api_add_verb_cb,
949 .hook_api_api_del_verb = hook_api_api_del_verb_cb,
950 .hook_api_api_set_on_event = hook_api_api_set_on_event_cb,
951 .hook_api_api_set_on_init = hook_api_api_set_on_init_cb,
952 .hook_api_api_seal = hook_api_api_seal_cb,
953 .hook_api_event_handler_add = hook_api_event_handler_add_cb,
954 .hook_api_event_handler_del = hook_api_event_handler_del_cb,
955 .hook_api_class_provide = hook_api_class_provide_cb,
956 .hook_api_class_require = hook_api_class_require_cb,
957 .hook_api_delete_api = hook_api_delete_api_cb,
958 .hook_api_on_event_handler_before = hook_api_on_event_handler_before_cb,
959 .hook_api_on_event_handler_after = hook_api_on_event_handler_after_cb,
960 .hook_api_settings = hook_api_settings_cb,
963 /******************************************************************************
964 * section: hooks for tracing daemon interface (export)
965 *****************************************************************************/
967 #define _HOOK_API_2_(flag,func,...) \
968 struct afb_hook_api *hook; \
969 struct afb_hookid hookid; \
970 const char *apiname = afb_export_apiname(export); \
971 pthread_rwlock_rdlock(&rwlock); \
972 init_hookid(&hookid); \
973 hook = list_of_api_hooks; \
976 && hook->itf->hook_api_##func \
977 && (hook->flags & afb_hook_flag_api_##flag) != 0 \
978 && MATCH_API(hook->api, apiname)) { \
979 hook->itf->hook_api_##func(hook->closure, &hookid, __VA_ARGS__); \
983 pthread_rwlock_unlock(&rwlock);
985 #define _HOOK_API_(what,...) _HOOK_API_2_(what,what,__VA_ARGS__)
987 void afb_hook_api_event_broadcast_before(const struct afb_export *export, const char *name, struct json_object *object)
989 _HOOK_API_2_(event_broadcast, event_broadcast_before, export, name, object);
992 int afb_hook_api_event_broadcast_after(const struct afb_export *export, const char *name, struct json_object *object, int result)
994 _HOOK_API_2_(event_broadcast, event_broadcast_after, export, name, object, result);
998 struct sd_event *afb_hook_api_get_event_loop(const struct afb_export *export, struct sd_event *result)
1000 _HOOK_API_(get_event_loop, export, result);
1004 struct sd_bus *afb_hook_api_get_user_bus(const struct afb_export *export, struct sd_bus *result)
1006 _HOOK_API_(get_user_bus, export, result);
1010 struct sd_bus *afb_hook_api_get_system_bus(const struct afb_export *export, struct sd_bus *result)
1012 _HOOK_API_(get_system_bus, export, result);
1016 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)
1018 _HOOK_API_(vverbose, export, level, file, line, function, fmt, args);
1021 struct afb_event_x2 *afb_hook_api_event_make(const struct afb_export *export, const char *name, struct afb_event_x2 *result)
1023 _HOOK_API_(event_make, export, name, result);
1027 int afb_hook_api_rootdir_get_fd(const struct afb_export *export, int result)
1029 _HOOK_API_(rootdir_get_fd, export, result);
1033 int afb_hook_api_rootdir_open_locale(const struct afb_export *export, const char *filename, int flags, const char *locale, int result)
1035 _HOOK_API_(rootdir_open_locale, export, filename, flags, locale, result);
1039 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)
1041 _HOOK_API_(queue_job, export, callback, argument, group, timeout, result);
1045 void afb_hook_api_legacy_unstore_req(const struct afb_export *export, struct afb_stored_req *sreq)
1047 _HOOK_API_(legacy_unstore_req, export, sreq);
1050 void afb_hook_api_require_api(const struct afb_export *export, const char *name, int initialized)
1052 _HOOK_API_(require_api, export, name, initialized);
1055 int afb_hook_api_require_api_result(const struct afb_export *export, const char *name, int initialized, int result)
1057 _HOOK_API_2_(require_api, require_api_result, export, name, initialized, result);
1061 int afb_hook_api_add_alias(const struct afb_export *export, const char *api, const char *alias, int result)
1063 _HOOK_API_(add_alias, export, api, alias, result);
1067 void afb_hook_api_start_before(const struct afb_export *export)
1069 _HOOK_API_2_(start, start_before, export);
1072 int afb_hook_api_start_after(const struct afb_export *export, int status)
1074 _HOOK_API_2_(start, start_after, export, status);
1078 void afb_hook_api_on_event_before(const struct afb_export *export, const char *event, int event_x2, struct json_object *object)
1080 _HOOK_API_2_(on_event, on_event_before, export, event, event_x2, object);
1083 void afb_hook_api_on_event_after(const struct afb_export *export, const char *event, int event_x2, struct json_object *object)
1085 _HOOK_API_2_(on_event, on_event_after, export, event, event_x2, object);
1088 void afb_hook_api_call(const struct afb_export *export, const char *api, const char *verb, struct json_object *args)
1090 _HOOK_API_(call, export, api, verb, args);
1093 void afb_hook_api_call_result(const struct afb_export *export, struct json_object *object, const char*error, const char *info)
1095 _HOOK_API_2_(call, call_result, export, object, error, info);
1099 void afb_hook_api_callsync(const struct afb_export *export, const char *api, const char *verb, struct json_object *args)
1101 _HOOK_API_(callsync, export, api, verb, args);
1104 int afb_hook_api_callsync_result(const struct afb_export *export, int status, struct json_object *object, const char *error, const char *info)
1106 _HOOK_API_2_(callsync, callsync_result, export, status, object, error, info);
1110 void afb_hook_api_new_api_before(const struct afb_export *export, const char *api, const char *info, int noconcurrency)
1112 _HOOK_API_2_(new_api, new_api_before, export, api, info, noconcurrency);
1115 int afb_hook_api_new_api_after(const struct afb_export *export, int result, const char *api)
1117 _HOOK_API_2_(new_api, new_api_after, export, result, api);
1121 int afb_hook_api_api_set_verbs_v2(const struct afb_export *export, int result, const struct afb_verb_v2 *verbs)
1123 _HOOK_API_2_(api_set_verbs, api_set_verbs_v2, export, result, verbs);
1127 int afb_hook_api_api_set_verbs_v3(const struct afb_export *export, int result, const struct afb_verb_v3 *verbs)
1129 _HOOK_API_2_(api_set_verbs, api_set_verbs_v3, export, result, verbs);
1133 int afb_hook_api_api_add_verb(const struct afb_export *export, int result, const char *verb, const char *info, int glob)
1135 _HOOK_API_(api_add_verb, export, result, verb, info, glob);
1139 int afb_hook_api_api_del_verb(const struct afb_export *export, int result, const char *verb)
1141 _HOOK_API_(api_del_verb, export, result, verb);
1145 int afb_hook_api_api_set_on_event(const struct afb_export *export, int result)
1147 _HOOK_API_(api_set_on_event, export, result);
1151 int afb_hook_api_api_set_on_init(const struct afb_export *export, int result)
1153 _HOOK_API_(api_set_on_init, export, result);
1157 void afb_hook_api_api_seal(const struct afb_export *export)
1159 _HOOK_API_(api_seal, export);
1162 int afb_hook_api_event_handler_add(const struct afb_export *export, int result, const char *pattern)
1164 _HOOK_API_(event_handler_add, export, result, pattern);
1167 int afb_hook_api_event_handler_del(const struct afb_export *export, int result, const char *pattern)
1169 _HOOK_API_(event_handler_del, export, result, pattern);
1172 int afb_hook_api_class_provide(const struct afb_export *export, int result, const char *name)
1174 _HOOK_API_(class_provide, export, result, name);
1177 int afb_hook_api_class_require(const struct afb_export *export, int result, const char *name)
1179 _HOOK_API_(class_require, export, result, name);
1183 int afb_hook_api_delete_api(const struct afb_export *export, int result)
1185 _HOOK_API_(delete_api, export, result);
1189 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)
1191 _HOOK_API_2_(on_event_handler, on_event_handler_before, export, event, event_x2, object, pattern);
1194 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)
1196 _HOOK_API_2_(on_event_handler, on_event_handler_after, export, event, event_x2, object, pattern);
1199 struct json_object *afb_hook_api_settings(const struct afb_export *export, struct json_object *object)
1201 _HOOK_API_(settings, export, object);
1205 /******************************************************************************
1206 * section: hooking export
1207 *****************************************************************************/
1209 int afb_hook_flags_api(const char *api)
1212 struct afb_hook_api *hook;
1215 pthread_rwlock_rdlock(&rwlock);
1216 hook = list_of_api_hooks;
1218 if (!api || MATCH_API(hook->api, api))
1219 flags |= hook->flags;
1222 pthread_rwlock_unlock(&rwlock);
1226 struct afb_hook_api *afb_hook_create_api(const char *api, int flags, struct afb_hook_api_itf *itf, void *closure)
1228 struct afb_hook_api *hook;
1230 /* alloc the result */
1231 hook = calloc(1, sizeof *hook);
1235 /* get a copy of the names */
1236 hook->api = api ? strdup(api) : NULL;
1237 if (api && !hook->api) {
1242 /* initialise the rest */
1244 hook->flags = flags;
1245 hook->itf = itf ? itf : &hook_api_default_itf;
1246 hook->closure = closure;
1248 /* record the hook */
1249 pthread_mutex_lock(&mutex);
1250 hook->next = list_of_api_hooks;
1251 list_of_api_hooks = hook;
1252 pthread_mutex_unlock(&mutex);
1258 struct afb_hook_api *afb_hook_addref_api(struct afb_hook_api *hook)
1260 __atomic_add_fetch(&hook->refcount, 1, __ATOMIC_RELAXED);
1264 static void hook_clean_api()
1266 struct afb_hook_api **prv, *hook, *head;
1268 if (pthread_rwlock_trywrlock(&rwlock) == 0) {
1269 /* unlink under mutex */
1271 pthread_mutex_lock(&mutex);
1272 prv = &list_of_api_hooks;
1273 while ((hook = *prv)) {
1275 prv = &(*prv)->next;
1282 pthread_mutex_unlock(&mutex);
1283 pthread_rwlock_unlock(&rwlock);
1285 /* free found hooks */
1286 while((hook = head)) {
1294 void afb_hook_unref_api(struct afb_hook_api *hook)
1296 if (hook && !__atomic_sub_fetch(&hook->refcount, 1, __ATOMIC_RELAXED))
1300 /******************************************************************************
1301 * section: default callbacks for tracing service interface (evt)
1302 *****************************************************************************/
1304 static void _hook_evt_(const char *evt, int id, const char *format, ...)
1307 va_start(ap, format);
1308 _hook_("evt-%s:%d", format, ap, evt, id);
1312 static void hook_evt_create_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
1314 _hook_evt_(evt, id, "create");
1317 static void hook_evt_push_before_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj)
1319 _hook_evt_(evt, id, "push.before(%s)", json_object_to_json_string_ext(obj, JSON_C_TO_STRING_NOSLASHESCAPE));
1323 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)
1325 _hook_evt_(evt, id, "push.after(%s) -> %d", json_object_to_json_string_ext(obj, JSON_C_TO_STRING_NOSLASHESCAPE), result);
1328 static void hook_evt_broadcast_before_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj)
1330 _hook_evt_(evt, id, "broadcast.before(%s)", json_object_to_json_string_ext(obj, JSON_C_TO_STRING_NOSLASHESCAPE));
1333 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)
1335 _hook_evt_(evt, id, "broadcast.after(%s) -> %d", json_object_to_json_string_ext(obj, JSON_C_TO_STRING_NOSLASHESCAPE), result);
1338 static void hook_evt_name_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, const char *result)
1340 _hook_evt_(evt, id, "name -> %s", result);
1343 static void hook_evt_addref_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
1345 _hook_evt_(evt, id, "addref");
1348 static void hook_evt_unref_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
1350 _hook_evt_(evt, id, "unref");
1353 static struct afb_hook_evt_itf hook_evt_default_itf = {
1354 .hook_evt_create = hook_evt_create_cb,
1355 .hook_evt_push_before = hook_evt_push_before_cb,
1356 .hook_evt_push_after = hook_evt_push_after_cb,
1357 .hook_evt_broadcast_before = hook_evt_broadcast_before_cb,
1358 .hook_evt_broadcast_after = hook_evt_broadcast_after_cb,
1359 .hook_evt_name = hook_evt_name_cb,
1360 .hook_evt_addref = hook_evt_addref_cb,
1361 .hook_evt_unref = hook_evt_unref_cb
1364 /******************************************************************************
1365 * section: hooks for tracing events interface (evt)
1366 *****************************************************************************/
1368 #define _HOOK_EVT_(what,...) \
1369 struct afb_hook_evt *hook; \
1370 struct afb_hookid hookid; \
1371 pthread_rwlock_rdlock(&rwlock); \
1372 init_hookid(&hookid); \
1373 hook = list_of_evt_hooks; \
1375 if (hook->refcount \
1376 && hook->itf->hook_evt_##what \
1377 && (hook->flags & afb_hook_flag_evt_##what) != 0 \
1378 && MATCH_EVENT(hook->pattern, evt)) { \
1379 hook->itf->hook_evt_##what(hook->closure, &hookid, __VA_ARGS__); \
1381 hook = hook->next; \
1383 pthread_rwlock_unlock(&rwlock);
1385 void afb_hook_evt_create(const char *evt, int id)
1387 _HOOK_EVT_(create, evt, id);
1390 void afb_hook_evt_push_before(const char *evt, int id, struct json_object *obj)
1392 _HOOK_EVT_(push_before, evt, id, obj);
1395 int afb_hook_evt_push_after(const char *evt, int id, struct json_object *obj, int result)
1397 _HOOK_EVT_(push_after, evt, id, obj, result);
1401 void afb_hook_evt_broadcast_before(const char *evt, int id, struct json_object *obj)
1403 _HOOK_EVT_(broadcast_before, evt, id, obj);
1406 int afb_hook_evt_broadcast_after(const char *evt, int id, struct json_object *obj, int result)
1408 _HOOK_EVT_(broadcast_after, evt, id, obj, result);
1412 void afb_hook_evt_name(const char *evt, int id, const char *result)
1414 _HOOK_EVT_(name, evt, id, result);
1417 void afb_hook_evt_addref(const char *evt, int id)
1419 _HOOK_EVT_(addref, evt, id);
1422 void afb_hook_evt_unref(const char *evt, int id)
1424 _HOOK_EVT_(unref, evt, id);
1427 /******************************************************************************
1428 * section: hooking services (evt)
1429 *****************************************************************************/
1431 int afb_hook_flags_evt(const char *name)
1434 struct afb_hook_evt *hook;
1437 pthread_rwlock_rdlock(&rwlock);
1438 hook = list_of_evt_hooks;
1440 if (!name || MATCH_EVENT(hook->pattern, name))
1441 flags |= hook->flags;
1444 pthread_rwlock_unlock(&rwlock);
1448 struct afb_hook_evt *afb_hook_create_evt(const char *pattern, int flags, struct afb_hook_evt_itf *itf, void *closure)
1450 struct afb_hook_evt *hook;
1452 /* alloc the result */
1453 hook = calloc(1, sizeof *hook);
1457 /* get a copy of the names */
1458 hook->pattern = pattern ? strdup(pattern) : NULL;
1459 if (pattern && !hook->pattern) {
1464 /* initialise the rest */
1466 hook->flags = flags;
1467 hook->itf = itf ? itf : &hook_evt_default_itf;
1468 hook->closure = closure;
1470 /* record the hook */
1471 pthread_mutex_lock(&mutex);
1472 hook->next = list_of_evt_hooks;
1473 list_of_evt_hooks = hook;
1474 pthread_mutex_unlock(&mutex);
1480 struct afb_hook_evt *afb_hook_addref_evt(struct afb_hook_evt *hook)
1482 __atomic_add_fetch(&hook->refcount, 1, __ATOMIC_RELAXED);
1486 static void hook_clean_evt()
1488 struct afb_hook_evt **prv, *hook, *head;
1490 if (pthread_rwlock_trywrlock(&rwlock) == 0) {
1491 /* unlink under mutex */
1493 pthread_mutex_lock(&mutex);
1494 prv = &list_of_evt_hooks;
1495 while ((hook = *prv)) {
1497 prv = &(*prv)->next;
1504 pthread_mutex_unlock(&mutex);
1505 pthread_rwlock_unlock(&rwlock);
1507 /* free found hooks */
1508 while((hook = head)) {
1510 free(hook->pattern);
1516 void afb_hook_unref_evt(struct afb_hook_evt *hook)
1518 if (hook && !__atomic_sub_fetch(&hook->refcount, 1, __ATOMIC_RELAXED))
1522 /******************************************************************************
1523 * section: default callbacks for sessions (session)
1524 *****************************************************************************/
1526 static void _hook_session_(struct afb_session *session, const char *format, ...)
1529 va_start(ap, format);
1530 _hook_("session-%s", format, ap, afb_session_uuid(session));
1534 static void hook_session_create_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1536 _hook_session_(session, "create");
1539 static void hook_session_close_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1541 _hook_session_(session, "close");
1544 static void hook_session_destroy_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1546 _hook_session_(session, "destroy");
1549 static void hook_session_addref_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1551 _hook_session_(session, "addref");
1554 static void hook_session_unref_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1556 _hook_session_(session, "unref");
1559 static struct afb_hook_session_itf hook_session_default_itf = {
1560 .hook_session_create = hook_session_create_cb,
1561 .hook_session_close = hook_session_close_cb,
1562 .hook_session_destroy = hook_session_destroy_cb,
1563 .hook_session_addref = hook_session_addref_cb,
1564 .hook_session_unref = hook_session_unref_cb
1567 /******************************************************************************
1568 * section: hooks for tracing sessions (session)
1569 *****************************************************************************/
1571 #define _HOOK_SESSION_(what,...) \
1572 struct afb_hook_session *hook; \
1573 struct afb_hookid hookid; \
1574 const char *sessid = 0; \
1575 pthread_rwlock_rdlock(&rwlock); \
1576 init_hookid(&hookid); \
1577 hook = list_of_session_hooks; \
1579 if (hook->refcount \
1580 && hook->itf->hook_session_##what \
1581 && (hook->flags & afb_hook_flag_session_##what) != 0 \
1582 && MATCH_SESSION(hook->pattern, (sessid?:(sessid=afb_session_uuid(session))))) { \
1583 hook->itf->hook_session_##what(hook->closure, &hookid, __VA_ARGS__); \
1585 hook = hook->next; \
1587 pthread_rwlock_unlock(&rwlock);
1589 void afb_hook_session_create(struct afb_session *session)
1591 _HOOK_SESSION_(create, session);
1594 void afb_hook_session_close(struct afb_session *session)
1596 _HOOK_SESSION_(close, session);
1599 void afb_hook_session_destroy(struct afb_session *session)
1601 _HOOK_SESSION_(destroy, session);
1604 void afb_hook_session_addref(struct afb_session *session)
1606 _HOOK_SESSION_(addref, session);
1609 void afb_hook_session_unref(struct afb_session *session)
1611 _HOOK_SESSION_(unref, session);
1615 /******************************************************************************
1616 * section: hooking sessions (session)
1617 *****************************************************************************/
1619 struct afb_hook_session *afb_hook_create_session(const char *pattern, int flags, struct afb_hook_session_itf *itf, void *closure)
1621 struct afb_hook_session *hook;
1623 /* alloc the result */
1624 hook = calloc(1, sizeof *hook);
1628 /* get a copy of the names */
1629 hook->pattern = pattern ? strdup(pattern) : NULL;
1630 if (pattern && !hook->pattern) {
1635 /* initialise the rest */
1637 hook->flags = flags;
1638 hook->itf = itf ? itf : &hook_session_default_itf;
1639 hook->closure = closure;
1641 /* record the hook */
1642 pthread_mutex_lock(&mutex);
1643 hook->next = list_of_session_hooks;
1644 list_of_session_hooks = hook;
1645 pthread_mutex_unlock(&mutex);
1651 struct afb_hook_session *afb_hook_addref_session(struct afb_hook_session *hook)
1653 __atomic_add_fetch(&hook->refcount, 1, __ATOMIC_RELAXED);
1657 static void hook_clean_session()
1659 struct afb_hook_session **prv, *hook, *head;
1661 if (pthread_rwlock_trywrlock(&rwlock) == 0) {
1662 /* unlink under mutex */
1664 pthread_mutex_lock(&mutex);
1665 prv = &list_of_session_hooks;
1666 while ((hook = *prv)) {
1668 prv = &(*prv)->next;
1675 pthread_mutex_unlock(&mutex);
1676 pthread_rwlock_unlock(&rwlock);
1678 /* free found hooks */
1679 while((hook = head)) {
1681 free(hook->pattern);
1687 void afb_hook_unref_session(struct afb_hook_session *hook)
1689 if (hook && !__atomic_sub_fetch(&hook->refcount, 1, __ATOMIC_RELAXED))
1690 hook_clean_session();
1693 /******************************************************************************
1694 * section: default callbacks for globals (global)
1695 *****************************************************************************/
1697 static void _hook_global_(const char *format, ...)
1700 va_start(ap, format);
1701 _hook_("global", format, ap);
1705 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)
1712 len = vasprintf(&msg, fmt, ap);
1716 _hook_global_("vverbose(%d:%s, %s, %d, %s) -> %s ? ? ?", level, verbose_name_of_level(level), file, line, func, fmt);
1718 _hook_global_("vverbose(%d:%s, %s, %d, %s) -> %s", level, verbose_name_of_level(level), file, line, func, msg);
1723 static struct afb_hook_global_itf hook_global_default_itf = {
1724 .hook_global_vverbose = hook_global_vverbose_cb
1727 /******************************************************************************
1728 * section: hooks for tracing globals (global)
1729 *****************************************************************************/
1731 #define _HOOK_GLOBAL_(what,...) \
1732 struct afb_hook_global *hook; \
1733 struct afb_hookid hookid; \
1734 pthread_rwlock_rdlock(&rwlock); \
1735 init_hookid(&hookid); \
1736 hook = list_of_global_hooks; \
1738 if (hook->refcount \
1739 && hook->itf->hook_global_##what \
1740 && (hook->flags & afb_hook_flag_global_##what) != 0) { \
1741 hook->itf->hook_global_##what(hook->closure, &hookid, __VA_ARGS__); \
1743 hook = hook->next; \
1745 pthread_rwlock_unlock(&rwlock);
1747 static void afb_hook_global_vverbose(int level, const char *file, int line, const char *func, const char *fmt, va_list args)
1749 _HOOK_GLOBAL_(vverbose, level, file ?: "?", line, func ?: "?", fmt ?: "", args);
1752 /******************************************************************************
1753 * section: hooking globals (global)
1754 *****************************************************************************/
1756 static void update_global()
1758 struct afb_hook_global *hook;
1761 pthread_rwlock_rdlock(&rwlock);
1762 hook = list_of_global_hooks;
1765 flags = hook->flags;
1768 verbose_observer = (flags & afb_hook_flag_global_vverbose) ? afb_hook_global_vverbose : NULL;
1769 pthread_rwlock_unlock(&rwlock);
1772 struct afb_hook_global *afb_hook_create_global(int flags, struct afb_hook_global_itf *itf, void *closure)
1774 struct afb_hook_global *hook;
1776 /* alloc the result */
1777 hook = calloc(1, sizeof *hook);
1781 /* initialise the rest */
1783 hook->flags = flags;
1784 hook->itf = itf ? itf : &hook_global_default_itf;
1785 hook->closure = closure;
1787 /* record the hook */
1788 pthread_mutex_lock(&mutex);
1789 hook->next = list_of_global_hooks;
1790 list_of_global_hooks = hook;
1791 pthread_mutex_unlock(&mutex);
1793 /* update hooking */
1800 struct afb_hook_global *afb_hook_addref_global(struct afb_hook_global *hook)
1802 __atomic_add_fetch(&hook->refcount, 1, __ATOMIC_RELAXED);
1806 static void hook_clean_global()
1808 struct afb_hook_global **prv, *hook, *head;
1810 if (pthread_rwlock_trywrlock(&rwlock) == 0) {
1811 /* unlink under mutex */
1813 pthread_mutex_lock(&mutex);
1814 prv = &list_of_global_hooks;
1815 while ((hook = *prv)) {
1817 prv = &(*prv)->next;
1824 pthread_mutex_unlock(&mutex);
1825 pthread_rwlock_unlock(&rwlock);
1827 /* free found hooks */
1828 while((hook = head)) {
1835 void afb_hook_unref_global(struct afb_hook_global *hook)
1837 if (hook && !__atomic_sub_fetch(&hook->refcount, 1, __ATOMIC_RELAXED)) {
1838 /* update hooking */
1840 hook_clean_global();
1844 #endif /* WITH_AFB_HOOK *******************************************************/