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 struct afb_hook_xreq_itf hook_xreq_default_itf = {
214 .hook_xreq_begin = hook_xreq_begin_default_cb,
215 .hook_xreq_end = hook_xreq_end_default_cb,
216 .hook_xreq_json = hook_xreq_json_default_cb,
217 .hook_xreq_get = hook_xreq_get_default_cb,
218 .hook_xreq_success = hook_xreq_success_default_cb,
219 .hook_xreq_fail = hook_xreq_fail_default_cb,
220 .hook_xreq_context_get = hook_xreq_context_get_default_cb,
221 .hook_xreq_context_set = hook_xreq_context_set_default_cb,
222 .hook_xreq_addref = hook_xreq_addref_default_cb,
223 .hook_xreq_unref = hook_xreq_unref_default_cb,
224 .hook_xreq_session_close = hook_xreq_session_close_default_cb,
225 .hook_xreq_session_set_LOA = hook_xreq_session_set_LOA_default_cb,
226 .hook_xreq_subscribe = hook_xreq_subscribe_default_cb,
227 .hook_xreq_unsubscribe = hook_xreq_unsubscribe_default_cb,
228 .hook_xreq_subcall = hook_xreq_subcall_default_cb,
229 .hook_xreq_subcall_result = hook_xreq_subcall_result_default_cb,
230 .hook_xreq_subcallsync = hook_xreq_subcallsync_default_cb,
231 .hook_xreq_subcallsync_result = hook_xreq_subcallsync_result_default_cb,
234 /******************************************************************************
235 * section: hooks for tracing requests
236 *****************************************************************************/
238 #define _HOOK_XREQ_(what,...) \
239 struct afb_hook_xreq *hook; \
240 pthread_rwlock_rdlock(&rwlock); \
241 hook = list_of_xreq_hooks; \
243 if (hook->itf->hook_xreq_##what \
244 && (hook->flags & afb_hook_flag_req_##what) != 0 \
245 && (!hook->session || hook->session == xreq->context.session) \
246 && (!hook->api || !strcasecmp(hook->api, xreq->api)) \
247 && (!hook->verb || !strcasecmp(hook->verb, xreq->verb))) { \
248 hook->itf->hook_xreq_##what(hook->closure, __VA_ARGS__); \
252 pthread_rwlock_unlock(&rwlock);
255 void afb_hook_xreq_begin(const struct afb_xreq *xreq)
257 _HOOK_XREQ_(begin, xreq);
260 void afb_hook_xreq_end(const struct afb_xreq *xreq)
262 _HOOK_XREQ_(end, xreq);
265 struct json_object *afb_hook_xreq_json(const struct afb_xreq *xreq, struct json_object *obj)
267 _HOOK_XREQ_(json, xreq, obj);
271 struct afb_arg afb_hook_xreq_get(const struct afb_xreq *xreq, const char *name, struct afb_arg arg)
273 _HOOK_XREQ_(get, xreq, name, arg);
277 void afb_hook_xreq_success(const struct afb_xreq *xreq, struct json_object *obj, const char *info)
279 _HOOK_XREQ_(success, xreq, obj, info);
282 void afb_hook_xreq_fail(const struct afb_xreq *xreq, const char *status, const char *info)
284 _HOOK_XREQ_(fail, xreq, status, info);
287 void *afb_hook_xreq_context_get(const struct afb_xreq *xreq, void *value)
289 _HOOK_XREQ_(context_get, xreq, value);
293 void afb_hook_xreq_context_set(const struct afb_xreq *xreq, void *value, void (*free_value)(void*))
295 _HOOK_XREQ_(context_set, xreq, value, free_value);
298 void afb_hook_xreq_addref(const struct afb_xreq *xreq)
300 _HOOK_XREQ_(addref, xreq);
303 void afb_hook_xreq_unref(const struct afb_xreq *xreq)
305 _HOOK_XREQ_(unref, xreq);
308 void afb_hook_xreq_session_close(const struct afb_xreq *xreq)
310 _HOOK_XREQ_(session_close, xreq);
313 int afb_hook_xreq_session_set_LOA(const struct afb_xreq *xreq, unsigned level, int result)
315 _HOOK_XREQ_(session_set_LOA, xreq, level, result);
319 int afb_hook_xreq_subscribe(const struct afb_xreq *xreq, struct afb_event event, int result)
321 _HOOK_XREQ_(subscribe, xreq, event, result);
325 int afb_hook_xreq_unsubscribe(const struct afb_xreq *xreq, struct afb_event event, int result)
327 _HOOK_XREQ_(unsubscribe, xreq, event, result);
331 void afb_hook_xreq_subcall(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
333 _HOOK_XREQ_(subcall, xreq, api, verb, args);
336 void afb_hook_xreq_subcall_result(const struct afb_xreq *xreq, int status, struct json_object *result)
338 _HOOK_XREQ_(subcall_result, xreq, status, result);
341 void afb_hook_xreq_subcallsync(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
343 _HOOK_XREQ_(subcallsync, xreq, api, verb, args);
346 int afb_hook_xreq_subcallsync_result(const struct afb_xreq *xreq, int status, struct json_object *result)
348 _HOOK_XREQ_(subcallsync_result, xreq, status, result);
352 /******************************************************************************
353 * section: hooking xreqs
354 *****************************************************************************/
356 void afb_hook_init_xreq(struct afb_xreq *xreq)
362 struct afb_hook_xreq *hook;
364 /* scan hook list to get the expected flags */
366 pthread_rwlock_rdlock(&rwlock);
367 hook = list_of_xreq_hooks;
369 f = hook->flags & afb_hook_flags_req_all;
371 && (!hook->session || hook->session == xreq->context.session)
372 && (!hook->api || !strcasecmp(hook->api, xreq->api))
373 && (!hook->verb || !strcasecmp(hook->verb, xreq->verb));
378 pthread_rwlock_unlock(&rwlock);
380 /* store the hooking data */
381 xreq->hookflags = flags;
383 pthread_rwlock_wrlock(&rwlock);
386 xreq->hookindex = reqindex;
387 pthread_rwlock_unlock(&rwlock);
391 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)
393 struct afb_hook_xreq *hook;
395 /* alloc the result */
396 hook = calloc(1, sizeof *hook);
400 /* get a copy of the names */
401 hook->api = api ? strdup(api) : NULL;
402 hook->verb = verb ? strdup(verb) : NULL;
403 if ((api && !hook->api) || (verb && !hook->verb)) {
410 /* initialise the rest */
411 hook->session = session;
413 afb_session_addref(session);
416 hook->itf = itf ? itf : &hook_xreq_default_itf;
417 hook->closure = closure;
419 /* record the hook */
420 pthread_rwlock_wrlock(&rwlock);
421 hook->next = list_of_xreq_hooks;
422 list_of_xreq_hooks = hook;
423 pthread_rwlock_unlock(&rwlock);
429 struct afb_hook_xreq *afb_hook_addref_xreq(struct afb_hook_xreq *hook)
431 pthread_rwlock_wrlock(&rwlock);
433 pthread_rwlock_unlock(&rwlock);
437 void afb_hook_unref_xreq(struct afb_hook_xreq *hook)
439 struct afb_hook_xreq **prv;
442 pthread_rwlock_wrlock(&rwlock);
443 if (--hook->refcount)
447 prv = &list_of_xreq_hooks;
448 while (*prv && *prv != hook)
453 pthread_rwlock_unlock(&rwlock);
459 afb_session_unref(hook->session);
465 /******************************************************************************
466 * section: default callbacks for tracing daemon interface
467 *****************************************************************************/
469 static void _hook_ditf_(const struct afb_ditf *ditf, const char *format, ...)
475 va_start(ap, format);
476 len = vasprintf(&buffer, format, ap);
480 NOTICE("hook ditf-%s allocation error for %s", ditf->prefix, format);
482 NOTICE("hook ditf-%s %s", ditf->prefix, buffer);
487 static void hook_ditf_event_broadcast_before_cb(void *closure, const struct afb_ditf *ditf, const char *name, struct json_object *object)
489 _hook_ditf_(ditf, "event_broadcast.before(%s, %s)....", name, json_object_to_json_string(object));
492 static void hook_ditf_event_broadcast_after_cb(void *closure, const struct afb_ditf *ditf, const char *name, struct json_object *object, int result)
494 _hook_ditf_(ditf, "event_broadcast.after(%s, %s) -> %d", name, json_object_to_json_string(object), result);
497 static void hook_ditf_get_event_loop_cb(void *closure, const struct afb_ditf *ditf, struct sd_event *result)
499 _hook_ditf_(ditf, "get_event_loop() -> %p", result);
502 static void hook_ditf_get_user_bus_cb(void *closure, const struct afb_ditf *ditf, struct sd_bus *result)
504 _hook_ditf_(ditf, "get_user_bus() -> %p", result);
507 static void hook_ditf_get_system_bus_cb(void *closure, const struct afb_ditf *ditf, struct sd_bus *result)
509 _hook_ditf_(ditf, "get_system_bus() -> %p", result);
512 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)
519 len = vasprintf(&msg, fmt, ap);
523 _hook_ditf_(ditf, "vverbose(%d, %s, %d, %s) -> %s ? ? ?", level, file, line, function, fmt);
525 _hook_ditf_(ditf, "vverbose(%d, %s, %d, %s) -> %s", level, file, line, function, msg);
530 static void hook_ditf_event_make_cb(void *closure, const struct afb_ditf *ditf, const char *name, struct afb_event result)
532 _hook_ditf_(ditf, "event_make(%s) -> %s:%p", name, afb_event_name(result), result.closure);
535 static void hook_ditf_rootdir_get_fd_cb(void *closure, const struct afb_ditf *ditf, int result)
539 _hook_ditf_(ditf, "rootdir_get_fd() -> %d, %m", result);
541 sprintf(path, "/proc/self/fd/%d", result);
542 readlink(path, path, sizeof path);
543 _hook_ditf_(ditf, "rootdir_get_fd() -> %d = %s", result, path);
547 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)
553 _hook_ditf_(ditf, "rootdir_open_locale(%s, %d, %s) -> %d, %m", filename, flags, locale, result);
555 sprintf(path, "/proc/self/fd/%d", result);
556 readlink(path, path, sizeof path);
557 _hook_ditf_(ditf, "rootdir_open_locale(%s, %d, %s) -> %d = %s", filename, flags, locale, result, path);
561 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)
563 _hook_ditf_(ditf, "queue_job(%p, %p, %p, %d) -> %d", callback, argument, group, timeout, result);
566 static struct afb_hook_ditf_itf hook_ditf_default_itf = {
567 .hook_ditf_event_broadcast_before = hook_ditf_event_broadcast_before_cb,
568 .hook_ditf_event_broadcast_after = hook_ditf_event_broadcast_after_cb,
569 .hook_ditf_get_event_loop = hook_ditf_get_event_loop_cb,
570 .hook_ditf_get_user_bus = hook_ditf_get_user_bus_cb,
571 .hook_ditf_get_system_bus = hook_ditf_get_system_bus_cb,
572 .hook_ditf_vverbose = hook_ditf_vverbose_cb,
573 .hook_ditf_event_make = hook_ditf_event_make_cb,
574 .hook_ditf_rootdir_get_fd = hook_ditf_rootdir_get_fd_cb,
575 .hook_ditf_rootdir_open_locale = hook_ditf_rootdir_open_locale_cb,
576 .hook_ditf_queue_job = hook_ditf_queue_job
579 /******************************************************************************
580 * section: hooks for tracing daemon interface (ditf)
581 *****************************************************************************/
583 #define _HOOK_DITF_(what,...) \
584 struct afb_hook_ditf *hook; \
585 pthread_rwlock_rdlock(&rwlock); \
586 hook = list_of_ditf_hooks; \
588 if (hook->itf->hook_ditf_##what \
589 && (hook->flags & afb_hook_flag_ditf_##what) != 0 \
590 && (!hook->api || !strcasecmp(hook->api, ditf->prefix))) { \
591 hook->itf->hook_ditf_##what(hook->closure, __VA_ARGS__); \
595 pthread_rwlock_unlock(&rwlock);
597 void afb_hook_ditf_event_broadcast_before(const struct afb_ditf *ditf, const char *name, struct json_object *object)
599 _HOOK_DITF_(event_broadcast_before, ditf, name, object);
602 int afb_hook_ditf_event_broadcast_after(const struct afb_ditf *ditf, const char *name, struct json_object *object, int result)
604 _HOOK_DITF_(event_broadcast_after, ditf, name, object, result);
608 struct sd_event *afb_hook_ditf_get_event_loop(const struct afb_ditf *ditf, struct sd_event *result)
610 _HOOK_DITF_(get_event_loop, ditf, result);
614 struct sd_bus *afb_hook_ditf_get_user_bus(const struct afb_ditf *ditf, struct sd_bus *result)
616 _HOOK_DITF_(get_user_bus, ditf, result);
620 struct sd_bus *afb_hook_ditf_get_system_bus(const struct afb_ditf *ditf, struct sd_bus *result)
622 _HOOK_DITF_(get_system_bus, ditf, result);
626 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)
628 _HOOK_DITF_(vverbose, ditf, level, file, line, function, fmt, args);
631 struct afb_event afb_hook_ditf_event_make(const struct afb_ditf *ditf, const char *name, struct afb_event result)
633 _HOOK_DITF_(event_make, ditf, name, result);
637 int afb_hook_ditf_rootdir_get_fd(const struct afb_ditf *ditf, int result)
639 _HOOK_DITF_(rootdir_get_fd, ditf, result);
643 int afb_hook_ditf_rootdir_open_locale(const struct afb_ditf *ditf, const char *filename, int flags, const char *locale, int result)
645 _HOOK_DITF_(rootdir_open_locale, ditf, filename, flags, locale, result);
649 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)
651 _HOOK_DITF_(queue_job, ditf, callback, argument, group, timeout, result);
655 /******************************************************************************
656 * section: hooking ditf
657 *****************************************************************************/
659 int afb_hook_flags_ditf(const char *api)
662 struct afb_hook_ditf *hook;
664 pthread_rwlock_rdlock(&rwlock);
666 hook = list_of_ditf_hooks;
668 if (!api || !hook->api || !strcasecmp(hook->api, api))
669 flags |= hook->flags;
672 pthread_rwlock_unlock(&rwlock);
676 struct afb_hook_ditf *afb_hook_create_ditf(const char *api, int flags, struct afb_hook_ditf_itf *itf, void *closure)
678 struct afb_hook_ditf *hook;
680 /* alloc the result */
681 hook = calloc(1, sizeof *hook);
685 /* get a copy of the names */
686 hook->api = api ? strdup(api) : NULL;
687 if (api && !hook->api) {
692 /* initialise the rest */
695 hook->itf = itf ? itf : &hook_ditf_default_itf;
696 hook->closure = closure;
698 /* record the hook */
699 pthread_rwlock_wrlock(&rwlock);
700 hook->next = list_of_ditf_hooks;
701 list_of_ditf_hooks = hook;
702 pthread_rwlock_unlock(&rwlock);
708 struct afb_hook_ditf *afb_hook_addref_ditf(struct afb_hook_ditf *hook)
710 pthread_rwlock_wrlock(&rwlock);
712 pthread_rwlock_unlock(&rwlock);
716 void afb_hook_unref_ditf(struct afb_hook_ditf *hook)
718 struct afb_hook_ditf **prv;
721 pthread_rwlock_wrlock(&rwlock);
722 if (--hook->refcount)
726 prv = &list_of_ditf_hooks;
727 while (*prv && *prv != hook)
732 pthread_rwlock_unlock(&rwlock);
741 /******************************************************************************
742 * section: default callbacks for tracing service interface (svc)
743 *****************************************************************************/
745 static void _hook_svc_(const struct afb_svc *svc, const char *format, ...)
751 va_start(ap, format);
752 len = vasprintf(&buffer, format, ap);
756 NOTICE("hook svc-%s allocation error for %s", svc->api, format);
758 NOTICE("hook svc-%s %s", svc->api, buffer);
763 static void hook_svc_start_before_default_cb(void *closure, const struct afb_svc *svc)
765 _hook_svc_(svc, "start.before");
768 static void hook_svc_start_after_default_cb(void *closure, const struct afb_svc *svc, int status)
770 _hook_svc_(svc, "start.after -> %d", status);
773 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)
775 _hook_svc_(svc, "on_event.before(%s, %d, %s)", event, eventid, json_object_to_json_string(object));
778 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)
780 _hook_svc_(svc, "on_event.after(%s, %d, %s)", event, eventid, json_object_to_json_string(object));
783 static void hook_svc_call_default_cb(void *closure, const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args)
785 _hook_svc_(svc, "call(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
788 static void hook_svc_call_result_default_cb(void *closure, const struct afb_svc *svc, int status, struct json_object *result)
790 _hook_svc_(svc, " ...call... -> %d: %s", status, json_object_to_json_string(result));
793 static void hook_svc_callsync_default_cb(void *closure, const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args)
795 _hook_svc_(svc, "callsync(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
798 static void hook_svc_callsync_result_default_cb(void *closure, const struct afb_svc *svc, int status, struct json_object *result)
800 _hook_svc_(svc, " ...callsync... -> %d: %s", status, json_object_to_json_string(result));
803 static struct afb_hook_svc_itf hook_svc_default_itf = {
804 .hook_svc_start_before = hook_svc_start_before_default_cb,
805 .hook_svc_start_after = hook_svc_start_after_default_cb,
806 .hook_svc_on_event_before = hook_svc_on_event_before_default_cb,
807 .hook_svc_on_event_after = hook_svc_on_event_after_default_cb,
808 .hook_svc_call = hook_svc_call_default_cb,
809 .hook_svc_call_result = hook_svc_call_result_default_cb,
810 .hook_svc_callsync = hook_svc_callsync_default_cb,
811 .hook_svc_callsync_result = hook_svc_callsync_result_default_cb
814 /******************************************************************************
815 * section: hooks for tracing service interface (svc)
816 *****************************************************************************/
818 #define _HOOK_SVC_(what,...) \
819 struct afb_hook_svc *hook; \
820 pthread_rwlock_rdlock(&rwlock); \
821 hook = list_of_svc_hooks; \
823 if (hook->itf->hook_svc_##what \
824 && (hook->flags & afb_hook_flag_svc_##what) != 0 \
825 && (!hook->api || !strcasecmp(hook->api, svc->api))) { \
826 hook->itf->hook_svc_##what(hook->closure, __VA_ARGS__); \
830 pthread_rwlock_unlock(&rwlock);
832 void afb_hook_svc_start_before(const struct afb_svc *svc)
834 _HOOK_SVC_(start_before, svc);
837 int afb_hook_svc_start_after(const struct afb_svc *svc, int status)
839 _HOOK_SVC_(start_after, svc, status);
843 void afb_hook_svc_on_event_before(const struct afb_svc *svc, const char *event, int eventid, struct json_object *object)
845 _HOOK_SVC_(on_event_before, svc, event, eventid, object);
848 void afb_hook_svc_on_event_after(const struct afb_svc *svc, const char *event, int eventid, struct json_object *object)
850 _HOOK_SVC_(on_event_after, svc, event, eventid, object);
853 void afb_hook_svc_call(const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args)
855 _HOOK_SVC_(call, svc, api, verb, args);
858 void afb_hook_svc_call_result(const struct afb_svc *svc, int status, struct json_object *result)
860 _HOOK_SVC_(call_result, svc, status, result);
863 void afb_hook_svc_callsync(const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args)
865 _HOOK_SVC_(callsync, svc, api, verb, args);
868 int afb_hook_svc_callsync_result(const struct afb_svc *svc, int status, struct json_object *result)
870 _HOOK_SVC_(callsync_result, svc, status, result);
874 /******************************************************************************
875 * section: hooking services (svc)
876 *****************************************************************************/
878 int afb_hook_flags_svc(const char *api)
881 struct afb_hook_svc *hook;
883 pthread_rwlock_rdlock(&rwlock);
885 hook = list_of_svc_hooks;
887 if (!api || !hook->api || !strcasecmp(hook->api, api))
888 flags |= hook->flags;
891 pthread_rwlock_unlock(&rwlock);
895 struct afb_hook_svc *afb_hook_create_svc(const char *api, int flags, struct afb_hook_svc_itf *itf, void *closure)
897 struct afb_hook_svc *hook;
899 /* alloc the result */
900 hook = calloc(1, sizeof *hook);
904 /* get a copy of the names */
905 hook->api = api ? strdup(api) : NULL;
906 if (api && !hook->api) {
911 /* initialise the rest */
914 hook->itf = itf ? itf : &hook_svc_default_itf;
915 hook->closure = closure;
917 /* record the hook */
918 pthread_rwlock_wrlock(&rwlock);
919 hook->next = list_of_svc_hooks;
920 list_of_svc_hooks = hook;
921 pthread_rwlock_unlock(&rwlock);
927 struct afb_hook_svc *afb_hook_addref_svc(struct afb_hook_svc *hook)
929 pthread_rwlock_wrlock(&rwlock);
931 pthread_rwlock_unlock(&rwlock);
935 void afb_hook_unref_svc(struct afb_hook_svc *hook)
937 struct afb_hook_svc **prv;
940 pthread_rwlock_wrlock(&rwlock);
941 if (--hook->refcount)
945 prv = &list_of_svc_hooks;
946 while (*prv && *prv != hook)
951 pthread_rwlock_unlock(&rwlock);