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"
40 * Definition of a hook for xreq
42 struct afb_hook_xreq {
43 struct afb_hook_xreq *next; /**< next hook */
44 unsigned refcount; /**< reference count */
45 char *api; /**< api hooked or NULL for any */
46 char *verb; /**< verb hooked or NULL for any */
47 struct afb_session *session; /**< session hooked or NULL if any */
48 unsigned flags; /**< hook flags */
49 struct afb_hook_xreq_itf *itf; /**< interface of hook */
50 void *closure; /**< closure for callbacks */
54 * Definition of a hook for ditf
56 struct afb_hook_ditf {
57 struct afb_hook_ditf *next; /**< next hook */
58 unsigned refcount; /**< reference count */
59 char *api; /**< api hooked or NULL for any */
60 unsigned flags; /**< hook flags */
61 struct afb_hook_ditf_itf *itf; /**< interface of hook */
62 void *closure; /**< closure for callbacks */
65 /* synchronisation across threads */
66 static pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
68 /* list of hooks for xreq */
69 static struct afb_hook_xreq *list_of_xreq_hooks = NULL;
71 /* list of hooks for ditf */
72 static struct afb_hook_ditf *list_of_ditf_hooks = NULL;
74 /******************************************************************************
75 * section: default callbacks for tracing requests
76 *****************************************************************************/
78 static void _hook_xreq_(const struct afb_xreq *xreq, const char *format, ...)
85 len = vasprintf(&buffer, format, ap);
89 NOTICE("hook xreq-%06d:%s/%s allocation error", xreq->hookindex, xreq->api, xreq->verb);
91 NOTICE("hook xreq-%06d:%s/%s %s", xreq->hookindex, xreq->api, xreq->verb, buffer);
96 static void hook_xreq_begin_default_cb(void * closure, const struct afb_xreq *xreq)
98 _hook_xreq_(xreq, "BEGIN");
101 static void hook_xreq_end_default_cb(void * closure, const struct afb_xreq *xreq)
103 _hook_xreq_(xreq, "END");
106 static void hook_xreq_json_default_cb(void * closure, const struct afb_xreq *xreq, struct json_object *obj)
108 _hook_xreq_(xreq, "json() -> %s", json_object_to_json_string(obj));
111 static void hook_xreq_get_default_cb(void * closure, const struct afb_xreq *xreq, const char *name, struct afb_arg arg)
113 _hook_xreq_(xreq, "get(%s) -> { name: %s, value: %s, path: %s }", name, arg.name, arg.value, arg.path);
116 static void hook_xreq_success_default_cb(void * closure, const struct afb_xreq *xreq, struct json_object *obj, const char *info)
118 _hook_xreq_(xreq, "success(%s, %s)", json_object_to_json_string(obj), info);
121 static void hook_xreq_fail_default_cb(void * closure, const struct afb_xreq *xreq, const char *status, const char *info)
123 _hook_xreq_(xreq, "fail(%s, %s)", status, info);
126 static void hook_xreq_raw_default_cb(void * closure, const struct afb_xreq *xreq, const char *buffer, size_t size)
128 _hook_xreq_(xreq, "raw() -> %.*s", (int)size, buffer);
131 static void hook_xreq_send_default_cb(void * closure, const struct afb_xreq *xreq, const char *buffer, size_t size)
133 _hook_xreq_(xreq, "send(%.*s)", (int)size, buffer);
136 static void hook_xreq_context_get_default_cb(void * closure, const struct afb_xreq *xreq, void *value)
138 _hook_xreq_(xreq, "context_get() -> %p", value);
141 static void hook_xreq_context_set_default_cb(void * closure, const struct afb_xreq *xreq, void *value, void (*free_value)(void*))
143 _hook_xreq_(xreq, "context_set(%p, %p)", value, free_value);
146 static void hook_xreq_addref_default_cb(void * closure, const struct afb_xreq *xreq)
148 _hook_xreq_(xreq, "addref()");
151 static void hook_xreq_unref_default_cb(void * closure, const struct afb_xreq *xreq)
153 _hook_xreq_(xreq, "unref()");
156 static void hook_xreq_session_close_default_cb(void * closure, const struct afb_xreq *xreq)
158 _hook_xreq_(xreq, "session_close()");
161 static void hook_xreq_session_set_LOA_default_cb(void * closure, const struct afb_xreq *xreq, unsigned level, int result)
163 _hook_xreq_(xreq, "session_set_LOA(%u) -> %d", level, result);
166 static void hook_xreq_subscribe_default_cb(void * closure, const struct afb_xreq *xreq, struct afb_event event, int result)
168 _hook_xreq_(xreq, "subscribe(%s:%p) -> %d", afb_event_name(event), event.closure, result);
171 static void hook_xreq_unsubscribe_default_cb(void * closure, const struct afb_xreq *xreq, struct afb_event event, int result)
173 _hook_xreq_(xreq, "unsubscribe(%s:%p) -> %d", afb_event_name(event), event.closure, result);
176 static void hook_xreq_subcall_default_cb(void * closure, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
178 _hook_xreq_(xreq, "subcall(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
181 static void hook_xreq_subcall_result_default_cb(void * closure, const struct afb_xreq *xreq, int status, struct json_object *result)
183 _hook_xreq_(xreq, " ...subcall... -> %d: %s", status, json_object_to_json_string(result));
186 static void hook_xreq_subcallsync_default_cb(void * closure, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
188 _hook_xreq_(xreq, "subcallsync(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
191 static void hook_xreq_subcallsync_result_default_cb(void * closure, const struct afb_xreq *xreq, int status, struct json_object *result)
193 _hook_xreq_(xreq, " ...subcallsync... -> %d: %s", status, json_object_to_json_string(result));
196 static struct afb_hook_xreq_itf hook_xreq_default_itf = {
197 .hook_xreq_begin = hook_xreq_begin_default_cb,
198 .hook_xreq_end = hook_xreq_end_default_cb,
199 .hook_xreq_json = hook_xreq_json_default_cb,
200 .hook_xreq_get = hook_xreq_get_default_cb,
201 .hook_xreq_success = hook_xreq_success_default_cb,
202 .hook_xreq_fail = hook_xreq_fail_default_cb,
203 .hook_xreq_raw = hook_xreq_raw_default_cb,
204 .hook_xreq_send = hook_xreq_send_default_cb,
205 .hook_xreq_context_get = hook_xreq_context_get_default_cb,
206 .hook_xreq_context_set = hook_xreq_context_set_default_cb,
207 .hook_xreq_addref = hook_xreq_addref_default_cb,
208 .hook_xreq_unref = hook_xreq_unref_default_cb,
209 .hook_xreq_session_close = hook_xreq_session_close_default_cb,
210 .hook_xreq_session_set_LOA = hook_xreq_session_set_LOA_default_cb,
211 .hook_xreq_subscribe = hook_xreq_subscribe_default_cb,
212 .hook_xreq_unsubscribe = hook_xreq_unsubscribe_default_cb,
213 .hook_xreq_subcall = hook_xreq_subcall_default_cb,
214 .hook_xreq_subcall_result = hook_xreq_subcall_result_default_cb,
215 .hook_xreq_subcallsync = hook_xreq_subcallsync_default_cb,
216 .hook_xreq_subcallsync_result = hook_xreq_subcallsync_result_default_cb,
219 /******************************************************************************
220 * section: hooks for tracing requests
221 *****************************************************************************/
223 #define _HOOK_XREQ_(what,...) \
224 struct afb_hook_xreq *hook; \
225 pthread_rwlock_rdlock(&rwlock); \
226 hook = list_of_xreq_hooks; \
228 if (hook->itf->hook_xreq_##what \
229 && (hook->flags & afb_hook_flag_req_##what) != 0 \
230 && (!hook->session || hook->session == xreq->context.session) \
231 && (!hook->api || !strcasecmp(hook->api, xreq->api)) \
232 && (!hook->verb || !strcasecmp(hook->verb, xreq->verb))) { \
233 hook->itf->hook_xreq_##what(hook->closure, __VA_ARGS__); \
237 pthread_rwlock_unlock(&rwlock);
240 void afb_hook_xreq_begin(const struct afb_xreq *xreq)
242 _HOOK_XREQ_(begin, xreq);
245 void afb_hook_xreq_end(const struct afb_xreq *xreq)
247 _HOOK_XREQ_(end, xreq);
250 struct json_object *afb_hook_xreq_json(const struct afb_xreq *xreq, struct json_object *obj)
252 _HOOK_XREQ_(json, xreq, obj);
256 struct afb_arg afb_hook_xreq_get(const struct afb_xreq *xreq, const char *name, struct afb_arg arg)
258 _HOOK_XREQ_(get, xreq, name, arg);
262 void afb_hook_xreq_success(const struct afb_xreq *xreq, struct json_object *obj, const char *info)
264 _HOOK_XREQ_(success, xreq, obj, info);
267 void afb_hook_xreq_fail(const struct afb_xreq *xreq, const char *status, const char *info)
269 _HOOK_XREQ_(fail, xreq, status, info);
272 const char *afb_hook_xreq_raw(const struct afb_xreq *xreq, const char *buffer, size_t size)
274 _HOOK_XREQ_(raw, xreq, buffer, size);
278 void afb_hook_xreq_send(const struct afb_xreq *xreq, const char *buffer, size_t size)
280 _HOOK_XREQ_(send, xreq, buffer, size);
283 void *afb_hook_xreq_context_get(const struct afb_xreq *xreq, void *value)
285 _HOOK_XREQ_(context_get, xreq, value);
289 void afb_hook_xreq_context_set(const struct afb_xreq *xreq, void *value, void (*free_value)(void*))
291 _HOOK_XREQ_(context_set, xreq, value, free_value);
294 void afb_hook_xreq_addref(const struct afb_xreq *xreq)
296 _HOOK_XREQ_(addref, xreq);
299 void afb_hook_xreq_unref(const struct afb_xreq *xreq)
301 _HOOK_XREQ_(unref, xreq);
304 void afb_hook_xreq_session_close(const struct afb_xreq *xreq)
306 _HOOK_XREQ_(session_close, xreq);
309 int afb_hook_xreq_session_set_LOA(const struct afb_xreq *xreq, unsigned level, int result)
311 _HOOK_XREQ_(session_set_LOA, xreq, level, result);
315 int afb_hook_xreq_subscribe(const struct afb_xreq *xreq, struct afb_event event, int result)
317 _HOOK_XREQ_(subscribe, xreq, event, result);
321 int afb_hook_xreq_unsubscribe(const struct afb_xreq *xreq, struct afb_event event, int result)
323 _HOOK_XREQ_(unsubscribe, xreq, event, result);
327 void afb_hook_xreq_subcall(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
329 _HOOK_XREQ_(subcall, xreq, api, verb, args);
332 void afb_hook_xreq_subcall_result(const struct afb_xreq *xreq, int status, struct json_object *result)
334 _HOOK_XREQ_(subcall_result, xreq, status, result);
337 void afb_hook_xreq_subcallsync(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
339 _HOOK_XREQ_(subcallsync, xreq, api, verb, args);
342 int afb_hook_xreq_subcallsync_result(const struct afb_xreq *xreq, int status, struct json_object *result)
344 _HOOK_XREQ_(subcallsync_result, xreq, status, result);
348 /******************************************************************************
350 *****************************************************************************/
352 void afb_hook_init_xreq(struct afb_xreq *xreq)
358 struct afb_hook_xreq *hook;
360 /* scan hook list to get the expected flags */
362 pthread_rwlock_rdlock(&rwlock);
363 hook = list_of_xreq_hooks;
365 f = hook->flags & afb_hook_flags_req_all;
367 && (!hook->session || hook->session == xreq->context.session)
368 && (!hook->api || !strcasecmp(hook->api, xreq->api))
369 && (!hook->verb || !strcasecmp(hook->verb, xreq->verb));
374 pthread_rwlock_unlock(&rwlock);
376 /* store the hooking data */
377 xreq->hookflags = flags;
379 pthread_rwlock_wrlock(&rwlock);
382 xreq->hookindex = reqindex;
383 pthread_rwlock_unlock(&rwlock);
387 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)
389 struct afb_hook_xreq *hook;
391 /* alloc the result */
392 hook = calloc(1, sizeof *hook);
396 /* get a copy of the names */
397 hook->api = api ? strdup(api) : NULL;
398 hook->verb = verb ? strdup(verb) : NULL;
399 if ((api && !hook->api) || (verb && !hook->verb)) {
406 /* initialise the rest */
407 hook->session = session;
409 afb_session_addref(session);
412 hook->itf = itf ? itf : &hook_xreq_default_itf;
413 hook->closure = closure;
415 /* record the hook */
416 pthread_rwlock_wrlock(&rwlock);
417 hook->next = list_of_xreq_hooks;
418 list_of_xreq_hooks = hook;
419 pthread_rwlock_unlock(&rwlock);
425 struct afb_hook_xreq *afb_hook_addref_xreq(struct afb_hook_xreq *hook)
427 pthread_rwlock_wrlock(&rwlock);
429 pthread_rwlock_unlock(&rwlock);
433 void afb_hook_unref_xreq(struct afb_hook_xreq *hook)
435 struct afb_hook_xreq **prv;
438 pthread_rwlock_wrlock(&rwlock);
439 if (--hook->refcount)
443 prv = &list_of_xreq_hooks;
444 while (*prv && *prv != hook)
449 pthread_rwlock_unlock(&rwlock);
455 afb_session_unref(hook->session);
461 /******************************************************************************
462 * section: default callbacks for tracing daemon interface
463 *****************************************************************************/
465 static void _hook_ditf_(const struct afb_ditf *ditf, const char *format, ...)
471 va_start(ap, format);
472 len = vasprintf(&buffer, format, ap);
476 NOTICE("hook ditf-%s allocation error for %s", ditf->prefix, format);
478 NOTICE("hook ditf-%s %s", ditf->prefix, buffer);
483 static void hook_ditf_event_broadcast_before_cb(void *closure, const struct afb_ditf *ditf, const char *name, struct json_object *object)
485 _hook_ditf_(ditf, "event_broadcast.before(%s, %s)....", name, json_object_to_json_string(object));
488 static void hook_ditf_event_broadcast_after_cb(void *closure, const struct afb_ditf *ditf, const char *name, struct json_object *object, int result)
490 _hook_ditf_(ditf, "event_broadcast.after(%s, %s) -> %d", name, json_object_to_json_string(object), result);
493 static void hook_ditf_get_event_loop_cb(void *closure, const struct afb_ditf *ditf, struct sd_event *result)
495 _hook_ditf_(ditf, "get_event_loop() -> %p", result);
498 static void hook_ditf_get_user_bus_cb(void *closure, const struct afb_ditf *ditf, struct sd_bus *result)
500 _hook_ditf_(ditf, "get_user_bus() -> %p", result);
503 static void hook_ditf_get_system_bus_cb(void *closure, const struct afb_ditf *ditf, struct sd_bus *result)
505 _hook_ditf_(ditf, "get_system_bus() -> %p", result);
508 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)
515 len = vasprintf(&msg, fmt, ap);
519 _hook_ditf_(ditf, "vverbose(%d, %s, %d, %s) -> %s ? ? ?", level, file, line, function, fmt);
521 _hook_ditf_(ditf, "vverbose(%d, %s, %d, %s) -> %s", level, file, line, function, msg);
526 static void hook_ditf_event_make_cb(void *closure, const struct afb_ditf *ditf, const char *name, struct afb_event result)
528 _hook_ditf_(ditf, "event_make(%s) -> %s:%p", name, afb_event_name(result), result.closure);
531 static void hook_ditf_rootdir_get_fd_cb(void *closure, const struct afb_ditf *ditf, int result)
535 _hook_ditf_(ditf, "rootdir_get_fd() -> %d, %m", result);
537 sprintf(path, "/proc/self/fd/%d", result);
538 readlink(path, path, sizeof path);
539 _hook_ditf_(ditf, "rootdir_get_fd() -> %d = %s", result, path);
543 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)
549 _hook_ditf_(ditf, "rootdir_open_locale(%s, %d, %s) -> %d, %m", filename, flags, locale, result);
551 sprintf(path, "/proc/self/fd/%d", result);
552 readlink(path, path, sizeof path);
553 _hook_ditf_(ditf, "rootdir_open_locale(%s, %d, %s) -> %d = %s", filename, flags, locale, result, path);
558 static struct afb_hook_ditf_itf hook_ditf_default_itf = {
559 .hook_ditf_event_broadcast_before = hook_ditf_event_broadcast_before_cb,
560 .hook_ditf_event_broadcast_after = hook_ditf_event_broadcast_after_cb,
561 .hook_ditf_get_event_loop = hook_ditf_get_event_loop_cb,
562 .hook_ditf_get_user_bus = hook_ditf_get_user_bus_cb,
563 .hook_ditf_get_system_bus = hook_ditf_get_system_bus_cb,
564 .hook_ditf_vverbose = hook_ditf_vverbose_cb,
565 .hook_ditf_event_make = hook_ditf_event_make_cb,
566 .hook_ditf_rootdir_get_fd = hook_ditf_rootdir_get_fd_cb,
567 .hook_ditf_rootdir_open_locale = hook_ditf_rootdir_open_locale_cb,
570 /******************************************************************************
571 * section: hooks for tracing requests
572 *****************************************************************************/
574 #define _HOOK_DITF_(what,...) \
575 struct afb_hook_ditf *hook; \
576 pthread_rwlock_rdlock(&rwlock); \
577 hook = list_of_ditf_hooks; \
579 if (hook->itf->hook_ditf_##what \
580 && (hook->flags & afb_hook_flag_ditf_##what) != 0 \
581 && (!hook->api || !strcasecmp(hook->api, ditf->prefix))) { \
582 hook->itf->hook_ditf_##what(hook->closure, __VA_ARGS__); \
586 pthread_rwlock_unlock(&rwlock);
588 void afb_hook_ditf_event_broadcast_before(const struct afb_ditf *ditf, const char *name, struct json_object *object)
590 _HOOK_DITF_(event_broadcast_before, ditf, name, object);
593 int afb_hook_ditf_event_broadcast_after(const struct afb_ditf *ditf, const char *name, struct json_object *object, int result)
595 _HOOK_DITF_(event_broadcast_after, ditf, name, object, result);
599 struct sd_event *afb_hook_ditf_get_event_loop(const struct afb_ditf *ditf, struct sd_event *result)
601 _HOOK_DITF_(get_event_loop, ditf, result);
605 struct sd_bus *afb_hook_ditf_get_user_bus(const struct afb_ditf *ditf, struct sd_bus *result)
607 _HOOK_DITF_(get_user_bus, ditf, result);
611 struct sd_bus *afb_hook_ditf_get_system_bus(const struct afb_ditf *ditf, struct sd_bus *result)
613 _HOOK_DITF_(get_system_bus, ditf, result);
617 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)
619 _HOOK_DITF_(vverbose, ditf, level, file, line, function, fmt, args);
622 struct afb_event afb_hook_ditf_event_make(const struct afb_ditf *ditf, const char *name, struct afb_event result)
624 _HOOK_DITF_(event_make, ditf, name, result);
628 int afb_hook_ditf_rootdir_get_fd(const struct afb_ditf *ditf, int result)
630 _HOOK_DITF_(rootdir_get_fd, ditf, result);
634 int afb_hook_ditf_rootdir_open_locale(const struct afb_ditf *ditf, const char *filename, int flags, const char *locale, int result)
636 _HOOK_DITF_(rootdir_open_locale, ditf, filename, flags, locale, result);
642 /******************************************************************************
644 *****************************************************************************/
646 int afb_hook_flags_ditf(const char *api)
649 struct afb_hook_ditf *hook;
651 pthread_rwlock_rdlock(&rwlock);
653 hook = list_of_ditf_hooks;
655 if (!api || !hook->api || !strcasecmp(hook->api, api))
656 flags |= hook->flags;
659 pthread_rwlock_unlock(&rwlock);
663 struct afb_hook_ditf *afb_hook_create_ditf(const char *api, int flags, struct afb_hook_ditf_itf *itf, void *closure)
665 struct afb_hook_ditf *hook;
667 /* alloc the result */
668 hook = calloc(1, sizeof *hook);
672 /* get a copy of the names */
673 hook->api = api ? strdup(api) : NULL;
674 if (api && !hook->api) {
679 /* initialise the rest */
682 hook->itf = itf ? itf : &hook_ditf_default_itf;
683 hook->closure = closure;
685 /* record the hook */
686 pthread_rwlock_wrlock(&rwlock);
687 hook->next = list_of_ditf_hooks;
688 list_of_ditf_hooks = hook;
689 pthread_rwlock_unlock(&rwlock);
695 struct afb_hook_ditf *afb_hook_addref_ditf(struct afb_hook_ditf *hook)
697 pthread_rwlock_wrlock(&rwlock);
699 pthread_rwlock_unlock(&rwlock);
703 void afb_hook_unref_ditf(struct afb_hook_ditf *hook)
705 struct afb_hook_ditf **prv;
708 pthread_rwlock_wrlock(&rwlock);
709 if (--hook->refcount)
713 prv = &list_of_ditf_hooks;
714 while (*prv && *prv != hook)
719 pthread_rwlock_unlock(&rwlock);