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.
27 #include <json-c/json.h>
29 #include <afb/afb-req-itf.h>
30 #include <afb/afb-event-itf.h>
32 #include "afb-context.h"
34 #include "afb-session.h"
42 * Definition of a hook for xreq
44 struct afb_hook_xreq {
45 struct afb_hook_xreq *next; /**< next hook */
46 unsigned refcount; /**< reference count */
47 char *api; /**< api hooked or NULL for any */
48 char *verb; /**< verb hooked or NULL for any */
49 struct afb_session *session; /**< session hooked or NULL if any */
50 unsigned flags; /**< hook flags */
51 struct afb_hook_xreq_itf *itf; /**< interface of hook */
52 void *closure; /**< closure for callbacks */
56 * Definition of a hook for ditf
58 struct afb_hook_ditf {
59 struct afb_hook_ditf *next; /**< next hook */
60 unsigned refcount; /**< reference count */
61 char *api; /**< api hooked or NULL for any */
62 unsigned flags; /**< hook flags */
63 struct afb_hook_ditf_itf *itf; /**< interface of hook */
64 void *closure; /**< closure for callbacks */
68 * Definition of a hook for svc
71 struct afb_hook_svc *next; /**< next hook */
72 unsigned refcount; /**< reference count */
73 char *api; /**< api hooked or NULL for any */
74 unsigned flags; /**< hook flags */
75 struct afb_hook_svc_itf *itf; /**< interface of hook */
76 void *closure; /**< closure for callbacks */
79 /* synchronisation across threads */
80 static pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
82 /* list of hooks for xreq */
83 static struct afb_hook_xreq *list_of_xreq_hooks = NULL;
85 /* list of hooks for ditf */
86 static struct afb_hook_ditf *list_of_ditf_hooks = NULL;
88 /* list of hooks for svc */
89 static struct afb_hook_svc *list_of_svc_hooks = NULL;
91 /******************************************************************************
92 * section: default callbacks for tracing requests
93 *****************************************************************************/
95 static void _hook_xreq_(const struct afb_xreq *xreq, const char *format, ...)
101 va_start(ap, format);
102 len = vasprintf(&buffer, format, ap);
106 NOTICE("hook xreq-%06d:%s/%s allocation error", xreq->hookindex, xreq->api, xreq->verb);
108 NOTICE("hook xreq-%06d:%s/%s %s", xreq->hookindex, xreq->api, xreq->verb, buffer);
113 static void hook_xreq_begin_default_cb(void * closure, const struct afb_xreq *xreq)
116 _hook_xreq_(xreq, "BEGIN");
118 _hook_xreq_(xreq, "BEGIN uid=%d=%s gid=%d pid=%d label=%s id=%s",
119 (int)xreq->cred->uid,
121 (int)xreq->cred->gid,
122 (int)xreq->cred->pid,
123 xreq->cred->label?:"(null)",
124 xreq->cred->id?:"(null)"
128 static void hook_xreq_end_default_cb(void * closure, const struct afb_xreq *xreq)
130 _hook_xreq_(xreq, "END");
133 static void hook_xreq_json_default_cb(void * closure, const struct afb_xreq *xreq, struct json_object *obj)
135 _hook_xreq_(xreq, "json() -> %s", json_object_to_json_string(obj));
138 static void hook_xreq_get_default_cb(void * closure, const struct afb_xreq *xreq, const char *name, struct afb_arg arg)
140 _hook_xreq_(xreq, "get(%s) -> { name: %s, value: %s, path: %s }", name, arg.name, arg.value, arg.path);
143 static void hook_xreq_success_default_cb(void * closure, const struct afb_xreq *xreq, struct json_object *obj, const char *info)
145 _hook_xreq_(xreq, "success(%s, %s)", json_object_to_json_string(obj), info);
148 static void hook_xreq_fail_default_cb(void * closure, const struct afb_xreq *xreq, const char *status, const char *info)
150 _hook_xreq_(xreq, "fail(%s, %s)", status, info);
153 static void hook_xreq_context_get_default_cb(void * closure, const struct afb_xreq *xreq, void *value)
155 _hook_xreq_(xreq, "context_get() -> %p", value);
158 static void hook_xreq_context_set_default_cb(void * closure, const struct afb_xreq *xreq, void *value, void (*free_value)(void*))
160 _hook_xreq_(xreq, "context_set(%p, %p)", value, free_value);
163 static void hook_xreq_addref_default_cb(void * closure, const struct afb_xreq *xreq)
165 _hook_xreq_(xreq, "addref()");
168 static void hook_xreq_unref_default_cb(void * closure, const struct afb_xreq *xreq)
170 _hook_xreq_(xreq, "unref()");
173 static void hook_xreq_session_close_default_cb(void * closure, const struct afb_xreq *xreq)
175 _hook_xreq_(xreq, "session_close()");
178 static void hook_xreq_session_set_LOA_default_cb(void * closure, const struct afb_xreq *xreq, unsigned level, int result)
180 _hook_xreq_(xreq, "session_set_LOA(%u) -> %d", level, result);
183 static void hook_xreq_subscribe_default_cb(void * closure, const struct afb_xreq *xreq, struct afb_event event, int result)
185 _hook_xreq_(xreq, "subscribe(%s:%p) -> %d", afb_event_name(event), event.closure, result);
188 static void hook_xreq_unsubscribe_default_cb(void * closure, const struct afb_xreq *xreq, struct afb_event event, int result)
190 _hook_xreq_(xreq, "unsubscribe(%s:%p) -> %d", afb_event_name(event), event.closure, result);
193 static void hook_xreq_subcall_default_cb(void * closure, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
195 _hook_xreq_(xreq, "subcall(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
198 static void hook_xreq_subcall_result_default_cb(void * closure, const struct afb_xreq *xreq, int status, struct json_object *result)
200 _hook_xreq_(xreq, " ...subcall... -> %d: %s", status, json_object_to_json_string(result));
203 static void hook_xreq_subcallsync_default_cb(void * closure, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
205 _hook_xreq_(xreq, "subcallsync(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
208 static void hook_xreq_subcallsync_result_default_cb(void * closure, const struct afb_xreq *xreq, int status, struct json_object *result)
210 _hook_xreq_(xreq, " ...subcallsync... -> %d: %s", status, json_object_to_json_string(result));
213 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)
220 len = vasprintf(&msg, fmt, ap);
224 _hook_xreq_(xreq, "vverbose(%d, %s, %d, %s) -> %s ? ? ?", level, file, line, func, fmt);
226 _hook_xreq_(xreq, "vverbose(%d, %s, %d, %s) -> %s", level, file, line, func, msg);
231 static struct afb_hook_xreq_itf hook_xreq_default_itf = {
232 .hook_xreq_begin = hook_xreq_begin_default_cb,
233 .hook_xreq_end = hook_xreq_end_default_cb,
234 .hook_xreq_json = hook_xreq_json_default_cb,
235 .hook_xreq_get = hook_xreq_get_default_cb,
236 .hook_xreq_success = hook_xreq_success_default_cb,
237 .hook_xreq_fail = hook_xreq_fail_default_cb,
238 .hook_xreq_context_get = hook_xreq_context_get_default_cb,
239 .hook_xreq_context_set = hook_xreq_context_set_default_cb,
240 .hook_xreq_addref = hook_xreq_addref_default_cb,
241 .hook_xreq_unref = hook_xreq_unref_default_cb,
242 .hook_xreq_session_close = hook_xreq_session_close_default_cb,
243 .hook_xreq_session_set_LOA = hook_xreq_session_set_LOA_default_cb,
244 .hook_xreq_subscribe = hook_xreq_subscribe_default_cb,
245 .hook_xreq_unsubscribe = hook_xreq_unsubscribe_default_cb,
246 .hook_xreq_subcall = hook_xreq_subcall_default_cb,
247 .hook_xreq_subcall_result = hook_xreq_subcall_result_default_cb,
248 .hook_xreq_subcallsync = hook_xreq_subcallsync_default_cb,
249 .hook_xreq_subcallsync_result = hook_xreq_subcallsync_result_default_cb,
250 .hook_xreq_vverbose = hook_xreq_vverbose_default_cb
253 /******************************************************************************
254 * section: hooks for tracing requests
255 *****************************************************************************/
257 #define _HOOK_XREQ_(what,...) \
258 struct afb_hook_xreq *hook; \
259 pthread_rwlock_rdlock(&rwlock); \
260 hook = list_of_xreq_hooks; \
262 if (hook->itf->hook_xreq_##what \
263 && (hook->flags & afb_hook_flag_req_##what) != 0 \
264 && (!hook->session || hook->session == xreq->context.session) \
265 && (!hook->api || !strcasecmp(hook->api, xreq->api)) \
266 && (!hook->verb || !strcasecmp(hook->verb, xreq->verb))) { \
267 hook->itf->hook_xreq_##what(hook->closure, __VA_ARGS__); \
271 pthread_rwlock_unlock(&rwlock);
274 void afb_hook_xreq_begin(const struct afb_xreq *xreq)
276 _HOOK_XREQ_(begin, xreq);
279 void afb_hook_xreq_end(const struct afb_xreq *xreq)
281 _HOOK_XREQ_(end, xreq);
284 struct json_object *afb_hook_xreq_json(const struct afb_xreq *xreq, struct json_object *obj)
286 _HOOK_XREQ_(json, xreq, obj);
290 struct afb_arg afb_hook_xreq_get(const struct afb_xreq *xreq, const char *name, struct afb_arg arg)
292 _HOOK_XREQ_(get, xreq, name, arg);
296 void afb_hook_xreq_success(const struct afb_xreq *xreq, struct json_object *obj, const char *info)
298 _HOOK_XREQ_(success, xreq, obj, info);
301 void afb_hook_xreq_fail(const struct afb_xreq *xreq, const char *status, const char *info)
303 _HOOK_XREQ_(fail, xreq, status, info);
306 void *afb_hook_xreq_context_get(const struct afb_xreq *xreq, void *value)
308 _HOOK_XREQ_(context_get, xreq, value);
312 void afb_hook_xreq_context_set(const struct afb_xreq *xreq, void *value, void (*free_value)(void*))
314 _HOOK_XREQ_(context_set, xreq, value, free_value);
317 void afb_hook_xreq_addref(const struct afb_xreq *xreq)
319 _HOOK_XREQ_(addref, xreq);
322 void afb_hook_xreq_unref(const struct afb_xreq *xreq)
324 _HOOK_XREQ_(unref, xreq);
327 void afb_hook_xreq_session_close(const struct afb_xreq *xreq)
329 _HOOK_XREQ_(session_close, xreq);
332 int afb_hook_xreq_session_set_LOA(const struct afb_xreq *xreq, unsigned level, int result)
334 _HOOK_XREQ_(session_set_LOA, xreq, level, result);
338 int afb_hook_xreq_subscribe(const struct afb_xreq *xreq, struct afb_event event, int result)
340 _HOOK_XREQ_(subscribe, xreq, event, result);
344 int afb_hook_xreq_unsubscribe(const struct afb_xreq *xreq, struct afb_event event, int result)
346 _HOOK_XREQ_(unsubscribe, xreq, event, result);
350 void afb_hook_xreq_subcall(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
352 _HOOK_XREQ_(subcall, xreq, api, verb, args);
355 void afb_hook_xreq_subcall_result(const struct afb_xreq *xreq, int status, struct json_object *result)
357 _HOOK_XREQ_(subcall_result, xreq, status, result);
360 void afb_hook_xreq_subcallsync(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
362 _HOOK_XREQ_(subcallsync, xreq, api, verb, args);
365 int afb_hook_xreq_subcallsync_result(const struct afb_xreq *xreq, int status, struct json_object *result)
367 _HOOK_XREQ_(subcallsync_result, xreq, status, result);
371 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)
373 _HOOK_XREQ_(vverbose, xreq, level, file ?: "?", line, func ?: "?", fmt, args);
376 /******************************************************************************
377 * section: hooking xreqs
378 *****************************************************************************/
380 void afb_hook_init_xreq(struct afb_xreq *xreq)
386 struct afb_hook_xreq *hook;
388 /* scan hook list to get the expected flags */
390 pthread_rwlock_rdlock(&rwlock);
391 hook = list_of_xreq_hooks;
393 f = hook->flags & afb_hook_flags_req_all;
395 && (!hook->session || hook->session == xreq->context.session)
396 && (!hook->api || !strcasecmp(hook->api, xreq->api))
397 && (!hook->verb || !strcasecmp(hook->verb, xreq->verb));
402 pthread_rwlock_unlock(&rwlock);
404 /* store the hooking data */
405 xreq->hookflags = flags;
407 pthread_rwlock_wrlock(&rwlock);
410 xreq->hookindex = reqindex;
411 pthread_rwlock_unlock(&rwlock);
415 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)
417 struct afb_hook_xreq *hook;
419 /* alloc the result */
420 hook = calloc(1, sizeof *hook);
424 /* get a copy of the names */
425 hook->api = api ? strdup(api) : NULL;
426 hook->verb = verb ? strdup(verb) : NULL;
427 if ((api && !hook->api) || (verb && !hook->verb)) {
434 /* initialise the rest */
435 hook->session = session;
437 afb_session_addref(session);
440 hook->itf = itf ? itf : &hook_xreq_default_itf;
441 hook->closure = closure;
443 /* record the hook */
444 pthread_rwlock_wrlock(&rwlock);
445 hook->next = list_of_xreq_hooks;
446 list_of_xreq_hooks = hook;
447 pthread_rwlock_unlock(&rwlock);
453 struct afb_hook_xreq *afb_hook_addref_xreq(struct afb_hook_xreq *hook)
455 pthread_rwlock_wrlock(&rwlock);
457 pthread_rwlock_unlock(&rwlock);
461 void afb_hook_unref_xreq(struct afb_hook_xreq *hook)
463 struct afb_hook_xreq **prv;
466 pthread_rwlock_wrlock(&rwlock);
467 if (--hook->refcount)
471 prv = &list_of_xreq_hooks;
472 while (*prv && *prv != hook)
477 pthread_rwlock_unlock(&rwlock);
483 afb_session_unref(hook->session);
489 /******************************************************************************
490 * section: default callbacks for tracing daemon interface
491 *****************************************************************************/
493 static void _hook_ditf_(const struct afb_ditf *ditf, const char *format, ...)
499 va_start(ap, format);
500 len = vasprintf(&buffer, format, ap);
504 NOTICE("hook ditf-%s allocation error for %s", ditf->prefix, format);
506 NOTICE("hook ditf-%s %s", ditf->prefix, buffer);
511 static void hook_ditf_event_broadcast_before_cb(void *closure, const struct afb_ditf *ditf, const char *name, struct json_object *object)
513 _hook_ditf_(ditf, "event_broadcast.before(%s, %s)....", name, json_object_to_json_string(object));
516 static void hook_ditf_event_broadcast_after_cb(void *closure, const struct afb_ditf *ditf, const char *name, struct json_object *object, int result)
518 _hook_ditf_(ditf, "event_broadcast.after(%s, %s) -> %d", name, json_object_to_json_string(object), result);
521 static void hook_ditf_get_event_loop_cb(void *closure, const struct afb_ditf *ditf, struct sd_event *result)
523 _hook_ditf_(ditf, "get_event_loop() -> %p", result);
526 static void hook_ditf_get_user_bus_cb(void *closure, const struct afb_ditf *ditf, struct sd_bus *result)
528 _hook_ditf_(ditf, "get_user_bus() -> %p", result);
531 static void hook_ditf_get_system_bus_cb(void *closure, const struct afb_ditf *ditf, struct sd_bus *result)
533 _hook_ditf_(ditf, "get_system_bus() -> %p", result);
536 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)
543 len = vasprintf(&msg, fmt, ap);
547 _hook_ditf_(ditf, "vverbose(%d, %s, %d, %s) -> %s ? ? ?", level, file, line, function, fmt);
549 _hook_ditf_(ditf, "vverbose(%d, %s, %d, %s) -> %s", level, file, line, function, msg);
554 static void hook_ditf_event_make_cb(void *closure, const struct afb_ditf *ditf, const char *name, struct afb_event result)
556 _hook_ditf_(ditf, "event_make(%s) -> %s:%p", name, afb_event_name(result), result.closure);
559 static void hook_ditf_rootdir_get_fd_cb(void *closure, const struct afb_ditf *ditf, int result)
563 _hook_ditf_(ditf, "rootdir_get_fd() -> %d, %m", result);
565 sprintf(path, "/proc/self/fd/%d", result);
566 readlink(path, path, sizeof path);
567 _hook_ditf_(ditf, "rootdir_get_fd() -> %d = %s", result, path);
571 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)
577 _hook_ditf_(ditf, "rootdir_open_locale(%s, %d, %s) -> %d, %m", filename, flags, locale, result);
579 sprintf(path, "/proc/self/fd/%d", result);
580 readlink(path, path, sizeof path);
581 _hook_ditf_(ditf, "rootdir_open_locale(%s, %d, %s) -> %d = %s", filename, flags, locale, result, path);
585 static void hook_ditf_queue_job(void *closure, const struct afb_ditf *ditf, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout, int result)
587 _hook_ditf_(ditf, "queue_job(%p, %p, %p, %d) -> %d", callback, argument, group, timeout, result);
590 static struct afb_hook_ditf_itf hook_ditf_default_itf = {
591 .hook_ditf_event_broadcast_before = hook_ditf_event_broadcast_before_cb,
592 .hook_ditf_event_broadcast_after = hook_ditf_event_broadcast_after_cb,
593 .hook_ditf_get_event_loop = hook_ditf_get_event_loop_cb,
594 .hook_ditf_get_user_bus = hook_ditf_get_user_bus_cb,
595 .hook_ditf_get_system_bus = hook_ditf_get_system_bus_cb,
596 .hook_ditf_vverbose = hook_ditf_vverbose_cb,
597 .hook_ditf_event_make = hook_ditf_event_make_cb,
598 .hook_ditf_rootdir_get_fd = hook_ditf_rootdir_get_fd_cb,
599 .hook_ditf_rootdir_open_locale = hook_ditf_rootdir_open_locale_cb,
600 .hook_ditf_queue_job = hook_ditf_queue_job
603 /******************************************************************************
604 * section: hooks for tracing daemon interface (ditf)
605 *****************************************************************************/
607 #define _HOOK_DITF_(what,...) \
608 struct afb_hook_ditf *hook; \
609 pthread_rwlock_rdlock(&rwlock); \
610 hook = list_of_ditf_hooks; \
612 if (hook->itf->hook_ditf_##what \
613 && (hook->flags & afb_hook_flag_ditf_##what) != 0 \
614 && (!hook->api || !strcasecmp(hook->api, ditf->prefix))) { \
615 hook->itf->hook_ditf_##what(hook->closure, __VA_ARGS__); \
619 pthread_rwlock_unlock(&rwlock);
621 void afb_hook_ditf_event_broadcast_before(const struct afb_ditf *ditf, const char *name, struct json_object *object)
623 _HOOK_DITF_(event_broadcast_before, ditf, name, object);
626 int afb_hook_ditf_event_broadcast_after(const struct afb_ditf *ditf, const char *name, struct json_object *object, int result)
628 _HOOK_DITF_(event_broadcast_after, ditf, name, object, result);
632 struct sd_event *afb_hook_ditf_get_event_loop(const struct afb_ditf *ditf, struct sd_event *result)
634 _HOOK_DITF_(get_event_loop, ditf, result);
638 struct sd_bus *afb_hook_ditf_get_user_bus(const struct afb_ditf *ditf, struct sd_bus *result)
640 _HOOK_DITF_(get_user_bus, ditf, result);
644 struct sd_bus *afb_hook_ditf_get_system_bus(const struct afb_ditf *ditf, struct sd_bus *result)
646 _HOOK_DITF_(get_system_bus, ditf, result);
650 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)
652 _HOOK_DITF_(vverbose, ditf, level, file, line, function, fmt, args);
655 struct afb_event afb_hook_ditf_event_make(const struct afb_ditf *ditf, const char *name, struct afb_event result)
657 _HOOK_DITF_(event_make, ditf, name, result);
661 int afb_hook_ditf_rootdir_get_fd(const struct afb_ditf *ditf, int result)
663 _HOOK_DITF_(rootdir_get_fd, ditf, result);
667 int afb_hook_ditf_rootdir_open_locale(const struct afb_ditf *ditf, const char *filename, int flags, const char *locale, int result)
669 _HOOK_DITF_(rootdir_open_locale, ditf, filename, flags, locale, result);
673 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)
675 _HOOK_DITF_(queue_job, ditf, callback, argument, group, timeout, result);
679 /******************************************************************************
680 * section: hooking ditf
681 *****************************************************************************/
683 int afb_hook_flags_ditf(const char *api)
686 struct afb_hook_ditf *hook;
688 pthread_rwlock_rdlock(&rwlock);
690 hook = list_of_ditf_hooks;
692 if (!api || !hook->api || !strcasecmp(hook->api, api))
693 flags |= hook->flags;
696 pthread_rwlock_unlock(&rwlock);
700 struct afb_hook_ditf *afb_hook_create_ditf(const char *api, int flags, struct afb_hook_ditf_itf *itf, void *closure)
702 struct afb_hook_ditf *hook;
704 /* alloc the result */
705 hook = calloc(1, sizeof *hook);
709 /* get a copy of the names */
710 hook->api = api ? strdup(api) : NULL;
711 if (api && !hook->api) {
716 /* initialise the rest */
719 hook->itf = itf ? itf : &hook_ditf_default_itf;
720 hook->closure = closure;
722 /* record the hook */
723 pthread_rwlock_wrlock(&rwlock);
724 hook->next = list_of_ditf_hooks;
725 list_of_ditf_hooks = hook;
726 pthread_rwlock_unlock(&rwlock);
732 struct afb_hook_ditf *afb_hook_addref_ditf(struct afb_hook_ditf *hook)
734 pthread_rwlock_wrlock(&rwlock);
736 pthread_rwlock_unlock(&rwlock);
740 void afb_hook_unref_ditf(struct afb_hook_ditf *hook)
742 struct afb_hook_ditf **prv;
745 pthread_rwlock_wrlock(&rwlock);
746 if (--hook->refcount)
750 prv = &list_of_ditf_hooks;
751 while (*prv && *prv != hook)
756 pthread_rwlock_unlock(&rwlock);
765 /******************************************************************************
766 * section: default callbacks for tracing service interface (svc)
767 *****************************************************************************/
769 static void _hook_svc_(const struct afb_svc *svc, const char *format, ...)
775 va_start(ap, format);
776 len = vasprintf(&buffer, format, ap);
780 NOTICE("hook svc-%s allocation error for %s", svc->api, format);
782 NOTICE("hook svc-%s %s", svc->api, buffer);
787 static void hook_svc_start_before_default_cb(void *closure, const struct afb_svc *svc)
789 _hook_svc_(svc, "start.before");
792 static void hook_svc_start_after_default_cb(void *closure, const struct afb_svc *svc, int status)
794 _hook_svc_(svc, "start.after -> %d", status);
797 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)
799 _hook_svc_(svc, "on_event.before(%s, %d, %s)", event, eventid, json_object_to_json_string(object));
802 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)
804 _hook_svc_(svc, "on_event.after(%s, %d, %s)", event, eventid, json_object_to_json_string(object));
807 static void hook_svc_call_default_cb(void *closure, const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args)
809 _hook_svc_(svc, "call(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
812 static void hook_svc_call_result_default_cb(void *closure, const struct afb_svc *svc, int status, struct json_object *result)
814 _hook_svc_(svc, " ...call... -> %d: %s", status, json_object_to_json_string(result));
817 static void hook_svc_callsync_default_cb(void *closure, const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args)
819 _hook_svc_(svc, "callsync(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
822 static void hook_svc_callsync_result_default_cb(void *closure, const struct afb_svc *svc, int status, struct json_object *result)
824 _hook_svc_(svc, " ...callsync... -> %d: %s", status, json_object_to_json_string(result));
827 static struct afb_hook_svc_itf hook_svc_default_itf = {
828 .hook_svc_start_before = hook_svc_start_before_default_cb,
829 .hook_svc_start_after = hook_svc_start_after_default_cb,
830 .hook_svc_on_event_before = hook_svc_on_event_before_default_cb,
831 .hook_svc_on_event_after = hook_svc_on_event_after_default_cb,
832 .hook_svc_call = hook_svc_call_default_cb,
833 .hook_svc_call_result = hook_svc_call_result_default_cb,
834 .hook_svc_callsync = hook_svc_callsync_default_cb,
835 .hook_svc_callsync_result = hook_svc_callsync_result_default_cb
838 /******************************************************************************
839 * section: hooks for tracing service interface (svc)
840 *****************************************************************************/
842 #define _HOOK_SVC_(what,...) \
843 struct afb_hook_svc *hook; \
844 pthread_rwlock_rdlock(&rwlock); \
845 hook = list_of_svc_hooks; \
847 if (hook->itf->hook_svc_##what \
848 && (hook->flags & afb_hook_flag_svc_##what) != 0 \
849 && (!hook->api || !strcasecmp(hook->api, svc->api))) { \
850 hook->itf->hook_svc_##what(hook->closure, __VA_ARGS__); \
854 pthread_rwlock_unlock(&rwlock);
856 void afb_hook_svc_start_before(const struct afb_svc *svc)
858 _HOOK_SVC_(start_before, svc);
861 int afb_hook_svc_start_after(const struct afb_svc *svc, int status)
863 _HOOK_SVC_(start_after, svc, status);
867 void afb_hook_svc_on_event_before(const struct afb_svc *svc, const char *event, int eventid, struct json_object *object)
869 _HOOK_SVC_(on_event_before, svc, event, eventid, object);
872 void afb_hook_svc_on_event_after(const struct afb_svc *svc, const char *event, int eventid, struct json_object *object)
874 _HOOK_SVC_(on_event_after, svc, event, eventid, object);
877 void afb_hook_svc_call(const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args)
879 _HOOK_SVC_(call, svc, api, verb, args);
882 void afb_hook_svc_call_result(const struct afb_svc *svc, int status, struct json_object *result)
884 _HOOK_SVC_(call_result, svc, status, result);
887 void afb_hook_svc_callsync(const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args)
889 _HOOK_SVC_(callsync, svc, api, verb, args);
892 int afb_hook_svc_callsync_result(const struct afb_svc *svc, int status, struct json_object *result)
894 _HOOK_SVC_(callsync_result, svc, status, result);
898 /******************************************************************************
899 * section: hooking services (svc)
900 *****************************************************************************/
902 int afb_hook_flags_svc(const char *api)
905 struct afb_hook_svc *hook;
907 pthread_rwlock_rdlock(&rwlock);
909 hook = list_of_svc_hooks;
911 if (!api || !hook->api || !strcasecmp(hook->api, api))
912 flags |= hook->flags;
915 pthread_rwlock_unlock(&rwlock);
919 struct afb_hook_svc *afb_hook_create_svc(const char *api, int flags, struct afb_hook_svc_itf *itf, void *closure)
921 struct afb_hook_svc *hook;
923 /* alloc the result */
924 hook = calloc(1, sizeof *hook);
928 /* get a copy of the names */
929 hook->api = api ? strdup(api) : NULL;
930 if (api && !hook->api) {
935 /* initialise the rest */
938 hook->itf = itf ? itf : &hook_svc_default_itf;
939 hook->closure = closure;
941 /* record the hook */
942 pthread_rwlock_wrlock(&rwlock);
943 hook->next = list_of_svc_hooks;
944 list_of_svc_hooks = hook;
945 pthread_rwlock_unlock(&rwlock);
951 struct afb_hook_svc *afb_hook_addref_svc(struct afb_hook_svc *hook)
953 pthread_rwlock_wrlock(&rwlock);
955 pthread_rwlock_unlock(&rwlock);
959 void afb_hook_unref_svc(struct afb_hook_svc *hook)
961 struct afb_hook_svc **prv;
964 pthread_rwlock_wrlock(&rwlock);
965 if (--hook->refcount)
969 prv = &list_of_svc_hooks;
970 while (*prv && *prv != hook)
975 pthread_rwlock_unlock(&rwlock);