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-itf.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 */
81 /* synchronisation across threads */
82 static pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
84 /* list of hooks for xreq */
85 static struct afb_hook_xreq *list_of_xreq_hooks = NULL;
87 /* list of hooks for ditf */
88 static struct afb_hook_ditf *list_of_ditf_hooks = NULL;
90 /* list of hooks for svc */
91 static struct afb_hook_svc *list_of_svc_hooks = NULL;
93 /******************************************************************************
94 * section: default callbacks for tracing requests
95 *****************************************************************************/
97 static void _hook_xreq_(const struct afb_xreq *xreq, const char *format, ...)
103 va_start(ap, format);
104 len = vasprintf(&buffer, format, ap);
108 NOTICE("hook xreq-%06d:%s/%s allocation error", xreq->hookindex, xreq->api, xreq->verb);
110 NOTICE("hook xreq-%06d:%s/%s %s", xreq->hookindex, xreq->api, xreq->verb, buffer);
115 static void hook_xreq_begin_default_cb(void * closure, const struct afb_xreq *xreq)
118 _hook_xreq_(xreq, "BEGIN");
120 _hook_xreq_(xreq, "BEGIN uid=%d=%s gid=%d pid=%d label=%s id=%s",
121 (int)xreq->cred->uid,
123 (int)xreq->cred->gid,
124 (int)xreq->cred->pid,
125 xreq->cred->label?:"(null)",
126 xreq->cred->id?:"(null)"
130 static void hook_xreq_end_default_cb(void * closure, const struct afb_xreq *xreq)
132 _hook_xreq_(xreq, "END");
135 static void hook_xreq_json_default_cb(void * closure, const struct afb_xreq *xreq, struct json_object *obj)
137 _hook_xreq_(xreq, "json() -> %s", json_object_to_json_string(obj));
140 static void hook_xreq_get_default_cb(void * closure, const struct afb_xreq *xreq, const char *name, struct afb_arg arg)
142 _hook_xreq_(xreq, "get(%s) -> { name: %s, value: %s, path: %s }", name, arg.name, arg.value, arg.path);
145 static void hook_xreq_success_default_cb(void * closure, const struct afb_xreq *xreq, struct json_object *obj, const char *info)
147 _hook_xreq_(xreq, "success(%s, %s)", json_object_to_json_string(obj), info);
150 static void hook_xreq_fail_default_cb(void * closure, const struct afb_xreq *xreq, const char *status, const char *info)
152 _hook_xreq_(xreq, "fail(%s, %s)", status, info);
155 static void hook_xreq_context_get_default_cb(void * closure, const struct afb_xreq *xreq, void *value)
157 _hook_xreq_(xreq, "context_get() -> %p", value);
160 static void hook_xreq_context_set_default_cb(void * closure, const struct afb_xreq *xreq, void *value, void (*free_value)(void*))
162 _hook_xreq_(xreq, "context_set(%p, %p)", value, free_value);
165 static void hook_xreq_addref_default_cb(void * closure, const struct afb_xreq *xreq)
167 _hook_xreq_(xreq, "addref()");
170 static void hook_xreq_unref_default_cb(void * closure, const struct afb_xreq *xreq)
172 _hook_xreq_(xreq, "unref()");
175 static void hook_xreq_session_close_default_cb(void * closure, const struct afb_xreq *xreq)
177 _hook_xreq_(xreq, "session_close()");
180 static void hook_xreq_session_set_LOA_default_cb(void * closure, const struct afb_xreq *xreq, unsigned level, int result)
182 _hook_xreq_(xreq, "session_set_LOA(%u) -> %d", level, result);
185 static void hook_xreq_subscribe_default_cb(void * closure, const struct afb_xreq *xreq, struct afb_event event, int result)
187 _hook_xreq_(xreq, "subscribe(%s:%d) -> %d", afb_evt_event_name(event), afb_evt_event_id(event), result);
190 static void hook_xreq_unsubscribe_default_cb(void * closure, const struct afb_xreq *xreq, struct afb_event event, int result)
192 _hook_xreq_(xreq, "unsubscribe(%s:%d) -> %d", afb_evt_event_name(event), afb_evt_event_id(event), result);
195 static void hook_xreq_subcall_default_cb(void * closure, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
197 _hook_xreq_(xreq, "subcall(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
200 static void hook_xreq_subcall_result_default_cb(void * closure, const struct afb_xreq *xreq, int status, struct json_object *result)
202 _hook_xreq_(xreq, " ...subcall... -> %d: %s", status, json_object_to_json_string(result));
205 static void hook_xreq_subcallsync_default_cb(void * closure, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
207 _hook_xreq_(xreq, "subcallsync(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
210 static void hook_xreq_subcallsync_result_default_cb(void * closure, const struct afb_xreq *xreq, int status, struct json_object *result)
212 _hook_xreq_(xreq, " ...subcallsync... -> %d: %s", status, json_object_to_json_string(result));
215 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)
222 len = vasprintf(&msg, fmt, ap);
226 _hook_xreq_(xreq, "vverbose(%d, %s, %d, %s) -> %s ? ? ?", level, file, line, func, fmt);
228 _hook_xreq_(xreq, "vverbose(%d, %s, %d, %s) -> %s", level, file, line, func, msg);
233 static struct afb_hook_xreq_itf hook_xreq_default_itf = {
234 .hook_xreq_begin = hook_xreq_begin_default_cb,
235 .hook_xreq_end = hook_xreq_end_default_cb,
236 .hook_xreq_json = hook_xreq_json_default_cb,
237 .hook_xreq_get = hook_xreq_get_default_cb,
238 .hook_xreq_success = hook_xreq_success_default_cb,
239 .hook_xreq_fail = hook_xreq_fail_default_cb,
240 .hook_xreq_context_get = hook_xreq_context_get_default_cb,
241 .hook_xreq_context_set = hook_xreq_context_set_default_cb,
242 .hook_xreq_addref = hook_xreq_addref_default_cb,
243 .hook_xreq_unref = hook_xreq_unref_default_cb,
244 .hook_xreq_session_close = hook_xreq_session_close_default_cb,
245 .hook_xreq_session_set_LOA = hook_xreq_session_set_LOA_default_cb,
246 .hook_xreq_subscribe = hook_xreq_subscribe_default_cb,
247 .hook_xreq_unsubscribe = hook_xreq_unsubscribe_default_cb,
248 .hook_xreq_subcall = hook_xreq_subcall_default_cb,
249 .hook_xreq_subcall_result = hook_xreq_subcall_result_default_cb,
250 .hook_xreq_subcallsync = hook_xreq_subcallsync_default_cb,
251 .hook_xreq_subcallsync_result = hook_xreq_subcallsync_result_default_cb,
252 .hook_xreq_vverbose = hook_xreq_vverbose_default_cb
255 /******************************************************************************
256 * section: hooks for tracing requests
257 *****************************************************************************/
259 #define _HOOK_XREQ_(what,...) \
260 struct afb_hook_xreq *hook; \
261 pthread_rwlock_rdlock(&rwlock); \
262 hook = list_of_xreq_hooks; \
264 if (hook->itf->hook_xreq_##what \
265 && (hook->flags & afb_hook_flag_req_##what) != 0 \
266 && (!hook->session || hook->session == xreq->context.session) \
267 && (!hook->api || !strcasecmp(hook->api, xreq->api)) \
268 && (!hook->verb || !strcasecmp(hook->verb, xreq->verb))) { \
269 hook->itf->hook_xreq_##what(hook->closure, __VA_ARGS__); \
273 pthread_rwlock_unlock(&rwlock);
276 void afb_hook_xreq_begin(const struct afb_xreq *xreq)
278 _HOOK_XREQ_(begin, xreq);
281 void afb_hook_xreq_end(const struct afb_xreq *xreq)
283 _HOOK_XREQ_(end, xreq);
286 struct json_object *afb_hook_xreq_json(const struct afb_xreq *xreq, struct json_object *obj)
288 _HOOK_XREQ_(json, xreq, obj);
292 struct afb_arg afb_hook_xreq_get(const struct afb_xreq *xreq, const char *name, struct afb_arg arg)
294 _HOOK_XREQ_(get, xreq, name, arg);
298 void afb_hook_xreq_success(const struct afb_xreq *xreq, struct json_object *obj, const char *info)
300 _HOOK_XREQ_(success, xreq, obj, info);
303 void afb_hook_xreq_fail(const struct afb_xreq *xreq, const char *status, const char *info)
305 _HOOK_XREQ_(fail, xreq, status, info);
308 void *afb_hook_xreq_context_get(const struct afb_xreq *xreq, void *value)
310 _HOOK_XREQ_(context_get, xreq, value);
314 void afb_hook_xreq_context_set(const struct afb_xreq *xreq, void *value, void (*free_value)(void*))
316 _HOOK_XREQ_(context_set, xreq, value, free_value);
319 void afb_hook_xreq_addref(const struct afb_xreq *xreq)
321 _HOOK_XREQ_(addref, xreq);
324 void afb_hook_xreq_unref(const struct afb_xreq *xreq)
326 _HOOK_XREQ_(unref, xreq);
329 void afb_hook_xreq_session_close(const struct afb_xreq *xreq)
331 _HOOK_XREQ_(session_close, xreq);
334 int afb_hook_xreq_session_set_LOA(const struct afb_xreq *xreq, unsigned level, int result)
336 _HOOK_XREQ_(session_set_LOA, xreq, level, result);
340 int afb_hook_xreq_subscribe(const struct afb_xreq *xreq, struct afb_event event, int result)
342 _HOOK_XREQ_(subscribe, xreq, event, result);
346 int afb_hook_xreq_unsubscribe(const struct afb_xreq *xreq, struct afb_event event, int result)
348 _HOOK_XREQ_(unsubscribe, xreq, event, result);
352 void afb_hook_xreq_subcall(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
354 _HOOK_XREQ_(subcall, xreq, api, verb, args);
357 void afb_hook_xreq_subcall_result(const struct afb_xreq *xreq, int status, struct json_object *result)
359 _HOOK_XREQ_(subcall_result, xreq, status, result);
362 void afb_hook_xreq_subcallsync(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
364 _HOOK_XREQ_(subcallsync, xreq, api, verb, args);
367 int afb_hook_xreq_subcallsync_result(const struct afb_xreq *xreq, int status, struct json_object *result)
369 _HOOK_XREQ_(subcallsync_result, xreq, status, result);
373 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)
375 _HOOK_XREQ_(vverbose, xreq, level, file ?: "?", line, func ?: "?", fmt, args);
378 /******************************************************************************
379 * section: hooking xreqs
380 *****************************************************************************/
382 void afb_hook_init_xreq(struct afb_xreq *xreq)
388 struct afb_hook_xreq *hook;
390 /* scan hook list to get the expected flags */
392 pthread_rwlock_rdlock(&rwlock);
393 hook = list_of_xreq_hooks;
395 f = hook->flags & afb_hook_flags_req_all;
397 && (!hook->session || hook->session == xreq->context.session)
398 && (!hook->api || !strcasecmp(hook->api, xreq->api))
399 && (!hook->verb || !strcasecmp(hook->verb, xreq->verb));
404 pthread_rwlock_unlock(&rwlock);
406 /* store the hooking data */
407 xreq->hookflags = flags;
409 pthread_rwlock_wrlock(&rwlock);
412 xreq->hookindex = reqindex;
413 pthread_rwlock_unlock(&rwlock);
417 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)
419 struct afb_hook_xreq *hook;
421 /* alloc the result */
422 hook = calloc(1, sizeof *hook);
426 /* get a copy of the names */
427 hook->api = api ? strdup(api) : NULL;
428 hook->verb = verb ? strdup(verb) : NULL;
429 if ((api && !hook->api) || (verb && !hook->verb)) {
436 /* initialise the rest */
437 hook->session = session;
439 afb_session_addref(session);
442 hook->itf = itf ? itf : &hook_xreq_default_itf;
443 hook->closure = closure;
445 /* record the hook */
446 pthread_rwlock_wrlock(&rwlock);
447 hook->next = list_of_xreq_hooks;
448 list_of_xreq_hooks = hook;
449 pthread_rwlock_unlock(&rwlock);
455 struct afb_hook_xreq *afb_hook_addref_xreq(struct afb_hook_xreq *hook)
457 pthread_rwlock_wrlock(&rwlock);
459 pthread_rwlock_unlock(&rwlock);
463 void afb_hook_unref_xreq(struct afb_hook_xreq *hook)
465 struct afb_hook_xreq **prv;
468 pthread_rwlock_wrlock(&rwlock);
469 if (--hook->refcount)
473 prv = &list_of_xreq_hooks;
474 while (*prv && *prv != hook)
479 pthread_rwlock_unlock(&rwlock);
485 afb_session_unref(hook->session);
491 /******************************************************************************
492 * section: default callbacks for tracing daemon interface
493 *****************************************************************************/
495 static void _hook_ditf_(const struct afb_ditf *ditf, const char *format, ...)
501 va_start(ap, format);
502 len = vasprintf(&buffer, format, ap);
506 NOTICE("hook ditf-%s allocation error for %s", ditf->prefix, format);
508 NOTICE("hook ditf-%s %s", ditf->prefix, buffer);
513 static void hook_ditf_event_broadcast_before_cb(void *closure, const struct afb_ditf *ditf, const char *name, struct json_object *object)
515 _hook_ditf_(ditf, "event_broadcast.before(%s, %s)....", name, json_object_to_json_string(object));
518 static void hook_ditf_event_broadcast_after_cb(void *closure, const struct afb_ditf *ditf, const char *name, struct json_object *object, int result)
520 _hook_ditf_(ditf, "event_broadcast.after(%s, %s) -> %d", name, json_object_to_json_string(object), result);
523 static void hook_ditf_get_event_loop_cb(void *closure, const struct afb_ditf *ditf, struct sd_event *result)
525 _hook_ditf_(ditf, "get_event_loop() -> %p", result);
528 static void hook_ditf_get_user_bus_cb(void *closure, const struct afb_ditf *ditf, struct sd_bus *result)
530 _hook_ditf_(ditf, "get_user_bus() -> %p", result);
533 static void hook_ditf_get_system_bus_cb(void *closure, const struct afb_ditf *ditf, struct sd_bus *result)
535 _hook_ditf_(ditf, "get_system_bus() -> %p", result);
538 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)
545 len = vasprintf(&msg, fmt, ap);
549 _hook_ditf_(ditf, "vverbose(%d, %s, %d, %s) -> %s ? ? ?", level, file, line, function, fmt);
551 _hook_ditf_(ditf, "vverbose(%d, %s, %d, %s) -> %s", level, file, line, function, msg);
556 static void hook_ditf_event_make_cb(void *closure, const struct afb_ditf *ditf, const char *name, struct afb_event result)
558 _hook_ditf_(ditf, "event_make(%s) -> %s:%d", name, afb_evt_event_name(result), afb_evt_event_id(result));
561 static void hook_ditf_rootdir_get_fd_cb(void *closure, const struct afb_ditf *ditf, int result)
565 _hook_ditf_(ditf, "rootdir_get_fd() -> %d, %m", result);
567 sprintf(path, "/proc/self/fd/%d", result);
568 readlink(path, path, sizeof path);
569 _hook_ditf_(ditf, "rootdir_get_fd() -> %d = %s", result, path);
573 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)
579 _hook_ditf_(ditf, "rootdir_open_locale(%s, %d, %s) -> %d, %m", filename, flags, locale, result);
581 sprintf(path, "/proc/self/fd/%d", result);
582 readlink(path, path, sizeof path);
583 _hook_ditf_(ditf, "rootdir_open_locale(%s, %d, %s) -> %d = %s", filename, flags, locale, result, path);
587 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)
589 _hook_ditf_(ditf, "queue_job(%p, %p, %p, %d) -> %d", callback, argument, group, timeout, result);
592 static struct afb_hook_ditf_itf hook_ditf_default_itf = {
593 .hook_ditf_event_broadcast_before = hook_ditf_event_broadcast_before_cb,
594 .hook_ditf_event_broadcast_after = hook_ditf_event_broadcast_after_cb,
595 .hook_ditf_get_event_loop = hook_ditf_get_event_loop_cb,
596 .hook_ditf_get_user_bus = hook_ditf_get_user_bus_cb,
597 .hook_ditf_get_system_bus = hook_ditf_get_system_bus_cb,
598 .hook_ditf_vverbose = hook_ditf_vverbose_cb,
599 .hook_ditf_event_make = hook_ditf_event_make_cb,
600 .hook_ditf_rootdir_get_fd = hook_ditf_rootdir_get_fd_cb,
601 .hook_ditf_rootdir_open_locale = hook_ditf_rootdir_open_locale_cb,
602 .hook_ditf_queue_job = hook_ditf_queue_job
605 /******************************************************************************
606 * section: hooks for tracing daemon interface (ditf)
607 *****************************************************************************/
609 #define _HOOK_DITF_(what,...) \
610 struct afb_hook_ditf *hook; \
611 pthread_rwlock_rdlock(&rwlock); \
612 hook = list_of_ditf_hooks; \
614 if (hook->itf->hook_ditf_##what \
615 && (hook->flags & afb_hook_flag_ditf_##what) != 0 \
616 && (!hook->api || !strcasecmp(hook->api, ditf->prefix))) { \
617 hook->itf->hook_ditf_##what(hook->closure, __VA_ARGS__); \
621 pthread_rwlock_unlock(&rwlock);
623 void afb_hook_ditf_event_broadcast_before(const struct afb_ditf *ditf, const char *name, struct json_object *object)
625 _HOOK_DITF_(event_broadcast_before, ditf, name, object);
628 int afb_hook_ditf_event_broadcast_after(const struct afb_ditf *ditf, const char *name, struct json_object *object, int result)
630 _HOOK_DITF_(event_broadcast_after, ditf, name, object, result);
634 struct sd_event *afb_hook_ditf_get_event_loop(const struct afb_ditf *ditf, struct sd_event *result)
636 _HOOK_DITF_(get_event_loop, ditf, result);
640 struct sd_bus *afb_hook_ditf_get_user_bus(const struct afb_ditf *ditf, struct sd_bus *result)
642 _HOOK_DITF_(get_user_bus, ditf, result);
646 struct sd_bus *afb_hook_ditf_get_system_bus(const struct afb_ditf *ditf, struct sd_bus *result)
648 _HOOK_DITF_(get_system_bus, ditf, result);
652 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)
654 _HOOK_DITF_(vverbose, ditf, level, file, line, function, fmt, args);
657 struct afb_event afb_hook_ditf_event_make(const struct afb_ditf *ditf, const char *name, struct afb_event result)
659 _HOOK_DITF_(event_make, ditf, name, result);
663 int afb_hook_ditf_rootdir_get_fd(const struct afb_ditf *ditf, int result)
665 _HOOK_DITF_(rootdir_get_fd, ditf, result);
669 int afb_hook_ditf_rootdir_open_locale(const struct afb_ditf *ditf, const char *filename, int flags, const char *locale, int result)
671 _HOOK_DITF_(rootdir_open_locale, ditf, filename, flags, locale, result);
675 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)
677 _HOOK_DITF_(queue_job, ditf, callback, argument, group, timeout, result);
681 /******************************************************************************
682 * section: hooking ditf
683 *****************************************************************************/
685 int afb_hook_flags_ditf(const char *api)
688 struct afb_hook_ditf *hook;
690 pthread_rwlock_rdlock(&rwlock);
692 hook = list_of_ditf_hooks;
694 if (!api || !hook->api || !strcasecmp(hook->api, api))
695 flags |= hook->flags;
698 pthread_rwlock_unlock(&rwlock);
702 struct afb_hook_ditf *afb_hook_create_ditf(const char *api, int flags, struct afb_hook_ditf_itf *itf, void *closure)
704 struct afb_hook_ditf *hook;
706 /* alloc the result */
707 hook = calloc(1, sizeof *hook);
711 /* get a copy of the names */
712 hook->api = api ? strdup(api) : NULL;
713 if (api && !hook->api) {
718 /* initialise the rest */
721 hook->itf = itf ? itf : &hook_ditf_default_itf;
722 hook->closure = closure;
724 /* record the hook */
725 pthread_rwlock_wrlock(&rwlock);
726 hook->next = list_of_ditf_hooks;
727 list_of_ditf_hooks = hook;
728 pthread_rwlock_unlock(&rwlock);
734 struct afb_hook_ditf *afb_hook_addref_ditf(struct afb_hook_ditf *hook)
736 pthread_rwlock_wrlock(&rwlock);
738 pthread_rwlock_unlock(&rwlock);
742 void afb_hook_unref_ditf(struct afb_hook_ditf *hook)
744 struct afb_hook_ditf **prv;
747 pthread_rwlock_wrlock(&rwlock);
748 if (--hook->refcount)
752 prv = &list_of_ditf_hooks;
753 while (*prv && *prv != hook)
758 pthread_rwlock_unlock(&rwlock);
767 /******************************************************************************
768 * section: default callbacks for tracing service interface (svc)
769 *****************************************************************************/
771 static void _hook_svc_(const struct afb_svc *svc, const char *format, ...)
777 va_start(ap, format);
778 len = vasprintf(&buffer, format, ap);
782 NOTICE("hook svc-%s allocation error for %s", svc->api, format);
784 NOTICE("hook svc-%s %s", svc->api, buffer);
789 static void hook_svc_start_before_default_cb(void *closure, const struct afb_svc *svc)
791 _hook_svc_(svc, "start.before");
794 static void hook_svc_start_after_default_cb(void *closure, const struct afb_svc *svc, int status)
796 _hook_svc_(svc, "start.after -> %d", status);
799 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)
801 _hook_svc_(svc, "on_event.before(%s, %d, %s)", event, eventid, json_object_to_json_string(object));
804 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)
806 _hook_svc_(svc, "on_event.after(%s, %d, %s)", event, eventid, json_object_to_json_string(object));
809 static void hook_svc_call_default_cb(void *closure, const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args)
811 _hook_svc_(svc, "call(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
814 static void hook_svc_call_result_default_cb(void *closure, const struct afb_svc *svc, int status, struct json_object *result)
816 _hook_svc_(svc, " ...call... -> %d: %s", status, json_object_to_json_string(result));
819 static void hook_svc_callsync_default_cb(void *closure, const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args)
821 _hook_svc_(svc, "callsync(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
824 static void hook_svc_callsync_result_default_cb(void *closure, const struct afb_svc *svc, int status, struct json_object *result)
826 _hook_svc_(svc, " ...callsync... -> %d: %s", status, json_object_to_json_string(result));
829 static struct afb_hook_svc_itf hook_svc_default_itf = {
830 .hook_svc_start_before = hook_svc_start_before_default_cb,
831 .hook_svc_start_after = hook_svc_start_after_default_cb,
832 .hook_svc_on_event_before = hook_svc_on_event_before_default_cb,
833 .hook_svc_on_event_after = hook_svc_on_event_after_default_cb,
834 .hook_svc_call = hook_svc_call_default_cb,
835 .hook_svc_call_result = hook_svc_call_result_default_cb,
836 .hook_svc_callsync = hook_svc_callsync_default_cb,
837 .hook_svc_callsync_result = hook_svc_callsync_result_default_cb
840 /******************************************************************************
841 * section: hooks for tracing service interface (svc)
842 *****************************************************************************/
844 #define _HOOK_SVC_(what,...) \
845 struct afb_hook_svc *hook; \
846 pthread_rwlock_rdlock(&rwlock); \
847 hook = list_of_svc_hooks; \
849 if (hook->itf->hook_svc_##what \
850 && (hook->flags & afb_hook_flag_svc_##what) != 0 \
851 && (!hook->api || !strcasecmp(hook->api, svc->api))) { \
852 hook->itf->hook_svc_##what(hook->closure, __VA_ARGS__); \
856 pthread_rwlock_unlock(&rwlock);
858 void afb_hook_svc_start_before(const struct afb_svc *svc)
860 _HOOK_SVC_(start_before, svc);
863 int afb_hook_svc_start_after(const struct afb_svc *svc, int status)
865 _HOOK_SVC_(start_after, svc, status);
869 void afb_hook_svc_on_event_before(const struct afb_svc *svc, const char *event, int eventid, struct json_object *object)
871 _HOOK_SVC_(on_event_before, svc, event, eventid, object);
874 void afb_hook_svc_on_event_after(const struct afb_svc *svc, const char *event, int eventid, struct json_object *object)
876 _HOOK_SVC_(on_event_after, svc, event, eventid, object);
879 void afb_hook_svc_call(const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args)
881 _HOOK_SVC_(call, svc, api, verb, args);
884 void afb_hook_svc_call_result(const struct afb_svc *svc, int status, struct json_object *result)
886 _HOOK_SVC_(call_result, svc, status, result);
889 void afb_hook_svc_callsync(const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args)
891 _HOOK_SVC_(callsync, svc, api, verb, args);
894 int afb_hook_svc_callsync_result(const struct afb_svc *svc, int status, struct json_object *result)
896 _HOOK_SVC_(callsync_result, svc, status, result);
900 /******************************************************************************
901 * section: hooking services (svc)
902 *****************************************************************************/
904 int afb_hook_flags_svc(const char *api)
907 struct afb_hook_svc *hook;
909 pthread_rwlock_rdlock(&rwlock);
911 hook = list_of_svc_hooks;
913 if (!api || !hook->api || !strcasecmp(hook->api, api))
914 flags |= hook->flags;
917 pthread_rwlock_unlock(&rwlock);
921 struct afb_hook_svc *afb_hook_create_svc(const char *api, int flags, struct afb_hook_svc_itf *itf, void *closure)
923 struct afb_hook_svc *hook;
925 /* alloc the result */
926 hook = calloc(1, sizeof *hook);
930 /* get a copy of the names */
931 hook->api = api ? strdup(api) : NULL;
932 if (api && !hook->api) {
937 /* initialise the rest */
940 hook->itf = itf ? itf : &hook_svc_default_itf;
941 hook->closure = closure;
943 /* record the hook */
944 pthread_rwlock_wrlock(&rwlock);
945 hook->next = list_of_svc_hooks;
946 list_of_svc_hooks = hook;
947 pthread_rwlock_unlock(&rwlock);
953 struct afb_hook_svc *afb_hook_addref_svc(struct afb_hook_svc *hook)
955 pthread_rwlock_wrlock(&rwlock);
957 pthread_rwlock_unlock(&rwlock);
961 void afb_hook_unref_svc(struct afb_hook_svc *hook)
963 struct afb_hook_svc **prv;
966 pthread_rwlock_wrlock(&rwlock);
967 if (--hook->refcount)
971 prv = &list_of_svc_hooks;
972 while (*prv && *prv != hook)
977 pthread_rwlock_unlock(&rwlock);
1026 /*********************************************************
1027 * section hooking evt (event interface)
1028 *********************************************************/
1031 * Definition of a hook for evt
1033 struct afb_hook_evt {
1034 struct afb_hook_evt *next; /**< next hook */
1035 unsigned refcount; /**< reference count */
1036 char *pattern; /**< event pattern name hooked or NULL for any */
1037 unsigned flags; /**< hook flags */
1038 struct afb_hook_evt_itf *itf; /**< interface of hook */
1039 void *closure; /**< closure for callbacks */
1042 /* list of hooks for evt */
1043 static struct afb_hook_evt *list_of_evt_hooks = NULL;
1046 /******************************************************************************
1047 * section: default callbacks for tracing service interface (evt)
1048 *****************************************************************************/
1050 static void _hook_evt_(const char *evt, int id, const char *format, ...)
1056 va_start(ap, format);
1057 len = vasprintf(&buffer, format, ap);
1061 NOTICE("hook evt-%s:%d allocation error for %s", evt, id, format);
1063 NOTICE("hook evt-%s:%d %s", evt, id, buffer);
1068 static void hook_evt_create_default_cb(void *closure, const char *evt, int id)
1070 _hook_evt_(evt, id, "create");
1073 static void hook_evt_push_before_default_cb(void *closure, const char *evt, int id, struct json_object *obj)
1075 _hook_evt_(evt, id, "push.before(%s)", json_object_to_json_string(obj));
1079 static void hook_evt_push_after_default_cb(void *closure, const char *evt, int id, struct json_object *obj, int result)
1081 _hook_evt_(evt, id, "push.after(%s) -> %d", json_object_to_json_string(obj), result);
1084 static void hook_evt_broadcast_before_default_cb(void *closure, const char *evt, int id, struct json_object *obj)
1086 _hook_evt_(evt, id, "broadcast.before(%s)", json_object_to_json_string(obj));
1089 static void hook_evt_broadcast_after_default_cb(void *closure, const char *evt, int id, struct json_object *obj, int result)
1091 _hook_evt_(evt, id, "broadcast.after(%s) -> %d", json_object_to_json_string(obj), result);
1094 static void hook_evt_name_default_cb(void *closure, const char *evt, int id)
1096 _hook_evt_(evt, id, "name");
1099 static void hook_evt_drop_default_cb(void *closure, const char *evt, int id)
1101 _hook_evt_(evt, id, "drop");
1104 static struct afb_hook_evt_itf hook_evt_default_itf = {
1105 .hook_evt_create = hook_evt_create_default_cb,
1106 .hook_evt_push_before = hook_evt_push_before_default_cb,
1107 .hook_evt_push_after = hook_evt_push_after_default_cb,
1108 .hook_evt_broadcast_before = hook_evt_broadcast_before_default_cb,
1109 .hook_evt_broadcast_after = hook_evt_broadcast_after_default_cb,
1110 .hook_evt_name = hook_evt_name_default_cb,
1111 .hook_evt_drop = hook_evt_drop_default_cb
1114 /******************************************************************************
1115 * section: hooks for tracing service interface (evt)
1116 *****************************************************************************/
1118 #define _HOOK_EVT_(what,...) \
1119 struct afb_hook_evt *hook; \
1120 pthread_rwlock_rdlock(&rwlock); \
1121 hook = list_of_evt_hooks; \
1123 if (hook->itf->hook_evt_##what \
1124 && (hook->flags & afb_hook_flag_evt_##what) != 0 \
1125 && (!hook->pattern || !fnmatch(hook->pattern, evt, FNM_CASEFOLD))) { \
1126 hook->itf->hook_evt_##what(hook->closure, __VA_ARGS__); \
1128 hook = hook->next; \
1130 pthread_rwlock_unlock(&rwlock);
1132 void afb_hook_evt_create(const char *evt, int id)
1134 _HOOK_EVT_(create, evt, id);
1137 void afb_hook_evt_push_before(const char *evt, int id, struct json_object *obj)
1139 _HOOK_EVT_(push_before, evt, id, obj);
1142 int afb_hook_evt_push_after(const char *evt, int id, struct json_object *obj, int result)
1144 _HOOK_EVT_(push_after, evt, id, obj, result);
1148 void afb_hook_evt_broadcast_before(const char *evt, int id, struct json_object *obj)
1150 _HOOK_EVT_(broadcast_before, evt, id, obj);
1153 int afb_hook_evt_broadcast_after(const char *evt, int id, struct json_object *obj, int result)
1155 _HOOK_EVT_(broadcast_after, evt, id, obj, result);
1159 void afb_hook_evt_name(const char *evt, int id)
1161 _HOOK_EVT_(name, evt, id);
1164 void afb_hook_evt_drop(const char *evt, int id)
1166 _HOOK_EVT_(drop, evt, id);
1169 /******************************************************************************
1170 * section: hooking services (evt)
1171 *****************************************************************************/
1173 int afb_hook_flags_evt(const char *name)
1176 struct afb_hook_evt *hook;
1178 pthread_rwlock_rdlock(&rwlock);
1180 hook = list_of_evt_hooks;
1182 if (!name || !hook->pattern || !fnmatch(hook->pattern, name, FNM_CASEFOLD))
1183 flags |= hook->flags;
1186 pthread_rwlock_unlock(&rwlock);
1190 struct afb_hook_evt *afb_hook_create_evt(const char *pattern, int flags, struct afb_hook_evt_itf *itf, void *closure)
1192 struct afb_hook_evt *hook;
1194 /* alloc the result */
1195 hook = calloc(1, sizeof *hook);
1199 /* get a copy of the names */
1200 hook->pattern = pattern ? strdup(pattern) : NULL;
1201 if (pattern && !hook->pattern) {
1206 /* initialise the rest */
1208 hook->flags = flags;
1209 hook->itf = itf ? itf : &hook_evt_default_itf;
1210 hook->closure = closure;
1212 /* record the hook */
1213 pthread_rwlock_wrlock(&rwlock);
1214 hook->next = list_of_evt_hooks;
1215 list_of_evt_hooks = hook;
1216 pthread_rwlock_unlock(&rwlock);
1222 struct afb_hook_evt *afb_hook_addref_evt(struct afb_hook_evt *hook)
1224 pthread_rwlock_wrlock(&rwlock);
1226 pthread_rwlock_unlock(&rwlock);
1230 void afb_hook_unref_evt(struct afb_hook_evt *hook)
1232 struct afb_hook_evt **prv;
1235 pthread_rwlock_wrlock(&rwlock);
1236 if (--hook->refcount)
1240 prv = &list_of_evt_hooks;
1241 while (*prv && *prv != hook)
1242 prv = &(*prv)->next;
1246 pthread_rwlock_unlock(&rwlock);
1249 free(hook->pattern);
1256 #define afb_hook_flag_evt_create 0x000001
1257 #define afb_hook_flag_evt_push_before 0x000002
1258 #define afb_hook_flag_evt_push_after 0x000004
1259 #define afb_hook_flag_evt_broadcast_before 0x000008
1260 #define afb_hook_flag_evt_broadcast_after 0x000010
1261 #define afb_hook_flag_evt_drop 0x000020
1262 #define afb_hook_flag_evt_name 0x000040
1264 struct afb_hook_evt_itf {
1265 void (*hook_evt_create)(void *closure, const char *evt);
1266 void (*hook_evt_push_before)(void *closure, const char *evt);
1267 void (*hook_evt_push_after)(void *closure, const char *evt, int result);
1268 void (*hook_evt_broadcast_before)(void *closure, const char *evt);
1269 void (*hook_evt_broadcast_after)(void *closure, const char *evt, int result);
1270 void (*hook_evt_drop)(void *closure, const char *evt);
1271 void (*hook_evt_name)(void *closure, const char *evt);
1274 extern void afb_hook_evt_create(const char *evt);
1275 extern void afb_hook_evt_push_before(const char *evt);
1276 extern int afb_hook_evt_push_after(const char *evt, int result);
1277 extern void afb_hook_evt_broadcast_before(const char *evt);
1278 extern int afb_hook_evt_broadcast_after(const char *evt, int result);
1279 extern void afb_hook_evt_drop(const char *evt);
1280 extern void afb_hook_evt_name(const char *evt);
1282 extern int afb_hook_flags_evt(const char *name);
1283 extern struct afb_hook_evt *afb_hook_create_evt(const char *name, int flags, struct afb_hook_evt_itf *itf, void *closure);
1284 extern struct afb_hook_evt *afb_hook_addref_evt(struct afb_hook_evt *hook);
1285 extern void afb_hook_unref_evt(struct afb_hook_evt *hook);