2 * Copyright (C) 2016, 2017, 2018 "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>
29 #if !defined(JSON_C_TO_STRING_NOSLASHESCAPE)
30 #define JSON_C_TO_STRING_NOSLASHESCAPE 0
33 #include <afb/afb-req-x1.h>
34 #include <afb/afb-event-x2.h>
36 #include "afb-context.h"
38 #include "afb-session.h"
41 #include "afb-export.h"
44 #include "afb-msg-json.h"
48 #define MATCH(pattern,string) (\
50 ? !fnmatch((pattern),(string),FNM_CASEFOLD|FNM_EXTMATCH|FNM_PERIOD) \
51 : afb_api_is_public(string))
53 #define MATCH_API(pattern,string) MATCH(pattern,string)
54 #define MATCH_VERB(pattern,string) MATCH(pattern,string)
55 #define MATCH_EVENT(pattern,string) MATCH(pattern,string)
56 #define MATCH_SESSION(pattern,string) MATCH(pattern,string)
59 * Definition of a hook for xreq
61 struct afb_hook_xreq {
62 struct afb_hook_xreq *next; /**< next hook */
63 unsigned refcount; /**< reference count */
64 unsigned flags; /**< hook flags */
65 char *api; /**< api hooked or NULL for any */
66 char *verb; /**< verb hooked or NULL for any */
67 struct afb_session *session; /**< session hooked or NULL if any */
68 struct afb_hook_xreq_itf *itf; /**< interface of hook */
69 void *closure; /**< closure for callbacks */
73 * Definition of a hook for export
76 struct afb_hook_api *next; /**< next hook */
77 unsigned refcount; /**< reference count */
78 unsigned flags; /**< hook flags */
79 char *api; /**< api hooked or NULL for any */
80 struct afb_hook_api_itf *itf; /**< interface of hook */
81 void *closure; /**< closure for callbacks */
85 * Definition of a hook for evt
88 struct afb_hook_evt *next; /**< next hook */
89 unsigned refcount; /**< reference count */
90 unsigned flags; /**< hook flags */
91 char *pattern; /**< event pattern name hooked or NULL for any */
92 struct afb_hook_evt_itf *itf; /**< interface of hook */
93 void *closure; /**< closure for callbacks */
97 * Definition of a hook for session
99 struct afb_hook_session {
100 struct afb_hook_session *next; /**< next hook */
101 unsigned refcount; /**< reference count */
102 unsigned flags; /**< hook flags */
103 char *pattern; /**< event pattern name hooked or NULL for any */
104 struct afb_hook_session_itf *itf; /**< interface of hook */
105 void *closure; /**< closure for callbacks */
109 * Definition of a hook for global
111 struct afb_hook_global {
112 struct afb_hook_global *next; /**< next hook */
113 unsigned refcount; /**< reference count */
114 unsigned flags; /**< hook flags */
115 struct afb_hook_global_itf *itf; /**< interface of hook */
116 void *closure; /**< closure for callbacks */
119 /* synchronisation across threads */
120 static pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
122 /* list of hooks for xreq */
123 static struct afb_hook_xreq *list_of_xreq_hooks = NULL;
125 /* list of hooks for export */
126 static struct afb_hook_api *list_of_api_hooks = NULL;
128 /* list of hooks for evt */
129 static struct afb_hook_evt *list_of_evt_hooks = NULL;
131 /* list of hooks for session */
132 static struct afb_hook_session *list_of_session_hooks = NULL;
134 /* list of hooks for global */
135 static struct afb_hook_global *list_of_global_hooks = NULL;
138 static unsigned next_hookid = 0;
140 /******************************************************************************
142 *****************************************************************************/
143 static void init_hookid(struct afb_hookid *hookid)
145 hookid->id = __atomic_add_fetch(&next_hookid, 1, __ATOMIC_RELAXED);
146 clock_gettime(CLOCK_REALTIME, &hookid->time);
149 /******************************************************************************
150 * section: default callbacks for tracing requests
151 *****************************************************************************/
153 static char *_pbuf_(const char *fmt, va_list args, char **palloc, char *sbuf, size_t szsbuf, size_t *outlen)
160 rc = vsnprintf(sbuf, szsbuf, fmt, args);
161 if ((size_t)rc >= szsbuf) {
163 sbuf[szsbuf-2] = sbuf[szsbuf-3] = sbuf[szsbuf-4] = '.';
164 rc = vasprintf(palloc, fmt, cp);
169 if (rc >= 0 && outlen)
170 *outlen = (size_t)rc;
174 #if 0 /* old behaviour: use NOTICE */
175 static void _hook_(const char *fmt1, const char *fmt2, va_list arg2, ...)
177 char *tag, *data, *mem1, *mem2, buf1[256], buf2[2000];
180 data = _pbuf_(fmt2, arg2, &mem2, buf2, sizeof buf2, NULL);
182 va_start(arg1, arg2);
183 tag = _pbuf_(fmt1, arg1, &mem1, buf1, sizeof buf1, NULL);
186 NOTICE("[HOOK %s] %s", tag, data);
191 #else /* new behaviour: emits directly to stderr */
192 static void _hook_(const char *fmt1, const char *fmt2, va_list arg2, ...)
194 static const char chars[] = "HOOK: [] \n";
195 char *mem1, *mem2, buf1[256], buf2[2000];
199 iov[0].iov_base = (void*)&chars[0];
202 va_start(arg1, arg2);
203 iov[1].iov_base = _pbuf_(fmt1, arg1, &mem1, buf1, sizeof buf1, &iov[1].iov_len);
206 iov[2].iov_base = (void*)&chars[7];
209 iov[3].iov_base = _pbuf_(fmt2, arg2, &mem2, buf2, sizeof buf2, &iov[3].iov_len);
211 iov[4].iov_base = (void*)&chars[9];
214 (void)writev(2, iov, 5);
221 static void _hook_xreq_(const struct afb_xreq *xreq, const char *format, ...)
224 va_start(ap, format);
225 _hook_("xreq-%06d:%s/%s", format, ap, xreq->hookindex, xreq->request.called_api, xreq->request.called_verb);
229 static void hook_xreq_begin_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
232 _hook_xreq_(xreq, "BEGIN");
234 _hook_xreq_(xreq, "BEGIN uid=%d=%s gid=%d pid=%d label=%s id=%s",
235 (int)xreq->cred->uid,
237 (int)xreq->cred->gid,
238 (int)xreq->cred->pid,
239 xreq->cred->label?:"(null)",
240 xreq->cred->id?:"(null)"
244 static void hook_xreq_end_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
246 _hook_xreq_(xreq, "END");
249 static void hook_xreq_json_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *obj)
251 _hook_xreq_(xreq, "json() -> %s", json_object_to_json_string_ext(obj, JSON_C_TO_STRING_NOSLASHESCAPE));
254 static void hook_xreq_get_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *name, struct afb_arg arg)
256 _hook_xreq_(xreq, "get(%s) -> { name: %s, value: %s, path: %s }", name, arg.name, arg.value, arg.path);
259 static void hook_xreq_reply_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *obj, const char *error, const char *info)
261 _hook_xreq_(xreq, "reply[%s](%s, %s)", error?:"success", json_object_to_json_string_ext(obj, JSON_C_TO_STRING_NOSLASHESCAPE), info);
264 static void hook_xreq_legacy_context_get_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, void *value)
266 _hook_xreq_(xreq, "context_get() -> %p", value);
269 static void hook_xreq_legacy_context_set_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, void *value, void (*free_value)(void*))
271 _hook_xreq_(xreq, "context_set(%p, %p)", value, free_value);
274 static void hook_xreq_addref_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
276 _hook_xreq_(xreq, "addref()");
279 static void hook_xreq_unref_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
281 _hook_xreq_(xreq, "unref()");
284 static void hook_xreq_session_close_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
286 _hook_xreq_(xreq, "session_close()");
289 static void hook_xreq_session_set_LOA_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, unsigned level, int result)
291 _hook_xreq_(xreq, "session_set_LOA(%u) -> %d", level, result);
294 static void hook_xreq_subscribe_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_event_x2 *event_x2, int result)
296 _hook_xreq_(xreq, "subscribe(%s:%d) -> %d", afb_evt_event_x2_fullname(event_x2), afb_evt_event_x2_id(event_x2), result);
299 static void hook_xreq_unsubscribe_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_event_x2 *event_x2, int result)
301 _hook_xreq_(xreq, "unsubscribe(%s:%d) -> %d", afb_evt_event_x2_fullname(event_x2), afb_evt_event_x2_id(event_x2), result);
304 static void hook_xreq_subcall_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, "subcall(%s/%s, %s) ...", api, verb, json_object_to_json_string_ext(args, JSON_C_TO_STRING_NOSLASHESCAPE));
309 static void hook_xreq_subcall_result_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *object, const char *error, const char *info)
311 _hook_xreq_(xreq, " ...subcall... [%s] -> %s (%s)", error?:"success", json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE), info?:"");
314 static void hook_xreq_subcallsync_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
316 _hook_xreq_(xreq, "subcallsync(%s/%s, %s) ...", api, verb, json_object_to_json_string_ext(args, JSON_C_TO_STRING_NOSLASHESCAPE));
319 static void hook_xreq_subcallsync_result_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int status, struct json_object *object, const char *error, const char *info)
321 _hook_xreq_(xreq, " ...subcallsync... %d [%s] -> %s (%s)", status, error?:"success", json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE), info?:"");
324 static void hook_xreq_vverbose_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)
331 len = vasprintf(&msg, fmt, ap);
335 _hook_xreq_(xreq, "vverbose(%d:%s, %s, %d, %s) -> %s ? ? ?", level, verbose_name_of_level(level), file, line, func, fmt);
337 _hook_xreq_(xreq, "vverbose(%d:%s, %s, %d, %s) -> %s", level, verbose_name_of_level(level), file, line, func, msg);
342 static void hook_xreq_legacy_store_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_stored_req *sreq)
344 _hook_xreq_(xreq, "store() -> %p", sreq);
347 static void hook_xreq_legacy_unstore_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
349 _hook_xreq_(xreq, "unstore()");
352 static void hook_xreq_has_permission_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 void hook_xreq_get_application_id_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, char *result)
359 _hook_xreq_(xreq, "get_application_id() -> %s", result);
362 static void hook_xreq_context_make_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int replace, void *(*create_value)(void*), void (*free_value)(void*), void *create_closure, void *result)
364 _hook_xreq_(xreq, "context_make(replace=%s, %p, %p, %p) -> %p", replace?"yes":"no", create_value, free_value, create_closure, result);
367 static void hook_xreq_get_uid_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int result)
369 _hook_xreq_(xreq, "get_uid() -> %d", result);
372 static void hook_xreq_get_client_info_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *result)
374 _hook_xreq_(xreq, "get_client_info() -> %s", json_object_to_json_string_ext(result, JSON_C_TO_STRING_NOSLASHESCAPE));
377 static struct afb_hook_xreq_itf hook_xreq_default_itf = {
378 .hook_xreq_begin = hook_xreq_begin_cb,
379 .hook_xreq_end = hook_xreq_end_cb,
380 .hook_xreq_json = hook_xreq_json_cb,
381 .hook_xreq_get = hook_xreq_get_cb,
382 .hook_xreq_reply = hook_xreq_reply_cb,
383 .hook_xreq_legacy_context_get = hook_xreq_legacy_context_get_cb,
384 .hook_xreq_legacy_context_set = hook_xreq_legacy_context_set_cb,
385 .hook_xreq_addref = hook_xreq_addref_cb,
386 .hook_xreq_unref = hook_xreq_unref_cb,
387 .hook_xreq_session_close = hook_xreq_session_close_cb,
388 .hook_xreq_session_set_LOA = hook_xreq_session_set_LOA_cb,
389 .hook_xreq_subscribe = hook_xreq_subscribe_cb,
390 .hook_xreq_unsubscribe = hook_xreq_unsubscribe_cb,
391 .hook_xreq_subcall = hook_xreq_subcall_cb,
392 .hook_xreq_subcall_result = hook_xreq_subcall_result_cb,
393 .hook_xreq_subcallsync = hook_xreq_subcallsync_cb,
394 .hook_xreq_subcallsync_result = hook_xreq_subcallsync_result_cb,
395 .hook_xreq_vverbose = hook_xreq_vverbose_cb,
396 .hook_xreq_legacy_store = hook_xreq_legacy_store_cb,
397 .hook_xreq_legacy_unstore = hook_xreq_legacy_unstore_cb,
398 .hook_xreq_has_permission = hook_xreq_has_permission_cb,
399 .hook_xreq_get_application_id = hook_xreq_get_application_id_cb,
400 .hook_xreq_context_make = hook_xreq_context_make_cb,
401 .hook_xreq_get_uid = hook_xreq_get_uid_cb,
402 .hook_xreq_get_client_info = hook_xreq_get_client_info_cb,
405 /******************************************************************************
406 * section: hooks for tracing requests
407 *****************************************************************************/
409 #define _HOOK_XREQ_2_(flag,func,...) \
410 struct afb_hook_xreq *hook; \
411 struct afb_hookid hookid; \
412 pthread_rwlock_rdlock(&rwlock); \
413 init_hookid(&hookid); \
414 hook = list_of_xreq_hooks; \
416 if (hook->itf->hook_xreq_##func \
417 && (hook->flags & afb_hook_flag_req_##flag) != 0 \
418 && (!hook->session || hook->session == xreq->context.session) \
419 && MATCH_API(hook->api, xreq->request.called_api) \
420 && MATCH_VERB(hook->verb, xreq->request.called_verb)) { \
421 hook->itf->hook_xreq_##func(hook->closure, &hookid, __VA_ARGS__); \
425 pthread_rwlock_unlock(&rwlock);
427 #define _HOOK_XREQ_(what,...) _HOOK_XREQ_2_(what,what,__VA_ARGS__)
429 void afb_hook_xreq_begin(const struct afb_xreq *xreq)
431 _HOOK_XREQ_(begin, xreq);
434 void afb_hook_xreq_end(const struct afb_xreq *xreq)
436 _HOOK_XREQ_(end, xreq);
439 struct json_object *afb_hook_xreq_json(const struct afb_xreq *xreq, struct json_object *obj)
441 _HOOK_XREQ_(json, xreq, obj);
445 struct afb_arg afb_hook_xreq_get(const struct afb_xreq *xreq, const char *name, struct afb_arg arg)
447 _HOOK_XREQ_(get, xreq, name, arg);
451 void afb_hook_xreq_reply(const struct afb_xreq *xreq, struct json_object *obj, const char *error, const char *info)
453 _HOOK_XREQ_(reply, xreq, obj, error, info);
456 void *afb_hook_xreq_legacy_context_get(const struct afb_xreq *xreq, void *value)
458 _HOOK_XREQ_(legacy_context_get, xreq, value);
462 void afb_hook_xreq_legacy_context_set(const struct afb_xreq *xreq, void *value, void (*free_value)(void*))
464 _HOOK_XREQ_(legacy_context_set, xreq, value, free_value);
467 void afb_hook_xreq_addref(const struct afb_xreq *xreq)
469 _HOOK_XREQ_(addref, xreq);
472 void afb_hook_xreq_unref(const struct afb_xreq *xreq)
474 _HOOK_XREQ_(unref, xreq);
477 void afb_hook_xreq_session_close(const struct afb_xreq *xreq)
479 _HOOK_XREQ_(session_close, xreq);
482 int afb_hook_xreq_session_set_LOA(const struct afb_xreq *xreq, unsigned level, int result)
484 _HOOK_XREQ_(session_set_LOA, xreq, level, result);
488 int afb_hook_xreq_subscribe(const struct afb_xreq *xreq, struct afb_event_x2 *event_x2, int result)
490 _HOOK_XREQ_(subscribe, xreq, event_x2, result);
494 int afb_hook_xreq_unsubscribe(const struct afb_xreq *xreq, struct afb_event_x2 *event_x2, int result)
496 _HOOK_XREQ_(unsubscribe, xreq, event_x2, result);
500 void afb_hook_xreq_subcall(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, int flags)
502 _HOOK_XREQ_(subcall, xreq, api, verb, args);
505 void afb_hook_xreq_subcall_result(const struct afb_xreq *xreq, struct json_object *object, const char *error, const char *info)
507 _HOOK_XREQ_(subcall_result, xreq, object, error, info);
510 void afb_hook_xreq_subcallsync(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, int flags)
512 _HOOK_XREQ_(subcallsync, xreq, api, verb, args);
515 int afb_hook_xreq_subcallsync_result(const struct afb_xreq *xreq, int status, struct json_object *object, const char *error, const char *info)
517 _HOOK_XREQ_(subcallsync_result, xreq, status, object, error, info);
521 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)
523 _HOOK_XREQ_(vverbose, xreq, level, file ?: "?", line, func ?: "?", fmt, args);
526 void afb_hook_xreq_legacy_store(const struct afb_xreq *xreq, struct afb_stored_req *sreq)
528 _HOOK_XREQ_(legacy_store, xreq, sreq);
531 void afb_hook_xreq_legacy_unstore(const struct afb_xreq *xreq)
533 _HOOK_XREQ_(legacy_unstore, xreq);
536 int afb_hook_xreq_has_permission(const struct afb_xreq *xreq, const char *permission, int result)
538 _HOOK_XREQ_(has_permission, xreq, permission, result);
542 char *afb_hook_xreq_get_application_id(const struct afb_xreq *xreq, char *result)
544 _HOOK_XREQ_(get_application_id, xreq, result);
548 void *afb_hook_xreq_context_make(const struct afb_xreq *xreq, int replace, void *(*create_value)(void*), void (*free_value)(void*), void *create_closure, void *result)
550 _HOOK_XREQ_(context_make, xreq, replace, create_value, free_value, create_closure, result);
554 int afb_hook_xreq_get_uid(const struct afb_xreq *xreq, int result)
556 _HOOK_XREQ_(get_uid, xreq, result);
560 struct json_object *afb_hook_xreq_get_client_info(const struct afb_xreq *xreq, struct json_object *result)
562 _HOOK_XREQ_(get_client_info, xreq, result);
566 /******************************************************************************
567 * section: hooking xreqs
568 *****************************************************************************/
570 void afb_hook_init_xreq(struct afb_xreq *xreq)
576 struct afb_hook_xreq *hook;
578 /* scan hook list to get the expected flags */
580 pthread_rwlock_rdlock(&rwlock);
581 hook = list_of_xreq_hooks;
583 f = hook->flags & afb_hook_flags_req_all;
585 && (!hook->session || hook->session == xreq->context.session)
586 && MATCH_API(hook->api, xreq->request.called_api)
587 && MATCH_VERB(hook->verb, xreq->request.called_verb);
592 pthread_rwlock_unlock(&rwlock);
594 /* store the hooking data */
595 xreq->hookflags = flags;
597 pthread_rwlock_wrlock(&rwlock);
600 xreq->hookindex = reqindex;
601 pthread_rwlock_unlock(&rwlock);
605 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)
607 struct afb_hook_xreq *hook;
609 /* alloc the result */
610 hook = calloc(1, sizeof *hook);
614 /* get a copy of the names */
615 hook->api = api ? strdup(api) : NULL;
616 hook->verb = verb ? strdup(verb) : NULL;
617 if ((api && !hook->api) || (verb && !hook->verb)) {
624 /* initialise the rest */
625 hook->session = session;
627 afb_session_addref(session);
630 hook->itf = itf ? itf : &hook_xreq_default_itf;
631 hook->closure = closure;
633 /* record the hook */
634 pthread_rwlock_wrlock(&rwlock);
635 hook->next = list_of_xreq_hooks;
636 list_of_xreq_hooks = hook;
637 pthread_rwlock_unlock(&rwlock);
643 struct afb_hook_xreq *afb_hook_addref_xreq(struct afb_hook_xreq *hook)
645 pthread_rwlock_wrlock(&rwlock);
647 pthread_rwlock_unlock(&rwlock);
651 void afb_hook_unref_xreq(struct afb_hook_xreq *hook)
653 struct afb_hook_xreq **prv;
656 pthread_rwlock_wrlock(&rwlock);
657 if (--hook->refcount)
661 prv = &list_of_xreq_hooks;
662 while (*prv && *prv != hook)
667 pthread_rwlock_unlock(&rwlock);
673 afb_session_unref(hook->session);
679 /******************************************************************************
680 * section: default callbacks for tracing daemon interface
681 *****************************************************************************/
683 static void _hook_api_(const struct afb_export *export, const char *format, ...)
686 va_start(ap, format);
687 _hook_("api-%s", format, ap, afb_export_apiname(export));
691 static void hook_api_event_broadcast_before_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, struct json_object *object)
693 _hook_api_(export, "event_broadcast.before(%s, %s)....", name, json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE));
696 static void hook_api_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)
698 _hook_api_(export, "event_broadcast.after(%s, %s) -> %d", name, json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE), result);
701 static void hook_api_get_event_loop_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_event *result)
703 _hook_api_(export, "get_event_loop() -> %p", result);
706 static void hook_api_get_user_bus_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_bus *result)
708 _hook_api_(export, "get_user_bus() -> %p", result);
711 static void hook_api_get_system_bus_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_bus *result)
713 _hook_api_(export, "get_system_bus() -> %p", result);
716 static void hook_api_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)
723 len = vasprintf(&msg, fmt, ap);
727 _hook_api_(export, "vverbose(%d:%s, %s, %d, %s) -> %s ? ? ?", level, verbose_name_of_level(level), file, line, function, fmt);
729 _hook_api_(export, "vverbose(%d:%s, %s, %d, %s) -> %s", level, verbose_name_of_level(level), file, line, function, msg);
734 static void hook_api_event_make_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, struct afb_event_x2 *result)
736 _hook_api_(export, "event_make(%s) -> %s:%d", name, afb_evt_event_x2_fullname(result), afb_evt_event_x2_id(result));
739 static void hook_api_rootdir_get_fd_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result)
741 char path[PATH_MAX], proc[100];
745 _hook_api_(export, "rootdir_get_fd() -> %d, %m", result);
747 snprintf(proc, sizeof proc, "/proc/self/fd/%d", result);
748 s = readlink(proc, path, sizeof path);
749 path[s < 0 ? 0 : s >= sizeof path ? sizeof path - 1 : s] = 0;
750 _hook_api_(export, "rootdir_get_fd() -> %d = %s", result, path);
754 static void hook_api_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)
756 char path[PATH_MAX], proc[100];
762 _hook_api_(export, "rootdir_open_locale(%s, %d, %s) -> %d, %m", filename, flags, locale, result);
764 snprintf(proc, sizeof proc, "/proc/self/fd/%d", result);
765 s = readlink(proc, path, sizeof path);
766 path[s < 0 ? 0 : s >= sizeof path ? sizeof path - 1 : s] = 0;
767 _hook_api_(export, "rootdir_open_locale(%s, %d, %s) -> %d = %s", filename, flags, locale, result, path);
771 static void hook_api_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)
773 _hook_api_(export, "queue_job(%p, %p, %p, %d) -> %d", callback, argument, group, timeout, result);
776 static void hook_api_unstore_req_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct afb_stored_req *sreq)
778 _hook_api_(export, "unstore_req(%p)", sreq);
781 static void hook_api_require_api_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, int initialized)
783 _hook_api_(export, "require_api(%s, %d)...", name, initialized);
786 static void hook_api_require_api_result_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, int initialized, int result)
788 _hook_api_(export, "...require_api(%s, %d) -> %d", name, initialized, result);
791 static void hook_api_add_alias_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *api, const char *alias, int result)
793 _hook_api_(export, "add_alias(%s -> %s) -> %d", api, alias?:"<null>", result);
796 static void hook_api_start_before_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export)
798 _hook_api_(export, "start.before");
801 static void hook_api_start_after_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int status)
803 _hook_api_(export, "start.after -> %d", status);
806 static void hook_api_on_event_before_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *event, int event_x2, struct json_object *object)
808 _hook_api_(export, "on_event.before(%s, %d, %s)", event, event_x2, json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE));
811 static void hook_api_on_event_after_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *event, int event_x2, struct json_object *object)
813 _hook_api_(export, "on_event.after(%s, %d, %s)", event, event_x2, json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE));
816 static void hook_api_call_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *api, const char *verb, struct json_object *args)
818 _hook_api_(export, "call(%s/%s, %s) ...", api, verb, json_object_to_json_string_ext(args, JSON_C_TO_STRING_NOSLASHESCAPE));
821 static void hook_api_call_result_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct json_object *object, const char *error, const char *info)
823 _hook_api_(export, " ...call... [%s] -> %s (%s)", error?:"success", json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE), info?:"");
826 static void hook_api_callsync_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *api, const char *verb, struct json_object *args)
828 _hook_api_(export, "callsync(%s/%s, %s) ...", api, verb, json_object_to_json_string_ext(args, JSON_C_TO_STRING_NOSLASHESCAPE));
831 static void hook_api_callsync_result_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int status, struct json_object *object, const char *error, const char *info)
833 _hook_api_(export, " ...callsync... %d [%s] -> %s (%s)", status, error?:"success", json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE), info?:"");
836 static void hook_api_new_api_before_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *api, const char *info, int noconcurrency)
838 _hook_api_(export, "new_api.before %s (%s)%s ...", api, info?:"", noconcurrency?" no-concurrency" : "");
841 static void hook_api_new_api_after_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *api)
843 _hook_api_(export, "... new_api.after %s -> %s (%d)", api, result >= 0 ? "OK" : "ERROR", result);
846 static void hook_api_api_set_verbs_v2_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const struct afb_verb_v2 *verbs)
848 _hook_api_(export, "set_verbs_v2 -> %s (%d)", result >= 0 ? "OK" : "ERROR", result);
851 static void hook_api_api_set_verbs_v3_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const struct afb_verb_v3 *verbs)
853 _hook_api_(export, "set_verbs_v3 -> %s (%d)", result >= 0 ? "OK" : "ERROR", result);
856 static void hook_api_api_add_verb_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *verb, const char *info, int glob)
858 _hook_api_(export, "add_verb(%s%s [%s]) -> %s (%d)", verb, glob?" (GLOB)":"", info?:"", result >= 0 ? "OK" : "ERROR", result);
861 static void hook_api_api_del_verb_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *verb)
863 _hook_api_(export, "del_verb(%s) -> %s (%d)", verb, result >= 0 ? "OK" : "ERROR", result);
866 static void hook_api_api_set_on_event_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result)
868 _hook_api_(export, "set_on_event -> %s (%d)", result >= 0 ? "OK" : "ERROR", result);
871 static void hook_api_api_set_on_init_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result)
873 _hook_api_(export, "set_on_init -> %s (%d)", result >= 0 ? "OK" : "ERROR", result);
876 static void hook_api_api_seal_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export)
878 _hook_api_(export, "seal");
881 static void hook_api_event_handler_add_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *pattern)
883 _hook_api_(export, "event_handler_add(%s) -> %s (%d)", pattern, result >= 0 ? "OK" : "ERROR", result);
886 static void hook_api_event_handler_del_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *pattern)
888 _hook_api_(export, "event_handler_del(%s) -> %s (%d)", pattern, result >= 0 ? "OK" : "ERROR", result);
891 static void hook_api_class_provide_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *name)
893 _hook_api_(export, "class_provide(%s) -> %s (%d)", name, result >= 0 ? "OK" : "ERROR", result);
896 static void hook_api_class_require_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *name)
898 _hook_api_(export, "class_require(%s) -> %s (%d)", name, result >= 0 ? "OK" : "ERROR", result);
901 static void hook_api_delete_api_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result)
903 _hook_api_(export, "delete_api -> %s (%d)", result >= 0 ? "OK" : "ERROR", result);
906 static void hook_api_on_event_handler_before_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *event, int event_x2, struct json_object *object, const char *pattern)
908 _hook_api_(export, "on_event_handler[%s].before(%s, %d, %s)", pattern, event, event_x2, json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE));
911 static void hook_api_on_event_handler_after_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *event, int event_x2, struct json_object *object, const char *pattern)
913 _hook_api_(export, "on_event_handler[%s].after(%s, %d, %s)", pattern, event, event_x2, json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE));
916 static void hook_api_settings_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct json_object *object)
918 _hook_api_(export, "settings -> %s", json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE));
921 static struct afb_hook_api_itf hook_api_default_itf = {
922 .hook_api_event_broadcast_before = hook_api_event_broadcast_before_cb,
923 .hook_api_event_broadcast_after = hook_api_event_broadcast_after_cb,
924 .hook_api_get_event_loop = hook_api_get_event_loop_cb,
925 .hook_api_get_user_bus = hook_api_get_user_bus_cb,
926 .hook_api_get_system_bus = hook_api_get_system_bus_cb,
927 .hook_api_vverbose = hook_api_vverbose_cb,
928 .hook_api_event_make = hook_api_event_make_cb,
929 .hook_api_rootdir_get_fd = hook_api_rootdir_get_fd_cb,
930 .hook_api_rootdir_open_locale = hook_api_rootdir_open_locale_cb,
931 .hook_api_queue_job = hook_api_queue_job_cb,
932 .hook_api_legacy_unstore_req = hook_api_unstore_req_cb,
933 .hook_api_require_api = hook_api_require_api_cb,
934 .hook_api_require_api_result = hook_api_require_api_result_cb,
935 .hook_api_add_alias = hook_api_add_alias_cb,
936 .hook_api_start_before = hook_api_start_before_cb,
937 .hook_api_start_after = hook_api_start_after_cb,
938 .hook_api_on_event_before = hook_api_on_event_before_cb,
939 .hook_api_on_event_after = hook_api_on_event_after_cb,
940 .hook_api_call = hook_api_call_cb,
941 .hook_api_call_result = hook_api_call_result_cb,
942 .hook_api_callsync = hook_api_callsync_cb,
943 .hook_api_callsync_result = hook_api_callsync_result_cb,
944 .hook_api_new_api_before = hook_api_new_api_before_cb,
945 .hook_api_new_api_after = hook_api_new_api_after_cb,
946 .hook_api_api_set_verbs_v2 = hook_api_api_set_verbs_v2_cb,
947 .hook_api_api_set_verbs_v3 = hook_api_api_set_verbs_v3_cb,
948 .hook_api_api_add_verb = hook_api_api_add_verb_cb,
949 .hook_api_api_del_verb = hook_api_api_del_verb_cb,
950 .hook_api_api_set_on_event = hook_api_api_set_on_event_cb,
951 .hook_api_api_set_on_init = hook_api_api_set_on_init_cb,
952 .hook_api_api_seal = hook_api_api_seal_cb,
953 .hook_api_event_handler_add = hook_api_event_handler_add_cb,
954 .hook_api_event_handler_del = hook_api_event_handler_del_cb,
955 .hook_api_class_provide = hook_api_class_provide_cb,
956 .hook_api_class_require = hook_api_class_require_cb,
957 .hook_api_delete_api = hook_api_delete_api_cb,
958 .hook_api_on_event_handler_before = hook_api_on_event_handler_before_cb,
959 .hook_api_on_event_handler_after = hook_api_on_event_handler_after_cb,
960 .hook_api_settings = hook_api_settings_cb,
963 /******************************************************************************
964 * section: hooks for tracing daemon interface (export)
965 *****************************************************************************/
967 #define _HOOK_API_2_(flag,func,...) \
968 struct afb_hook_api *hook; \
969 struct afb_hookid hookid; \
970 const char *apiname = afb_export_apiname(export); \
971 pthread_rwlock_rdlock(&rwlock); \
972 init_hookid(&hookid); \
973 hook = list_of_api_hooks; \
975 if (hook->itf->hook_api_##func \
976 && (hook->flags & afb_hook_flag_api_##flag) != 0 \
977 && MATCH_API(hook->api, apiname)) { \
978 hook->itf->hook_api_##func(hook->closure, &hookid, __VA_ARGS__); \
982 pthread_rwlock_unlock(&rwlock);
984 #define _HOOK_API_(what,...) _HOOK_API_2_(what,what,__VA_ARGS__)
986 void afb_hook_api_event_broadcast_before(const struct afb_export *export, const char *name, struct json_object *object)
988 _HOOK_API_2_(event_broadcast, event_broadcast_before, export, name, object);
991 int afb_hook_api_event_broadcast_after(const struct afb_export *export, const char *name, struct json_object *object, int result)
993 _HOOK_API_2_(event_broadcast, event_broadcast_after, export, name, object, result);
997 struct sd_event *afb_hook_api_get_event_loop(const struct afb_export *export, struct sd_event *result)
999 _HOOK_API_(get_event_loop, export, result);
1003 struct sd_bus *afb_hook_api_get_user_bus(const struct afb_export *export, struct sd_bus *result)
1005 _HOOK_API_(get_user_bus, export, result);
1009 struct sd_bus *afb_hook_api_get_system_bus(const struct afb_export *export, struct sd_bus *result)
1011 _HOOK_API_(get_system_bus, export, result);
1015 void afb_hook_api_vverbose(const struct afb_export *export, int level, const char *file, int line, const char *function, const char *fmt, va_list args)
1017 _HOOK_API_(vverbose, export, level, file, line, function, fmt, args);
1020 struct afb_event_x2 *afb_hook_api_event_make(const struct afb_export *export, const char *name, struct afb_event_x2 *result)
1022 _HOOK_API_(event_make, export, name, result);
1026 int afb_hook_api_rootdir_get_fd(const struct afb_export *export, int result)
1028 _HOOK_API_(rootdir_get_fd, export, result);
1032 int afb_hook_api_rootdir_open_locale(const struct afb_export *export, const char *filename, int flags, const char *locale, int result)
1034 _HOOK_API_(rootdir_open_locale, export, filename, flags, locale, result);
1038 int afb_hook_api_queue_job(const struct afb_export *export, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout, int result)
1040 _HOOK_API_(queue_job, export, callback, argument, group, timeout, result);
1044 void afb_hook_api_legacy_unstore_req(const struct afb_export *export, struct afb_stored_req *sreq)
1046 _HOOK_API_(legacy_unstore_req, export, sreq);
1049 void afb_hook_api_require_api(const struct afb_export *export, const char *name, int initialized)
1051 _HOOK_API_(require_api, export, name, initialized);
1054 int afb_hook_api_require_api_result(const struct afb_export *export, const char *name, int initialized, int result)
1056 _HOOK_API_2_(require_api, require_api_result, export, name, initialized, result);
1060 int afb_hook_api_add_alias(const struct afb_export *export, const char *api, const char *alias, int result)
1062 _HOOK_API_(add_alias, export, api, alias, result);
1066 void afb_hook_api_start_before(const struct afb_export *export)
1068 _HOOK_API_2_(start, start_before, export);
1071 int afb_hook_api_start_after(const struct afb_export *export, int status)
1073 _HOOK_API_2_(start, start_after, export, status);
1077 void afb_hook_api_on_event_before(const struct afb_export *export, const char *event, int event_x2, struct json_object *object)
1079 _HOOK_API_2_(on_event, on_event_before, export, event, event_x2, object);
1082 void afb_hook_api_on_event_after(const struct afb_export *export, const char *event, int event_x2, struct json_object *object)
1084 _HOOK_API_2_(on_event, on_event_after, export, event, event_x2, object);
1087 void afb_hook_api_call(const struct afb_export *export, const char *api, const char *verb, struct json_object *args)
1089 _HOOK_API_(call, export, api, verb, args);
1092 void afb_hook_api_call_result(const struct afb_export *export, struct json_object *object, const char*error, const char *info)
1094 _HOOK_API_2_(call, call_result, export, object, error, info);
1098 void afb_hook_api_callsync(const struct afb_export *export, const char *api, const char *verb, struct json_object *args)
1100 _HOOK_API_(callsync, export, api, verb, args);
1103 int afb_hook_api_callsync_result(const struct afb_export *export, int status, struct json_object *object, const char *error, const char *info)
1105 _HOOK_API_2_(callsync, callsync_result, export, status, object, error, info);
1109 void afb_hook_api_new_api_before(const struct afb_export *export, const char *api, const char *info, int noconcurrency)
1111 _HOOK_API_2_(new_api, new_api_before, export, api, info, noconcurrency);
1114 int afb_hook_api_new_api_after(const struct afb_export *export, int result, const char *api)
1116 _HOOK_API_2_(new_api, new_api_after, export, result, api);
1120 int afb_hook_api_api_set_verbs_v2(const struct afb_export *export, int result, const struct afb_verb_v2 *verbs)
1122 _HOOK_API_2_(api_set_verbs, api_set_verbs_v2, export, result, verbs);
1126 int afb_hook_api_api_set_verbs_v3(const struct afb_export *export, int result, const struct afb_verb_v3 *verbs)
1128 _HOOK_API_2_(api_set_verbs, api_set_verbs_v3, export, result, verbs);
1132 int afb_hook_api_api_add_verb(const struct afb_export *export, int result, const char *verb, const char *info, int glob)
1134 _HOOK_API_(api_add_verb, export, result, verb, info, glob);
1138 int afb_hook_api_api_del_verb(const struct afb_export *export, int result, const char *verb)
1140 _HOOK_API_(api_del_verb, export, result, verb);
1144 int afb_hook_api_api_set_on_event(const struct afb_export *export, int result)
1146 _HOOK_API_(api_set_on_event, export, result);
1150 int afb_hook_api_api_set_on_init(const struct afb_export *export, int result)
1152 _HOOK_API_(api_set_on_init, export, result);
1156 void afb_hook_api_api_seal(const struct afb_export *export)
1158 _HOOK_API_(api_seal, export);
1161 int afb_hook_api_event_handler_add(const struct afb_export *export, int result, const char *pattern)
1163 _HOOK_API_(event_handler_add, export, result, pattern);
1166 int afb_hook_api_event_handler_del(const struct afb_export *export, int result, const char *pattern)
1168 _HOOK_API_(event_handler_del, export, result, pattern);
1171 int afb_hook_api_class_provide(const struct afb_export *export, int result, const char *name)
1173 _HOOK_API_(class_provide, export, result, name);
1176 int afb_hook_api_class_require(const struct afb_export *export, int result, const char *name)
1178 _HOOK_API_(class_require, export, result, name);
1182 int afb_hook_api_delete_api(const struct afb_export *export, int result)
1184 _HOOK_API_(delete_api, export, result);
1188 void afb_hook_api_on_event_handler_before(const struct afb_export *export, const char *event, int event_x2, struct json_object *object, const char *pattern)
1190 _HOOK_API_2_(on_event_handler, on_event_handler_before, export, event, event_x2, object, pattern);
1193 void afb_hook_api_on_event_handler_after(const struct afb_export *export, const char *event, int event_x2, struct json_object *object, const char *pattern)
1195 _HOOK_API_2_(on_event_handler, on_event_handler_after, export, event, event_x2, object, pattern);
1198 struct json_object *afb_hook_api_settings(const struct afb_export *export, struct json_object *object)
1200 _HOOK_API_(settings, export, object);
1204 /******************************************************************************
1205 * section: hooking export
1206 *****************************************************************************/
1208 int afb_hook_flags_api(const char *api)
1211 struct afb_hook_api *hook;
1214 pthread_rwlock_rdlock(&rwlock);
1215 hook = list_of_api_hooks;
1217 if (!api || MATCH_API(hook->api, api))
1218 flags |= hook->flags;
1221 pthread_rwlock_unlock(&rwlock);
1225 struct afb_hook_api *afb_hook_create_api(const char *api, int flags, struct afb_hook_api_itf *itf, void *closure)
1227 struct afb_hook_api *hook;
1229 /* alloc the result */
1230 hook = calloc(1, sizeof *hook);
1234 /* get a copy of the names */
1235 hook->api = api ? strdup(api) : NULL;
1236 if (api && !hook->api) {
1241 /* initialise the rest */
1243 hook->flags = flags;
1244 hook->itf = itf ? itf : &hook_api_default_itf;
1245 hook->closure = closure;
1247 /* record the hook */
1248 pthread_rwlock_wrlock(&rwlock);
1249 hook->next = list_of_api_hooks;
1250 list_of_api_hooks = hook;
1251 pthread_rwlock_unlock(&rwlock);
1257 struct afb_hook_api *afb_hook_addref_api(struct afb_hook_api *hook)
1259 pthread_rwlock_wrlock(&rwlock);
1261 pthread_rwlock_unlock(&rwlock);
1265 void afb_hook_unref_api(struct afb_hook_api *hook)
1267 struct afb_hook_api **prv;
1270 pthread_rwlock_wrlock(&rwlock);
1271 if (--hook->refcount)
1275 prv = &list_of_api_hooks;
1276 while (*prv && *prv != hook)
1277 prv = &(*prv)->next;
1281 pthread_rwlock_unlock(&rwlock);
1290 /******************************************************************************
1291 * section: default callbacks for tracing service interface (evt)
1292 *****************************************************************************/
1294 static void _hook_evt_(const char *evt, int id, const char *format, ...)
1297 va_start(ap, format);
1298 _hook_("evt-%s:%d", format, ap, evt, id);
1302 static void hook_evt_create_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
1304 _hook_evt_(evt, id, "create");
1307 static void hook_evt_push_before_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj)
1309 _hook_evt_(evt, id, "push.before(%s)", json_object_to_json_string_ext(obj, JSON_C_TO_STRING_NOSLASHESCAPE));
1313 static void hook_evt_push_after_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj, int result)
1315 _hook_evt_(evt, id, "push.after(%s) -> %d", json_object_to_json_string_ext(obj, JSON_C_TO_STRING_NOSLASHESCAPE), result);
1318 static void hook_evt_broadcast_before_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj)
1320 _hook_evt_(evt, id, "broadcast.before(%s)", json_object_to_json_string_ext(obj, JSON_C_TO_STRING_NOSLASHESCAPE));
1323 static void hook_evt_broadcast_after_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj, int result)
1325 _hook_evt_(evt, id, "broadcast.after(%s) -> %d", json_object_to_json_string_ext(obj, JSON_C_TO_STRING_NOSLASHESCAPE), result);
1328 static void hook_evt_name_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, const char *result)
1330 _hook_evt_(evt, id, "name -> %s", result);
1333 static void hook_evt_addref_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
1335 _hook_evt_(evt, id, "addref");
1338 static void hook_evt_unref_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
1340 _hook_evt_(evt, id, "unref");
1343 static struct afb_hook_evt_itf hook_evt_default_itf = {
1344 .hook_evt_create = hook_evt_create_cb,
1345 .hook_evt_push_before = hook_evt_push_before_cb,
1346 .hook_evt_push_after = hook_evt_push_after_cb,
1347 .hook_evt_broadcast_before = hook_evt_broadcast_before_cb,
1348 .hook_evt_broadcast_after = hook_evt_broadcast_after_cb,
1349 .hook_evt_name = hook_evt_name_cb,
1350 .hook_evt_addref = hook_evt_addref_cb,
1351 .hook_evt_unref = hook_evt_unref_cb
1354 /******************************************************************************
1355 * section: hooks for tracing events interface (evt)
1356 *****************************************************************************/
1358 #define _HOOK_EVT_(what,...) \
1359 struct afb_hook_evt *hook; \
1360 struct afb_hookid hookid; \
1361 pthread_rwlock_rdlock(&rwlock); \
1362 init_hookid(&hookid); \
1363 hook = list_of_evt_hooks; \
1365 if (hook->itf->hook_evt_##what \
1366 && (hook->flags & afb_hook_flag_evt_##what) != 0 \
1367 && MATCH_EVENT(hook->pattern, evt)) { \
1368 hook->itf->hook_evt_##what(hook->closure, &hookid, __VA_ARGS__); \
1370 hook = hook->next; \
1372 pthread_rwlock_unlock(&rwlock);
1374 void afb_hook_evt_create(const char *evt, int id)
1376 _HOOK_EVT_(create, evt, id);
1379 void afb_hook_evt_push_before(const char *evt, int id, struct json_object *obj)
1381 _HOOK_EVT_(push_before, evt, id, obj);
1384 int afb_hook_evt_push_after(const char *evt, int id, struct json_object *obj, int result)
1386 _HOOK_EVT_(push_after, evt, id, obj, result);
1390 void afb_hook_evt_broadcast_before(const char *evt, int id, struct json_object *obj)
1392 _HOOK_EVT_(broadcast_before, evt, id, obj);
1395 int afb_hook_evt_broadcast_after(const char *evt, int id, struct json_object *obj, int result)
1397 _HOOK_EVT_(broadcast_after, evt, id, obj, result);
1401 void afb_hook_evt_name(const char *evt, int id, const char *result)
1403 _HOOK_EVT_(name, evt, id, result);
1406 void afb_hook_evt_addref(const char *evt, int id)
1408 _HOOK_EVT_(addref, evt, id);
1411 void afb_hook_evt_unref(const char *evt, int id)
1413 _HOOK_EVT_(unref, evt, id);
1416 /******************************************************************************
1417 * section: hooking services (evt)
1418 *****************************************************************************/
1420 int afb_hook_flags_evt(const char *name)
1423 struct afb_hook_evt *hook;
1426 pthread_rwlock_rdlock(&rwlock);
1427 hook = list_of_evt_hooks;
1429 if (!name || MATCH_EVENT(hook->pattern, name))
1430 flags |= hook->flags;
1433 pthread_rwlock_unlock(&rwlock);
1437 struct afb_hook_evt *afb_hook_create_evt(const char *pattern, int flags, struct afb_hook_evt_itf *itf, void *closure)
1439 struct afb_hook_evt *hook;
1441 /* alloc the result */
1442 hook = calloc(1, sizeof *hook);
1446 /* get a copy of the names */
1447 hook->pattern = pattern ? strdup(pattern) : NULL;
1448 if (pattern && !hook->pattern) {
1453 /* initialise the rest */
1455 hook->flags = flags;
1456 hook->itf = itf ? itf : &hook_evt_default_itf;
1457 hook->closure = closure;
1459 /* record the hook */
1460 pthread_rwlock_wrlock(&rwlock);
1461 hook->next = list_of_evt_hooks;
1462 list_of_evt_hooks = hook;
1463 pthread_rwlock_unlock(&rwlock);
1469 struct afb_hook_evt *afb_hook_addref_evt(struct afb_hook_evt *hook)
1471 pthread_rwlock_wrlock(&rwlock);
1473 pthread_rwlock_unlock(&rwlock);
1477 void afb_hook_unref_evt(struct afb_hook_evt *hook)
1479 struct afb_hook_evt **prv;
1482 pthread_rwlock_wrlock(&rwlock);
1483 if (--hook->refcount)
1487 prv = &list_of_evt_hooks;
1488 while (*prv && *prv != hook)
1489 prv = &(*prv)->next;
1493 pthread_rwlock_unlock(&rwlock);
1496 free(hook->pattern);
1502 /******************************************************************************
1503 * section: default callbacks for sessions (session)
1504 *****************************************************************************/
1506 static void _hook_session_(struct afb_session *session, const char *format, ...)
1509 va_start(ap, format);
1510 _hook_("session-%s", format, ap, afb_session_uuid(session));
1514 static void hook_session_create_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1516 _hook_session_(session, "create -> token=%s", afb_session_token(session));
1519 static void hook_session_close_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1521 _hook_session_(session, "close");
1524 static void hook_session_destroy_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1526 _hook_session_(session, "destroy");
1529 static void hook_session_renew_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1531 _hook_session_(session, "renew -> token=%s", afb_session_token(session));
1534 static void hook_session_addref_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1536 _hook_session_(session, "addref");
1539 static void hook_session_unref_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1541 _hook_session_(session, "unref");
1544 static struct afb_hook_session_itf hook_session_default_itf = {
1545 .hook_session_create = hook_session_create_cb,
1546 .hook_session_close = hook_session_close_cb,
1547 .hook_session_destroy = hook_session_destroy_cb,
1548 .hook_session_renew = hook_session_renew_cb,
1549 .hook_session_addref = hook_session_addref_cb,
1550 .hook_session_unref = hook_session_unref_cb
1553 /******************************************************************************
1554 * section: hooks for tracing sessions (session)
1555 *****************************************************************************/
1557 #define _HOOK_SESSION_(what,...) \
1558 struct afb_hook_session *hook; \
1559 struct afb_hookid hookid; \
1560 const char *sessid = 0; \
1561 pthread_rwlock_rdlock(&rwlock); \
1562 init_hookid(&hookid); \
1563 hook = list_of_session_hooks; \
1565 if (hook->itf->hook_session_##what \
1566 && (hook->flags & afb_hook_flag_session_##what) != 0 \
1567 && MATCH_SESSION(hook->pattern, (sessid?:(sessid=afb_session_uuid(session))))) { \
1568 hook->itf->hook_session_##what(hook->closure, &hookid, __VA_ARGS__); \
1570 hook = hook->next; \
1572 pthread_rwlock_unlock(&rwlock);
1574 void afb_hook_session_create(struct afb_session *session)
1576 _HOOK_SESSION_(create, session);
1579 void afb_hook_session_close(struct afb_session *session)
1581 _HOOK_SESSION_(close, session);
1584 void afb_hook_session_destroy(struct afb_session *session)
1586 _HOOK_SESSION_(destroy, session);
1589 void afb_hook_session_renew(struct afb_session *session)
1591 _HOOK_SESSION_(renew, session);
1594 void afb_hook_session_addref(struct afb_session *session)
1596 _HOOK_SESSION_(addref, session);
1599 void afb_hook_session_unref(struct afb_session *session)
1601 _HOOK_SESSION_(unref, session);
1605 /******************************************************************************
1606 * section: hooking sessions (session)
1607 *****************************************************************************/
1609 struct afb_hook_session *afb_hook_create_session(const char *pattern, int flags, struct afb_hook_session_itf *itf, void *closure)
1611 struct afb_hook_session *hook;
1613 /* alloc the result */
1614 hook = calloc(1, sizeof *hook);
1618 /* get a copy of the names */
1619 hook->pattern = pattern ? strdup(pattern) : NULL;
1620 if (pattern && !hook->pattern) {
1625 /* initialise the rest */
1627 hook->flags = flags;
1628 hook->itf = itf ? itf : &hook_session_default_itf;
1629 hook->closure = closure;
1631 /* record the hook */
1632 pthread_rwlock_wrlock(&rwlock);
1633 hook->next = list_of_session_hooks;
1634 list_of_session_hooks = hook;
1635 pthread_rwlock_unlock(&rwlock);
1641 struct afb_hook_session *afb_hook_addref_session(struct afb_hook_session *hook)
1643 pthread_rwlock_wrlock(&rwlock);
1645 pthread_rwlock_unlock(&rwlock);
1649 void afb_hook_unref_session(struct afb_hook_session *hook)
1651 struct afb_hook_session **prv;
1654 pthread_rwlock_wrlock(&rwlock);
1655 if (--hook->refcount)
1659 prv = &list_of_session_hooks;
1660 while (*prv && *prv != hook)
1661 prv = &(*prv)->next;
1665 pthread_rwlock_unlock(&rwlock);
1668 free(hook->pattern);
1674 /******************************************************************************
1675 * section: default callbacks for globals (global)
1676 *****************************************************************************/
1678 static void _hook_global_(const char *format, ...)
1681 va_start(ap, format);
1682 _hook_("global", format, ap);
1686 static void hook_global_vverbose_cb(void *closure, const struct afb_hookid *hookid, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
1693 len = vasprintf(&msg, fmt, ap);
1697 _hook_global_("vverbose(%d:%s, %s, %d, %s) -> %s ? ? ?", level, verbose_name_of_level(level), file, line, func, fmt);
1699 _hook_global_("vverbose(%d:%s, %s, %d, %s) -> %s", level, verbose_name_of_level(level), file, line, func, msg);
1704 static struct afb_hook_global_itf hook_global_default_itf = {
1705 .hook_global_vverbose = hook_global_vverbose_cb
1708 /******************************************************************************
1709 * section: hooks for tracing globals (global)
1710 *****************************************************************************/
1712 #define _HOOK_GLOBAL_(what,...) \
1713 struct afb_hook_global *hook; \
1714 struct afb_hookid hookid; \
1715 pthread_rwlock_rdlock(&rwlock); \
1716 init_hookid(&hookid); \
1717 hook = list_of_global_hooks; \
1719 if (hook->itf->hook_global_##what \
1720 && (hook->flags & afb_hook_flag_global_##what) != 0) { \
1721 hook->itf->hook_global_##what(hook->closure, &hookid, __VA_ARGS__); \
1723 hook = hook->next; \
1725 pthread_rwlock_unlock(&rwlock);
1727 static void afb_hook_global_vverbose(int level, const char *file, int line, const char *func, const char *fmt, va_list args)
1729 _HOOK_GLOBAL_(vverbose, level, file ?: "?", line, func ?: "?", fmt ?: "", args);
1732 /******************************************************************************
1733 * section: hooking globals (global)
1734 *****************************************************************************/
1736 static void update_global()
1738 struct afb_hook_global *hook;
1741 pthread_rwlock_rdlock(&rwlock);
1742 hook = list_of_global_hooks;
1744 flags = hook->flags;
1747 verbose_observer = (flags & afb_hook_flag_global_vverbose) ? afb_hook_global_vverbose : NULL;
1748 pthread_rwlock_unlock(&rwlock);
1751 struct afb_hook_global *afb_hook_create_global(int flags, struct afb_hook_global_itf *itf, void *closure)
1753 struct afb_hook_global *hook;
1755 /* alloc the result */
1756 hook = calloc(1, sizeof *hook);
1760 /* initialise the rest */
1762 hook->flags = flags;
1763 hook->itf = itf ? itf : &hook_global_default_itf;
1764 hook->closure = closure;
1766 /* record the hook */
1767 pthread_rwlock_wrlock(&rwlock);
1768 hook->next = list_of_global_hooks;
1769 list_of_global_hooks = hook;
1770 pthread_rwlock_unlock(&rwlock);
1772 /* update hooking */
1779 struct afb_hook_global *afb_hook_addref_global(struct afb_hook_global *hook)
1781 pthread_rwlock_wrlock(&rwlock);
1783 pthread_rwlock_unlock(&rwlock);
1787 void afb_hook_unref_global(struct afb_hook_global *hook)
1789 struct afb_hook_global **prv;
1792 pthread_rwlock_wrlock(&rwlock);
1793 if (--hook->refcount)
1797 prv = &list_of_global_hooks;
1798 while (*prv && *prv != hook)
1799 prv = &(*prv)->next;
1803 pthread_rwlock_unlock(&rwlock);
1808 /* update hooking */