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.
29 #include <json-c/json.h>
31 #include <afb/afb-req-common.h>
32 #include <afb/afb-event-itf.h>
34 #include "afb-context.h"
36 #include "afb-session.h"
39 #include "afb-export.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 export
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 export
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 */
94 * Definition of a hook for global
96 struct afb_hook_global {
97 struct afb_hook_global *next; /**< next hook */
98 unsigned refcount; /**< reference count */
99 unsigned flags; /**< hook flags */
100 struct afb_hook_global_itf *itf; /**< interface of hook */
101 void *closure; /**< closure for callbacks */
104 /* synchronisation across threads */
105 static pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
107 /* list of hooks for xreq */
108 static struct afb_hook_xreq *list_of_xreq_hooks = NULL;
110 /* list of hooks for export */
111 static struct afb_hook_ditf *list_of_ditf_hooks = NULL;
113 /* list of hooks for export */
114 static struct afb_hook_svc *list_of_svc_hooks = NULL;
116 /* list of hooks for evt */
117 static struct afb_hook_evt *list_of_evt_hooks = NULL;
119 /* list of hooks for global */
120 static struct afb_hook_global *list_of_global_hooks = NULL;
123 static unsigned next_hookid = 0;
125 /******************************************************************************
127 *****************************************************************************/
128 static void init_hookid(struct afb_hookid *hookid)
130 hookid->id = __atomic_add_fetch(&next_hookid, 1, __ATOMIC_RELAXED);
131 clock_gettime(CLOCK_MONOTONIC, &hookid->time);
134 /******************************************************************************
135 * section: default callbacks for tracing requests
136 *****************************************************************************/
138 static char *_pbuf_(const char *fmt, va_list args, char **palloc, char *sbuf, size_t szsbuf, size_t *outlen)
145 rc = vsnprintf(sbuf, szsbuf, fmt, args);
146 if ((size_t)rc >= szsbuf) {
148 sbuf[szsbuf-2] = sbuf[szsbuf-3] = sbuf[szsbuf-4] = '.';
149 rc = vasprintf(palloc, fmt, cp);
154 if (rc >= 0 && outlen)
155 *outlen = (size_t)rc;
159 #if 0 /* old behaviour: use NOTICE */
160 static void _hook_(const char *fmt1, const char *fmt2, va_list arg2, ...)
162 char *tag, *data, *mem1, *mem2, buf1[256], buf2[2000];
165 data = _pbuf_(fmt2, arg2, &mem2, buf2, sizeof buf2, NULL);
167 va_start(arg1, arg2);
168 tag = _pbuf_(fmt1, arg1, &mem1, buf1, sizeof buf1, NULL);
171 NOTICE("[HOOK %s] %s", tag, data);
176 #else /* new behaviour: emits directly to stderr */
177 static void _hook_(const char *fmt1, const char *fmt2, va_list arg2, ...)
179 static const char chars[] = "HOOK: [] \n";
180 char *mem1, *mem2, buf1[256], buf2[2000];
184 iov[0].iov_base = (void*)&chars[0];
187 va_start(arg1, arg2);
188 iov[1].iov_base = _pbuf_(fmt1, arg1, &mem1, buf1, sizeof buf1, &iov[1].iov_len);
191 iov[2].iov_base = (void*)&chars[7];
194 iov[3].iov_base = _pbuf_(fmt2, arg2, &mem2, buf2, sizeof buf2, &iov[3].iov_len);
196 iov[4].iov_base = (void*)&chars[9];
206 static void _hook_xreq_(const struct afb_xreq *xreq, const char *format, ...)
209 va_start(ap, format);
210 _hook_("xreq-%06d:%s/%s", format, ap, xreq->hookindex, xreq->api, xreq->verb);
214 static void hook_xreq_begin_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
217 _hook_xreq_(xreq, "BEGIN");
219 _hook_xreq_(xreq, "BEGIN uid=%d=%s gid=%d pid=%d label=%s id=%s",
220 (int)xreq->cred->uid,
222 (int)xreq->cred->gid,
223 (int)xreq->cred->pid,
224 xreq->cred->label?:"(null)",
225 xreq->cred->id?:"(null)"
229 static void hook_xreq_end_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
231 _hook_xreq_(xreq, "END");
234 static void hook_xreq_json_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *obj)
236 _hook_xreq_(xreq, "json() -> %s", json_object_to_json_string(obj));
239 static void hook_xreq_get_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *name, struct afb_arg arg)
241 _hook_xreq_(xreq, "get(%s) -> { name: %s, value: %s, path: %s }", name, arg.name, arg.value, arg.path);
244 static void hook_xreq_success_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *obj, const char *info)
246 _hook_xreq_(xreq, "success(%s, %s)", json_object_to_json_string(obj), info);
249 static void hook_xreq_fail_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *status, const char *info)
251 _hook_xreq_(xreq, "fail(%s, %s)", status, info);
254 static void hook_xreq_context_get_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, void *value)
256 _hook_xreq_(xreq, "context_get() -> %p", value);
259 static void hook_xreq_context_set_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, void *value, void (*free_value)(void*))
261 _hook_xreq_(xreq, "context_set(%p, %p)", value, free_value);
264 static void hook_xreq_addref_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
266 _hook_xreq_(xreq, "addref()");
269 static void hook_xreq_unref_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
271 _hook_xreq_(xreq, "unref()");
274 static void hook_xreq_session_close_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
276 _hook_xreq_(xreq, "session_close()");
279 static void hook_xreq_session_set_LOA_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, unsigned level, int result)
281 _hook_xreq_(xreq, "session_set_LOA(%u) -> %d", level, result);
284 static void hook_xreq_subscribe_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_event event, int result)
286 _hook_xreq_(xreq, "subscribe(%s:%d) -> %d", afb_evt_event_name(event), afb_evt_event_id(event), result);
289 static void hook_xreq_unsubscribe_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_event event, int result)
291 _hook_xreq_(xreq, "unsubscribe(%s:%d) -> %d", afb_evt_event_name(event), afb_evt_event_id(event), result);
294 static void hook_xreq_subcall_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
296 _hook_xreq_(xreq, "subcall(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
299 static void hook_xreq_subcall_result_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int status, struct json_object *result)
301 _hook_xreq_(xreq, " ...subcall... -> %d: %s", status, json_object_to_json_string(result));
304 static void hook_xreq_subcallsync_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
306 _hook_xreq_(xreq, "subcallsync(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
309 static void hook_xreq_subcallsync_result_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int status, struct json_object *result)
311 _hook_xreq_(xreq, " ...subcallsync... -> %d: %s", status, json_object_to_json_string(result));
314 static void hook_xreq_vverbose_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
321 len = vasprintf(&msg, fmt, ap);
325 _hook_xreq_(xreq, "vverbose(%d, %s, %d, %s) -> %s ? ? ?", level, file, line, func, fmt);
327 _hook_xreq_(xreq, "vverbose(%d, %s, %d, %s) -> %s", level, file, line, func, msg);
332 static void hook_xreq_store_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_stored_req *sreq)
334 _hook_xreq_(xreq, "store() -> %p", sreq);
337 static void hook_xreq_unstore_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
339 _hook_xreq_(xreq, "unstore()");
342 static void hook_xreq_subcall_req_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
344 _hook_xreq_(xreq, "subcall_req(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
347 static void hook_xreq_subcall_req_result_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int status, struct json_object *result)
349 _hook_xreq_(xreq, " ...subcall_req... -> %d: %s", status, json_object_to_json_string(result));
352 static void hook_xreq_has_permission_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *permission, int result)
354 _hook_xreq_(xreq, "has_permission(%s) -> %d", permission, result);
357 static struct afb_hook_xreq_itf hook_xreq_default_itf = {
358 .hook_xreq_begin = hook_xreq_begin_default_cb,
359 .hook_xreq_end = hook_xreq_end_default_cb,
360 .hook_xreq_json = hook_xreq_json_default_cb,
361 .hook_xreq_get = hook_xreq_get_default_cb,
362 .hook_xreq_success = hook_xreq_success_default_cb,
363 .hook_xreq_fail = hook_xreq_fail_default_cb,
364 .hook_xreq_context_get = hook_xreq_context_get_default_cb,
365 .hook_xreq_context_set = hook_xreq_context_set_default_cb,
366 .hook_xreq_addref = hook_xreq_addref_default_cb,
367 .hook_xreq_unref = hook_xreq_unref_default_cb,
368 .hook_xreq_session_close = hook_xreq_session_close_default_cb,
369 .hook_xreq_session_set_LOA = hook_xreq_session_set_LOA_default_cb,
370 .hook_xreq_subscribe = hook_xreq_subscribe_default_cb,
371 .hook_xreq_unsubscribe = hook_xreq_unsubscribe_default_cb,
372 .hook_xreq_subcall = hook_xreq_subcall_default_cb,
373 .hook_xreq_subcall_result = hook_xreq_subcall_result_default_cb,
374 .hook_xreq_subcallsync = hook_xreq_subcallsync_default_cb,
375 .hook_xreq_subcallsync_result = hook_xreq_subcallsync_result_default_cb,
376 .hook_xreq_vverbose = hook_xreq_vverbose_default_cb,
377 .hook_xreq_store = hook_xreq_store_default_cb,
378 .hook_xreq_unstore = hook_xreq_unstore_default_cb,
379 .hook_xreq_subcall_req = hook_xreq_subcall_req_default_cb,
380 .hook_xreq_subcall_req_result = hook_xreq_subcall_req_result_default_cb,
381 .hook_xreq_has_permission = hook_xreq_has_permission_default_cb
384 /******************************************************************************
385 * section: hooks for tracing requests
386 *****************************************************************************/
388 #define _HOOK_XREQ_(what,...) \
389 struct afb_hook_xreq *hook; \
390 struct afb_hookid hookid; \
391 pthread_rwlock_rdlock(&rwlock); \
392 init_hookid(&hookid); \
393 hook = list_of_xreq_hooks; \
395 if (hook->itf->hook_xreq_##what \
396 && (hook->flags & afb_hook_flag_req_##what) != 0 \
397 && (!hook->session || hook->session == xreq->context.session) \
398 && (!hook->api || !strcasecmp(hook->api, xreq->api)) \
399 && (!hook->verb || !strcasecmp(hook->verb, xreq->verb))) { \
400 hook->itf->hook_xreq_##what(hook->closure, &hookid, __VA_ARGS__); \
404 pthread_rwlock_unlock(&rwlock);
407 void afb_hook_xreq_begin(const struct afb_xreq *xreq)
409 _HOOK_XREQ_(begin, xreq);
412 void afb_hook_xreq_end(const struct afb_xreq *xreq)
414 _HOOK_XREQ_(end, xreq);
417 struct json_object *afb_hook_xreq_json(const struct afb_xreq *xreq, struct json_object *obj)
419 _HOOK_XREQ_(json, xreq, obj);
423 struct afb_arg afb_hook_xreq_get(const struct afb_xreq *xreq, const char *name, struct afb_arg arg)
425 _HOOK_XREQ_(get, xreq, name, arg);
429 void afb_hook_xreq_success(const struct afb_xreq *xreq, struct json_object *obj, const char *info)
431 _HOOK_XREQ_(success, xreq, obj, info);
434 void afb_hook_xreq_fail(const struct afb_xreq *xreq, const char *status, const char *info)
436 _HOOK_XREQ_(fail, xreq, status, info);
439 void *afb_hook_xreq_context_get(const struct afb_xreq *xreq, void *value)
441 _HOOK_XREQ_(context_get, xreq, value);
445 void afb_hook_xreq_context_set(const struct afb_xreq *xreq, void *value, void (*free_value)(void*))
447 _HOOK_XREQ_(context_set, xreq, value, free_value);
450 void afb_hook_xreq_addref(const struct afb_xreq *xreq)
452 _HOOK_XREQ_(addref, xreq);
455 void afb_hook_xreq_unref(const struct afb_xreq *xreq)
457 _HOOK_XREQ_(unref, xreq);
460 void afb_hook_xreq_session_close(const struct afb_xreq *xreq)
462 _HOOK_XREQ_(session_close, xreq);
465 int afb_hook_xreq_session_set_LOA(const struct afb_xreq *xreq, unsigned level, int result)
467 _HOOK_XREQ_(session_set_LOA, xreq, level, result);
471 int afb_hook_xreq_subscribe(const struct afb_xreq *xreq, struct afb_event event, int result)
473 _HOOK_XREQ_(subscribe, xreq, event, result);
477 int afb_hook_xreq_unsubscribe(const struct afb_xreq *xreq, struct afb_event event, int result)
479 _HOOK_XREQ_(unsubscribe, xreq, event, result);
483 void afb_hook_xreq_subcall(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
485 _HOOK_XREQ_(subcall, xreq, api, verb, args);
488 void afb_hook_xreq_subcall_result(const struct afb_xreq *xreq, int status, struct json_object *result)
490 _HOOK_XREQ_(subcall_result, xreq, status, result);
493 void afb_hook_xreq_subcallsync(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
495 _HOOK_XREQ_(subcallsync, xreq, api, verb, args);
498 int afb_hook_xreq_subcallsync_result(const struct afb_xreq *xreq, int status, struct json_object *result)
500 _HOOK_XREQ_(subcallsync_result, xreq, status, result);
504 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)
506 _HOOK_XREQ_(vverbose, xreq, level, file ?: "?", line, func ?: "?", fmt, args);
509 void afb_hook_xreq_store(const struct afb_xreq *xreq, struct afb_stored_req *sreq)
511 _HOOK_XREQ_(store, xreq, sreq);
514 void afb_hook_xreq_unstore(const struct afb_xreq *xreq)
516 _HOOK_XREQ_(unstore, xreq);
519 void afb_hook_xreq_subcall_req(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
521 _HOOK_XREQ_(subcall_req, xreq, api, verb, args);
524 void afb_hook_xreq_subcall_req_result(const struct afb_xreq *xreq, int status, struct json_object *result)
526 _HOOK_XREQ_(subcall_req_result, xreq, status, result);
529 int afb_hook_xreq_has_permission(const struct afb_xreq *xreq, const char *permission, int result)
531 _HOOK_XREQ_(has_permission, xreq, permission, result);
535 /******************************************************************************
536 * section: hooking xreqs
537 *****************************************************************************/
539 void afb_hook_init_xreq(struct afb_xreq *xreq)
545 struct afb_hook_xreq *hook;
547 /* scan hook list to get the expected flags */
549 pthread_rwlock_rdlock(&rwlock);
550 hook = list_of_xreq_hooks;
552 f = hook->flags & afb_hook_flags_req_all;
554 && (!hook->session || hook->session == xreq->context.session)
555 && (!hook->api || !strcasecmp(hook->api, xreq->api))
556 && (!hook->verb || !strcasecmp(hook->verb, xreq->verb));
561 pthread_rwlock_unlock(&rwlock);
563 /* store the hooking data */
564 xreq->hookflags = flags;
566 pthread_rwlock_wrlock(&rwlock);
569 xreq->hookindex = reqindex;
570 pthread_rwlock_unlock(&rwlock);
574 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)
576 struct afb_hook_xreq *hook;
578 /* alloc the result */
579 hook = calloc(1, sizeof *hook);
583 /* get a copy of the names */
584 hook->api = api ? strdup(api) : NULL;
585 hook->verb = verb ? strdup(verb) : NULL;
586 if ((api && !hook->api) || (verb && !hook->verb)) {
593 /* initialise the rest */
594 hook->session = session;
596 afb_session_addref(session);
599 hook->itf = itf ? itf : &hook_xreq_default_itf;
600 hook->closure = closure;
602 /* record the hook */
603 pthread_rwlock_wrlock(&rwlock);
604 hook->next = list_of_xreq_hooks;
605 list_of_xreq_hooks = hook;
606 pthread_rwlock_unlock(&rwlock);
612 struct afb_hook_xreq *afb_hook_addref_xreq(struct afb_hook_xreq *hook)
614 pthread_rwlock_wrlock(&rwlock);
616 pthread_rwlock_unlock(&rwlock);
620 void afb_hook_unref_xreq(struct afb_hook_xreq *hook)
622 struct afb_hook_xreq **prv;
625 pthread_rwlock_wrlock(&rwlock);
626 if (--hook->refcount)
630 prv = &list_of_xreq_hooks;
631 while (*prv && *prv != hook)
636 pthread_rwlock_unlock(&rwlock);
642 afb_session_unref(hook->session);
648 /******************************************************************************
649 * section: default callbacks for tracing daemon interface
650 *****************************************************************************/
652 static void _hook_ditf_(const struct afb_export *export, const char *format, ...)
655 va_start(ap, format);
656 _hook_("export-%s", format, ap, afb_export_apiname(export));
660 static void hook_ditf_event_broadcast_before_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, struct json_object *object)
662 _hook_ditf_(export, "event_broadcast.before(%s, %s)....", name, json_object_to_json_string(object));
665 static void hook_ditf_event_broadcast_after_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, struct json_object *object, int result)
667 _hook_ditf_(export, "event_broadcast.after(%s, %s) -> %d", name, json_object_to_json_string(object), result);
670 static void hook_ditf_get_event_loop_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_event *result)
672 _hook_ditf_(export, "get_event_loop() -> %p", result);
675 static void hook_ditf_get_user_bus_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_bus *result)
677 _hook_ditf_(export, "get_user_bus() -> %p", result);
680 static void hook_ditf_get_system_bus_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_bus *result)
682 _hook_ditf_(export, "get_system_bus() -> %p", result);
685 static void hook_ditf_vverbose_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int level, const char *file, int line, const char *function, const char *fmt, va_list args)
692 len = vasprintf(&msg, fmt, ap);
696 _hook_ditf_(export, "vverbose(%d, %s, %d, %s) -> %s ? ? ?", level, file, line, function, fmt);
698 _hook_ditf_(export, "vverbose(%d, %s, %d, %s) -> %s", level, file, line, function, msg);
703 static void hook_ditf_event_make_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, struct afb_event result)
705 _hook_ditf_(export, "event_make(%s) -> %s:%d", name, afb_evt_event_name(result), afb_evt_event_id(result));
708 static void hook_ditf_rootdir_get_fd_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result)
712 _hook_ditf_(export, "rootdir_get_fd() -> %d, %m", result);
714 sprintf(path, "/proc/self/fd/%d", result);
715 readlink(path, path, sizeof path);
716 _hook_ditf_(export, "rootdir_get_fd() -> %d = %s", result, path);
720 static void hook_ditf_rootdir_open_locale_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *filename, int flags, const char *locale, int result)
726 _hook_ditf_(export, "rootdir_open_locale(%s, %d, %s) -> %d, %m", filename, flags, locale, result);
728 sprintf(path, "/proc/self/fd/%d", result);
729 readlink(path, path, sizeof path);
730 _hook_ditf_(export, "rootdir_open_locale(%s, %d, %s) -> %d = %s", filename, flags, locale, result, path);
734 static void hook_ditf_queue_job_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout, int result)
736 _hook_ditf_(export, "queue_job(%p, %p, %p, %d) -> %d", callback, argument, group, timeout, result);
739 static void hook_ditf_unstore_req_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct afb_stored_req *sreq)
741 _hook_ditf_(export, "unstore_req(%p)", sreq);
744 static void hook_ditf_require_api_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, int initialized)
746 _hook_ditf_(export, "require_api(%s, %d)...", name, initialized);
749 static void hook_ditf_require_api_result_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, int initialized, int result)
751 _hook_ditf_(export, "...require_api(%s, %d) -> %d", name, initialized, result);
754 static void hook_ditf_rename_api_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *oldname, const char *newname, int result)
756 _hook_ditf_(export, "rename_api(%s -> %s) -> %d", oldname, newname, result);
759 static struct afb_hook_ditf_itf hook_ditf_default_itf = {
760 .hook_ditf_event_broadcast_before = hook_ditf_event_broadcast_before_cb,
761 .hook_ditf_event_broadcast_after = hook_ditf_event_broadcast_after_cb,
762 .hook_ditf_get_event_loop = hook_ditf_get_event_loop_cb,
763 .hook_ditf_get_user_bus = hook_ditf_get_user_bus_cb,
764 .hook_ditf_get_system_bus = hook_ditf_get_system_bus_cb,
765 .hook_ditf_vverbose = hook_ditf_vverbose_cb,
766 .hook_ditf_event_make = hook_ditf_event_make_cb,
767 .hook_ditf_rootdir_get_fd = hook_ditf_rootdir_get_fd_cb,
768 .hook_ditf_rootdir_open_locale = hook_ditf_rootdir_open_locale_cb,
769 .hook_ditf_queue_job = hook_ditf_queue_job_cb,
770 .hook_ditf_unstore_req = hook_ditf_unstore_req_cb,
771 .hook_ditf_require_api = hook_ditf_require_api_cb,
772 .hook_ditf_require_api_result = hook_ditf_require_api_result_cb,
773 .hook_ditf_rename_api = hook_ditf_rename_api_cb
776 /******************************************************************************
777 * section: hooks for tracing daemon interface (export)
778 *****************************************************************************/
780 #define _HOOK_DITF_(what,...) \
781 struct afb_hook_ditf *hook; \
782 struct afb_hookid hookid; \
783 const char *apiname = afb_export_apiname(export); \
784 pthread_rwlock_rdlock(&rwlock); \
785 init_hookid(&hookid); \
786 hook = list_of_ditf_hooks; \
788 if (hook->itf->hook_ditf_##what \
789 && (hook->flags & afb_hook_flag_ditf_##what) != 0 \
790 && (!hook->api || !strcasecmp(hook->api, apiname))) { \
791 hook->itf->hook_ditf_##what(hook->closure, &hookid, __VA_ARGS__); \
795 pthread_rwlock_unlock(&rwlock);
797 void afb_hook_ditf_event_broadcast_before(const struct afb_export *export, const char *name, struct json_object *object)
799 _HOOK_DITF_(event_broadcast_before, export, name, object);
802 int afb_hook_ditf_event_broadcast_after(const struct afb_export *export, const char *name, struct json_object *object, int result)
804 _HOOK_DITF_(event_broadcast_after, export, name, object, result);
808 struct sd_event *afb_hook_ditf_get_event_loop(const struct afb_export *export, struct sd_event *result)
810 _HOOK_DITF_(get_event_loop, export, result);
814 struct sd_bus *afb_hook_ditf_get_user_bus(const struct afb_export *export, struct sd_bus *result)
816 _HOOK_DITF_(get_user_bus, export, result);
820 struct sd_bus *afb_hook_ditf_get_system_bus(const struct afb_export *export, struct sd_bus *result)
822 _HOOK_DITF_(get_system_bus, export, result);
826 void afb_hook_ditf_vverbose(const struct afb_export *export, int level, const char *file, int line, const char *function, const char *fmt, va_list args)
828 _HOOK_DITF_(vverbose, export, level, file, line, function, fmt, args);
831 struct afb_event afb_hook_ditf_event_make(const struct afb_export *export, const char *name, struct afb_event result)
833 _HOOK_DITF_(event_make, export, name, result);
837 int afb_hook_ditf_rootdir_get_fd(const struct afb_export *export, int result)
839 _HOOK_DITF_(rootdir_get_fd, export, result);
843 int afb_hook_ditf_rootdir_open_locale(const struct afb_export *export, const char *filename, int flags, const char *locale, int result)
845 _HOOK_DITF_(rootdir_open_locale, export, filename, flags, locale, result);
849 int afb_hook_ditf_queue_job(const struct afb_export *export, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout, int result)
851 _HOOK_DITF_(queue_job, export, callback, argument, group, timeout, result);
855 void afb_hook_ditf_unstore_req(const struct afb_export *export, struct afb_stored_req *sreq)
857 _HOOK_DITF_(unstore_req, export, sreq);
860 void afb_hook_ditf_require_api(const struct afb_export *export, const char *name, int initialized)
862 _HOOK_DITF_(require_api, export, name, initialized);
865 int afb_hook_ditf_require_api_result(const struct afb_export *export, const char *name, int initialized, int result)
867 _HOOK_DITF_(require_api_result, export, name, initialized, result);
871 int afb_hook_ditf_rename_api(const struct afb_export *export, const char *oldname, const char *newname, int result)
873 _HOOK_DITF_(rename_api, export, oldname, newname, result);
877 /******************************************************************************
878 * section: hooking export
879 *****************************************************************************/
881 int afb_hook_flags_ditf(const char *api)
884 struct afb_hook_ditf *hook;
886 pthread_rwlock_rdlock(&rwlock);
888 hook = list_of_ditf_hooks;
890 if (!api || !hook->api || !strcasecmp(hook->api, api))
891 flags |= hook->flags;
894 pthread_rwlock_unlock(&rwlock);
898 struct afb_hook_ditf *afb_hook_create_ditf(const char *api, int flags, struct afb_hook_ditf_itf *itf, void *closure)
900 struct afb_hook_ditf *hook;
902 /* alloc the result */
903 hook = calloc(1, sizeof *hook);
907 /* get a copy of the names */
908 hook->api = api ? strdup(api) : NULL;
909 if (api && !hook->api) {
914 /* initialise the rest */
917 hook->itf = itf ? itf : &hook_ditf_default_itf;
918 hook->closure = closure;
920 /* record the hook */
921 pthread_rwlock_wrlock(&rwlock);
922 hook->next = list_of_ditf_hooks;
923 list_of_ditf_hooks = hook;
924 pthread_rwlock_unlock(&rwlock);
930 struct afb_hook_ditf *afb_hook_addref_ditf(struct afb_hook_ditf *hook)
932 pthread_rwlock_wrlock(&rwlock);
934 pthread_rwlock_unlock(&rwlock);
938 void afb_hook_unref_ditf(struct afb_hook_ditf *hook)
940 struct afb_hook_ditf **prv;
943 pthread_rwlock_wrlock(&rwlock);
944 if (--hook->refcount)
948 prv = &list_of_ditf_hooks;
949 while (*prv && *prv != hook)
954 pthread_rwlock_unlock(&rwlock);
963 /******************************************************************************
964 * section: default callbacks for tracing service interface (export)
965 *****************************************************************************/
967 static void _hook_svc_(const struct afb_export *export, const char *format, ...)
970 va_start(ap, format);
971 _hook_("export-%s", format, ap, afb_export_apiname(export));
975 static void hook_svc_start_before_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export)
977 _hook_svc_(export, "start.before");
980 static void hook_svc_start_after_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int status)
982 _hook_svc_(export, "start.after -> %d", status);
985 static void hook_svc_on_event_before_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *event, int eventid, struct json_object *object)
987 _hook_svc_(export, "on_event.before(%s, %d, %s)", event, eventid, json_object_to_json_string(object));
990 static void hook_svc_on_event_after_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *event, int eventid, struct json_object *object)
992 _hook_svc_(export, "on_event.after(%s, %d, %s)", event, eventid, json_object_to_json_string(object));
995 static void hook_svc_call_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *api, const char *verb, struct json_object *args)
997 _hook_svc_(export, "call(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
1000 static void hook_svc_call_result_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int status, struct json_object *result)
1002 _hook_svc_(export, " ...call... -> %d: %s", status, json_object_to_json_string(result));
1005 static void hook_svc_callsync_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *api, const char *verb, struct json_object *args)
1007 _hook_svc_(export, "callsync(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
1010 static void hook_svc_callsync_result_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int status, struct json_object *result)
1012 _hook_svc_(export, " ...callsync... -> %d: %s", status, json_object_to_json_string(result));
1015 static struct afb_hook_svc_itf hook_svc_default_itf = {
1016 .hook_svc_start_before = hook_svc_start_before_default_cb,
1017 .hook_svc_start_after = hook_svc_start_after_default_cb,
1018 .hook_svc_on_event_before = hook_svc_on_event_before_default_cb,
1019 .hook_svc_on_event_after = hook_svc_on_event_after_default_cb,
1020 .hook_svc_call = hook_svc_call_default_cb,
1021 .hook_svc_call_result = hook_svc_call_result_default_cb,
1022 .hook_svc_callsync = hook_svc_callsync_default_cb,
1023 .hook_svc_callsync_result = hook_svc_callsync_result_default_cb
1026 /******************************************************************************
1027 * section: hooks for tracing service interface (export)
1028 *****************************************************************************/
1030 #define _HOOK_SVC_(what,...) \
1031 struct afb_hook_svc *hook; \
1032 struct afb_hookid hookid; \
1033 const char *apiname = afb_export_apiname(export); \
1034 pthread_rwlock_rdlock(&rwlock); \
1035 init_hookid(&hookid); \
1036 hook = list_of_svc_hooks; \
1038 if (hook->itf->hook_svc_##what \
1039 && (hook->flags & afb_hook_flag_svc_##what) != 0 \
1040 && (!hook->api || !strcasecmp(hook->api, apiname))) { \
1041 hook->itf->hook_svc_##what(hook->closure, &hookid, __VA_ARGS__); \
1043 hook = hook->next; \
1045 pthread_rwlock_unlock(&rwlock);
1047 void afb_hook_svc_start_before(const struct afb_export *export)
1049 _HOOK_SVC_(start_before, export);
1052 int afb_hook_svc_start_after(const struct afb_export *export, int status)
1054 _HOOK_SVC_(start_after, export, status);
1058 void afb_hook_svc_on_event_before(const struct afb_export *export, const char *event, int eventid, struct json_object *object)
1060 _HOOK_SVC_(on_event_before, export, event, eventid, object);
1063 void afb_hook_svc_on_event_after(const struct afb_export *export, const char *event, int eventid, struct json_object *object)
1065 _HOOK_SVC_(on_event_after, export, event, eventid, object);
1068 void afb_hook_svc_call(const struct afb_export *export, const char *api, const char *verb, struct json_object *args)
1070 _HOOK_SVC_(call, export, api, verb, args);
1073 void afb_hook_svc_call_result(const struct afb_export *export, int status, struct json_object *result)
1075 _HOOK_SVC_(call_result, export, status, result);
1078 void afb_hook_svc_callsync(const struct afb_export *export, const char *api, const char *verb, struct json_object *args)
1080 _HOOK_SVC_(callsync, export, api, verb, args);
1083 int afb_hook_svc_callsync_result(const struct afb_export *export, int status, struct json_object *result)
1085 _HOOK_SVC_(callsync_result, export, status, result);
1089 /******************************************************************************
1090 * section: hooking services (export)
1091 *****************************************************************************/
1093 int afb_hook_flags_svc(const char *api)
1096 struct afb_hook_svc *hook;
1098 pthread_rwlock_rdlock(&rwlock);
1100 hook = list_of_svc_hooks;
1102 if (!api || !hook->api || !strcasecmp(hook->api, api))
1103 flags |= hook->flags;
1106 pthread_rwlock_unlock(&rwlock);
1110 struct afb_hook_svc *afb_hook_create_svc(const char *api, int flags, struct afb_hook_svc_itf *itf, void *closure)
1112 struct afb_hook_svc *hook;
1114 /* alloc the result */
1115 hook = calloc(1, sizeof *hook);
1119 /* get a copy of the names */
1120 hook->api = api ? strdup(api) : NULL;
1121 if (api && !hook->api) {
1126 /* initialise the rest */
1128 hook->flags = flags;
1129 hook->itf = itf ? itf : &hook_svc_default_itf;
1130 hook->closure = closure;
1132 /* record the hook */
1133 pthread_rwlock_wrlock(&rwlock);
1134 hook->next = list_of_svc_hooks;
1135 list_of_svc_hooks = hook;
1136 pthread_rwlock_unlock(&rwlock);
1142 struct afb_hook_svc *afb_hook_addref_svc(struct afb_hook_svc *hook)
1144 pthread_rwlock_wrlock(&rwlock);
1146 pthread_rwlock_unlock(&rwlock);
1150 void afb_hook_unref_svc(struct afb_hook_svc *hook)
1152 struct afb_hook_svc **prv;
1155 pthread_rwlock_wrlock(&rwlock);
1156 if (--hook->refcount)
1160 prv = &list_of_svc_hooks;
1161 while (*prv && *prv != hook)
1162 prv = &(*prv)->next;
1166 pthread_rwlock_unlock(&rwlock);
1175 /******************************************************************************
1176 * section: default callbacks for tracing service interface (evt)
1177 *****************************************************************************/
1179 static void _hook_evt_(const char *evt, int id, const char *format, ...)
1182 va_start(ap, format);
1183 _hook_("evt-%s:%d", format, ap, evt, id);
1187 static void hook_evt_create_default_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
1189 _hook_evt_(evt, id, "create");
1192 static void hook_evt_push_before_default_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj)
1194 _hook_evt_(evt, id, "push.before(%s)", json_object_to_json_string(obj));
1198 static void hook_evt_push_after_default_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj, int result)
1200 _hook_evt_(evt, id, "push.after(%s) -> %d", json_object_to_json_string(obj), result);
1203 static void hook_evt_broadcast_before_default_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj)
1205 _hook_evt_(evt, id, "broadcast.before(%s)", json_object_to_json_string(obj));
1208 static void hook_evt_broadcast_after_default_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj, int result)
1210 _hook_evt_(evt, id, "broadcast.after(%s) -> %d", json_object_to_json_string(obj), result);
1213 static void hook_evt_name_default_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
1215 _hook_evt_(evt, id, "name");
1218 static void hook_evt_drop_default_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
1220 _hook_evt_(evt, id, "drop");
1223 static struct afb_hook_evt_itf hook_evt_default_itf = {
1224 .hook_evt_create = hook_evt_create_default_cb,
1225 .hook_evt_push_before = hook_evt_push_before_default_cb,
1226 .hook_evt_push_after = hook_evt_push_after_default_cb,
1227 .hook_evt_broadcast_before = hook_evt_broadcast_before_default_cb,
1228 .hook_evt_broadcast_after = hook_evt_broadcast_after_default_cb,
1229 .hook_evt_name = hook_evt_name_default_cb,
1230 .hook_evt_drop = hook_evt_drop_default_cb
1233 /******************************************************************************
1234 * section: hooks for tracing events interface (evt)
1235 *****************************************************************************/
1237 #define _HOOK_EVT_(what,...) \
1238 struct afb_hook_evt *hook; \
1239 struct afb_hookid hookid; \
1240 pthread_rwlock_rdlock(&rwlock); \
1241 init_hookid(&hookid); \
1242 hook = list_of_evt_hooks; \
1244 if (hook->itf->hook_evt_##what \
1245 && (hook->flags & afb_hook_flag_evt_##what) != 0 \
1246 && (!hook->pattern || !fnmatch(hook->pattern, evt, FNM_CASEFOLD))) { \
1247 hook->itf->hook_evt_##what(hook->closure, &hookid, __VA_ARGS__); \
1249 hook = hook->next; \
1251 pthread_rwlock_unlock(&rwlock);
1253 void afb_hook_evt_create(const char *evt, int id)
1255 _HOOK_EVT_(create, evt, id);
1258 void afb_hook_evt_push_before(const char *evt, int id, struct json_object *obj)
1260 _HOOK_EVT_(push_before, evt, id, obj);
1263 int afb_hook_evt_push_after(const char *evt, int id, struct json_object *obj, int result)
1265 _HOOK_EVT_(push_after, evt, id, obj, result);
1269 void afb_hook_evt_broadcast_before(const char *evt, int id, struct json_object *obj)
1271 _HOOK_EVT_(broadcast_before, evt, id, obj);
1274 int afb_hook_evt_broadcast_after(const char *evt, int id, struct json_object *obj, int result)
1276 _HOOK_EVT_(broadcast_after, evt, id, obj, result);
1280 void afb_hook_evt_name(const char *evt, int id)
1282 _HOOK_EVT_(name, evt, id);
1285 void afb_hook_evt_drop(const char *evt, int id)
1287 _HOOK_EVT_(drop, evt, id);
1290 /******************************************************************************
1291 * section: hooking services (evt)
1292 *****************************************************************************/
1294 int afb_hook_flags_evt(const char *name)
1297 struct afb_hook_evt *hook;
1299 pthread_rwlock_rdlock(&rwlock);
1301 hook = list_of_evt_hooks;
1303 if (!name || !hook->pattern || !fnmatch(hook->pattern, name, FNM_CASEFOLD))
1304 flags |= hook->flags;
1307 pthread_rwlock_unlock(&rwlock);
1311 struct afb_hook_evt *afb_hook_create_evt(const char *pattern, int flags, struct afb_hook_evt_itf *itf, void *closure)
1313 struct afb_hook_evt *hook;
1315 /* alloc the result */
1316 hook = calloc(1, sizeof *hook);
1320 /* get a copy of the names */
1321 hook->pattern = pattern ? strdup(pattern) : NULL;
1322 if (pattern && !hook->pattern) {
1327 /* initialise the rest */
1329 hook->flags = flags;
1330 hook->itf = itf ? itf : &hook_evt_default_itf;
1331 hook->closure = closure;
1333 /* record the hook */
1334 pthread_rwlock_wrlock(&rwlock);
1335 hook->next = list_of_evt_hooks;
1336 list_of_evt_hooks = hook;
1337 pthread_rwlock_unlock(&rwlock);
1343 struct afb_hook_evt *afb_hook_addref_evt(struct afb_hook_evt *hook)
1345 pthread_rwlock_wrlock(&rwlock);
1347 pthread_rwlock_unlock(&rwlock);
1351 void afb_hook_unref_evt(struct afb_hook_evt *hook)
1353 struct afb_hook_evt **prv;
1356 pthread_rwlock_wrlock(&rwlock);
1357 if (--hook->refcount)
1361 prv = &list_of_evt_hooks;
1362 while (*prv && *prv != hook)
1363 prv = &(*prv)->next;
1367 pthread_rwlock_unlock(&rwlock);
1370 free(hook->pattern);
1376 /******************************************************************************
1377 * section: default callbacks for globals (global)
1378 *****************************************************************************/
1380 static void _hook_global_(const char *format, ...)
1383 va_start(ap, format);
1384 _hook_("global", format, ap);
1388 static void hook_global_vverbose_default_cb(void *closure, const struct afb_hookid *hookid, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
1395 len = vasprintf(&msg, fmt, ap);
1399 _hook_global_("vverbose(%d, %s, %d, %s) -> %s ? ? ?", level, file, line, func, fmt);
1401 _hook_global_("vverbose(%d, %s, %d, %s) -> %s", level, file, line, func, msg);
1406 static struct afb_hook_global_itf hook_global_default_itf = {
1407 .hook_global_vverbose = hook_global_vverbose_default_cb
1410 /******************************************************************************
1411 * section: hooks for tracing globals (global)
1412 *****************************************************************************/
1414 #define _HOOK_GLOBAL_(what,...) \
1415 struct afb_hook_global *hook; \
1416 struct afb_hookid hookid; \
1417 pthread_rwlock_rdlock(&rwlock); \
1418 init_hookid(&hookid); \
1419 hook = list_of_global_hooks; \
1421 if (hook->itf->hook_global_##what \
1422 && (hook->flags & afb_hook_flag_global_##what) != 0) { \
1423 hook->itf->hook_global_##what(hook->closure, &hookid, __VA_ARGS__); \
1425 hook = hook->next; \
1427 pthread_rwlock_unlock(&rwlock);
1429 static void afb_hook_global_vverbose(int level, const char *file, int line, const char *func, const char *fmt, va_list args)
1431 _HOOK_GLOBAL_(vverbose, level, file ?: "?", line, func ?: "?", fmt ?: "", args);
1434 /******************************************************************************
1435 * section: hooking globals (global)
1436 *****************************************************************************/
1438 static void update_global()
1440 struct afb_hook_global *hook;
1443 pthread_rwlock_rdlock(&rwlock);
1444 hook = list_of_global_hooks;
1446 flags = hook->flags;
1449 verbose_observer = (flags & afb_hook_flag_global_vverbose) ? afb_hook_global_vverbose : NULL;
1450 pthread_rwlock_unlock(&rwlock);
1453 struct afb_hook_global *afb_hook_create_global(int flags, struct afb_hook_global_itf *itf, void *closure)
1455 struct afb_hook_global *hook;
1457 /* alloc the result */
1458 hook = calloc(1, sizeof *hook);
1462 /* initialise the rest */
1464 hook->flags = flags;
1465 hook->itf = itf ? itf : &hook_global_default_itf;
1466 hook->closure = closure;
1468 /* record the hook */
1469 pthread_rwlock_wrlock(&rwlock);
1470 hook->next = list_of_global_hooks;
1471 list_of_global_hooks = hook;
1472 pthread_rwlock_unlock(&rwlock);
1474 /* update hooking */
1481 struct afb_hook_global *afb_hook_addref_global(struct afb_hook_global *hook)
1483 pthread_rwlock_wrlock(&rwlock);
1485 pthread_rwlock_unlock(&rwlock);
1489 void afb_hook_unref_global(struct afb_hook_global *hook)
1491 struct afb_hook_global **prv;
1494 pthread_rwlock_wrlock(&rwlock);
1495 if (--hook->refcount)
1499 prv = &list_of_global_hooks;
1500 while (*prv && *prv != hook)
1501 prv = &(*prv)->next;
1505 pthread_rwlock_unlock(&rwlock);
1510 /* update hooking */