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-common.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 */
82 * Definition of a hook for evt
85 struct afb_hook_evt *next; /**< next hook */
86 unsigned refcount; /**< reference count */
87 char *pattern; /**< event pattern name hooked or NULL for any */
88 unsigned flags; /**< hook flags */
89 struct afb_hook_evt_itf *itf; /**< interface of hook */
90 void *closure; /**< closure for callbacks */
93 /* synchronisation across threads */
94 static pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
96 /* list of hooks for xreq */
97 static struct afb_hook_xreq *list_of_xreq_hooks = NULL;
99 /* list of hooks for ditf */
100 static struct afb_hook_ditf *list_of_ditf_hooks = NULL;
102 /* list of hooks for svc */
103 static struct afb_hook_svc *list_of_svc_hooks = NULL;
105 /* list of hooks for evt */
106 static struct afb_hook_evt *list_of_evt_hooks = NULL;
109 static unsigned next_hookid = 0;
111 /******************************************************************************
113 *****************************************************************************/
114 static inline unsigned new_hookid()
116 return __atomic_add_fetch(&next_hookid, 1, __ATOMIC_RELAXED);
119 /******************************************************************************
120 * section: default callbacks for tracing requests
121 *****************************************************************************/
123 static char *_pbuf_(const char *fmt, va_list args, char **palloc, char *sbuf, size_t szsbuf)
130 rc = vsnprintf(sbuf, szsbuf, fmt, args);
131 if ((size_t)rc >= szsbuf) {
133 sbuf[szsbuf-2] = sbuf[szsbuf-3] = sbuf[szsbuf-4] = '.';
134 rc = vasprintf(palloc, fmt, cp);
142 static void _hook_(const char *fmt1, const char *fmt2, va_list arg2, ...)
144 char *tag, *data, *mem1, *mem2, buf1[256], buf2[2000];
147 data = _pbuf_(fmt2, arg2, &mem2, buf2, sizeof buf2);
149 va_start(arg1, arg2);
150 tag = _pbuf_(fmt1, arg1, &mem1, buf1, sizeof buf1);
153 NOTICE("[HOOK %s] %s", tag, data);
159 static void _hook_xreq_(const struct afb_xreq *xreq, const char *format, ...)
162 va_start(ap, format);
163 _hook_("xreq-%06d:%s/%s", format, ap, xreq->hookindex, xreq->api, xreq->verb);
167 static void hook_xreq_begin_default_cb(void *closure, unsigned hookid, const struct afb_xreq *xreq)
170 _hook_xreq_(xreq, "BEGIN");
172 _hook_xreq_(xreq, "BEGIN uid=%d=%s gid=%d pid=%d label=%s id=%s",
173 (int)xreq->cred->uid,
175 (int)xreq->cred->gid,
176 (int)xreq->cred->pid,
177 xreq->cred->label?:"(null)",
178 xreq->cred->id?:"(null)"
182 static void hook_xreq_end_default_cb(void *closure, unsigned hookid, const struct afb_xreq *xreq)
184 _hook_xreq_(xreq, "END");
187 static void hook_xreq_json_default_cb(void *closure, unsigned hookid, const struct afb_xreq *xreq, struct json_object *obj)
189 _hook_xreq_(xreq, "json() -> %s", json_object_to_json_string(obj));
192 static void hook_xreq_get_default_cb(void *closure, unsigned hookid, const struct afb_xreq *xreq, const char *name, struct afb_arg arg)
194 _hook_xreq_(xreq, "get(%s) -> { name: %s, value: %s, path: %s }", name, arg.name, arg.value, arg.path);
197 static void hook_xreq_success_default_cb(void *closure, unsigned hookid, const struct afb_xreq *xreq, struct json_object *obj, const char *info)
199 _hook_xreq_(xreq, "success(%s, %s)", json_object_to_json_string(obj), info);
202 static void hook_xreq_fail_default_cb(void *closure, unsigned hookid, const struct afb_xreq *xreq, const char *status, const char *info)
204 _hook_xreq_(xreq, "fail(%s, %s)", status, info);
207 static void hook_xreq_context_get_default_cb(void *closure, unsigned hookid, const struct afb_xreq *xreq, void *value)
209 _hook_xreq_(xreq, "context_get() -> %p", value);
212 static void hook_xreq_context_set_default_cb(void *closure, unsigned hookid, const struct afb_xreq *xreq, void *value, void (*free_value)(void*))
214 _hook_xreq_(xreq, "context_set(%p, %p)", value, free_value);
217 static void hook_xreq_addref_default_cb(void *closure, unsigned hookid, const struct afb_xreq *xreq)
219 _hook_xreq_(xreq, "addref()");
222 static void hook_xreq_unref_default_cb(void *closure, unsigned hookid, const struct afb_xreq *xreq)
224 _hook_xreq_(xreq, "unref()");
227 static void hook_xreq_session_close_default_cb(void *closure, unsigned hookid, const struct afb_xreq *xreq)
229 _hook_xreq_(xreq, "session_close()");
232 static void hook_xreq_session_set_LOA_default_cb(void *closure, unsigned hookid, const struct afb_xreq *xreq, unsigned level, int result)
234 _hook_xreq_(xreq, "session_set_LOA(%u) -> %d", level, result);
237 static void hook_xreq_subscribe_default_cb(void *closure, unsigned hookid, const struct afb_xreq *xreq, struct afb_event event, int result)
239 _hook_xreq_(xreq, "subscribe(%s:%d) -> %d", afb_evt_event_name(event), afb_evt_event_id(event), result);
242 static void hook_xreq_unsubscribe_default_cb(void *closure, unsigned hookid, const struct afb_xreq *xreq, struct afb_event event, int result)
244 _hook_xreq_(xreq, "unsubscribe(%s:%d) -> %d", afb_evt_event_name(event), afb_evt_event_id(event), result);
247 static void hook_xreq_subcall_default_cb(void *closure, unsigned hookid, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
249 _hook_xreq_(xreq, "subcall(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
252 static void hook_xreq_subcall_result_default_cb(void *closure, unsigned hookid, const struct afb_xreq *xreq, int status, struct json_object *result)
254 _hook_xreq_(xreq, " ...subcall... -> %d: %s", status, json_object_to_json_string(result));
257 static void hook_xreq_subcallsync_default_cb(void *closure, unsigned hookid, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
259 _hook_xreq_(xreq, "subcallsync(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
262 static void hook_xreq_subcallsync_result_default_cb(void *closure, unsigned hookid, const struct afb_xreq *xreq, int status, struct json_object *result)
264 _hook_xreq_(xreq, " ...subcallsync... -> %d: %s", status, json_object_to_json_string(result));
267 static void hook_xreq_vverbose_default_cb(void *closure, unsigned hookid, const struct afb_xreq *xreq, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
274 len = vasprintf(&msg, fmt, ap);
278 _hook_xreq_(xreq, "vverbose(%d, %s, %d, %s) -> %s ? ? ?", level, file, line, func, fmt);
280 _hook_xreq_(xreq, "vverbose(%d, %s, %d, %s) -> %s", level, file, line, func, msg);
285 static void hook_xreq_store_default_cb(void *closure, unsigned hookid, const struct afb_xreq *xreq, struct afb_stored_req *sreq)
287 _hook_xreq_(xreq, "store() -> %p", sreq);
290 static void hook_xreq_unstore_default_cb(void *closure, unsigned hookid, const struct afb_xreq *xreq)
292 _hook_xreq_(xreq, "unstore()");
295 static void hook_xreq_subcall_req_default_cb(void *closure, unsigned hookid, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
297 _hook_xreq_(xreq, "subcall_req(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
300 static void hook_xreq_subcall_req_result_default_cb(void *closure, unsigned hookid, const struct afb_xreq *xreq, int status, struct json_object *result)
302 _hook_xreq_(xreq, " ...subcall_req... -> %d: %s", status, json_object_to_json_string(result));
305 static struct afb_hook_xreq_itf hook_xreq_default_itf = {
306 .hook_xreq_begin = hook_xreq_begin_default_cb,
307 .hook_xreq_end = hook_xreq_end_default_cb,
308 .hook_xreq_json = hook_xreq_json_default_cb,
309 .hook_xreq_get = hook_xreq_get_default_cb,
310 .hook_xreq_success = hook_xreq_success_default_cb,
311 .hook_xreq_fail = hook_xreq_fail_default_cb,
312 .hook_xreq_context_get = hook_xreq_context_get_default_cb,
313 .hook_xreq_context_set = hook_xreq_context_set_default_cb,
314 .hook_xreq_addref = hook_xreq_addref_default_cb,
315 .hook_xreq_unref = hook_xreq_unref_default_cb,
316 .hook_xreq_session_close = hook_xreq_session_close_default_cb,
317 .hook_xreq_session_set_LOA = hook_xreq_session_set_LOA_default_cb,
318 .hook_xreq_subscribe = hook_xreq_subscribe_default_cb,
319 .hook_xreq_unsubscribe = hook_xreq_unsubscribe_default_cb,
320 .hook_xreq_subcall = hook_xreq_subcall_default_cb,
321 .hook_xreq_subcall_result = hook_xreq_subcall_result_default_cb,
322 .hook_xreq_subcallsync = hook_xreq_subcallsync_default_cb,
323 .hook_xreq_subcallsync_result = hook_xreq_subcallsync_result_default_cb,
324 .hook_xreq_vverbose = hook_xreq_vverbose_default_cb,
325 .hook_xreq_store = hook_xreq_store_default_cb,
326 .hook_xreq_unstore = hook_xreq_unstore_default_cb,
327 .hook_xreq_subcall_req = hook_xreq_subcall_req_default_cb,
328 .hook_xreq_subcall_req_result = hook_xreq_subcall_req_result_default_cb
331 /******************************************************************************
332 * section: hooks for tracing requests
333 *****************************************************************************/
335 #define _HOOK_XREQ_(what,...) \
336 struct afb_hook_xreq *hook; \
337 unsigned hookid = new_hookid(); \
338 pthread_rwlock_rdlock(&rwlock); \
339 hook = list_of_xreq_hooks; \
341 if (hook->itf->hook_xreq_##what \
342 && (hook->flags & afb_hook_flag_req_##what) != 0 \
343 && (!hook->session || hook->session == xreq->context.session) \
344 && (!hook->api || !strcasecmp(hook->api, xreq->api)) \
345 && (!hook->verb || !strcasecmp(hook->verb, xreq->verb))) { \
346 hook->itf->hook_xreq_##what(hook->closure, hookid, __VA_ARGS__); \
350 pthread_rwlock_unlock(&rwlock);
353 void afb_hook_xreq_begin(const struct afb_xreq *xreq)
355 _HOOK_XREQ_(begin, xreq);
358 void afb_hook_xreq_end(const struct afb_xreq *xreq)
360 _HOOK_XREQ_(end, xreq);
363 struct json_object *afb_hook_xreq_json(const struct afb_xreq *xreq, struct json_object *obj)
365 _HOOK_XREQ_(json, xreq, obj);
369 struct afb_arg afb_hook_xreq_get(const struct afb_xreq *xreq, const char *name, struct afb_arg arg)
371 _HOOK_XREQ_(get, xreq, name, arg);
375 void afb_hook_xreq_success(const struct afb_xreq *xreq, struct json_object *obj, const char *info)
377 _HOOK_XREQ_(success, xreq, obj, info);
380 void afb_hook_xreq_fail(const struct afb_xreq *xreq, const char *status, const char *info)
382 _HOOK_XREQ_(fail, xreq, status, info);
385 void *afb_hook_xreq_context_get(const struct afb_xreq *xreq, void *value)
387 _HOOK_XREQ_(context_get, xreq, value);
391 void afb_hook_xreq_context_set(const struct afb_xreq *xreq, void *value, void (*free_value)(void*))
393 _HOOK_XREQ_(context_set, xreq, value, free_value);
396 void afb_hook_xreq_addref(const struct afb_xreq *xreq)
398 _HOOK_XREQ_(addref, xreq);
401 void afb_hook_xreq_unref(const struct afb_xreq *xreq)
403 _HOOK_XREQ_(unref, xreq);
406 void afb_hook_xreq_session_close(const struct afb_xreq *xreq)
408 _HOOK_XREQ_(session_close, xreq);
411 int afb_hook_xreq_session_set_LOA(const struct afb_xreq *xreq, unsigned level, int result)
413 _HOOK_XREQ_(session_set_LOA, xreq, level, result);
417 int afb_hook_xreq_subscribe(const struct afb_xreq *xreq, struct afb_event event, int result)
419 _HOOK_XREQ_(subscribe, xreq, event, result);
423 int afb_hook_xreq_unsubscribe(const struct afb_xreq *xreq, struct afb_event event, int result)
425 _HOOK_XREQ_(unsubscribe, xreq, event, result);
429 void afb_hook_xreq_subcall(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
431 _HOOK_XREQ_(subcall, xreq, api, verb, args);
434 void afb_hook_xreq_subcall_result(const struct afb_xreq *xreq, int status, struct json_object *result)
436 _HOOK_XREQ_(subcall_result, xreq, status, result);
439 void afb_hook_xreq_subcallsync(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
441 _HOOK_XREQ_(subcallsync, xreq, api, verb, args);
444 int afb_hook_xreq_subcallsync_result(const struct afb_xreq *xreq, int status, struct json_object *result)
446 _HOOK_XREQ_(subcallsync_result, xreq, status, result);
450 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)
452 _HOOK_XREQ_(vverbose, xreq, level, file ?: "?", line, func ?: "?", fmt, args);
455 void afb_hook_xreq_store(const struct afb_xreq *xreq, struct afb_stored_req *sreq)
457 _HOOK_XREQ_(store, xreq, sreq);
460 void afb_hook_xreq_unstore(const struct afb_xreq *xreq)
462 _HOOK_XREQ_(unstore, xreq);
465 void afb_hook_xreq_subcall_req(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
467 _HOOK_XREQ_(subcall_req, xreq, api, verb, args);
470 void afb_hook_xreq_subcall_req_result(const struct afb_xreq *xreq, int status, struct json_object *result)
472 _HOOK_XREQ_(subcall_req_result, xreq, status, result);
475 /******************************************************************************
476 * section: hooking xreqs
477 *****************************************************************************/
479 void afb_hook_init_xreq(struct afb_xreq *xreq)
485 struct afb_hook_xreq *hook;
487 /* scan hook list to get the expected flags */
489 pthread_rwlock_rdlock(&rwlock);
490 hook = list_of_xreq_hooks;
492 f = hook->flags & afb_hook_flags_req_all;
494 && (!hook->session || hook->session == xreq->context.session)
495 && (!hook->api || !strcasecmp(hook->api, xreq->api))
496 && (!hook->verb || !strcasecmp(hook->verb, xreq->verb));
501 pthread_rwlock_unlock(&rwlock);
503 /* store the hooking data */
504 xreq->hookflags = flags;
506 pthread_rwlock_wrlock(&rwlock);
509 xreq->hookindex = reqindex;
510 pthread_rwlock_unlock(&rwlock);
514 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)
516 struct afb_hook_xreq *hook;
518 /* alloc the result */
519 hook = calloc(1, sizeof *hook);
523 /* get a copy of the names */
524 hook->api = api ? strdup(api) : NULL;
525 hook->verb = verb ? strdup(verb) : NULL;
526 if ((api && !hook->api) || (verb && !hook->verb)) {
533 /* initialise the rest */
534 hook->session = session;
536 afb_session_addref(session);
539 hook->itf = itf ? itf : &hook_xreq_default_itf;
540 hook->closure = closure;
542 /* record the hook */
543 pthread_rwlock_wrlock(&rwlock);
544 hook->next = list_of_xreq_hooks;
545 list_of_xreq_hooks = hook;
546 pthread_rwlock_unlock(&rwlock);
552 struct afb_hook_xreq *afb_hook_addref_xreq(struct afb_hook_xreq *hook)
554 pthread_rwlock_wrlock(&rwlock);
556 pthread_rwlock_unlock(&rwlock);
560 void afb_hook_unref_xreq(struct afb_hook_xreq *hook)
562 struct afb_hook_xreq **prv;
565 pthread_rwlock_wrlock(&rwlock);
566 if (--hook->refcount)
570 prv = &list_of_xreq_hooks;
571 while (*prv && *prv != hook)
576 pthread_rwlock_unlock(&rwlock);
582 afb_session_unref(hook->session);
588 /******************************************************************************
589 * section: default callbacks for tracing daemon interface
590 *****************************************************************************/
592 static void _hook_ditf_(const struct afb_ditf *ditf, const char *format, ...)
595 va_start(ap, format);
596 _hook_("ditf-%s", format, ap, ditf->api);
600 static void hook_ditf_event_broadcast_before_cb(void *closure, unsigned hookid, const struct afb_ditf *ditf, const char *name, struct json_object *object)
602 _hook_ditf_(ditf, "event_broadcast.before(%s, %s)....", name, json_object_to_json_string(object));
605 static void hook_ditf_event_broadcast_after_cb(void *closure, unsigned hookid, const struct afb_ditf *ditf, const char *name, struct json_object *object, int result)
607 _hook_ditf_(ditf, "event_broadcast.after(%s, %s) -> %d", name, json_object_to_json_string(object), result);
610 static void hook_ditf_get_event_loop_cb(void *closure, unsigned hookid, const struct afb_ditf *ditf, struct sd_event *result)
612 _hook_ditf_(ditf, "get_event_loop() -> %p", result);
615 static void hook_ditf_get_user_bus_cb(void *closure, unsigned hookid, const struct afb_ditf *ditf, struct sd_bus *result)
617 _hook_ditf_(ditf, "get_user_bus() -> %p", result);
620 static void hook_ditf_get_system_bus_cb(void *closure, unsigned hookid, const struct afb_ditf *ditf, struct sd_bus *result)
622 _hook_ditf_(ditf, "get_system_bus() -> %p", result);
625 static void hook_ditf_vverbose_cb(void *closure, unsigned hookid, const struct afb_ditf *ditf, int level, const char *file, int line, const char *function, const char *fmt, va_list args)
632 len = vasprintf(&msg, fmt, ap);
636 _hook_ditf_(ditf, "vverbose(%d, %s, %d, %s) -> %s ? ? ?", level, file, line, function, fmt);
638 _hook_ditf_(ditf, "vverbose(%d, %s, %d, %s) -> %s", level, file, line, function, msg);
643 static void hook_ditf_event_make_cb(void *closure, unsigned hookid, const struct afb_ditf *ditf, const char *name, struct afb_event result)
645 _hook_ditf_(ditf, "event_make(%s) -> %s:%d", name, afb_evt_event_name(result), afb_evt_event_id(result));
648 static void hook_ditf_rootdir_get_fd_cb(void *closure, unsigned hookid, const struct afb_ditf *ditf, int result)
652 _hook_ditf_(ditf, "rootdir_get_fd() -> %d, %m", result);
654 sprintf(path, "/proc/self/fd/%d", result);
655 readlink(path, path, sizeof path);
656 _hook_ditf_(ditf, "rootdir_get_fd() -> %d = %s", result, path);
660 static void hook_ditf_rootdir_open_locale_cb(void *closure, unsigned hookid, const struct afb_ditf *ditf, const char *filename, int flags, const char *locale, int result)
666 _hook_ditf_(ditf, "rootdir_open_locale(%s, %d, %s) -> %d, %m", filename, flags, locale, result);
668 sprintf(path, "/proc/self/fd/%d", result);
669 readlink(path, path, sizeof path);
670 _hook_ditf_(ditf, "rootdir_open_locale(%s, %d, %s) -> %d = %s", filename, flags, locale, result, path);
674 static void hook_ditf_queue_job_cb(void *closure, unsigned hookid, const struct afb_ditf *ditf, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout, int result)
676 _hook_ditf_(ditf, "queue_job(%p, %p, %p, %d) -> %d", callback, argument, group, timeout, result);
679 static void hook_ditf_unstore_req_cb(void *closure, unsigned hookid, const struct afb_ditf *ditf, struct afb_stored_req *sreq)
681 _hook_ditf_(ditf, "unstore_req(%p)", sreq);
684 static void hook_ditf_require_api_cb(void *closure, unsigned hookid, const struct afb_ditf *ditf, const char *name, int initialized)
686 _hook_ditf_(ditf, "require_api(%s, %d)...", name, initialized);
689 static void hook_ditf_require_api_result_cb(void *closure, unsigned hookid, const struct afb_ditf *ditf, const char *name, int initialized, int result)
691 _hook_ditf_(ditf, "...require_api(%s, %d) -> %d", name, initialized, result);
694 static struct afb_hook_ditf_itf hook_ditf_default_itf = {
695 .hook_ditf_event_broadcast_before = hook_ditf_event_broadcast_before_cb,
696 .hook_ditf_event_broadcast_after = hook_ditf_event_broadcast_after_cb,
697 .hook_ditf_get_event_loop = hook_ditf_get_event_loop_cb,
698 .hook_ditf_get_user_bus = hook_ditf_get_user_bus_cb,
699 .hook_ditf_get_system_bus = hook_ditf_get_system_bus_cb,
700 .hook_ditf_vverbose = hook_ditf_vverbose_cb,
701 .hook_ditf_event_make = hook_ditf_event_make_cb,
702 .hook_ditf_rootdir_get_fd = hook_ditf_rootdir_get_fd_cb,
703 .hook_ditf_rootdir_open_locale = hook_ditf_rootdir_open_locale_cb,
704 .hook_ditf_queue_job = hook_ditf_queue_job_cb,
705 .hook_ditf_unstore_req = hook_ditf_unstore_req_cb,
706 .hook_ditf_require_api = hook_ditf_require_api_cb,
707 .hook_ditf_require_api_result = hook_ditf_require_api_result_cb
710 /******************************************************************************
711 * section: hooks for tracing daemon interface (ditf)
712 *****************************************************************************/
714 #define _HOOK_DITF_(what,...) \
715 struct afb_hook_ditf *hook; \
716 unsigned hookid = new_hookid(); \
717 pthread_rwlock_rdlock(&rwlock); \
718 hook = list_of_ditf_hooks; \
720 if (hook->itf->hook_ditf_##what \
721 && (hook->flags & afb_hook_flag_ditf_##what) != 0 \
722 && (!hook->api || !strcasecmp(hook->api, ditf->api))) { \
723 hook->itf->hook_ditf_##what(hook->closure, hookid, __VA_ARGS__); \
727 pthread_rwlock_unlock(&rwlock);
729 void afb_hook_ditf_event_broadcast_before(const struct afb_ditf *ditf, const char *name, struct json_object *object)
731 _HOOK_DITF_(event_broadcast_before, ditf, name, object);
734 int afb_hook_ditf_event_broadcast_after(const struct afb_ditf *ditf, const char *name, struct json_object *object, int result)
736 _HOOK_DITF_(event_broadcast_after, ditf, name, object, result);
740 struct sd_event *afb_hook_ditf_get_event_loop(const struct afb_ditf *ditf, struct sd_event *result)
742 _HOOK_DITF_(get_event_loop, ditf, result);
746 struct sd_bus *afb_hook_ditf_get_user_bus(const struct afb_ditf *ditf, struct sd_bus *result)
748 _HOOK_DITF_(get_user_bus, ditf, result);
752 struct sd_bus *afb_hook_ditf_get_system_bus(const struct afb_ditf *ditf, struct sd_bus *result)
754 _HOOK_DITF_(get_system_bus, ditf, result);
758 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)
760 _HOOK_DITF_(vverbose, ditf, level, file, line, function, fmt, args);
763 struct afb_event afb_hook_ditf_event_make(const struct afb_ditf *ditf, const char *name, struct afb_event result)
765 _HOOK_DITF_(event_make, ditf, name, result);
769 int afb_hook_ditf_rootdir_get_fd(const struct afb_ditf *ditf, int result)
771 _HOOK_DITF_(rootdir_get_fd, ditf, result);
775 int afb_hook_ditf_rootdir_open_locale(const struct afb_ditf *ditf, const char *filename, int flags, const char *locale, int result)
777 _HOOK_DITF_(rootdir_open_locale, ditf, filename, flags, locale, result);
781 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)
783 _HOOK_DITF_(queue_job, ditf, callback, argument, group, timeout, result);
787 void afb_hook_ditf_unstore_req(const struct afb_ditf *ditf, struct afb_stored_req *sreq)
789 _HOOK_DITF_(unstore_req, ditf, sreq);
792 void afb_hook_ditf_require_api(const struct afb_ditf *ditf, const char *name, int initialized)
794 _HOOK_DITF_(require_api, ditf, name, initialized);
797 int afb_hook_ditf_require_api_result(const struct afb_ditf *ditf, const char *name, int initialized, int result)
799 _HOOK_DITF_(require_api_result, ditf, name, initialized, result);
803 /******************************************************************************
804 * section: hooking ditf
805 *****************************************************************************/
807 int afb_hook_flags_ditf(const char *api)
810 struct afb_hook_ditf *hook;
812 pthread_rwlock_rdlock(&rwlock);
814 hook = list_of_ditf_hooks;
816 if (!api || !hook->api || !strcasecmp(hook->api, api))
817 flags |= hook->flags;
820 pthread_rwlock_unlock(&rwlock);
824 struct afb_hook_ditf *afb_hook_create_ditf(const char *api, int flags, struct afb_hook_ditf_itf *itf, void *closure)
826 struct afb_hook_ditf *hook;
828 /* alloc the result */
829 hook = calloc(1, sizeof *hook);
833 /* get a copy of the names */
834 hook->api = api ? strdup(api) : NULL;
835 if (api && !hook->api) {
840 /* initialise the rest */
843 hook->itf = itf ? itf : &hook_ditf_default_itf;
844 hook->closure = closure;
846 /* record the hook */
847 pthread_rwlock_wrlock(&rwlock);
848 hook->next = list_of_ditf_hooks;
849 list_of_ditf_hooks = hook;
850 pthread_rwlock_unlock(&rwlock);
856 struct afb_hook_ditf *afb_hook_addref_ditf(struct afb_hook_ditf *hook)
858 pthread_rwlock_wrlock(&rwlock);
860 pthread_rwlock_unlock(&rwlock);
864 void afb_hook_unref_ditf(struct afb_hook_ditf *hook)
866 struct afb_hook_ditf **prv;
869 pthread_rwlock_wrlock(&rwlock);
870 if (--hook->refcount)
874 prv = &list_of_ditf_hooks;
875 while (*prv && *prv != hook)
880 pthread_rwlock_unlock(&rwlock);
889 /******************************************************************************
890 * section: default callbacks for tracing service interface (svc)
891 *****************************************************************************/
893 static void _hook_svc_(const struct afb_svc *svc, const char *format, ...)
896 va_start(ap, format);
897 _hook_("svc-%s", format, ap, svc->api);
901 static void hook_svc_start_before_default_cb(void *closure, unsigned hookid, const struct afb_svc *svc)
903 _hook_svc_(svc, "start.before");
906 static void hook_svc_start_after_default_cb(void *closure, unsigned hookid, const struct afb_svc *svc, int status)
908 _hook_svc_(svc, "start.after -> %d", status);
911 static void hook_svc_on_event_before_default_cb(void *closure, unsigned hookid, const struct afb_svc *svc, const char *event, int eventid, struct json_object *object)
913 _hook_svc_(svc, "on_event.before(%s, %d, %s)", event, eventid, json_object_to_json_string(object));
916 static void hook_svc_on_event_after_default_cb(void *closure, unsigned hookid, const struct afb_svc *svc, const char *event, int eventid, struct json_object *object)
918 _hook_svc_(svc, "on_event.after(%s, %d, %s)", event, eventid, json_object_to_json_string(object));
921 static void hook_svc_call_default_cb(void *closure, unsigned hookid, const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args)
923 _hook_svc_(svc, "call(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
926 static void hook_svc_call_result_default_cb(void *closure, unsigned hookid, const struct afb_svc *svc, int status, struct json_object *result)
928 _hook_svc_(svc, " ...call... -> %d: %s", status, json_object_to_json_string(result));
931 static void hook_svc_callsync_default_cb(void *closure, unsigned hookid, const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args)
933 _hook_svc_(svc, "callsync(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
936 static void hook_svc_callsync_result_default_cb(void *closure, unsigned hookid, const struct afb_svc *svc, int status, struct json_object *result)
938 _hook_svc_(svc, " ...callsync... -> %d: %s", status, json_object_to_json_string(result));
941 static struct afb_hook_svc_itf hook_svc_default_itf = {
942 .hook_svc_start_before = hook_svc_start_before_default_cb,
943 .hook_svc_start_after = hook_svc_start_after_default_cb,
944 .hook_svc_on_event_before = hook_svc_on_event_before_default_cb,
945 .hook_svc_on_event_after = hook_svc_on_event_after_default_cb,
946 .hook_svc_call = hook_svc_call_default_cb,
947 .hook_svc_call_result = hook_svc_call_result_default_cb,
948 .hook_svc_callsync = hook_svc_callsync_default_cb,
949 .hook_svc_callsync_result = hook_svc_callsync_result_default_cb
952 /******************************************************************************
953 * section: hooks for tracing service interface (svc)
954 *****************************************************************************/
956 #define _HOOK_SVC_(what,...) \
957 struct afb_hook_svc *hook; \
958 unsigned hookid = new_hookid(); \
959 pthread_rwlock_rdlock(&rwlock); \
960 hook = list_of_svc_hooks; \
962 if (hook->itf->hook_svc_##what \
963 && (hook->flags & afb_hook_flag_svc_##what) != 0 \
964 && (!hook->api || !strcasecmp(hook->api, svc->api))) { \
965 hook->itf->hook_svc_##what(hook->closure, hookid, __VA_ARGS__); \
969 pthread_rwlock_unlock(&rwlock);
971 void afb_hook_svc_start_before(const struct afb_svc *svc)
973 _HOOK_SVC_(start_before, svc);
976 int afb_hook_svc_start_after(const struct afb_svc *svc, int status)
978 _HOOK_SVC_(start_after, svc, status);
982 void afb_hook_svc_on_event_before(const struct afb_svc *svc, const char *event, int eventid, struct json_object *object)
984 _HOOK_SVC_(on_event_before, svc, event, eventid, object);
987 void afb_hook_svc_on_event_after(const struct afb_svc *svc, const char *event, int eventid, struct json_object *object)
989 _HOOK_SVC_(on_event_after, svc, event, eventid, object);
992 void afb_hook_svc_call(const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args)
994 _HOOK_SVC_(call, svc, api, verb, args);
997 void afb_hook_svc_call_result(const struct afb_svc *svc, int status, struct json_object *result)
999 _HOOK_SVC_(call_result, svc, status, result);
1002 void afb_hook_svc_callsync(const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args)
1004 _HOOK_SVC_(callsync, svc, api, verb, args);
1007 int afb_hook_svc_callsync_result(const struct afb_svc *svc, int status, struct json_object *result)
1009 _HOOK_SVC_(callsync_result, svc, status, result);
1013 /******************************************************************************
1014 * section: hooking services (svc)
1015 *****************************************************************************/
1017 int afb_hook_flags_svc(const char *api)
1020 struct afb_hook_svc *hook;
1022 pthread_rwlock_rdlock(&rwlock);
1024 hook = list_of_svc_hooks;
1026 if (!api || !hook->api || !strcasecmp(hook->api, api))
1027 flags |= hook->flags;
1030 pthread_rwlock_unlock(&rwlock);
1034 struct afb_hook_svc *afb_hook_create_svc(const char *api, int flags, struct afb_hook_svc_itf *itf, void *closure)
1036 struct afb_hook_svc *hook;
1038 /* alloc the result */
1039 hook = calloc(1, sizeof *hook);
1043 /* get a copy of the names */
1044 hook->api = api ? strdup(api) : NULL;
1045 if (api && !hook->api) {
1050 /* initialise the rest */
1052 hook->flags = flags;
1053 hook->itf = itf ? itf : &hook_svc_default_itf;
1054 hook->closure = closure;
1056 /* record the hook */
1057 pthread_rwlock_wrlock(&rwlock);
1058 hook->next = list_of_svc_hooks;
1059 list_of_svc_hooks = hook;
1060 pthread_rwlock_unlock(&rwlock);
1066 struct afb_hook_svc *afb_hook_addref_svc(struct afb_hook_svc *hook)
1068 pthread_rwlock_wrlock(&rwlock);
1070 pthread_rwlock_unlock(&rwlock);
1074 void afb_hook_unref_svc(struct afb_hook_svc *hook)
1076 struct afb_hook_svc **prv;
1079 pthread_rwlock_wrlock(&rwlock);
1080 if (--hook->refcount)
1084 prv = &list_of_svc_hooks;
1085 while (*prv && *prv != hook)
1086 prv = &(*prv)->next;
1090 pthread_rwlock_unlock(&rwlock);
1099 /******************************************************************************
1100 * section: default callbacks for tracing service interface (evt)
1101 *****************************************************************************/
1103 static void _hook_evt_(const char *evt, int id, const char *format, ...)
1106 va_start(ap, format);
1107 _hook_("evt-%s:%d", format, ap, evt, id);
1111 static void hook_evt_create_default_cb(void *closure, unsigned hookid, const char *evt, int id)
1113 _hook_evt_(evt, id, "create");
1116 static void hook_evt_push_before_default_cb(void *closure, unsigned hookid, const char *evt, int id, struct json_object *obj)
1118 _hook_evt_(evt, id, "push.before(%s)", json_object_to_json_string(obj));
1122 static void hook_evt_push_after_default_cb(void *closure, unsigned hookid, const char *evt, int id, struct json_object *obj, int result)
1124 _hook_evt_(evt, id, "push.after(%s) -> %d", json_object_to_json_string(obj), result);
1127 static void hook_evt_broadcast_before_default_cb(void *closure, unsigned hookid, const char *evt, int id, struct json_object *obj)
1129 _hook_evt_(evt, id, "broadcast.before(%s)", json_object_to_json_string(obj));
1132 static void hook_evt_broadcast_after_default_cb(void *closure, unsigned hookid, const char *evt, int id, struct json_object *obj, int result)
1134 _hook_evt_(evt, id, "broadcast.after(%s) -> %d", json_object_to_json_string(obj), result);
1137 static void hook_evt_name_default_cb(void *closure, unsigned hookid, const char *evt, int id)
1139 _hook_evt_(evt, id, "name");
1142 static void hook_evt_drop_default_cb(void *closure, unsigned hookid, const char *evt, int id)
1144 _hook_evt_(evt, id, "drop");
1147 static struct afb_hook_evt_itf hook_evt_default_itf = {
1148 .hook_evt_create = hook_evt_create_default_cb,
1149 .hook_evt_push_before = hook_evt_push_before_default_cb,
1150 .hook_evt_push_after = hook_evt_push_after_default_cb,
1151 .hook_evt_broadcast_before = hook_evt_broadcast_before_default_cb,
1152 .hook_evt_broadcast_after = hook_evt_broadcast_after_default_cb,
1153 .hook_evt_name = hook_evt_name_default_cb,
1154 .hook_evt_drop = hook_evt_drop_default_cb
1157 /******************************************************************************
1158 * section: hooks for tracing service interface (evt)
1159 *****************************************************************************/
1161 #define _HOOK_EVT_(what,...) \
1162 struct afb_hook_evt *hook; \
1163 unsigned hookid = new_hookid(); \
1164 pthread_rwlock_rdlock(&rwlock); \
1165 hook = list_of_evt_hooks; \
1167 if (hook->itf->hook_evt_##what \
1168 && (hook->flags & afb_hook_flag_evt_##what) != 0 \
1169 && (!hook->pattern || !fnmatch(hook->pattern, evt, FNM_CASEFOLD))) { \
1170 hook->itf->hook_evt_##what(hook->closure, hookid, __VA_ARGS__); \
1172 hook = hook->next; \
1174 pthread_rwlock_unlock(&rwlock);
1176 void afb_hook_evt_create(const char *evt, int id)
1178 _HOOK_EVT_(create, evt, id);
1181 void afb_hook_evt_push_before(const char *evt, int id, struct json_object *obj)
1183 _HOOK_EVT_(push_before, evt, id, obj);
1186 int afb_hook_evt_push_after(const char *evt, int id, struct json_object *obj, int result)
1188 _HOOK_EVT_(push_after, evt, id, obj, result);
1192 void afb_hook_evt_broadcast_before(const char *evt, int id, struct json_object *obj)
1194 _HOOK_EVT_(broadcast_before, evt, id, obj);
1197 int afb_hook_evt_broadcast_after(const char *evt, int id, struct json_object *obj, int result)
1199 _HOOK_EVT_(broadcast_after, evt, id, obj, result);
1203 void afb_hook_evt_name(const char *evt, int id)
1205 _HOOK_EVT_(name, evt, id);
1208 void afb_hook_evt_drop(const char *evt, int id)
1210 _HOOK_EVT_(drop, evt, id);
1213 /******************************************************************************
1214 * section: hooking services (evt)
1215 *****************************************************************************/
1217 int afb_hook_flags_evt(const char *name)
1220 struct afb_hook_evt *hook;
1222 pthread_rwlock_rdlock(&rwlock);
1224 hook = list_of_evt_hooks;
1226 if (!name || !hook->pattern || !fnmatch(hook->pattern, name, FNM_CASEFOLD))
1227 flags |= hook->flags;
1230 pthread_rwlock_unlock(&rwlock);
1234 struct afb_hook_evt *afb_hook_create_evt(const char *pattern, int flags, struct afb_hook_evt_itf *itf, void *closure)
1236 struct afb_hook_evt *hook;
1238 /* alloc the result */
1239 hook = calloc(1, sizeof *hook);
1243 /* get a copy of the names */
1244 hook->pattern = pattern ? strdup(pattern) : NULL;
1245 if (pattern && !hook->pattern) {
1250 /* initialise the rest */
1252 hook->flags = flags;
1253 hook->itf = itf ? itf : &hook_evt_default_itf;
1254 hook->closure = closure;
1256 /* record the hook */
1257 pthread_rwlock_wrlock(&rwlock);
1258 hook->next = list_of_evt_hooks;
1259 list_of_evt_hooks = hook;
1260 pthread_rwlock_unlock(&rwlock);
1266 struct afb_hook_evt *afb_hook_addref_evt(struct afb_hook_evt *hook)
1268 pthread_rwlock_wrlock(&rwlock);
1270 pthread_rwlock_unlock(&rwlock);
1274 void afb_hook_unref_evt(struct afb_hook_evt *hook)
1276 struct afb_hook_evt **prv;
1279 pthread_rwlock_wrlock(&rwlock);
1280 if (--hook->refcount)
1284 prv = &list_of_evt_hooks;
1285 while (*prv && *prv != hook)
1286 prv = &(*prv)->next;
1290 pthread_rwlock_unlock(&rwlock);
1293 free(hook->pattern);