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 */
93 /* synchronisation across threads */
94 static pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
96 /* list of hooks for xreq */
97 static struct afb_hook_xreq *list_of_xreq_hooks = NULL;
99 /* list of hooks for ditf */
100 static struct afb_hook_ditf *list_of_ditf_hooks = NULL;
102 /* list of hooks for svc */
103 static struct afb_hook_svc *list_of_svc_hooks = NULL;
105 /* list of hooks for evt */
106 static struct afb_hook_evt *list_of_evt_hooks = NULL;
108 /******************************************************************************
109 * section: default callbacks for tracing requests
110 *****************************************************************************/
112 static char *_pbuf_(const char *fmt, va_list args, char **palloc, char *sbuf, size_t szsbuf)
119 rc = vsnprintf(sbuf, szsbuf, fmt, args);
120 if ((size_t)rc >= szsbuf) {
122 sbuf[szsbuf-2] = sbuf[szsbuf-3] = sbuf[szsbuf-4] = '.';
123 rc = vasprintf(palloc, fmt, cp);
131 static void _hook_(const char *fmt1, const char *fmt2, va_list arg2, ...)
133 char *tag, *data, *mem1, *mem2, buf1[256], buf2[2000];
136 data = _pbuf_(fmt2, arg2, &mem2, buf2, sizeof buf2);
138 va_start(arg1, arg2);
139 tag = _pbuf_(fmt1, arg1, &mem1, buf1, sizeof buf1);
142 NOTICE("[HOOK %s] %s", tag, data);
148 static void _hook_xreq_(const struct afb_xreq *xreq, const char *format, ...)
151 va_start(ap, format);
152 _hook_("xreq-%06d:%s/%s", format, ap, xreq->hookindex, xreq->api, xreq->verb);
156 static void hook_xreq_begin_default_cb(void * closure, const struct afb_xreq *xreq)
159 _hook_xreq_(xreq, "BEGIN");
161 _hook_xreq_(xreq, "BEGIN uid=%d=%s gid=%d pid=%d label=%s id=%s",
162 (int)xreq->cred->uid,
164 (int)xreq->cred->gid,
165 (int)xreq->cred->pid,
166 xreq->cred->label?:"(null)",
167 xreq->cred->id?:"(null)"
171 static void hook_xreq_end_default_cb(void * closure, const struct afb_xreq *xreq)
173 _hook_xreq_(xreq, "END");
176 static void hook_xreq_json_default_cb(void * closure, const struct afb_xreq *xreq, struct json_object *obj)
178 _hook_xreq_(xreq, "json() -> %s", json_object_to_json_string(obj));
181 static void hook_xreq_get_default_cb(void * closure, const struct afb_xreq *xreq, const char *name, struct afb_arg arg)
183 _hook_xreq_(xreq, "get(%s) -> { name: %s, value: %s, path: %s }", name, arg.name, arg.value, arg.path);
186 static void hook_xreq_success_default_cb(void * closure, const struct afb_xreq *xreq, struct json_object *obj, const char *info)
188 _hook_xreq_(xreq, "success(%s, %s)", json_object_to_json_string(obj), info);
191 static void hook_xreq_fail_default_cb(void * closure, const struct afb_xreq *xreq, const char *status, const char *info)
193 _hook_xreq_(xreq, "fail(%s, %s)", status, info);
196 static void hook_xreq_context_get_default_cb(void * closure, const struct afb_xreq *xreq, void *value)
198 _hook_xreq_(xreq, "context_get() -> %p", value);
201 static void hook_xreq_context_set_default_cb(void * closure, const struct afb_xreq *xreq, void *value, void (*free_value)(void*))
203 _hook_xreq_(xreq, "context_set(%p, %p)", value, free_value);
206 static void hook_xreq_addref_default_cb(void * closure, const struct afb_xreq *xreq)
208 _hook_xreq_(xreq, "addref()");
211 static void hook_xreq_unref_default_cb(void * closure, const struct afb_xreq *xreq)
213 _hook_xreq_(xreq, "unref()");
216 static void hook_xreq_session_close_default_cb(void * closure, const struct afb_xreq *xreq)
218 _hook_xreq_(xreq, "session_close()");
221 static void hook_xreq_session_set_LOA_default_cb(void * closure, const struct afb_xreq *xreq, unsigned level, int result)
223 _hook_xreq_(xreq, "session_set_LOA(%u) -> %d", level, result);
226 static void hook_xreq_subscribe_default_cb(void * closure, const struct afb_xreq *xreq, struct afb_event event, int result)
228 _hook_xreq_(xreq, "subscribe(%s:%d) -> %d", afb_evt_event_name(event), afb_evt_event_id(event), result);
231 static void hook_xreq_unsubscribe_default_cb(void * closure, const struct afb_xreq *xreq, struct afb_event event, int result)
233 _hook_xreq_(xreq, "unsubscribe(%s:%d) -> %d", afb_evt_event_name(event), afb_evt_event_id(event), result);
236 static void hook_xreq_subcall_default_cb(void * closure, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
238 _hook_xreq_(xreq, "subcall(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
241 static void hook_xreq_subcall_result_default_cb(void * closure, const struct afb_xreq *xreq, int status, struct json_object *result)
243 _hook_xreq_(xreq, " ...subcall... -> %d: %s", status, json_object_to_json_string(result));
246 static void hook_xreq_subcallsync_default_cb(void * closure, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
248 _hook_xreq_(xreq, "subcallsync(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
251 static void hook_xreq_subcallsync_result_default_cb(void * closure, const struct afb_xreq *xreq, int status, struct json_object *result)
253 _hook_xreq_(xreq, " ...subcallsync... -> %d: %s", status, json_object_to_json_string(result));
256 static void hook_xreq_vverbose_default_cb(void * closure, const struct afb_xreq *xreq, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
263 len = vasprintf(&msg, fmt, ap);
267 _hook_xreq_(xreq, "vverbose(%d, %s, %d, %s) -> %s ? ? ?", level, file, line, func, fmt);
269 _hook_xreq_(xreq, "vverbose(%d, %s, %d, %s) -> %s", level, file, line, func, msg);
274 static void hook_xreq_store_default_cb(void * closure, const struct afb_xreq *xreq, struct afb_stored_req *sreq)
276 _hook_xreq_(xreq, "store() -> %p", sreq);
279 static void hook_xreq_unstore_default_cb(void * closure, const struct afb_xreq *xreq)
281 _hook_xreq_(xreq, "unstore()");
284 static struct afb_hook_xreq_itf hook_xreq_default_itf = {
285 .hook_xreq_begin = hook_xreq_begin_default_cb,
286 .hook_xreq_end = hook_xreq_end_default_cb,
287 .hook_xreq_json = hook_xreq_json_default_cb,
288 .hook_xreq_get = hook_xreq_get_default_cb,
289 .hook_xreq_success = hook_xreq_success_default_cb,
290 .hook_xreq_fail = hook_xreq_fail_default_cb,
291 .hook_xreq_context_get = hook_xreq_context_get_default_cb,
292 .hook_xreq_context_set = hook_xreq_context_set_default_cb,
293 .hook_xreq_addref = hook_xreq_addref_default_cb,
294 .hook_xreq_unref = hook_xreq_unref_default_cb,
295 .hook_xreq_session_close = hook_xreq_session_close_default_cb,
296 .hook_xreq_session_set_LOA = hook_xreq_session_set_LOA_default_cb,
297 .hook_xreq_subscribe = hook_xreq_subscribe_default_cb,
298 .hook_xreq_unsubscribe = hook_xreq_unsubscribe_default_cb,
299 .hook_xreq_subcall = hook_xreq_subcall_default_cb,
300 .hook_xreq_subcall_result = hook_xreq_subcall_result_default_cb,
301 .hook_xreq_subcallsync = hook_xreq_subcallsync_default_cb,
302 .hook_xreq_subcallsync_result = hook_xreq_subcallsync_result_default_cb,
303 .hook_xreq_vverbose = hook_xreq_vverbose_default_cb,
304 .hook_xreq_store = hook_xreq_store_default_cb,
305 .hook_xreq_unstore = hook_xreq_unstore_default_cb
308 /******************************************************************************
309 * section: hooks for tracing requests
310 *****************************************************************************/
312 #define _HOOK_XREQ_(what,...) \
313 struct afb_hook_xreq *hook; \
314 pthread_rwlock_rdlock(&rwlock); \
315 hook = list_of_xreq_hooks; \
317 if (hook->itf->hook_xreq_##what \
318 && (hook->flags & afb_hook_flag_req_##what) != 0 \
319 && (!hook->session || hook->session == xreq->context.session) \
320 && (!hook->api || !strcasecmp(hook->api, xreq->api)) \
321 && (!hook->verb || !strcasecmp(hook->verb, xreq->verb))) { \
322 hook->itf->hook_xreq_##what(hook->closure, __VA_ARGS__); \
326 pthread_rwlock_unlock(&rwlock);
329 void afb_hook_xreq_begin(const struct afb_xreq *xreq)
331 _HOOK_XREQ_(begin, xreq);
334 void afb_hook_xreq_end(const struct afb_xreq *xreq)
336 _HOOK_XREQ_(end, xreq);
339 struct json_object *afb_hook_xreq_json(const struct afb_xreq *xreq, struct json_object *obj)
341 _HOOK_XREQ_(json, xreq, obj);
345 struct afb_arg afb_hook_xreq_get(const struct afb_xreq *xreq, const char *name, struct afb_arg arg)
347 _HOOK_XREQ_(get, xreq, name, arg);
351 void afb_hook_xreq_success(const struct afb_xreq *xreq, struct json_object *obj, const char *info)
353 _HOOK_XREQ_(success, xreq, obj, info);
356 void afb_hook_xreq_fail(const struct afb_xreq *xreq, const char *status, const char *info)
358 _HOOK_XREQ_(fail, xreq, status, info);
361 void *afb_hook_xreq_context_get(const struct afb_xreq *xreq, void *value)
363 _HOOK_XREQ_(context_get, xreq, value);
367 void afb_hook_xreq_context_set(const struct afb_xreq *xreq, void *value, void (*free_value)(void*))
369 _HOOK_XREQ_(context_set, xreq, value, free_value);
372 void afb_hook_xreq_addref(const struct afb_xreq *xreq)
374 _HOOK_XREQ_(addref, xreq);
377 void afb_hook_xreq_unref(const struct afb_xreq *xreq)
379 _HOOK_XREQ_(unref, xreq);
382 void afb_hook_xreq_session_close(const struct afb_xreq *xreq)
384 _HOOK_XREQ_(session_close, xreq);
387 int afb_hook_xreq_session_set_LOA(const struct afb_xreq *xreq, unsigned level, int result)
389 _HOOK_XREQ_(session_set_LOA, xreq, level, result);
393 int afb_hook_xreq_subscribe(const struct afb_xreq *xreq, struct afb_event event, int result)
395 _HOOK_XREQ_(subscribe, xreq, event, result);
399 int afb_hook_xreq_unsubscribe(const struct afb_xreq *xreq, struct afb_event event, int result)
401 _HOOK_XREQ_(unsubscribe, xreq, event, result);
405 void afb_hook_xreq_subcall(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
407 _HOOK_XREQ_(subcall, xreq, api, verb, args);
410 void afb_hook_xreq_subcall_result(const struct afb_xreq *xreq, int status, struct json_object *result)
412 _HOOK_XREQ_(subcall_result, xreq, status, result);
415 void afb_hook_xreq_subcallsync(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
417 _HOOK_XREQ_(subcallsync, xreq, api, verb, args);
420 int afb_hook_xreq_subcallsync_result(const struct afb_xreq *xreq, int status, struct json_object *result)
422 _HOOK_XREQ_(subcallsync_result, xreq, status, result);
426 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)
428 _HOOK_XREQ_(vverbose, xreq, level, file ?: "?", line, func ?: "?", fmt, args);
431 void afb_hook_xreq_store(const struct afb_xreq *xreq, struct afb_stored_req *sreq)
433 _HOOK_XREQ_(store, xreq, sreq);
436 void afb_hook_xreq_unstore(const struct afb_xreq *xreq)
438 _HOOK_XREQ_(unstore, xreq);
441 /******************************************************************************
442 * section: hooking xreqs
443 *****************************************************************************/
445 void afb_hook_init_xreq(struct afb_xreq *xreq)
451 struct afb_hook_xreq *hook;
453 /* scan hook list to get the expected flags */
455 pthread_rwlock_rdlock(&rwlock);
456 hook = list_of_xreq_hooks;
458 f = hook->flags & afb_hook_flags_req_all;
460 && (!hook->session || hook->session == xreq->context.session)
461 && (!hook->api || !strcasecmp(hook->api, xreq->api))
462 && (!hook->verb || !strcasecmp(hook->verb, xreq->verb));
467 pthread_rwlock_unlock(&rwlock);
469 /* store the hooking data */
470 xreq->hookflags = flags;
472 pthread_rwlock_wrlock(&rwlock);
475 xreq->hookindex = reqindex;
476 pthread_rwlock_unlock(&rwlock);
480 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)
482 struct afb_hook_xreq *hook;
484 /* alloc the result */
485 hook = calloc(1, sizeof *hook);
489 /* get a copy of the names */
490 hook->api = api ? strdup(api) : NULL;
491 hook->verb = verb ? strdup(verb) : NULL;
492 if ((api && !hook->api) || (verb && !hook->verb)) {
499 /* initialise the rest */
500 hook->session = session;
502 afb_session_addref(session);
505 hook->itf = itf ? itf : &hook_xreq_default_itf;
506 hook->closure = closure;
508 /* record the hook */
509 pthread_rwlock_wrlock(&rwlock);
510 hook->next = list_of_xreq_hooks;
511 list_of_xreq_hooks = hook;
512 pthread_rwlock_unlock(&rwlock);
518 struct afb_hook_xreq *afb_hook_addref_xreq(struct afb_hook_xreq *hook)
520 pthread_rwlock_wrlock(&rwlock);
522 pthread_rwlock_unlock(&rwlock);
526 void afb_hook_unref_xreq(struct afb_hook_xreq *hook)
528 struct afb_hook_xreq **prv;
531 pthread_rwlock_wrlock(&rwlock);
532 if (--hook->refcount)
536 prv = &list_of_xreq_hooks;
537 while (*prv && *prv != hook)
542 pthread_rwlock_unlock(&rwlock);
548 afb_session_unref(hook->session);
554 /******************************************************************************
555 * section: default callbacks for tracing daemon interface
556 *****************************************************************************/
558 static void _hook_ditf_(const struct afb_ditf *ditf, const char *format, ...)
561 va_start(ap, format);
562 _hook_("ditf-%s", format, ap, ditf->api);
566 static void hook_ditf_event_broadcast_before_cb(void *closure, const struct afb_ditf *ditf, const char *name, struct json_object *object)
568 _hook_ditf_(ditf, "event_broadcast.before(%s, %s)....", name, json_object_to_json_string(object));
571 static void hook_ditf_event_broadcast_after_cb(void *closure, const struct afb_ditf *ditf, const char *name, struct json_object *object, int result)
573 _hook_ditf_(ditf, "event_broadcast.after(%s, %s) -> %d", name, json_object_to_json_string(object), result);
576 static void hook_ditf_get_event_loop_cb(void *closure, const struct afb_ditf *ditf, struct sd_event *result)
578 _hook_ditf_(ditf, "get_event_loop() -> %p", result);
581 static void hook_ditf_get_user_bus_cb(void *closure, const struct afb_ditf *ditf, struct sd_bus *result)
583 _hook_ditf_(ditf, "get_user_bus() -> %p", result);
586 static void hook_ditf_get_system_bus_cb(void *closure, const struct afb_ditf *ditf, struct sd_bus *result)
588 _hook_ditf_(ditf, "get_system_bus() -> %p", result);
591 static void hook_ditf_vverbose_cb(void*closure, const struct afb_ditf *ditf, int level, const char *file, int line, const char *function, const char *fmt, va_list args)
598 len = vasprintf(&msg, fmt, ap);
602 _hook_ditf_(ditf, "vverbose(%d, %s, %d, %s) -> %s ? ? ?", level, file, line, function, fmt);
604 _hook_ditf_(ditf, "vverbose(%d, %s, %d, %s) -> %s", level, file, line, function, msg);
609 static void hook_ditf_event_make_cb(void *closure, const struct afb_ditf *ditf, const char *name, struct afb_event result)
611 _hook_ditf_(ditf, "event_make(%s) -> %s:%d", name, afb_evt_event_name(result), afb_evt_event_id(result));
614 static void hook_ditf_rootdir_get_fd_cb(void *closure, const struct afb_ditf *ditf, int result)
618 _hook_ditf_(ditf, "rootdir_get_fd() -> %d, %m", result);
620 sprintf(path, "/proc/self/fd/%d", result);
621 readlink(path, path, sizeof path);
622 _hook_ditf_(ditf, "rootdir_get_fd() -> %d = %s", result, path);
626 static void hook_ditf_rootdir_open_locale_cb(void *closure, const struct afb_ditf *ditf, const char *filename, int flags, const char *locale, int result)
632 _hook_ditf_(ditf, "rootdir_open_locale(%s, %d, %s) -> %d, %m", filename, flags, locale, result);
634 sprintf(path, "/proc/self/fd/%d", result);
635 readlink(path, path, sizeof path);
636 _hook_ditf_(ditf, "rootdir_open_locale(%s, %d, %s) -> %d = %s", filename, flags, locale, result, path);
640 static void hook_ditf_queue_job_cb(void *closure, const struct afb_ditf *ditf, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout, int result)
642 _hook_ditf_(ditf, "queue_job(%p, %p, %p, %d) -> %d", callback, argument, group, timeout, result);
645 static void hook_ditf_unstore_req_cb(void * closure, const struct afb_ditf *ditf, struct afb_stored_req *sreq)
647 _hook_ditf_(ditf, "unstore_req(%p)", sreq);
650 static void hook_ditf_require_api_cb(void *closure, const struct afb_ditf *ditf, const char *name, int initialized)
652 _hook_ditf_(ditf, "require_api(%s, %d)...", name, initialized);
655 static void hook_ditf_require_api_result_cb(void *closure, const struct afb_ditf *ditf, const char *name, int initialized, int result)
657 _hook_ditf_(ditf, "...require_api(%s, %d) -> %d", name, initialized, result);
660 static struct afb_hook_ditf_itf hook_ditf_default_itf = {
661 .hook_ditf_event_broadcast_before = hook_ditf_event_broadcast_before_cb,
662 .hook_ditf_event_broadcast_after = hook_ditf_event_broadcast_after_cb,
663 .hook_ditf_get_event_loop = hook_ditf_get_event_loop_cb,
664 .hook_ditf_get_user_bus = hook_ditf_get_user_bus_cb,
665 .hook_ditf_get_system_bus = hook_ditf_get_system_bus_cb,
666 .hook_ditf_vverbose = hook_ditf_vverbose_cb,
667 .hook_ditf_event_make = hook_ditf_event_make_cb,
668 .hook_ditf_rootdir_get_fd = hook_ditf_rootdir_get_fd_cb,
669 .hook_ditf_rootdir_open_locale = hook_ditf_rootdir_open_locale_cb,
670 .hook_ditf_queue_job = hook_ditf_queue_job_cb,
671 .hook_ditf_unstore_req = hook_ditf_unstore_req_cb,
672 .hook_ditf_require_api = hook_ditf_require_api_cb,
673 .hook_ditf_require_api_result = hook_ditf_require_api_result_cb
676 /******************************************************************************
677 * section: hooks for tracing daemon interface (ditf)
678 *****************************************************************************/
680 #define _HOOK_DITF_(what,...) \
681 struct afb_hook_ditf *hook; \
682 pthread_rwlock_rdlock(&rwlock); \
683 hook = list_of_ditf_hooks; \
685 if (hook->itf->hook_ditf_##what \
686 && (hook->flags & afb_hook_flag_ditf_##what) != 0 \
687 && (!hook->api || !strcasecmp(hook->api, ditf->api))) { \
688 hook->itf->hook_ditf_##what(hook->closure, __VA_ARGS__); \
692 pthread_rwlock_unlock(&rwlock);
694 void afb_hook_ditf_event_broadcast_before(const struct afb_ditf *ditf, const char *name, struct json_object *object)
696 _HOOK_DITF_(event_broadcast_before, ditf, name, object);
699 int afb_hook_ditf_event_broadcast_after(const struct afb_ditf *ditf, const char *name, struct json_object *object, int result)
701 _HOOK_DITF_(event_broadcast_after, ditf, name, object, result);
705 struct sd_event *afb_hook_ditf_get_event_loop(const struct afb_ditf *ditf, struct sd_event *result)
707 _HOOK_DITF_(get_event_loop, ditf, result);
711 struct sd_bus *afb_hook_ditf_get_user_bus(const struct afb_ditf *ditf, struct sd_bus *result)
713 _HOOK_DITF_(get_user_bus, ditf, result);
717 struct sd_bus *afb_hook_ditf_get_system_bus(const struct afb_ditf *ditf, struct sd_bus *result)
719 _HOOK_DITF_(get_system_bus, ditf, result);
723 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)
725 _HOOK_DITF_(vverbose, ditf, level, file, line, function, fmt, args);
728 struct afb_event afb_hook_ditf_event_make(const struct afb_ditf *ditf, const char *name, struct afb_event result)
730 _HOOK_DITF_(event_make, ditf, name, result);
734 int afb_hook_ditf_rootdir_get_fd(const struct afb_ditf *ditf, int result)
736 _HOOK_DITF_(rootdir_get_fd, ditf, result);
740 int afb_hook_ditf_rootdir_open_locale(const struct afb_ditf *ditf, const char *filename, int flags, const char *locale, int result)
742 _HOOK_DITF_(rootdir_open_locale, ditf, filename, flags, locale, result);
746 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)
748 _HOOK_DITF_(queue_job, ditf, callback, argument, group, timeout, result);
752 void afb_hook_ditf_unstore_req(const struct afb_ditf *ditf, struct afb_stored_req *sreq)
754 _HOOK_DITF_(unstore_req, ditf, sreq);
757 void afb_hook_ditf_require_api(const struct afb_ditf *ditf, const char *name, int initialized)
759 _HOOK_DITF_(require_api, ditf, name, initialized);
762 int afb_hook_ditf_require_api_result(const struct afb_ditf *ditf, const char *name, int initialized, int result)
764 _HOOK_DITF_(require_api_result, ditf, name, initialized, result);
768 /******************************************************************************
769 * section: hooking ditf
770 *****************************************************************************/
772 int afb_hook_flags_ditf(const char *api)
775 struct afb_hook_ditf *hook;
777 pthread_rwlock_rdlock(&rwlock);
779 hook = list_of_ditf_hooks;
781 if (!api || !hook->api || !strcasecmp(hook->api, api))
782 flags |= hook->flags;
785 pthread_rwlock_unlock(&rwlock);
789 struct afb_hook_ditf *afb_hook_create_ditf(const char *api, int flags, struct afb_hook_ditf_itf *itf, void *closure)
791 struct afb_hook_ditf *hook;
793 /* alloc the result */
794 hook = calloc(1, sizeof *hook);
798 /* get a copy of the names */
799 hook->api = api ? strdup(api) : NULL;
800 if (api && !hook->api) {
805 /* initialise the rest */
808 hook->itf = itf ? itf : &hook_ditf_default_itf;
809 hook->closure = closure;
811 /* record the hook */
812 pthread_rwlock_wrlock(&rwlock);
813 hook->next = list_of_ditf_hooks;
814 list_of_ditf_hooks = hook;
815 pthread_rwlock_unlock(&rwlock);
821 struct afb_hook_ditf *afb_hook_addref_ditf(struct afb_hook_ditf *hook)
823 pthread_rwlock_wrlock(&rwlock);
825 pthread_rwlock_unlock(&rwlock);
829 void afb_hook_unref_ditf(struct afb_hook_ditf *hook)
831 struct afb_hook_ditf **prv;
834 pthread_rwlock_wrlock(&rwlock);
835 if (--hook->refcount)
839 prv = &list_of_ditf_hooks;
840 while (*prv && *prv != hook)
845 pthread_rwlock_unlock(&rwlock);
854 /******************************************************************************
855 * section: default callbacks for tracing service interface (svc)
856 *****************************************************************************/
858 static void _hook_svc_(const struct afb_svc *svc, const char *format, ...)
861 va_start(ap, format);
862 _hook_("svc-%s", format, ap, svc->api);
866 static void hook_svc_start_before_default_cb(void *closure, const struct afb_svc *svc)
868 _hook_svc_(svc, "start.before");
871 static void hook_svc_start_after_default_cb(void *closure, const struct afb_svc *svc, int status)
873 _hook_svc_(svc, "start.after -> %d", status);
876 static void hook_svc_on_event_before_default_cb(void *closure, const struct afb_svc *svc, const char *event, int eventid, struct json_object *object)
878 _hook_svc_(svc, "on_event.before(%s, %d, %s)", event, eventid, json_object_to_json_string(object));
881 static void hook_svc_on_event_after_default_cb(void *closure, const struct afb_svc *svc, const char *event, int eventid, struct json_object *object)
883 _hook_svc_(svc, "on_event.after(%s, %d, %s)", event, eventid, json_object_to_json_string(object));
886 static void hook_svc_call_default_cb(void *closure, const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args)
888 _hook_svc_(svc, "call(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
891 static void hook_svc_call_result_default_cb(void *closure, const struct afb_svc *svc, int status, struct json_object *result)
893 _hook_svc_(svc, " ...call... -> %d: %s", status, json_object_to_json_string(result));
896 static void hook_svc_callsync_default_cb(void *closure, const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args)
898 _hook_svc_(svc, "callsync(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
901 static void hook_svc_callsync_result_default_cb(void *closure, const struct afb_svc *svc, int status, struct json_object *result)
903 _hook_svc_(svc, " ...callsync... -> %d: %s", status, json_object_to_json_string(result));
906 static struct afb_hook_svc_itf hook_svc_default_itf = {
907 .hook_svc_start_before = hook_svc_start_before_default_cb,
908 .hook_svc_start_after = hook_svc_start_after_default_cb,
909 .hook_svc_on_event_before = hook_svc_on_event_before_default_cb,
910 .hook_svc_on_event_after = hook_svc_on_event_after_default_cb,
911 .hook_svc_call = hook_svc_call_default_cb,
912 .hook_svc_call_result = hook_svc_call_result_default_cb,
913 .hook_svc_callsync = hook_svc_callsync_default_cb,
914 .hook_svc_callsync_result = hook_svc_callsync_result_default_cb
917 /******************************************************************************
918 * section: hooks for tracing service interface (svc)
919 *****************************************************************************/
921 #define _HOOK_SVC_(what,...) \
922 struct afb_hook_svc *hook; \
923 pthread_rwlock_rdlock(&rwlock); \
924 hook = list_of_svc_hooks; \
926 if (hook->itf->hook_svc_##what \
927 && (hook->flags & afb_hook_flag_svc_##what) != 0 \
928 && (!hook->api || !strcasecmp(hook->api, svc->api))) { \
929 hook->itf->hook_svc_##what(hook->closure, __VA_ARGS__); \
933 pthread_rwlock_unlock(&rwlock);
935 void afb_hook_svc_start_before(const struct afb_svc *svc)
937 _HOOK_SVC_(start_before, svc);
940 int afb_hook_svc_start_after(const struct afb_svc *svc, int status)
942 _HOOK_SVC_(start_after, svc, status);
946 void afb_hook_svc_on_event_before(const struct afb_svc *svc, const char *event, int eventid, struct json_object *object)
948 _HOOK_SVC_(on_event_before, svc, event, eventid, object);
951 void afb_hook_svc_on_event_after(const struct afb_svc *svc, const char *event, int eventid, struct json_object *object)
953 _HOOK_SVC_(on_event_after, svc, event, eventid, object);
956 void afb_hook_svc_call(const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args)
958 _HOOK_SVC_(call, svc, api, verb, args);
961 void afb_hook_svc_call_result(const struct afb_svc *svc, int status, struct json_object *result)
963 _HOOK_SVC_(call_result, svc, status, result);
966 void afb_hook_svc_callsync(const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args)
968 _HOOK_SVC_(callsync, svc, api, verb, args);
971 int afb_hook_svc_callsync_result(const struct afb_svc *svc, int status, struct json_object *result)
973 _HOOK_SVC_(callsync_result, svc, status, result);
977 /******************************************************************************
978 * section: hooking services (svc)
979 *****************************************************************************/
981 int afb_hook_flags_svc(const char *api)
984 struct afb_hook_svc *hook;
986 pthread_rwlock_rdlock(&rwlock);
988 hook = list_of_svc_hooks;
990 if (!api || !hook->api || !strcasecmp(hook->api, api))
991 flags |= hook->flags;
994 pthread_rwlock_unlock(&rwlock);
998 struct afb_hook_svc *afb_hook_create_svc(const char *api, int flags, struct afb_hook_svc_itf *itf, void *closure)
1000 struct afb_hook_svc *hook;
1002 /* alloc the result */
1003 hook = calloc(1, sizeof *hook);
1007 /* get a copy of the names */
1008 hook->api = api ? strdup(api) : NULL;
1009 if (api && !hook->api) {
1014 /* initialise the rest */
1016 hook->flags = flags;
1017 hook->itf = itf ? itf : &hook_svc_default_itf;
1018 hook->closure = closure;
1020 /* record the hook */
1021 pthread_rwlock_wrlock(&rwlock);
1022 hook->next = list_of_svc_hooks;
1023 list_of_svc_hooks = hook;
1024 pthread_rwlock_unlock(&rwlock);
1030 struct afb_hook_svc *afb_hook_addref_svc(struct afb_hook_svc *hook)
1032 pthread_rwlock_wrlock(&rwlock);
1034 pthread_rwlock_unlock(&rwlock);
1038 void afb_hook_unref_svc(struct afb_hook_svc *hook)
1040 struct afb_hook_svc **prv;
1043 pthread_rwlock_wrlock(&rwlock);
1044 if (--hook->refcount)
1048 prv = &list_of_svc_hooks;
1049 while (*prv && *prv != hook)
1050 prv = &(*prv)->next;
1054 pthread_rwlock_unlock(&rwlock);
1063 /******************************************************************************
1064 * section: default callbacks for tracing service interface (evt)
1065 *****************************************************************************/
1067 static void _hook_evt_(const char *evt, int id, const char *format, ...)
1070 va_start(ap, format);
1071 _hook_("evt-%s:%d", format, ap, evt, id);
1075 static void hook_evt_create_default_cb(void *closure, const char *evt, int id)
1077 _hook_evt_(evt, id, "create");
1080 static void hook_evt_push_before_default_cb(void *closure, const char *evt, int id, struct json_object *obj)
1082 _hook_evt_(evt, id, "push.before(%s)", json_object_to_json_string(obj));
1086 static void hook_evt_push_after_default_cb(void *closure, const char *evt, int id, struct json_object *obj, int result)
1088 _hook_evt_(evt, id, "push.after(%s) -> %d", json_object_to_json_string(obj), result);
1091 static void hook_evt_broadcast_before_default_cb(void *closure, const char *evt, int id, struct json_object *obj)
1093 _hook_evt_(evt, id, "broadcast.before(%s)", json_object_to_json_string(obj));
1096 static void hook_evt_broadcast_after_default_cb(void *closure, const char *evt, int id, struct json_object *obj, int result)
1098 _hook_evt_(evt, id, "broadcast.after(%s) -> %d", json_object_to_json_string(obj), result);
1101 static void hook_evt_name_default_cb(void *closure, const char *evt, int id)
1103 _hook_evt_(evt, id, "name");
1106 static void hook_evt_drop_default_cb(void *closure, const char *evt, int id)
1108 _hook_evt_(evt, id, "drop");
1111 static struct afb_hook_evt_itf hook_evt_default_itf = {
1112 .hook_evt_create = hook_evt_create_default_cb,
1113 .hook_evt_push_before = hook_evt_push_before_default_cb,
1114 .hook_evt_push_after = hook_evt_push_after_default_cb,
1115 .hook_evt_broadcast_before = hook_evt_broadcast_before_default_cb,
1116 .hook_evt_broadcast_after = hook_evt_broadcast_after_default_cb,
1117 .hook_evt_name = hook_evt_name_default_cb,
1118 .hook_evt_drop = hook_evt_drop_default_cb
1121 /******************************************************************************
1122 * section: hooks for tracing service interface (evt)
1123 *****************************************************************************/
1125 #define _HOOK_EVT_(what,...) \
1126 struct afb_hook_evt *hook; \
1127 pthread_rwlock_rdlock(&rwlock); \
1128 hook = list_of_evt_hooks; \
1130 if (hook->itf->hook_evt_##what \
1131 && (hook->flags & afb_hook_flag_evt_##what) != 0 \
1132 && (!hook->pattern || !fnmatch(hook->pattern, evt, FNM_CASEFOLD))) { \
1133 hook->itf->hook_evt_##what(hook->closure, __VA_ARGS__); \
1135 hook = hook->next; \
1137 pthread_rwlock_unlock(&rwlock);
1139 void afb_hook_evt_create(const char *evt, int id)
1141 _HOOK_EVT_(create, evt, id);
1144 void afb_hook_evt_push_before(const char *evt, int id, struct json_object *obj)
1146 _HOOK_EVT_(push_before, evt, id, obj);
1149 int afb_hook_evt_push_after(const char *evt, int id, struct json_object *obj, int result)
1151 _HOOK_EVT_(push_after, evt, id, obj, result);
1155 void afb_hook_evt_broadcast_before(const char *evt, int id, struct json_object *obj)
1157 _HOOK_EVT_(broadcast_before, evt, id, obj);
1160 int afb_hook_evt_broadcast_after(const char *evt, int id, struct json_object *obj, int result)
1162 _HOOK_EVT_(broadcast_after, evt, id, obj, result);
1166 void afb_hook_evt_name(const char *evt, int id)
1168 _HOOK_EVT_(name, evt, id);
1171 void afb_hook_evt_drop(const char *evt, int id)
1173 _HOOK_EVT_(drop, evt, id);
1176 /******************************************************************************
1177 * section: hooking services (evt)
1178 *****************************************************************************/
1180 int afb_hook_flags_evt(const char *name)
1183 struct afb_hook_evt *hook;
1185 pthread_rwlock_rdlock(&rwlock);
1187 hook = list_of_evt_hooks;
1189 if (!name || !hook->pattern || !fnmatch(hook->pattern, name, FNM_CASEFOLD))
1190 flags |= hook->flags;
1193 pthread_rwlock_unlock(&rwlock);
1197 struct afb_hook_evt *afb_hook_create_evt(const char *pattern, int flags, struct afb_hook_evt_itf *itf, void *closure)
1199 struct afb_hook_evt *hook;
1201 /* alloc the result */
1202 hook = calloc(1, sizeof *hook);
1206 /* get a copy of the names */
1207 hook->pattern = pattern ? strdup(pattern) : NULL;
1208 if (pattern && !hook->pattern) {
1213 /* initialise the rest */
1215 hook->flags = flags;
1216 hook->itf = itf ? itf : &hook_evt_default_itf;
1217 hook->closure = closure;
1219 /* record the hook */
1220 pthread_rwlock_wrlock(&rwlock);
1221 hook->next = list_of_evt_hooks;
1222 list_of_evt_hooks = hook;
1223 pthread_rwlock_unlock(&rwlock);
1229 struct afb_hook_evt *afb_hook_addref_evt(struct afb_hook_evt *hook)
1231 pthread_rwlock_wrlock(&rwlock);
1233 pthread_rwlock_unlock(&rwlock);
1237 void afb_hook_unref_evt(struct afb_hook_evt *hook)
1239 struct afb_hook_evt **prv;
1242 pthread_rwlock_wrlock(&rwlock);
1243 if (--hook->refcount)
1247 prv = &list_of_evt_hooks;
1248 while (*prv && *prv != hook)
1249 prv = &(*prv)->next;
1253 pthread_rwlock_unlock(&rwlock);
1256 free(hook->pattern);