2 * Copyright (C) 2016, 2017 "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-common.h>
31 #include <afb/afb-event-itf.h>
33 #include "afb-context.h"
35 #include "afb-session.h"
44 * Definition of a hook for xreq
46 struct afb_hook_xreq {
47 struct afb_hook_xreq *next; /**< next hook */
48 unsigned refcount; /**< reference count */
49 char *api; /**< api hooked or NULL for any */
50 char *verb; /**< verb hooked or NULL for any */
51 struct afb_session *session; /**< session hooked or NULL if any */
52 unsigned flags; /**< hook flags */
53 struct afb_hook_xreq_itf *itf; /**< interface of hook */
54 void *closure; /**< closure for callbacks */
58 * Definition of a hook for ditf
60 struct afb_hook_ditf {
61 struct afb_hook_ditf *next; /**< next hook */
62 unsigned refcount; /**< reference count */
63 char *api; /**< api hooked or NULL for any */
64 unsigned flags; /**< hook flags */
65 struct afb_hook_ditf_itf *itf; /**< interface of hook */
66 void *closure; /**< closure for callbacks */
70 * Definition of a hook for svc
73 struct afb_hook_svc *next; /**< next hook */
74 unsigned refcount; /**< reference count */
75 char *api; /**< api hooked or NULL for any */
76 unsigned flags; /**< hook flags */
77 struct afb_hook_svc_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 char *pattern; /**< event pattern name hooked or NULL for any */
88 unsigned flags; /**< hook flags */
89 struct afb_hook_evt_itf *itf; /**< interface of hook */
90 void *closure; /**< closure for callbacks */
94 * Definition of a hook for global
96 struct afb_hook_global {
97 struct afb_hook_global *next; /**< next hook */
98 unsigned refcount; /**< reference count */
99 unsigned flags; /**< hook flags */
100 struct afb_hook_global_itf *itf; /**< interface of hook */
101 void *closure; /**< closure for callbacks */
104 /* synchronisation across threads */
105 static pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
107 /* list of hooks for xreq */
108 static struct afb_hook_xreq *list_of_xreq_hooks = NULL;
110 /* list of hooks for ditf */
111 static struct afb_hook_ditf *list_of_ditf_hooks = NULL;
113 /* list of hooks for svc */
114 static struct afb_hook_svc *list_of_svc_hooks = NULL;
116 /* list of hooks for evt */
117 static struct afb_hook_evt *list_of_evt_hooks = NULL;
119 /* list of hooks for global */
120 static struct afb_hook_global *list_of_global_hooks = NULL;
123 static unsigned next_hookid = 0;
125 /******************************************************************************
127 *****************************************************************************/
128 static void init_hookid(struct afb_hookid *hookid)
130 hookid->id = __atomic_add_fetch(&next_hookid, 1, __ATOMIC_RELAXED);
131 clock_gettime(CLOCK_MONOTONIC, &hookid->time);
134 /******************************************************************************
135 * section: default callbacks for tracing requests
136 *****************************************************************************/
138 static char *_pbuf_(const char *fmt, va_list args, char **palloc, char *sbuf, size_t szsbuf)
145 rc = vsnprintf(sbuf, szsbuf, fmt, args);
146 if ((size_t)rc >= szsbuf) {
148 sbuf[szsbuf-2] = sbuf[szsbuf-3] = sbuf[szsbuf-4] = '.';
149 rc = vasprintf(palloc, fmt, cp);
157 static void _hook_(const char *fmt1, const char *fmt2, va_list arg2, ...)
159 char *tag, *data, *mem1, *mem2, buf1[256], buf2[2000];
162 data = _pbuf_(fmt2, arg2, &mem2, buf2, sizeof buf2);
164 va_start(arg1, arg2);
165 tag = _pbuf_(fmt1, arg1, &mem1, buf1, sizeof buf1);
168 NOTICE("[HOOK %s] %s", tag, data);
174 static void _hook_xreq_(const struct afb_xreq *xreq, const char *format, ...)
177 va_start(ap, format);
178 _hook_("xreq-%06d:%s/%s", format, ap, xreq->hookindex, xreq->api, xreq->verb);
182 static void hook_xreq_begin_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
185 _hook_xreq_(xreq, "BEGIN");
187 _hook_xreq_(xreq, "BEGIN uid=%d=%s gid=%d pid=%d label=%s id=%s",
188 (int)xreq->cred->uid,
190 (int)xreq->cred->gid,
191 (int)xreq->cred->pid,
192 xreq->cred->label?:"(null)",
193 xreq->cred->id?:"(null)"
197 static void hook_xreq_end_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
199 _hook_xreq_(xreq, "END");
202 static void hook_xreq_json_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *obj)
204 _hook_xreq_(xreq, "json() -> %s", json_object_to_json_string(obj));
207 static void hook_xreq_get_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *name, struct afb_arg arg)
209 _hook_xreq_(xreq, "get(%s) -> { name: %s, value: %s, path: %s }", name, arg.name, arg.value, arg.path);
212 static void hook_xreq_success_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *obj, const char *info)
214 _hook_xreq_(xreq, "success(%s, %s)", json_object_to_json_string(obj), info);
217 static void hook_xreq_fail_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *status, const char *info)
219 _hook_xreq_(xreq, "fail(%s, %s)", status, info);
222 static void hook_xreq_context_get_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, void *value)
224 _hook_xreq_(xreq, "context_get() -> %p", value);
227 static void hook_xreq_context_set_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, void *value, void (*free_value)(void*))
229 _hook_xreq_(xreq, "context_set(%p, %p)", value, free_value);
232 static void hook_xreq_addref_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
234 _hook_xreq_(xreq, "addref()");
237 static void hook_xreq_unref_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
239 _hook_xreq_(xreq, "unref()");
242 static void hook_xreq_session_close_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
244 _hook_xreq_(xreq, "session_close()");
247 static void hook_xreq_session_set_LOA_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, unsigned level, int result)
249 _hook_xreq_(xreq, "session_set_LOA(%u) -> %d", level, result);
252 static void hook_xreq_subscribe_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_event event, int result)
254 _hook_xreq_(xreq, "subscribe(%s:%d) -> %d", afb_evt_event_name(event), afb_evt_event_id(event), result);
257 static void hook_xreq_unsubscribe_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_event event, int result)
259 _hook_xreq_(xreq, "unsubscribe(%s:%d) -> %d", afb_evt_event_name(event), afb_evt_event_id(event), result);
262 static void hook_xreq_subcall_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
264 _hook_xreq_(xreq, "subcall(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
267 static void hook_xreq_subcall_result_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int status, struct json_object *result)
269 _hook_xreq_(xreq, " ...subcall... -> %d: %s", status, json_object_to_json_string(result));
272 static void hook_xreq_subcallsync_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
274 _hook_xreq_(xreq, "subcallsync(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
277 static void hook_xreq_subcallsync_result_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int status, struct json_object *result)
279 _hook_xreq_(xreq, " ...subcallsync... -> %d: %s", status, json_object_to_json_string(result));
282 static void hook_xreq_vverbose_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
289 len = vasprintf(&msg, fmt, ap);
293 _hook_xreq_(xreq, "vverbose(%d, %s, %d, %s) -> %s ? ? ?", level, file, line, func, fmt);
295 _hook_xreq_(xreq, "vverbose(%d, %s, %d, %s) -> %s", level, file, line, func, msg);
300 static void hook_xreq_store_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_stored_req *sreq)
302 _hook_xreq_(xreq, "store() -> %p", sreq);
305 static void hook_xreq_unstore_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
307 _hook_xreq_(xreq, "unstore()");
310 static void hook_xreq_subcall_req_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
312 _hook_xreq_(xreq, "subcall_req(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
315 static void hook_xreq_subcall_req_result_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int status, struct json_object *result)
317 _hook_xreq_(xreq, " ...subcall_req... -> %d: %s", status, json_object_to_json_string(result));
320 static struct afb_hook_xreq_itf hook_xreq_default_itf = {
321 .hook_xreq_begin = hook_xreq_begin_default_cb,
322 .hook_xreq_end = hook_xreq_end_default_cb,
323 .hook_xreq_json = hook_xreq_json_default_cb,
324 .hook_xreq_get = hook_xreq_get_default_cb,
325 .hook_xreq_success = hook_xreq_success_default_cb,
326 .hook_xreq_fail = hook_xreq_fail_default_cb,
327 .hook_xreq_context_get = hook_xreq_context_get_default_cb,
328 .hook_xreq_context_set = hook_xreq_context_set_default_cb,
329 .hook_xreq_addref = hook_xreq_addref_default_cb,
330 .hook_xreq_unref = hook_xreq_unref_default_cb,
331 .hook_xreq_session_close = hook_xreq_session_close_default_cb,
332 .hook_xreq_session_set_LOA = hook_xreq_session_set_LOA_default_cb,
333 .hook_xreq_subscribe = hook_xreq_subscribe_default_cb,
334 .hook_xreq_unsubscribe = hook_xreq_unsubscribe_default_cb,
335 .hook_xreq_subcall = hook_xreq_subcall_default_cb,
336 .hook_xreq_subcall_result = hook_xreq_subcall_result_default_cb,
337 .hook_xreq_subcallsync = hook_xreq_subcallsync_default_cb,
338 .hook_xreq_subcallsync_result = hook_xreq_subcallsync_result_default_cb,
339 .hook_xreq_vverbose = hook_xreq_vverbose_default_cb,
340 .hook_xreq_store = hook_xreq_store_default_cb,
341 .hook_xreq_unstore = hook_xreq_unstore_default_cb,
342 .hook_xreq_subcall_req = hook_xreq_subcall_req_default_cb,
343 .hook_xreq_subcall_req_result = hook_xreq_subcall_req_result_default_cb
346 /******************************************************************************
347 * section: hooks for tracing requests
348 *****************************************************************************/
350 #define _HOOK_XREQ_(what,...) \
351 struct afb_hook_xreq *hook; \
352 struct afb_hookid hookid; \
353 pthread_rwlock_rdlock(&rwlock); \
354 init_hookid(&hookid); \
355 hook = list_of_xreq_hooks; \
357 if (hook->itf->hook_xreq_##what \
358 && (hook->flags & afb_hook_flag_req_##what) != 0 \
359 && (!hook->session || hook->session == xreq->context.session) \
360 && (!hook->api || !strcasecmp(hook->api, xreq->api)) \
361 && (!hook->verb || !strcasecmp(hook->verb, xreq->verb))) { \
362 hook->itf->hook_xreq_##what(hook->closure, &hookid, __VA_ARGS__); \
366 pthread_rwlock_unlock(&rwlock);
369 void afb_hook_xreq_begin(const struct afb_xreq *xreq)
371 _HOOK_XREQ_(begin, xreq);
374 void afb_hook_xreq_end(const struct afb_xreq *xreq)
376 _HOOK_XREQ_(end, xreq);
379 struct json_object *afb_hook_xreq_json(const struct afb_xreq *xreq, struct json_object *obj)
381 _HOOK_XREQ_(json, xreq, obj);
385 struct afb_arg afb_hook_xreq_get(const struct afb_xreq *xreq, const char *name, struct afb_arg arg)
387 _HOOK_XREQ_(get, xreq, name, arg);
391 void afb_hook_xreq_success(const struct afb_xreq *xreq, struct json_object *obj, const char *info)
393 _HOOK_XREQ_(success, xreq, obj, info);
396 void afb_hook_xreq_fail(const struct afb_xreq *xreq, const char *status, const char *info)
398 _HOOK_XREQ_(fail, xreq, status, info);
401 void *afb_hook_xreq_context_get(const struct afb_xreq *xreq, void *value)
403 _HOOK_XREQ_(context_get, xreq, value);
407 void afb_hook_xreq_context_set(const struct afb_xreq *xreq, void *value, void (*free_value)(void*))
409 _HOOK_XREQ_(context_set, xreq, value, free_value);
412 void afb_hook_xreq_addref(const struct afb_xreq *xreq)
414 _HOOK_XREQ_(addref, xreq);
417 void afb_hook_xreq_unref(const struct afb_xreq *xreq)
419 _HOOK_XREQ_(unref, xreq);
422 void afb_hook_xreq_session_close(const struct afb_xreq *xreq)
424 _HOOK_XREQ_(session_close, xreq);
427 int afb_hook_xreq_session_set_LOA(const struct afb_xreq *xreq, unsigned level, int result)
429 _HOOK_XREQ_(session_set_LOA, xreq, level, result);
433 int afb_hook_xreq_subscribe(const struct afb_xreq *xreq, struct afb_event event, int result)
435 _HOOK_XREQ_(subscribe, xreq, event, result);
439 int afb_hook_xreq_unsubscribe(const struct afb_xreq *xreq, struct afb_event event, int result)
441 _HOOK_XREQ_(unsubscribe, xreq, event, result);
445 void afb_hook_xreq_subcall(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
447 _HOOK_XREQ_(subcall, xreq, api, verb, args);
450 void afb_hook_xreq_subcall_result(const struct afb_xreq *xreq, int status, struct json_object *result)
452 _HOOK_XREQ_(subcall_result, xreq, status, result);
455 void afb_hook_xreq_subcallsync(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
457 _HOOK_XREQ_(subcallsync, xreq, api, verb, args);
460 int afb_hook_xreq_subcallsync_result(const struct afb_xreq *xreq, int status, struct json_object *result)
462 _HOOK_XREQ_(subcallsync_result, xreq, status, result);
466 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)
468 _HOOK_XREQ_(vverbose, xreq, level, file ?: "?", line, func ?: "?", fmt, args);
471 void afb_hook_xreq_store(const struct afb_xreq *xreq, struct afb_stored_req *sreq)
473 _HOOK_XREQ_(store, xreq, sreq);
476 void afb_hook_xreq_unstore(const struct afb_xreq *xreq)
478 _HOOK_XREQ_(unstore, xreq);
481 void afb_hook_xreq_subcall_req(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
483 _HOOK_XREQ_(subcall_req, xreq, api, verb, args);
486 void afb_hook_xreq_subcall_req_result(const struct afb_xreq *xreq, int status, struct json_object *result)
488 _HOOK_XREQ_(subcall_req_result, xreq, status, result);
491 /******************************************************************************
492 * section: hooking xreqs
493 *****************************************************************************/
495 void afb_hook_init_xreq(struct afb_xreq *xreq)
501 struct afb_hook_xreq *hook;
503 /* scan hook list to get the expected flags */
505 pthread_rwlock_rdlock(&rwlock);
506 hook = list_of_xreq_hooks;
508 f = hook->flags & afb_hook_flags_req_all;
510 && (!hook->session || hook->session == xreq->context.session)
511 && (!hook->api || !strcasecmp(hook->api, xreq->api))
512 && (!hook->verb || !strcasecmp(hook->verb, xreq->verb));
517 pthread_rwlock_unlock(&rwlock);
519 /* store the hooking data */
520 xreq->hookflags = flags;
522 pthread_rwlock_wrlock(&rwlock);
525 xreq->hookindex = reqindex;
526 pthread_rwlock_unlock(&rwlock);
530 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)
532 struct afb_hook_xreq *hook;
534 /* alloc the result */
535 hook = calloc(1, sizeof *hook);
539 /* get a copy of the names */
540 hook->api = api ? strdup(api) : NULL;
541 hook->verb = verb ? strdup(verb) : NULL;
542 if ((api && !hook->api) || (verb && !hook->verb)) {
549 /* initialise the rest */
550 hook->session = session;
552 afb_session_addref(session);
555 hook->itf = itf ? itf : &hook_xreq_default_itf;
556 hook->closure = closure;
558 /* record the hook */
559 pthread_rwlock_wrlock(&rwlock);
560 hook->next = list_of_xreq_hooks;
561 list_of_xreq_hooks = hook;
562 pthread_rwlock_unlock(&rwlock);
568 struct afb_hook_xreq *afb_hook_addref_xreq(struct afb_hook_xreq *hook)
570 pthread_rwlock_wrlock(&rwlock);
572 pthread_rwlock_unlock(&rwlock);
576 void afb_hook_unref_xreq(struct afb_hook_xreq *hook)
578 struct afb_hook_xreq **prv;
581 pthread_rwlock_wrlock(&rwlock);
582 if (--hook->refcount)
586 prv = &list_of_xreq_hooks;
587 while (*prv && *prv != hook)
592 pthread_rwlock_unlock(&rwlock);
598 afb_session_unref(hook->session);
604 /******************************************************************************
605 * section: default callbacks for tracing daemon interface
606 *****************************************************************************/
608 static void _hook_ditf_(const struct afb_ditf *ditf, const char *format, ...)
611 va_start(ap, format);
612 _hook_("ditf-%s", format, ap, ditf->api);
616 static void hook_ditf_event_broadcast_before_cb(void *closure, const struct afb_hookid *hookid, const struct afb_ditf *ditf, const char *name, struct json_object *object)
618 _hook_ditf_(ditf, "event_broadcast.before(%s, %s)....", name, json_object_to_json_string(object));
621 static void hook_ditf_event_broadcast_after_cb(void *closure, const struct afb_hookid *hookid, const struct afb_ditf *ditf, const char *name, struct json_object *object, int result)
623 _hook_ditf_(ditf, "event_broadcast.after(%s, %s) -> %d", name, json_object_to_json_string(object), result);
626 static void hook_ditf_get_event_loop_cb(void *closure, const struct afb_hookid *hookid, const struct afb_ditf *ditf, struct sd_event *result)
628 _hook_ditf_(ditf, "get_event_loop() -> %p", result);
631 static void hook_ditf_get_user_bus_cb(void *closure, const struct afb_hookid *hookid, const struct afb_ditf *ditf, struct sd_bus *result)
633 _hook_ditf_(ditf, "get_user_bus() -> %p", result);
636 static void hook_ditf_get_system_bus_cb(void *closure, const struct afb_hookid *hookid, const struct afb_ditf *ditf, struct sd_bus *result)
638 _hook_ditf_(ditf, "get_system_bus() -> %p", result);
641 static void hook_ditf_vverbose_cb(void *closure, const struct afb_hookid *hookid, const struct afb_ditf *ditf, int level, const char *file, int line, const char *function, const char *fmt, va_list args)
648 len = vasprintf(&msg, fmt, ap);
652 _hook_ditf_(ditf, "vverbose(%d, %s, %d, %s) -> %s ? ? ?", level, file, line, function, fmt);
654 _hook_ditf_(ditf, "vverbose(%d, %s, %d, %s) -> %s", level, file, line, function, msg);
659 static void hook_ditf_event_make_cb(void *closure, const struct afb_hookid *hookid, const struct afb_ditf *ditf, const char *name, struct afb_event result)
661 _hook_ditf_(ditf, "event_make(%s) -> %s:%d", name, afb_evt_event_name(result), afb_evt_event_id(result));
664 static void hook_ditf_rootdir_get_fd_cb(void *closure, const struct afb_hookid *hookid, const struct afb_ditf *ditf, int result)
668 _hook_ditf_(ditf, "rootdir_get_fd() -> %d, %m", result);
670 sprintf(path, "/proc/self/fd/%d", result);
671 readlink(path, path, sizeof path);
672 _hook_ditf_(ditf, "rootdir_get_fd() -> %d = %s", result, path);
676 static void hook_ditf_rootdir_open_locale_cb(void *closure, const struct afb_hookid *hookid, const struct afb_ditf *ditf, const char *filename, int flags, const char *locale, int result)
682 _hook_ditf_(ditf, "rootdir_open_locale(%s, %d, %s) -> %d, %m", filename, flags, locale, result);
684 sprintf(path, "/proc/self/fd/%d", result);
685 readlink(path, path, sizeof path);
686 _hook_ditf_(ditf, "rootdir_open_locale(%s, %d, %s) -> %d = %s", filename, flags, locale, result, path);
690 static void hook_ditf_queue_job_cb(void *closure, const struct afb_hookid *hookid, const struct afb_ditf *ditf, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout, int result)
692 _hook_ditf_(ditf, "queue_job(%p, %p, %p, %d) -> %d", callback, argument, group, timeout, result);
695 static void hook_ditf_unstore_req_cb(void *closure, const struct afb_hookid *hookid, const struct afb_ditf *ditf, struct afb_stored_req *sreq)
697 _hook_ditf_(ditf, "unstore_req(%p)", sreq);
700 static void hook_ditf_require_api_cb(void *closure, const struct afb_hookid *hookid, const struct afb_ditf *ditf, const char *name, int initialized)
702 _hook_ditf_(ditf, "require_api(%s, %d)...", name, initialized);
705 static void hook_ditf_require_api_result_cb(void *closure, const struct afb_hookid *hookid, const struct afb_ditf *ditf, const char *name, int initialized, int result)
707 _hook_ditf_(ditf, "...require_api(%s, %d) -> %d", name, initialized, result);
710 static struct afb_hook_ditf_itf hook_ditf_default_itf = {
711 .hook_ditf_event_broadcast_before = hook_ditf_event_broadcast_before_cb,
712 .hook_ditf_event_broadcast_after = hook_ditf_event_broadcast_after_cb,
713 .hook_ditf_get_event_loop = hook_ditf_get_event_loop_cb,
714 .hook_ditf_get_user_bus = hook_ditf_get_user_bus_cb,
715 .hook_ditf_get_system_bus = hook_ditf_get_system_bus_cb,
716 .hook_ditf_vverbose = hook_ditf_vverbose_cb,
717 .hook_ditf_event_make = hook_ditf_event_make_cb,
718 .hook_ditf_rootdir_get_fd = hook_ditf_rootdir_get_fd_cb,
719 .hook_ditf_rootdir_open_locale = hook_ditf_rootdir_open_locale_cb,
720 .hook_ditf_queue_job = hook_ditf_queue_job_cb,
721 .hook_ditf_unstore_req = hook_ditf_unstore_req_cb,
722 .hook_ditf_require_api = hook_ditf_require_api_cb,
723 .hook_ditf_require_api_result = hook_ditf_require_api_result_cb
726 /******************************************************************************
727 * section: hooks for tracing daemon interface (ditf)
728 *****************************************************************************/
730 #define _HOOK_DITF_(what,...) \
731 struct afb_hook_ditf *hook; \
732 struct afb_hookid hookid; \
733 pthread_rwlock_rdlock(&rwlock); \
734 init_hookid(&hookid); \
735 hook = list_of_ditf_hooks; \
737 if (hook->itf->hook_ditf_##what \
738 && (hook->flags & afb_hook_flag_ditf_##what) != 0 \
739 && (!hook->api || !strcasecmp(hook->api, ditf->api))) { \
740 hook->itf->hook_ditf_##what(hook->closure, &hookid, __VA_ARGS__); \
744 pthread_rwlock_unlock(&rwlock);
746 void afb_hook_ditf_event_broadcast_before(const struct afb_ditf *ditf, const char *name, struct json_object *object)
748 _HOOK_DITF_(event_broadcast_before, ditf, name, object);
751 int afb_hook_ditf_event_broadcast_after(const struct afb_ditf *ditf, const char *name, struct json_object *object, int result)
753 _HOOK_DITF_(event_broadcast_after, ditf, name, object, result);
757 struct sd_event *afb_hook_ditf_get_event_loop(const struct afb_ditf *ditf, struct sd_event *result)
759 _HOOK_DITF_(get_event_loop, ditf, result);
763 struct sd_bus *afb_hook_ditf_get_user_bus(const struct afb_ditf *ditf, struct sd_bus *result)
765 _HOOK_DITF_(get_user_bus, ditf, result);
769 struct sd_bus *afb_hook_ditf_get_system_bus(const struct afb_ditf *ditf, struct sd_bus *result)
771 _HOOK_DITF_(get_system_bus, ditf, result);
775 void afb_hook_ditf_vverbose(const struct afb_ditf *ditf, int level, const char *file, int line, const char *function, const char *fmt, va_list args)
777 _HOOK_DITF_(vverbose, ditf, level, file, line, function, fmt, args);
780 struct afb_event afb_hook_ditf_event_make(const struct afb_ditf *ditf, const char *name, struct afb_event result)
782 _HOOK_DITF_(event_make, ditf, name, result);
786 int afb_hook_ditf_rootdir_get_fd(const struct afb_ditf *ditf, int result)
788 _HOOK_DITF_(rootdir_get_fd, ditf, result);
792 int afb_hook_ditf_rootdir_open_locale(const struct afb_ditf *ditf, const char *filename, int flags, const char *locale, int result)
794 _HOOK_DITF_(rootdir_open_locale, ditf, filename, flags, locale, result);
798 int afb_hook_ditf_queue_job(const struct afb_ditf *ditf, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout, int result)
800 _HOOK_DITF_(queue_job, ditf, callback, argument, group, timeout, result);
804 void afb_hook_ditf_unstore_req(const struct afb_ditf *ditf, struct afb_stored_req *sreq)
806 _HOOK_DITF_(unstore_req, ditf, sreq);
809 void afb_hook_ditf_require_api(const struct afb_ditf *ditf, const char *name, int initialized)
811 _HOOK_DITF_(require_api, ditf, name, initialized);
814 int afb_hook_ditf_require_api_result(const struct afb_ditf *ditf, const char *name, int initialized, int result)
816 _HOOK_DITF_(require_api_result, ditf, name, initialized, result);
820 /******************************************************************************
821 * section: hooking ditf
822 *****************************************************************************/
824 int afb_hook_flags_ditf(const char *api)
827 struct afb_hook_ditf *hook;
829 pthread_rwlock_rdlock(&rwlock);
831 hook = list_of_ditf_hooks;
833 if (!api || !hook->api || !strcasecmp(hook->api, api))
834 flags |= hook->flags;
837 pthread_rwlock_unlock(&rwlock);
841 struct afb_hook_ditf *afb_hook_create_ditf(const char *api, int flags, struct afb_hook_ditf_itf *itf, void *closure)
843 struct afb_hook_ditf *hook;
845 /* alloc the result */
846 hook = calloc(1, sizeof *hook);
850 /* get a copy of the names */
851 hook->api = api ? strdup(api) : NULL;
852 if (api && !hook->api) {
857 /* initialise the rest */
860 hook->itf = itf ? itf : &hook_ditf_default_itf;
861 hook->closure = closure;
863 /* record the hook */
864 pthread_rwlock_wrlock(&rwlock);
865 hook->next = list_of_ditf_hooks;
866 list_of_ditf_hooks = hook;
867 pthread_rwlock_unlock(&rwlock);
873 struct afb_hook_ditf *afb_hook_addref_ditf(struct afb_hook_ditf *hook)
875 pthread_rwlock_wrlock(&rwlock);
877 pthread_rwlock_unlock(&rwlock);
881 void afb_hook_unref_ditf(struct afb_hook_ditf *hook)
883 struct afb_hook_ditf **prv;
886 pthread_rwlock_wrlock(&rwlock);
887 if (--hook->refcount)
891 prv = &list_of_ditf_hooks;
892 while (*prv && *prv != hook)
897 pthread_rwlock_unlock(&rwlock);
906 /******************************************************************************
907 * section: default callbacks for tracing service interface (svc)
908 *****************************************************************************/
910 static void _hook_svc_(const struct afb_svc *svc, const char *format, ...)
913 va_start(ap, format);
914 _hook_("svc-%s", format, ap, svc->api);
918 static void hook_svc_start_before_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_svc *svc)
920 _hook_svc_(svc, "start.before");
923 static void hook_svc_start_after_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_svc *svc, int status)
925 _hook_svc_(svc, "start.after -> %d", status);
928 static void hook_svc_on_event_before_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_svc *svc, const char *event, int eventid, struct json_object *object)
930 _hook_svc_(svc, "on_event.before(%s, %d, %s)", event, eventid, json_object_to_json_string(object));
933 static void hook_svc_on_event_after_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_svc *svc, const char *event, int eventid, struct json_object *object)
935 _hook_svc_(svc, "on_event.after(%s, %d, %s)", event, eventid, json_object_to_json_string(object));
938 static void hook_svc_call_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args)
940 _hook_svc_(svc, "call(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
943 static void hook_svc_call_result_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_svc *svc, int status, struct json_object *result)
945 _hook_svc_(svc, " ...call... -> %d: %s", status, json_object_to_json_string(result));
948 static void hook_svc_callsync_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args)
950 _hook_svc_(svc, "callsync(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
953 static void hook_svc_callsync_result_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_svc *svc, int status, struct json_object *result)
955 _hook_svc_(svc, " ...callsync... -> %d: %s", status, json_object_to_json_string(result));
958 static struct afb_hook_svc_itf hook_svc_default_itf = {
959 .hook_svc_start_before = hook_svc_start_before_default_cb,
960 .hook_svc_start_after = hook_svc_start_after_default_cb,
961 .hook_svc_on_event_before = hook_svc_on_event_before_default_cb,
962 .hook_svc_on_event_after = hook_svc_on_event_after_default_cb,
963 .hook_svc_call = hook_svc_call_default_cb,
964 .hook_svc_call_result = hook_svc_call_result_default_cb,
965 .hook_svc_callsync = hook_svc_callsync_default_cb,
966 .hook_svc_callsync_result = hook_svc_callsync_result_default_cb
969 /******************************************************************************
970 * section: hooks for tracing service interface (svc)
971 *****************************************************************************/
973 #define _HOOK_SVC_(what,...) \
974 struct afb_hook_svc *hook; \
975 struct afb_hookid hookid; \
976 pthread_rwlock_rdlock(&rwlock); \
977 init_hookid(&hookid); \
978 hook = list_of_svc_hooks; \
980 if (hook->itf->hook_svc_##what \
981 && (hook->flags & afb_hook_flag_svc_##what) != 0 \
982 && (!hook->api || !strcasecmp(hook->api, svc->api))) { \
983 hook->itf->hook_svc_##what(hook->closure, &hookid, __VA_ARGS__); \
987 pthread_rwlock_unlock(&rwlock);
989 void afb_hook_svc_start_before(const struct afb_svc *svc)
991 _HOOK_SVC_(start_before, svc);
994 int afb_hook_svc_start_after(const struct afb_svc *svc, int status)
996 _HOOK_SVC_(start_after, svc, status);
1000 void afb_hook_svc_on_event_before(const struct afb_svc *svc, const char *event, int eventid, struct json_object *object)
1002 _HOOK_SVC_(on_event_before, svc, event, eventid, object);
1005 void afb_hook_svc_on_event_after(const struct afb_svc *svc, const char *event, int eventid, struct json_object *object)
1007 _HOOK_SVC_(on_event_after, svc, event, eventid, object);
1010 void afb_hook_svc_call(const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args)
1012 _HOOK_SVC_(call, svc, api, verb, args);
1015 void afb_hook_svc_call_result(const struct afb_svc *svc, int status, struct json_object *result)
1017 _HOOK_SVC_(call_result, svc, status, result);
1020 void afb_hook_svc_callsync(const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args)
1022 _HOOK_SVC_(callsync, svc, api, verb, args);
1025 int afb_hook_svc_callsync_result(const struct afb_svc *svc, int status, struct json_object *result)
1027 _HOOK_SVC_(callsync_result, svc, status, result);
1031 /******************************************************************************
1032 * section: hooking services (svc)
1033 *****************************************************************************/
1035 int afb_hook_flags_svc(const char *api)
1038 struct afb_hook_svc *hook;
1040 pthread_rwlock_rdlock(&rwlock);
1042 hook = list_of_svc_hooks;
1044 if (!api || !hook->api || !strcasecmp(hook->api, api))
1045 flags |= hook->flags;
1048 pthread_rwlock_unlock(&rwlock);
1052 struct afb_hook_svc *afb_hook_create_svc(const char *api, int flags, struct afb_hook_svc_itf *itf, void *closure)
1054 struct afb_hook_svc *hook;
1056 /* alloc the result */
1057 hook = calloc(1, sizeof *hook);
1061 /* get a copy of the names */
1062 hook->api = api ? strdup(api) : NULL;
1063 if (api && !hook->api) {
1068 /* initialise the rest */
1070 hook->flags = flags;
1071 hook->itf = itf ? itf : &hook_svc_default_itf;
1072 hook->closure = closure;
1074 /* record the hook */
1075 pthread_rwlock_wrlock(&rwlock);
1076 hook->next = list_of_svc_hooks;
1077 list_of_svc_hooks = hook;
1078 pthread_rwlock_unlock(&rwlock);
1084 struct afb_hook_svc *afb_hook_addref_svc(struct afb_hook_svc *hook)
1086 pthread_rwlock_wrlock(&rwlock);
1088 pthread_rwlock_unlock(&rwlock);
1092 void afb_hook_unref_svc(struct afb_hook_svc *hook)
1094 struct afb_hook_svc **prv;
1097 pthread_rwlock_wrlock(&rwlock);
1098 if (--hook->refcount)
1102 prv = &list_of_svc_hooks;
1103 while (*prv && *prv != hook)
1104 prv = &(*prv)->next;
1108 pthread_rwlock_unlock(&rwlock);
1117 /******************************************************************************
1118 * section: default callbacks for tracing service interface (evt)
1119 *****************************************************************************/
1121 static void _hook_evt_(const char *evt, int id, const char *format, ...)
1124 va_start(ap, format);
1125 _hook_("evt-%s:%d", format, ap, evt, id);
1129 static void hook_evt_create_default_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
1131 _hook_evt_(evt, id, "create");
1134 static void hook_evt_push_before_default_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj)
1136 _hook_evt_(evt, id, "push.before(%s)", json_object_to_json_string(obj));
1140 static void hook_evt_push_after_default_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj, int result)
1142 _hook_evt_(evt, id, "push.after(%s) -> %d", json_object_to_json_string(obj), result);
1145 static void hook_evt_broadcast_before_default_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj)
1147 _hook_evt_(evt, id, "broadcast.before(%s)", json_object_to_json_string(obj));
1150 static void hook_evt_broadcast_after_default_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj, int result)
1152 _hook_evt_(evt, id, "broadcast.after(%s) -> %d", json_object_to_json_string(obj), result);
1155 static void hook_evt_name_default_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
1157 _hook_evt_(evt, id, "name");
1160 static void hook_evt_drop_default_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
1162 _hook_evt_(evt, id, "drop");
1165 static struct afb_hook_evt_itf hook_evt_default_itf = {
1166 .hook_evt_create = hook_evt_create_default_cb,
1167 .hook_evt_push_before = hook_evt_push_before_default_cb,
1168 .hook_evt_push_after = hook_evt_push_after_default_cb,
1169 .hook_evt_broadcast_before = hook_evt_broadcast_before_default_cb,
1170 .hook_evt_broadcast_after = hook_evt_broadcast_after_default_cb,
1171 .hook_evt_name = hook_evt_name_default_cb,
1172 .hook_evt_drop = hook_evt_drop_default_cb
1175 /******************************************************************************
1176 * section: hooks for tracing events interface (evt)
1177 *****************************************************************************/
1179 #define _HOOK_EVT_(what,...) \
1180 struct afb_hook_evt *hook; \
1181 struct afb_hookid hookid; \
1182 pthread_rwlock_rdlock(&rwlock); \
1183 init_hookid(&hookid); \
1184 hook = list_of_evt_hooks; \
1186 if (hook->itf->hook_evt_##what \
1187 && (hook->flags & afb_hook_flag_evt_##what) != 0 \
1188 && (!hook->pattern || !fnmatch(hook->pattern, evt, FNM_CASEFOLD))) { \
1189 hook->itf->hook_evt_##what(hook->closure, &hookid, __VA_ARGS__); \
1191 hook = hook->next; \
1193 pthread_rwlock_unlock(&rwlock);
1195 void afb_hook_evt_create(const char *evt, int id)
1197 _HOOK_EVT_(create, evt, id);
1200 void afb_hook_evt_push_before(const char *evt, int id, struct json_object *obj)
1202 _HOOK_EVT_(push_before, evt, id, obj);
1205 int afb_hook_evt_push_after(const char *evt, int id, struct json_object *obj, int result)
1207 _HOOK_EVT_(push_after, evt, id, obj, result);
1211 void afb_hook_evt_broadcast_before(const char *evt, int id, struct json_object *obj)
1213 _HOOK_EVT_(broadcast_before, evt, id, obj);
1216 int afb_hook_evt_broadcast_after(const char *evt, int id, struct json_object *obj, int result)
1218 _HOOK_EVT_(broadcast_after, evt, id, obj, result);
1222 void afb_hook_evt_name(const char *evt, int id)
1224 _HOOK_EVT_(name, evt, id);
1227 void afb_hook_evt_drop(const char *evt, int id)
1229 _HOOK_EVT_(drop, evt, id);
1232 /******************************************************************************
1233 * section: hooking services (evt)
1234 *****************************************************************************/
1236 int afb_hook_flags_evt(const char *name)
1239 struct afb_hook_evt *hook;
1241 pthread_rwlock_rdlock(&rwlock);
1243 hook = list_of_evt_hooks;
1245 if (!name || !hook->pattern || !fnmatch(hook->pattern, name, FNM_CASEFOLD))
1246 flags |= hook->flags;
1249 pthread_rwlock_unlock(&rwlock);
1253 struct afb_hook_evt *afb_hook_create_evt(const char *pattern, int flags, struct afb_hook_evt_itf *itf, void *closure)
1255 struct afb_hook_evt *hook;
1257 /* alloc the result */
1258 hook = calloc(1, sizeof *hook);
1262 /* get a copy of the names */
1263 hook->pattern = pattern ? strdup(pattern) : NULL;
1264 if (pattern && !hook->pattern) {
1269 /* initialise the rest */
1271 hook->flags = flags;
1272 hook->itf = itf ? itf : &hook_evt_default_itf;
1273 hook->closure = closure;
1275 /* record the hook */
1276 pthread_rwlock_wrlock(&rwlock);
1277 hook->next = list_of_evt_hooks;
1278 list_of_evt_hooks = hook;
1279 pthread_rwlock_unlock(&rwlock);
1285 struct afb_hook_evt *afb_hook_addref_evt(struct afb_hook_evt *hook)
1287 pthread_rwlock_wrlock(&rwlock);
1289 pthread_rwlock_unlock(&rwlock);
1293 void afb_hook_unref_evt(struct afb_hook_evt *hook)
1295 struct afb_hook_evt **prv;
1298 pthread_rwlock_wrlock(&rwlock);
1299 if (--hook->refcount)
1303 prv = &list_of_evt_hooks;
1304 while (*prv && *prv != hook)
1305 prv = &(*prv)->next;
1309 pthread_rwlock_unlock(&rwlock);
1312 free(hook->pattern);
1318 /******************************************************************************
1319 * section: default callbacks for globals (global)
1320 *****************************************************************************/
1322 static void _hook_global_(const char *format, ...)
1325 va_start(ap, format);
1326 _hook_("global", format, ap);
1330 static void hook_global_vverbose_default_cb(void *closure, const struct afb_hookid *hookid, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
1337 len = vasprintf(&msg, fmt, ap);
1341 _hook_global_("vverbose(%d, %s, %d, %s) -> %s ? ? ?", level, file, line, func, fmt);
1343 _hook_global_("vverbose(%d, %s, %d, %s) -> %s", level, file, line, func, msg);
1348 static struct afb_hook_global_itf hook_global_default_itf = {
1349 .hook_global_vverbose = hook_global_vverbose_default_cb
1352 /******************************************************************************
1353 * section: hooks for tracing globals (global)
1354 *****************************************************************************/
1356 #define _HOOK_GLOBAL_(what,...) \
1357 struct afb_hook_global *hook; \
1358 struct afb_hookid hookid; \
1359 pthread_rwlock_rdlock(&rwlock); \
1360 init_hookid(&hookid); \
1361 hook = list_of_global_hooks; \
1363 if (hook->itf->hook_global_##what \
1364 && (hook->flags & afb_hook_flag_global_##what) != 0) { \
1365 hook->itf->hook_global_##what(hook->closure, &hookid, __VA_ARGS__); \
1367 hook = hook->next; \
1369 pthread_rwlock_unlock(&rwlock);
1371 static void afb_hook_global_vverbose(int level, const char *file, int line, const char *func, const char *fmt, va_list args)
1373 _HOOK_GLOBAL_(vverbose, level, file ?: "?", line, func ?: "?", fmt, args);
1376 /******************************************************************************
1377 * section: hooking globals (global)
1378 *****************************************************************************/
1380 static void update_global()
1382 struct afb_hook_global *hook;
1385 pthread_rwlock_rdlock(&rwlock);
1386 hook = list_of_global_hooks;
1388 flags = hook->flags;
1391 verbose_observer = (flags & afb_hook_flag_global_vverbose) ? afb_hook_global_vverbose : NULL;
1392 pthread_rwlock_unlock(&rwlock);
1395 struct afb_hook_global *afb_hook_create_global(int flags, struct afb_hook_global_itf *itf, void *closure)
1397 struct afb_hook_global *hook;
1399 /* alloc the result */
1400 hook = calloc(1, sizeof *hook);
1404 /* initialise the rest */
1406 hook->flags = flags;
1407 hook->itf = itf ? itf : &hook_global_default_itf;
1408 hook->closure = closure;
1410 /* record the hook */
1411 pthread_rwlock_wrlock(&rwlock);
1412 hook->next = list_of_global_hooks;
1413 list_of_global_hooks = hook;
1414 pthread_rwlock_unlock(&rwlock);
1416 /* update hooking */
1423 struct afb_hook_global *afb_hook_addref_global(struct afb_hook_global *hook)
1425 pthread_rwlock_wrlock(&rwlock);
1427 pthread_rwlock_unlock(&rwlock);
1431 void afb_hook_unref_global(struct afb_hook_global *hook)
1433 struct afb_hook_global **prv;
1436 pthread_rwlock_wrlock(&rwlock);
1437 if (--hook->refcount)
1441 prv = &list_of_global_hooks;
1442 while (*prv && *prv != hook)
1443 prv = &(*prv)->next;
1447 pthread_rwlock_unlock(&rwlock);
1452 /* update hooking */