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"
41 * Definition of a hook for xreq
43 struct afb_hook_xreq {
44 struct afb_hook_xreq *next; /**< next hook */
45 unsigned refcount; /**< reference count */
46 char *api; /**< api hooked or NULL for any */
47 char *verb; /**< verb hooked or NULL for any */
48 struct afb_session *session; /**< session hooked or NULL if any */
49 unsigned flags; /**< hook flags */
50 struct afb_hook_xreq_itf *itf; /**< interface of hook */
51 void *closure; /**< closure for callbacks */
55 * Definition of a hook for ditf
57 struct afb_hook_ditf {
58 struct afb_hook_ditf *next; /**< next hook */
59 unsigned refcount; /**< reference count */
60 char *api; /**< api hooked or NULL for any */
61 unsigned flags; /**< hook flags */
62 struct afb_hook_ditf_itf *itf; /**< interface of hook */
63 void *closure; /**< closure for callbacks */
66 /* synchronisation across threads */
67 static pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
69 /* list of hooks for xreq */
70 static struct afb_hook_xreq *list_of_xreq_hooks = NULL;
72 /* list of hooks for ditf */
73 static struct afb_hook_ditf *list_of_ditf_hooks = NULL;
75 /******************************************************************************
76 * section: default callbacks for tracing requests
77 *****************************************************************************/
79 static void _hook_xreq_(const struct afb_xreq *xreq, const char *format, ...)
86 len = vasprintf(&buffer, format, ap);
90 NOTICE("hook xreq-%06d:%s/%s allocation error", xreq->hookindex, xreq->api, xreq->verb);
92 NOTICE("hook xreq-%06d:%s/%s %s", xreq->hookindex, xreq->api, xreq->verb, buffer);
97 static void hook_xreq_begin_default_cb(void * closure, const struct afb_xreq *xreq)
100 _hook_xreq_(xreq, "BEGIN");
102 _hook_xreq_(xreq, "BEGIN uid=%d gid=%d pid=%d label=%s id=%s",
103 (int)xreq->cred->uid,
104 (int)xreq->cred->gid,
105 (int)xreq->cred->pid,
106 xreq->cred->label?:"(null)",
107 xreq->cred->id?:"(null)"
111 static void hook_xreq_end_default_cb(void * closure, const struct afb_xreq *xreq)
113 _hook_xreq_(xreq, "END");
116 static void hook_xreq_json_default_cb(void * closure, const struct afb_xreq *xreq, struct json_object *obj)
118 _hook_xreq_(xreq, "json() -> %s", json_object_to_json_string(obj));
121 static void hook_xreq_get_default_cb(void * closure, const struct afb_xreq *xreq, const char *name, struct afb_arg arg)
123 _hook_xreq_(xreq, "get(%s) -> { name: %s, value: %s, path: %s }", name, arg.name, arg.value, arg.path);
126 static void hook_xreq_success_default_cb(void * closure, const struct afb_xreq *xreq, struct json_object *obj, const char *info)
128 _hook_xreq_(xreq, "success(%s, %s)", json_object_to_json_string(obj), info);
131 static void hook_xreq_fail_default_cb(void * closure, const struct afb_xreq *xreq, const char *status, const char *info)
133 _hook_xreq_(xreq, "fail(%s, %s)", status, info);
136 static void hook_xreq_raw_default_cb(void * closure, const struct afb_xreq *xreq, const char *buffer, size_t size)
138 _hook_xreq_(xreq, "raw() -> %.*s", (int)size, buffer);
141 static void hook_xreq_send_default_cb(void * closure, const struct afb_xreq *xreq, const char *buffer, size_t size)
143 _hook_xreq_(xreq, "send(%.*s)", (int)size, buffer);
146 static void hook_xreq_context_get_default_cb(void * closure, const struct afb_xreq *xreq, void *value)
148 _hook_xreq_(xreq, "context_get() -> %p", value);
151 static void hook_xreq_context_set_default_cb(void * closure, const struct afb_xreq *xreq, void *value, void (*free_value)(void*))
153 _hook_xreq_(xreq, "context_set(%p, %p)", value, free_value);
156 static void hook_xreq_addref_default_cb(void * closure, const struct afb_xreq *xreq)
158 _hook_xreq_(xreq, "addref()");
161 static void hook_xreq_unref_default_cb(void * closure, const struct afb_xreq *xreq)
163 _hook_xreq_(xreq, "unref()");
166 static void hook_xreq_session_close_default_cb(void * closure, const struct afb_xreq *xreq)
168 _hook_xreq_(xreq, "session_close()");
171 static void hook_xreq_session_set_LOA_default_cb(void * closure, const struct afb_xreq *xreq, unsigned level, int result)
173 _hook_xreq_(xreq, "session_set_LOA(%u) -> %d", level, result);
176 static void hook_xreq_subscribe_default_cb(void * closure, const struct afb_xreq *xreq, struct afb_event event, int result)
178 _hook_xreq_(xreq, "subscribe(%s:%p) -> %d", afb_event_name(event), event.closure, result);
181 static void hook_xreq_unsubscribe_default_cb(void * closure, const struct afb_xreq *xreq, struct afb_event event, int result)
183 _hook_xreq_(xreq, "unsubscribe(%s:%p) -> %d", afb_event_name(event), event.closure, result);
186 static void hook_xreq_subcall_default_cb(void * closure, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
188 _hook_xreq_(xreq, "subcall(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
191 static void hook_xreq_subcall_result_default_cb(void * closure, const struct afb_xreq *xreq, int status, struct json_object *result)
193 _hook_xreq_(xreq, " ...subcall... -> %d: %s", status, json_object_to_json_string(result));
196 static void hook_xreq_subcallsync_default_cb(void * closure, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
198 _hook_xreq_(xreq, "subcallsync(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
201 static void hook_xreq_subcallsync_result_default_cb(void * closure, const struct afb_xreq *xreq, int status, struct json_object *result)
203 _hook_xreq_(xreq, " ...subcallsync... -> %d: %s", status, json_object_to_json_string(result));
206 static struct afb_hook_xreq_itf hook_xreq_default_itf = {
207 .hook_xreq_begin = hook_xreq_begin_default_cb,
208 .hook_xreq_end = hook_xreq_end_default_cb,
209 .hook_xreq_json = hook_xreq_json_default_cb,
210 .hook_xreq_get = hook_xreq_get_default_cb,
211 .hook_xreq_success = hook_xreq_success_default_cb,
212 .hook_xreq_fail = hook_xreq_fail_default_cb,
213 .hook_xreq_raw = hook_xreq_raw_default_cb,
214 .hook_xreq_send = hook_xreq_send_default_cb,
215 .hook_xreq_context_get = hook_xreq_context_get_default_cb,
216 .hook_xreq_context_set = hook_xreq_context_set_default_cb,
217 .hook_xreq_addref = hook_xreq_addref_default_cb,
218 .hook_xreq_unref = hook_xreq_unref_default_cb,
219 .hook_xreq_session_close = hook_xreq_session_close_default_cb,
220 .hook_xreq_session_set_LOA = hook_xreq_session_set_LOA_default_cb,
221 .hook_xreq_subscribe = hook_xreq_subscribe_default_cb,
222 .hook_xreq_unsubscribe = hook_xreq_unsubscribe_default_cb,
223 .hook_xreq_subcall = hook_xreq_subcall_default_cb,
224 .hook_xreq_subcall_result = hook_xreq_subcall_result_default_cb,
225 .hook_xreq_subcallsync = hook_xreq_subcallsync_default_cb,
226 .hook_xreq_subcallsync_result = hook_xreq_subcallsync_result_default_cb,
229 /******************************************************************************
230 * section: hooks for tracing requests
231 *****************************************************************************/
233 #define _HOOK_XREQ_(what,...) \
234 struct afb_hook_xreq *hook; \
235 pthread_rwlock_rdlock(&rwlock); \
236 hook = list_of_xreq_hooks; \
238 if (hook->itf->hook_xreq_##what \
239 && (hook->flags & afb_hook_flag_req_##what) != 0 \
240 && (!hook->session || hook->session == xreq->context.session) \
241 && (!hook->api || !strcasecmp(hook->api, xreq->api)) \
242 && (!hook->verb || !strcasecmp(hook->verb, xreq->verb))) { \
243 hook->itf->hook_xreq_##what(hook->closure, __VA_ARGS__); \
247 pthread_rwlock_unlock(&rwlock);
250 void afb_hook_xreq_begin(const struct afb_xreq *xreq)
252 _HOOK_XREQ_(begin, xreq);
255 void afb_hook_xreq_end(const struct afb_xreq *xreq)
257 _HOOK_XREQ_(end, xreq);
260 struct json_object *afb_hook_xreq_json(const struct afb_xreq *xreq, struct json_object *obj)
262 _HOOK_XREQ_(json, xreq, obj);
266 struct afb_arg afb_hook_xreq_get(const struct afb_xreq *xreq, const char *name, struct afb_arg arg)
268 _HOOK_XREQ_(get, xreq, name, arg);
272 void afb_hook_xreq_success(const struct afb_xreq *xreq, struct json_object *obj, const char *info)
274 _HOOK_XREQ_(success, xreq, obj, info);
277 void afb_hook_xreq_fail(const struct afb_xreq *xreq, const char *status, const char *info)
279 _HOOK_XREQ_(fail, xreq, status, info);
282 const char *afb_hook_xreq_raw(const struct afb_xreq *xreq, const char *buffer, size_t size)
284 _HOOK_XREQ_(raw, xreq, buffer, size);
288 void afb_hook_xreq_send(const struct afb_xreq *xreq, const char *buffer, size_t size)
290 _HOOK_XREQ_(send, xreq, buffer, size);
293 void *afb_hook_xreq_context_get(const struct afb_xreq *xreq, void *value)
295 _HOOK_XREQ_(context_get, xreq, value);
299 void afb_hook_xreq_context_set(const struct afb_xreq *xreq, void *value, void (*free_value)(void*))
301 _HOOK_XREQ_(context_set, xreq, value, free_value);
304 void afb_hook_xreq_addref(const struct afb_xreq *xreq)
306 _HOOK_XREQ_(addref, xreq);
309 void afb_hook_xreq_unref(const struct afb_xreq *xreq)
311 _HOOK_XREQ_(unref, xreq);
314 void afb_hook_xreq_session_close(const struct afb_xreq *xreq)
316 _HOOK_XREQ_(session_close, xreq);
319 int afb_hook_xreq_session_set_LOA(const struct afb_xreq *xreq, unsigned level, int result)
321 _HOOK_XREQ_(session_set_LOA, xreq, level, result);
325 int afb_hook_xreq_subscribe(const struct afb_xreq *xreq, struct afb_event event, int result)
327 _HOOK_XREQ_(subscribe, xreq, event, result);
331 int afb_hook_xreq_unsubscribe(const struct afb_xreq *xreq, struct afb_event event, int result)
333 _HOOK_XREQ_(unsubscribe, xreq, event, result);
337 void afb_hook_xreq_subcall(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
339 _HOOK_XREQ_(subcall, xreq, api, verb, args);
342 void afb_hook_xreq_subcall_result(const struct afb_xreq *xreq, int status, struct json_object *result)
344 _HOOK_XREQ_(subcall_result, xreq, status, result);
347 void afb_hook_xreq_subcallsync(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
349 _HOOK_XREQ_(subcallsync, xreq, api, verb, args);
352 int afb_hook_xreq_subcallsync_result(const struct afb_xreq *xreq, int status, struct json_object *result)
354 _HOOK_XREQ_(subcallsync_result, xreq, status, result);
358 /******************************************************************************
360 *****************************************************************************/
362 void afb_hook_init_xreq(struct afb_xreq *xreq)
368 struct afb_hook_xreq *hook;
370 /* scan hook list to get the expected flags */
372 pthread_rwlock_rdlock(&rwlock);
373 hook = list_of_xreq_hooks;
375 f = hook->flags & afb_hook_flags_req_all;
377 && (!hook->session || hook->session == xreq->context.session)
378 && (!hook->api || !strcasecmp(hook->api, xreq->api))
379 && (!hook->verb || !strcasecmp(hook->verb, xreq->verb));
384 pthread_rwlock_unlock(&rwlock);
386 /* store the hooking data */
387 xreq->hookflags = flags;
389 pthread_rwlock_wrlock(&rwlock);
392 xreq->hookindex = reqindex;
393 pthread_rwlock_unlock(&rwlock);
397 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)
399 struct afb_hook_xreq *hook;
401 /* alloc the result */
402 hook = calloc(1, sizeof *hook);
406 /* get a copy of the names */
407 hook->api = api ? strdup(api) : NULL;
408 hook->verb = verb ? strdup(verb) : NULL;
409 if ((api && !hook->api) || (verb && !hook->verb)) {
416 /* initialise the rest */
417 hook->session = session;
419 afb_session_addref(session);
422 hook->itf = itf ? itf : &hook_xreq_default_itf;
423 hook->closure = closure;
425 /* record the hook */
426 pthread_rwlock_wrlock(&rwlock);
427 hook->next = list_of_xreq_hooks;
428 list_of_xreq_hooks = hook;
429 pthread_rwlock_unlock(&rwlock);
435 struct afb_hook_xreq *afb_hook_addref_xreq(struct afb_hook_xreq *hook)
437 pthread_rwlock_wrlock(&rwlock);
439 pthread_rwlock_unlock(&rwlock);
443 void afb_hook_unref_xreq(struct afb_hook_xreq *hook)
445 struct afb_hook_xreq **prv;
448 pthread_rwlock_wrlock(&rwlock);
449 if (--hook->refcount)
453 prv = &list_of_xreq_hooks;
454 while (*prv && *prv != hook)
459 pthread_rwlock_unlock(&rwlock);
465 afb_session_unref(hook->session);
471 /******************************************************************************
472 * section: default callbacks for tracing daemon interface
473 *****************************************************************************/
475 static void _hook_ditf_(const struct afb_ditf *ditf, const char *format, ...)
481 va_start(ap, format);
482 len = vasprintf(&buffer, format, ap);
486 NOTICE("hook ditf-%s allocation error for %s", ditf->prefix, format);
488 NOTICE("hook ditf-%s %s", ditf->prefix, buffer);
493 static void hook_ditf_event_broadcast_before_cb(void *closure, const struct afb_ditf *ditf, const char *name, struct json_object *object)
495 _hook_ditf_(ditf, "event_broadcast.before(%s, %s)....", name, json_object_to_json_string(object));
498 static void hook_ditf_event_broadcast_after_cb(void *closure, const struct afb_ditf *ditf, const char *name, struct json_object *object, int result)
500 _hook_ditf_(ditf, "event_broadcast.after(%s, %s) -> %d", name, json_object_to_json_string(object), result);
503 static void hook_ditf_get_event_loop_cb(void *closure, const struct afb_ditf *ditf, struct sd_event *result)
505 _hook_ditf_(ditf, "get_event_loop() -> %p", result);
508 static void hook_ditf_get_user_bus_cb(void *closure, const struct afb_ditf *ditf, struct sd_bus *result)
510 _hook_ditf_(ditf, "get_user_bus() -> %p", result);
513 static void hook_ditf_get_system_bus_cb(void *closure, const struct afb_ditf *ditf, struct sd_bus *result)
515 _hook_ditf_(ditf, "get_system_bus() -> %p", result);
518 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)
525 len = vasprintf(&msg, fmt, ap);
529 _hook_ditf_(ditf, "vverbose(%d, %s, %d, %s) -> %s ? ? ?", level, file, line, function, fmt);
531 _hook_ditf_(ditf, "vverbose(%d, %s, %d, %s) -> %s", level, file, line, function, msg);
536 static void hook_ditf_event_make_cb(void *closure, const struct afb_ditf *ditf, const char *name, struct afb_event result)
538 _hook_ditf_(ditf, "event_make(%s) -> %s:%p", name, afb_event_name(result), result.closure);
541 static void hook_ditf_rootdir_get_fd_cb(void *closure, const struct afb_ditf *ditf, int result)
545 _hook_ditf_(ditf, "rootdir_get_fd() -> %d, %m", result);
547 sprintf(path, "/proc/self/fd/%d", result);
548 readlink(path, path, sizeof path);
549 _hook_ditf_(ditf, "rootdir_get_fd() -> %d = %s", result, path);
553 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)
559 _hook_ditf_(ditf, "rootdir_open_locale(%s, %d, %s) -> %d, %m", filename, flags, locale, result);
561 sprintf(path, "/proc/self/fd/%d", result);
562 readlink(path, path, sizeof path);
563 _hook_ditf_(ditf, "rootdir_open_locale(%s, %d, %s) -> %d = %s", filename, flags, locale, result, path);
567 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)
569 _hook_ditf_(ditf, "queue_job(%p, %p, %p, %d) -> %d", callback, argument, group, timeout, result);
572 static struct afb_hook_ditf_itf hook_ditf_default_itf = {
573 .hook_ditf_event_broadcast_before = hook_ditf_event_broadcast_before_cb,
574 .hook_ditf_event_broadcast_after = hook_ditf_event_broadcast_after_cb,
575 .hook_ditf_get_event_loop = hook_ditf_get_event_loop_cb,
576 .hook_ditf_get_user_bus = hook_ditf_get_user_bus_cb,
577 .hook_ditf_get_system_bus = hook_ditf_get_system_bus_cb,
578 .hook_ditf_vverbose = hook_ditf_vverbose_cb,
579 .hook_ditf_event_make = hook_ditf_event_make_cb,
580 .hook_ditf_rootdir_get_fd = hook_ditf_rootdir_get_fd_cb,
581 .hook_ditf_rootdir_open_locale = hook_ditf_rootdir_open_locale_cb,
582 .hook_ditf_queue_job = hook_ditf_queue_job
585 /******************************************************************************
586 * section: hooks for tracing requests
587 *****************************************************************************/
589 #define _HOOK_DITF_(what,...) \
590 struct afb_hook_ditf *hook; \
591 pthread_rwlock_rdlock(&rwlock); \
592 hook = list_of_ditf_hooks; \
594 if (hook->itf->hook_ditf_##what \
595 && (hook->flags & afb_hook_flag_ditf_##what) != 0 \
596 && (!hook->api || !strcasecmp(hook->api, ditf->prefix))) { \
597 hook->itf->hook_ditf_##what(hook->closure, __VA_ARGS__); \
601 pthread_rwlock_unlock(&rwlock);
603 void afb_hook_ditf_event_broadcast_before(const struct afb_ditf *ditf, const char *name, struct json_object *object)
605 _HOOK_DITF_(event_broadcast_before, ditf, name, object);
608 int afb_hook_ditf_event_broadcast_after(const struct afb_ditf *ditf, const char *name, struct json_object *object, int result)
610 _HOOK_DITF_(event_broadcast_after, ditf, name, object, result);
614 struct sd_event *afb_hook_ditf_get_event_loop(const struct afb_ditf *ditf, struct sd_event *result)
616 _HOOK_DITF_(get_event_loop, ditf, result);
620 struct sd_bus *afb_hook_ditf_get_user_bus(const struct afb_ditf *ditf, struct sd_bus *result)
622 _HOOK_DITF_(get_user_bus, ditf, result);
626 struct sd_bus *afb_hook_ditf_get_system_bus(const struct afb_ditf *ditf, struct sd_bus *result)
628 _HOOK_DITF_(get_system_bus, ditf, result);
632 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)
634 _HOOK_DITF_(vverbose, ditf, level, file, line, function, fmt, args);
637 struct afb_event afb_hook_ditf_event_make(const struct afb_ditf *ditf, const char *name, struct afb_event result)
639 _HOOK_DITF_(event_make, ditf, name, result);
643 int afb_hook_ditf_rootdir_get_fd(const struct afb_ditf *ditf, int result)
645 _HOOK_DITF_(rootdir_get_fd, ditf, result);
649 int afb_hook_ditf_rootdir_open_locale(const struct afb_ditf *ditf, const char *filename, int flags, const char *locale, int result)
651 _HOOK_DITF_(rootdir_open_locale, ditf, filename, flags, locale, result);
655 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)
657 _HOOK_DITF_(queue_job, ditf, callback, argument, group, timeout, result);
661 /******************************************************************************
663 *****************************************************************************/
665 int afb_hook_flags_ditf(const char *api)
668 struct afb_hook_ditf *hook;
670 pthread_rwlock_rdlock(&rwlock);
672 hook = list_of_ditf_hooks;
674 if (!api || !hook->api || !strcasecmp(hook->api, api))
675 flags |= hook->flags;
678 pthread_rwlock_unlock(&rwlock);
682 struct afb_hook_ditf *afb_hook_create_ditf(const char *api, int flags, struct afb_hook_ditf_itf *itf, void *closure)
684 struct afb_hook_ditf *hook;
686 /* alloc the result */
687 hook = calloc(1, sizeof *hook);
691 /* get a copy of the names */
692 hook->api = api ? strdup(api) : NULL;
693 if (api && !hook->api) {
698 /* initialise the rest */
701 hook->itf = itf ? itf : &hook_ditf_default_itf;
702 hook->closure = closure;
704 /* record the hook */
705 pthread_rwlock_wrlock(&rwlock);
706 hook->next = list_of_ditf_hooks;
707 list_of_ditf_hooks = hook;
708 pthread_rwlock_unlock(&rwlock);
714 struct afb_hook_ditf *afb_hook_addref_ditf(struct afb_hook_ditf *hook)
716 pthread_rwlock_wrlock(&rwlock);
718 pthread_rwlock_unlock(&rwlock);
722 void afb_hook_unref_ditf(struct afb_hook_ditf *hook)
724 struct afb_hook_ditf **prv;
727 pthread_rwlock_wrlock(&rwlock);
728 if (--hook->refcount)
732 prv = &list_of_ditf_hooks;
733 while (*prv && *prv != hook)
738 pthread_rwlock_unlock(&rwlock);