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=%s gid=%d pid=%d label=%s id=%s",
103 (int)xreq->cred->uid,
105 (int)xreq->cred->gid,
106 (int)xreq->cred->pid,
107 xreq->cred->label?:"(null)",
108 xreq->cred->id?:"(null)"
112 static void hook_xreq_end_default_cb(void * closure, const struct afb_xreq *xreq)
114 _hook_xreq_(xreq, "END");
117 static void hook_xreq_json_default_cb(void * closure, const struct afb_xreq *xreq, struct json_object *obj)
119 _hook_xreq_(xreq, "json() -> %s", json_object_to_json_string(obj));
122 static void hook_xreq_get_default_cb(void * closure, const struct afb_xreq *xreq, const char *name, struct afb_arg arg)
124 _hook_xreq_(xreq, "get(%s) -> { name: %s, value: %s, path: %s }", name, arg.name, arg.value, arg.path);
127 static void hook_xreq_success_default_cb(void * closure, const struct afb_xreq *xreq, struct json_object *obj, const char *info)
129 _hook_xreq_(xreq, "success(%s, %s)", json_object_to_json_string(obj), info);
132 static void hook_xreq_fail_default_cb(void * closure, const struct afb_xreq *xreq, const char *status, const char *info)
134 _hook_xreq_(xreq, "fail(%s, %s)", status, info);
137 static void hook_xreq_context_get_default_cb(void * closure, const struct afb_xreq *xreq, void *value)
139 _hook_xreq_(xreq, "context_get() -> %p", value);
142 static void hook_xreq_context_set_default_cb(void * closure, const struct afb_xreq *xreq, void *value, void (*free_value)(void*))
144 _hook_xreq_(xreq, "context_set(%p, %p)", value, free_value);
147 static void hook_xreq_addref_default_cb(void * closure, const struct afb_xreq *xreq)
149 _hook_xreq_(xreq, "addref()");
152 static void hook_xreq_unref_default_cb(void * closure, const struct afb_xreq *xreq)
154 _hook_xreq_(xreq, "unref()");
157 static void hook_xreq_session_close_default_cb(void * closure, const struct afb_xreq *xreq)
159 _hook_xreq_(xreq, "session_close()");
162 static void hook_xreq_session_set_LOA_default_cb(void * closure, const struct afb_xreq *xreq, unsigned level, int result)
164 _hook_xreq_(xreq, "session_set_LOA(%u) -> %d", level, result);
167 static void hook_xreq_subscribe_default_cb(void * closure, const struct afb_xreq *xreq, struct afb_event event, int result)
169 _hook_xreq_(xreq, "subscribe(%s:%p) -> %d", afb_event_name(event), event.closure, result);
172 static void hook_xreq_unsubscribe_default_cb(void * closure, const struct afb_xreq *xreq, struct afb_event event, int result)
174 _hook_xreq_(xreq, "unsubscribe(%s:%p) -> %d", afb_event_name(event), event.closure, result);
177 static void hook_xreq_subcall_default_cb(void * closure, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
179 _hook_xreq_(xreq, "subcall(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
182 static void hook_xreq_subcall_result_default_cb(void * closure, const struct afb_xreq *xreq, int status, struct json_object *result)
184 _hook_xreq_(xreq, " ...subcall... -> %d: %s", status, json_object_to_json_string(result));
187 static void hook_xreq_subcallsync_default_cb(void * closure, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
189 _hook_xreq_(xreq, "subcallsync(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
192 static void hook_xreq_subcallsync_result_default_cb(void * closure, const struct afb_xreq *xreq, int status, struct json_object *result)
194 _hook_xreq_(xreq, " ...subcallsync... -> %d: %s", status, json_object_to_json_string(result));
197 static struct afb_hook_xreq_itf hook_xreq_default_itf = {
198 .hook_xreq_begin = hook_xreq_begin_default_cb,
199 .hook_xreq_end = hook_xreq_end_default_cb,
200 .hook_xreq_json = hook_xreq_json_default_cb,
201 .hook_xreq_get = hook_xreq_get_default_cb,
202 .hook_xreq_success = hook_xreq_success_default_cb,
203 .hook_xreq_fail = hook_xreq_fail_default_cb,
204 .hook_xreq_context_get = hook_xreq_context_get_default_cb,
205 .hook_xreq_context_set = hook_xreq_context_set_default_cb,
206 .hook_xreq_addref = hook_xreq_addref_default_cb,
207 .hook_xreq_unref = hook_xreq_unref_default_cb,
208 .hook_xreq_session_close = hook_xreq_session_close_default_cb,
209 .hook_xreq_session_set_LOA = hook_xreq_session_set_LOA_default_cb,
210 .hook_xreq_subscribe = hook_xreq_subscribe_default_cb,
211 .hook_xreq_unsubscribe = hook_xreq_unsubscribe_default_cb,
212 .hook_xreq_subcall = hook_xreq_subcall_default_cb,
213 .hook_xreq_subcall_result = hook_xreq_subcall_result_default_cb,
214 .hook_xreq_subcallsync = hook_xreq_subcallsync_default_cb,
215 .hook_xreq_subcallsync_result = hook_xreq_subcallsync_result_default_cb,
218 /******************************************************************************
219 * section: hooks for tracing requests
220 *****************************************************************************/
222 #define _HOOK_XREQ_(what,...) \
223 struct afb_hook_xreq *hook; \
224 pthread_rwlock_rdlock(&rwlock); \
225 hook = list_of_xreq_hooks; \
227 if (hook->itf->hook_xreq_##what \
228 && (hook->flags & afb_hook_flag_req_##what) != 0 \
229 && (!hook->session || hook->session == xreq->context.session) \
230 && (!hook->api || !strcasecmp(hook->api, xreq->api)) \
231 && (!hook->verb || !strcasecmp(hook->verb, xreq->verb))) { \
232 hook->itf->hook_xreq_##what(hook->closure, __VA_ARGS__); \
236 pthread_rwlock_unlock(&rwlock);
239 void afb_hook_xreq_begin(const struct afb_xreq *xreq)
241 _HOOK_XREQ_(begin, xreq);
244 void afb_hook_xreq_end(const struct afb_xreq *xreq)
246 _HOOK_XREQ_(end, xreq);
249 struct json_object *afb_hook_xreq_json(const struct afb_xreq *xreq, struct json_object *obj)
251 _HOOK_XREQ_(json, xreq, obj);
255 struct afb_arg afb_hook_xreq_get(const struct afb_xreq *xreq, const char *name, struct afb_arg arg)
257 _HOOK_XREQ_(get, xreq, name, arg);
261 void afb_hook_xreq_success(const struct afb_xreq *xreq, struct json_object *obj, const char *info)
263 _HOOK_XREQ_(success, xreq, obj, info);
266 void afb_hook_xreq_fail(const struct afb_xreq *xreq, const char *status, const char *info)
268 _HOOK_XREQ_(fail, xreq, status, info);
271 void *afb_hook_xreq_context_get(const struct afb_xreq *xreq, void *value)
273 _HOOK_XREQ_(context_get, xreq, value);
277 void afb_hook_xreq_context_set(const struct afb_xreq *xreq, void *value, void (*free_value)(void*))
279 _HOOK_XREQ_(context_set, xreq, value, free_value);
282 void afb_hook_xreq_addref(const struct afb_xreq *xreq)
284 _HOOK_XREQ_(addref, xreq);
287 void afb_hook_xreq_unref(const struct afb_xreq *xreq)
289 _HOOK_XREQ_(unref, xreq);
292 void afb_hook_xreq_session_close(const struct afb_xreq *xreq)
294 _HOOK_XREQ_(session_close, xreq);
297 int afb_hook_xreq_session_set_LOA(const struct afb_xreq *xreq, unsigned level, int result)
299 _HOOK_XREQ_(session_set_LOA, xreq, level, result);
303 int afb_hook_xreq_subscribe(const struct afb_xreq *xreq, struct afb_event event, int result)
305 _HOOK_XREQ_(subscribe, xreq, event, result);
309 int afb_hook_xreq_unsubscribe(const struct afb_xreq *xreq, struct afb_event event, int result)
311 _HOOK_XREQ_(unsubscribe, xreq, event, result);
315 void afb_hook_xreq_subcall(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
317 _HOOK_XREQ_(subcall, xreq, api, verb, args);
320 void afb_hook_xreq_subcall_result(const struct afb_xreq *xreq, int status, struct json_object *result)
322 _HOOK_XREQ_(subcall_result, xreq, status, result);
325 void afb_hook_xreq_subcallsync(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
327 _HOOK_XREQ_(subcallsync, xreq, api, verb, args);
330 int afb_hook_xreq_subcallsync_result(const struct afb_xreq *xreq, int status, struct json_object *result)
332 _HOOK_XREQ_(subcallsync_result, xreq, status, result);
336 /******************************************************************************
338 *****************************************************************************/
340 void afb_hook_init_xreq(struct afb_xreq *xreq)
346 struct afb_hook_xreq *hook;
348 /* scan hook list to get the expected flags */
350 pthread_rwlock_rdlock(&rwlock);
351 hook = list_of_xreq_hooks;
353 f = hook->flags & afb_hook_flags_req_all;
355 && (!hook->session || hook->session == xreq->context.session)
356 && (!hook->api || !strcasecmp(hook->api, xreq->api))
357 && (!hook->verb || !strcasecmp(hook->verb, xreq->verb));
362 pthread_rwlock_unlock(&rwlock);
364 /* store the hooking data */
365 xreq->hookflags = flags;
367 pthread_rwlock_wrlock(&rwlock);
370 xreq->hookindex = reqindex;
371 pthread_rwlock_unlock(&rwlock);
375 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)
377 struct afb_hook_xreq *hook;
379 /* alloc the result */
380 hook = calloc(1, sizeof *hook);
384 /* get a copy of the names */
385 hook->api = api ? strdup(api) : NULL;
386 hook->verb = verb ? strdup(verb) : NULL;
387 if ((api && !hook->api) || (verb && !hook->verb)) {
394 /* initialise the rest */
395 hook->session = session;
397 afb_session_addref(session);
400 hook->itf = itf ? itf : &hook_xreq_default_itf;
401 hook->closure = closure;
403 /* record the hook */
404 pthread_rwlock_wrlock(&rwlock);
405 hook->next = list_of_xreq_hooks;
406 list_of_xreq_hooks = hook;
407 pthread_rwlock_unlock(&rwlock);
413 struct afb_hook_xreq *afb_hook_addref_xreq(struct afb_hook_xreq *hook)
415 pthread_rwlock_wrlock(&rwlock);
417 pthread_rwlock_unlock(&rwlock);
421 void afb_hook_unref_xreq(struct afb_hook_xreq *hook)
423 struct afb_hook_xreq **prv;
426 pthread_rwlock_wrlock(&rwlock);
427 if (--hook->refcount)
431 prv = &list_of_xreq_hooks;
432 while (*prv && *prv != hook)
437 pthread_rwlock_unlock(&rwlock);
443 afb_session_unref(hook->session);
449 /******************************************************************************
450 * section: default callbacks for tracing daemon interface
451 *****************************************************************************/
453 static void _hook_ditf_(const struct afb_ditf *ditf, const char *format, ...)
459 va_start(ap, format);
460 len = vasprintf(&buffer, format, ap);
464 NOTICE("hook ditf-%s allocation error for %s", ditf->prefix, format);
466 NOTICE("hook ditf-%s %s", ditf->prefix, buffer);
471 static void hook_ditf_event_broadcast_before_cb(void *closure, const struct afb_ditf *ditf, const char *name, struct json_object *object)
473 _hook_ditf_(ditf, "event_broadcast.before(%s, %s)....", name, json_object_to_json_string(object));
476 static void hook_ditf_event_broadcast_after_cb(void *closure, const struct afb_ditf *ditf, const char *name, struct json_object *object, int result)
478 _hook_ditf_(ditf, "event_broadcast.after(%s, %s) -> %d", name, json_object_to_json_string(object), result);
481 static void hook_ditf_get_event_loop_cb(void *closure, const struct afb_ditf *ditf, struct sd_event *result)
483 _hook_ditf_(ditf, "get_event_loop() -> %p", result);
486 static void hook_ditf_get_user_bus_cb(void *closure, const struct afb_ditf *ditf, struct sd_bus *result)
488 _hook_ditf_(ditf, "get_user_bus() -> %p", result);
491 static void hook_ditf_get_system_bus_cb(void *closure, const struct afb_ditf *ditf, struct sd_bus *result)
493 _hook_ditf_(ditf, "get_system_bus() -> %p", result);
496 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)
503 len = vasprintf(&msg, fmt, ap);
507 _hook_ditf_(ditf, "vverbose(%d, %s, %d, %s) -> %s ? ? ?", level, file, line, function, fmt);
509 _hook_ditf_(ditf, "vverbose(%d, %s, %d, %s) -> %s", level, file, line, function, msg);
514 static void hook_ditf_event_make_cb(void *closure, const struct afb_ditf *ditf, const char *name, struct afb_event result)
516 _hook_ditf_(ditf, "event_make(%s) -> %s:%p", name, afb_event_name(result), result.closure);
519 static void hook_ditf_rootdir_get_fd_cb(void *closure, const struct afb_ditf *ditf, int result)
523 _hook_ditf_(ditf, "rootdir_get_fd() -> %d, %m", result);
525 sprintf(path, "/proc/self/fd/%d", result);
526 readlink(path, path, sizeof path);
527 _hook_ditf_(ditf, "rootdir_get_fd() -> %d = %s", result, path);
531 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)
537 _hook_ditf_(ditf, "rootdir_open_locale(%s, %d, %s) -> %d, %m", filename, flags, locale, result);
539 sprintf(path, "/proc/self/fd/%d", result);
540 readlink(path, path, sizeof path);
541 _hook_ditf_(ditf, "rootdir_open_locale(%s, %d, %s) -> %d = %s", filename, flags, locale, result, path);
545 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)
547 _hook_ditf_(ditf, "queue_job(%p, %p, %p, %d) -> %d", callback, argument, group, timeout, result);
550 static struct afb_hook_ditf_itf hook_ditf_default_itf = {
551 .hook_ditf_event_broadcast_before = hook_ditf_event_broadcast_before_cb,
552 .hook_ditf_event_broadcast_after = hook_ditf_event_broadcast_after_cb,
553 .hook_ditf_get_event_loop = hook_ditf_get_event_loop_cb,
554 .hook_ditf_get_user_bus = hook_ditf_get_user_bus_cb,
555 .hook_ditf_get_system_bus = hook_ditf_get_system_bus_cb,
556 .hook_ditf_vverbose = hook_ditf_vverbose_cb,
557 .hook_ditf_event_make = hook_ditf_event_make_cb,
558 .hook_ditf_rootdir_get_fd = hook_ditf_rootdir_get_fd_cb,
559 .hook_ditf_rootdir_open_locale = hook_ditf_rootdir_open_locale_cb,
560 .hook_ditf_queue_job = hook_ditf_queue_job
563 /******************************************************************************
564 * section: hooks for tracing requests
565 *****************************************************************************/
567 #define _HOOK_DITF_(what,...) \
568 struct afb_hook_ditf *hook; \
569 pthread_rwlock_rdlock(&rwlock); \
570 hook = list_of_ditf_hooks; \
572 if (hook->itf->hook_ditf_##what \
573 && (hook->flags & afb_hook_flag_ditf_##what) != 0 \
574 && (!hook->api || !strcasecmp(hook->api, ditf->prefix))) { \
575 hook->itf->hook_ditf_##what(hook->closure, __VA_ARGS__); \
579 pthread_rwlock_unlock(&rwlock);
581 void afb_hook_ditf_event_broadcast_before(const struct afb_ditf *ditf, const char *name, struct json_object *object)
583 _HOOK_DITF_(event_broadcast_before, ditf, name, object);
586 int afb_hook_ditf_event_broadcast_after(const struct afb_ditf *ditf, const char *name, struct json_object *object, int result)
588 _HOOK_DITF_(event_broadcast_after, ditf, name, object, result);
592 struct sd_event *afb_hook_ditf_get_event_loop(const struct afb_ditf *ditf, struct sd_event *result)
594 _HOOK_DITF_(get_event_loop, ditf, result);
598 struct sd_bus *afb_hook_ditf_get_user_bus(const struct afb_ditf *ditf, struct sd_bus *result)
600 _HOOK_DITF_(get_user_bus, ditf, result);
604 struct sd_bus *afb_hook_ditf_get_system_bus(const struct afb_ditf *ditf, struct sd_bus *result)
606 _HOOK_DITF_(get_system_bus, ditf, result);
610 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)
612 _HOOK_DITF_(vverbose, ditf, level, file, line, function, fmt, args);
615 struct afb_event afb_hook_ditf_event_make(const struct afb_ditf *ditf, const char *name, struct afb_event result)
617 _HOOK_DITF_(event_make, ditf, name, result);
621 int afb_hook_ditf_rootdir_get_fd(const struct afb_ditf *ditf, int result)
623 _HOOK_DITF_(rootdir_get_fd, ditf, result);
627 int afb_hook_ditf_rootdir_open_locale(const struct afb_ditf *ditf, const char *filename, int flags, const char *locale, int result)
629 _HOOK_DITF_(rootdir_open_locale, ditf, filename, flags, locale, result);
633 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)
635 _HOOK_DITF_(queue_job, ditf, callback, argument, group, timeout, result);
639 /******************************************************************************
641 *****************************************************************************/
643 int afb_hook_flags_ditf(const char *api)
646 struct afb_hook_ditf *hook;
648 pthread_rwlock_rdlock(&rwlock);
650 hook = list_of_ditf_hooks;
652 if (!api || !hook->api || !strcasecmp(hook->api, api))
653 flags |= hook->flags;
656 pthread_rwlock_unlock(&rwlock);
660 struct afb_hook_ditf *afb_hook_create_ditf(const char *api, int flags, struct afb_hook_ditf_itf *itf, void *closure)
662 struct afb_hook_ditf *hook;
664 /* alloc the result */
665 hook = calloc(1, sizeof *hook);
669 /* get a copy of the names */
670 hook->api = api ? strdup(api) : NULL;
671 if (api && !hook->api) {
676 /* initialise the rest */
679 hook->itf = itf ? itf : &hook_ditf_default_itf;
680 hook->closure = closure;
682 /* record the hook */
683 pthread_rwlock_wrlock(&rwlock);
684 hook->next = list_of_ditf_hooks;
685 list_of_ditf_hooks = hook;
686 pthread_rwlock_unlock(&rwlock);
692 struct afb_hook_ditf *afb_hook_addref_ditf(struct afb_hook_ditf *hook)
694 pthread_rwlock_wrlock(&rwlock);
696 pthread_rwlock_unlock(&rwlock);
700 void afb_hook_unref_ditf(struct afb_hook_ditf *hook)
702 struct afb_hook_ditf **prv;
705 pthread_rwlock_wrlock(&rwlock);
706 if (--hook->refcount)
710 prv = &list_of_ditf_hooks;
711 while (*prv && *prv != hook)
716 pthread_rwlock_unlock(&rwlock);