2 * Copyright (C) 2015-2020 "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.
18 #if WITH_AFB_HOOK && WITH_AFB_TRACE
31 #include <json-c/json.h>
32 #if !defined(JSON_C_TO_STRING_NOSLASHESCAPE)
33 #define JSON_C_TO_STRING_NOSLASHESCAPE 0
36 #define AFB_BINDING_VERSION 3
37 #define AFB_BINDING_NO_ROOT
38 #include <afb/afb-binding.h>
41 #include "afb-hook-flags.h"
43 #include "afb-session.h"
45 #include "afb-export.h"
47 #include "afb-session.h"
48 #include "afb-trace.h"
50 #include "wrap-json.h"
53 /*******************************************************************************/
54 /***** default names *****/
55 /*******************************************************************************/
57 #if !defined(DEFAULT_EVENT_NAME)
58 # define DEFAULT_EVENT_NAME "trace"
60 #if !defined(DEFAULT_TAG_NAME)
61 # define DEFAULT_TAG_NAME "trace"
64 /*******************************************************************************/
66 /*******************************************************************************/
70 struct tag *next; /* link to the next */
71 char tag[]; /* name of the tag */
74 /* struct for events */
76 struct event *next; /* link to the next event */
77 struct afb_evtid *evtid; /* the event */
80 /* struct for sessions */
82 struct afb_session *session; /* the session */
83 struct afb_trace *trace; /* the tracer */
86 /* struct for recording hooks */
88 struct hook *next; /* link to next hook */
89 void *handler; /* the handler of the hook */
90 struct event *event; /* the associated event */
91 struct tag *tag; /* the associated tag */
92 struct afb_session *session; /* the associated session */
98 Trace_Type_Xreq, /* xreq hooks */
99 Trace_Type_Api, /* api hooks */
100 Trace_Type_Evt, /* evt hooks */
101 Trace_Type_Session, /* session hooks */
102 Trace_Type_Global, /* global hooks */
103 #if !defined(REMOVE_LEGACY_TRACE)
104 Trace_Legacy_Type_Ditf, /* export hooks */
105 Trace_Legacy_Type_Svc, /* export hooks */
107 Trace_Type_Count, /* count of types of hooks */
113 int refcount; /* reference count */
114 pthread_mutex_t mutex; /* concurrency management */
115 const char *apiname; /* api name for events */
116 struct afb_session *bound; /* bound to session */
117 struct event *events; /* list of events */
118 struct tag *tags; /* list of tags */
119 struct hook *hooks[Trace_Type_Count]; /* hooks */
122 /*******************************************************************************/
123 /***** utility functions *****/
124 /*******************************************************************************/
126 static void ctxt_error(char **errors, const char *format, ...)
134 va_start(ap, format);
135 len = vsnprintf(buffer, sizeof buffer, format, ap);
137 if (len > (int)(sizeof buffer - 2))
138 len = (int)(sizeof buffer - 2);
139 buffer[len++] = '\n';
143 sz = errs ? strlen(errs) : 0;
144 errs = realloc(errs, sz + (size_t)len);
146 memcpy(errs + sz, buffer, len);
152 static struct json_object *timestamp(const struct afb_hookid *hookid)
154 return json_object_new_double((double)hookid->time.tv_sec +
155 (double)hookid->time.tv_nsec * .000000001);
158 /* verbosity level name or NULL */
159 static const char *verbosity_level_name(int level)
161 static const char *names[] = {
169 return level >= Log_Level_Error && level <= Log_Level_Debug ? names[level - Log_Level_Error] : NULL;
173 static void emit(void *closure, const struct afb_hookid *hookid, const char *type, const char *fmt1, const char *fmt2, va_list ap2, ...)
175 struct hook *hook = closure;
176 struct json_object *data, *data1, *data2;
179 data1 = data2 = data = NULL;
181 wrap_json_vpack(&data1, fmt1, ap1);
184 wrap_json_vpack(&data2, fmt2, ap2);
186 wrap_json_pack(&data, "{so ss ss si so so*}",
187 "time", timestamp(hookid),
188 "tag", hook->tag->tag,
190 "id", (int)(hookid->id & INT_MAX),
194 afb_evt_evtid_push(hook->event->evtid, data);
197 /*******************************************************************************/
198 /***** trace the requests *****/
199 /*******************************************************************************/
201 static void hook_xreq(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *action, const char *format, ...)
203 struct afb_cred *cred;
204 struct json_object *jcred = NULL;
205 const char *session = NULL;
208 if (xreq->context.session)
209 session = afb_session_uuid(xreq->context.session);
211 cred = xreq->context.credentials;
213 wrap_json_pack(&jcred, "{si ss si si ss* ss*}",
214 "uid", (int)cred->uid,
216 "gid", (int)cred->gid,
217 "pid", (int)cred->pid,
218 "label", cred->label,
221 va_start(ap, format);
222 emit(closure, hookid, "request", "{si ss ss ss so* ss*}", format, ap,
223 "index", xreq->hookindex,
224 "api", xreq->request.called_api,
225 "verb", xreq->request.called_verb,
227 "credentials", jcred,
232 static void hook_xreq_begin(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
234 hook_xreq(closure, hookid, xreq, "begin", "{sO?}",
235 "json", afb_xreq_unhooked_json((struct afb_xreq*)xreq));
238 static void hook_xreq_end(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
240 hook_xreq(closure, hookid, xreq, "end", NULL);
243 static void hook_xreq_json(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *obj)
245 hook_xreq(closure, hookid, xreq, "json", "{sO?}",
249 static void hook_xreq_get(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *name, struct afb_arg arg)
251 hook_xreq(closure, hookid, xreq, "get", "{ss? ss? ss? ss?}",
258 static void hook_xreq_reply(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *obj, const char *error, const char *info)
260 hook_xreq(closure, hookid, xreq, "reply", "{sO? ss? ss?}",
266 static void hook_xreq_context_get(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, void *value)
268 hook_xreq(closure, hookid, xreq, "context_get", NULL);
271 static void hook_xreq_context_set(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, void *value, void (*free_value)(void*))
273 hook_xreq(closure, hookid, xreq, "context_set", NULL);
276 static void hook_xreq_addref(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
278 hook_xreq(closure, hookid, xreq, "addref", NULL);
281 static void hook_xreq_unref(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
283 hook_xreq(closure, hookid, xreq, "unref", NULL);
286 static void hook_xreq_session_close(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
288 hook_xreq(closure, hookid, xreq, "session_close", NULL);
291 static void hook_xreq_session_set_LOA(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, unsigned level, int result)
293 hook_xreq(closure, hookid, xreq, "session_set_LOA", "{si si}",
298 static void hook_xreq_subscribe(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_event_x2 *event, int result)
300 hook_xreq(closure, hookid, xreq, "subscribe", "{s{ss si} si}",
302 "name", afb_evt_event_x2_fullname(event),
303 "id", afb_evt_event_x2_id(event),
307 static void hook_xreq_unsubscribe(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_event_x2 *event, int result)
309 hook_xreq(closure, hookid, xreq, "unsubscribe", "{s{ss? si} si}",
311 "name", afb_evt_event_x2_fullname(event),
312 "id", afb_evt_event_x2_id(event),
316 static void hook_xreq_subcall(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
318 hook_xreq(closure, hookid, xreq, "subcall", "{ss? ss? sO?}",
324 static void hook_xreq_subcall_result(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *object, const char *error, const char *info)
326 hook_xreq(closure, hookid, xreq, "subcall_result", "{sO? ss? ss?}",
332 static void hook_xreq_subcallsync(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
334 hook_xreq(closure, hookid, xreq, "subcallsync", "{ss? ss? sO?}",
340 static void hook_xreq_subcallsync_result(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int status, struct json_object *object, const char *error, const char *info)
342 hook_xreq(closure, hookid, xreq, "subcallsync_result", "{si sO? ss? ss?}",
349 static void hook_xreq_vverbose(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)
351 struct json_object *pos;
360 len = vasprintf(&msg, fmt, ap);
364 wrap_json_pack(&pos, "{ss si ss*}", "file", file, "line", line, "function", func);
366 hook_xreq(closure, hookid, xreq, "vverbose", "{si ss* ss? so*}",
368 "type", verbosity_level_name(level),
369 len < 0 ? "format" : "message", len < 0 ? fmt : msg,
375 static void hook_xreq_store(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_stored_req *sreq)
377 hook_xreq(closure, hookid, xreq, "store", NULL);
380 static void hook_xreq_unstore(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
382 hook_xreq(closure, hookid, xreq, "unstore", NULL);
385 static void hook_xreq_has_permission(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *permission, int result)
387 hook_xreq(closure, hookid, xreq, "has_permission", "{ss sb}",
388 "permission", permission,
392 static void hook_xreq_get_application_id(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, char *result)
394 hook_xreq(closure, hookid, xreq, "get_application_id", "{ss?}",
398 static void hook_xreq_context_make(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)
400 char pc[50], pf[50], pv[50], pr[50];
401 snprintf(pc, sizeof pc, "%p", create_value);
402 snprintf(pf, sizeof pf, "%p", free_value);
403 snprintf(pv, sizeof pv, "%p", create_closure);
404 snprintf(pr, sizeof pr, "%p", result);
405 hook_xreq(closure, hookid, xreq, "context_make", "{sb ss ss ss ss}",
413 static void hook_xreq_get_uid(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int result)
415 hook_xreq(closure, hookid, xreq, "get_uid", "{si}",
419 static void hook_xreq_get_client_info(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *result)
421 hook_xreq(closure, hookid, xreq, "get_client_info", "{sO}",
425 static struct afb_hook_xreq_itf hook_xreq_itf = {
426 .hook_xreq_begin = hook_xreq_begin,
427 .hook_xreq_end = hook_xreq_end,
428 .hook_xreq_json = hook_xreq_json,
429 .hook_xreq_get = hook_xreq_get,
430 .hook_xreq_reply = hook_xreq_reply,
431 .hook_xreq_legacy_context_get = hook_xreq_context_get,
432 .hook_xreq_legacy_context_set = hook_xreq_context_set,
433 .hook_xreq_addref = hook_xreq_addref,
434 .hook_xreq_unref = hook_xreq_unref,
435 .hook_xreq_session_close = hook_xreq_session_close,
436 .hook_xreq_session_set_LOA = hook_xreq_session_set_LOA,
437 .hook_xreq_subscribe = hook_xreq_subscribe,
438 .hook_xreq_unsubscribe = hook_xreq_unsubscribe,
439 .hook_xreq_subcall = hook_xreq_subcall,
440 .hook_xreq_subcall_result = hook_xreq_subcall_result,
441 .hook_xreq_subcallsync = hook_xreq_subcallsync,
442 .hook_xreq_subcallsync_result = hook_xreq_subcallsync_result,
443 .hook_xreq_vverbose = hook_xreq_vverbose,
444 .hook_xreq_legacy_store = hook_xreq_store,
445 .hook_xreq_legacy_unstore = hook_xreq_unstore,
446 .hook_xreq_has_permission = hook_xreq_has_permission,
447 .hook_xreq_get_application_id = hook_xreq_get_application_id,
448 .hook_xreq_context_make = hook_xreq_context_make,
449 .hook_xreq_get_uid = hook_xreq_get_uid,
450 .hook_xreq_get_client_info = hook_xreq_get_client_info,
453 /*******************************************************************************/
454 /***** trace the api interface *****/
455 /*******************************************************************************/
457 static void hook_api(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *action, const char *format, ...)
461 va_start(ap, format);
462 emit(closure, hookid, "api", "{ss ss}", format, ap,
463 "api", afb_export_apiname(export),
468 static void hook_api_event_broadcast_before(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, struct json_object *object)
470 hook_api(closure, hookid, export, "event_broadcast_before", "{ss sO?}",
471 "name", name, "data", object);
474 static void hook_api_event_broadcast_after(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, struct json_object *object, int result)
476 hook_api(closure, hookid, export, "event_broadcast_after", "{ss sO? si}",
477 "name", name, "data", object, "result", result);
480 static void hook_api_get_event_loop(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_event *result)
482 hook_api(closure, hookid, export, "get_event_loop", NULL);
485 static void hook_api_get_user_bus(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_bus *result)
487 hook_api(closure, hookid, export, "get_user_bus", NULL);
490 static void hook_api_get_system_bus(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_bus *result)
492 hook_api(closure, hookid, export, "get_system_bus", NULL);
495 static void hook_api_vverbose(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)
497 struct json_object *pos;
506 len = vasprintf(&msg, fmt, ap);
510 wrap_json_pack(&pos, "{ss si ss*}", "file", file, "line", line, "function", function);
512 hook_api(closure, hookid, export, "vverbose", "{si ss* ss? so*}",
514 "type", verbosity_level_name(level),
515 len < 0 ? "format" : "message", len < 0 ? fmt : msg,
521 static void hook_api_event_make(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, struct afb_event_x2 *result)
523 hook_api(closure, hookid, export, "event_make", "{ss ss si}",
524 "name", name, "event", afb_evt_event_x2_fullname(result), "id", afb_evt_event_x2_id(result));
527 static void hook_api_rootdir_get_fd(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result)
529 char path[PATH_MAX], proc[100];
530 const char *key, *val;
534 snprintf(proc, sizeof proc, "/proc/self/fd/%d", result);
535 s = readlink(proc, path, sizeof path);
536 path[s < 0 ? 0 : s >= sizeof path ? sizeof path - 1 : s] = 0;
541 val = strerror(errno);
544 hook_api(closure, hookid, export, "rootdir_get_fd", "{ss}", key, val);
547 static void hook_api_rootdir_open_locale(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *filename, int flags, const char *locale, int result)
549 char path[PATH_MAX], proc[100];
550 const char *key, *val;
554 snprintf(proc, sizeof proc, "/proc/self/fd/%d", result);
555 s = readlink(proc, path, sizeof path);
556 path[s < 0 ? 0 : s >= sizeof path ? sizeof path - 1 : s] = 0;
561 val = strerror(errno);
564 hook_api(closure, hookid, export, "rootdir_open_locale", "{ss si ss* ss}",
571 static void hook_api_queue_job(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)
573 hook_api(closure, hookid, export, "queue_job", "{ss}", "result", result);
576 static void hook_api_unstore_req(void * closure, const struct afb_hookid *hookid, const struct afb_export *export, struct afb_stored_req *sreq)
578 hook_api(closure, hookid, export, "unstore_req", NULL);
581 static void hook_api_require_api(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, int initialized)
583 hook_api(closure, hookid, export, "require_api", "{ss sb}", "name", name, "initialized", initialized);
586 static void hook_api_require_api_result(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, int initialized, int result)
588 hook_api(closure, hookid, export, "require_api_result", "{ss sb si}", "name", name, "initialized", initialized, "result", result);
591 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)
593 hook_api(closure, hookid, export, "add_alias", "{si ss? ss}", "status", result, "api", api, "alias", alias);
596 static void hook_api_start_before(void *closure, const struct afb_hookid *hookid, const struct afb_export *export)
598 hook_api(closure, hookid, export, "start_before", NULL);
601 static void hook_api_start_after(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int status)
603 hook_api(closure, hookid, export, "start_after", "{si}", "result", status);
606 static void hook_api_on_event_before(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *event, int evtid, struct json_object *object)
608 hook_api(closure, hookid, export, "on_event_before", "{ss si sO*}",
609 "event", event, "id", evtid, "data", object);
612 static void hook_api_on_event_after(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *event, int evtid, struct json_object *object)
614 hook_api(closure, hookid, export, "on_event_after", "{ss si sO?}",
615 "event", event, "id", evtid, "data", object);
618 static void hook_api_call(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *api, const char *verb, struct json_object *args)
620 hook_api(closure, hookid, export, "call", "{ss ss sO?}",
621 "api", api, "verb", verb, "args", args);
624 static void hook_api_call_result(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct json_object *object, const char *error, const char *info)
626 hook_api(closure, hookid, export, "call_result", "{sO? ss? ss?}",
627 "object", object, "error", error, "info", info);
630 static void hook_api_callsync(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *api, const char *verb, struct json_object *args)
632 hook_api(closure, hookid, export, "callsync", "{ss ss sO?}",
633 "api", api, "verb", verb, "args", args);
636 static void hook_api_callsync_result(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int status, struct json_object *object, const char *error, const char *info)
638 hook_api(closure, hookid, export, "callsync_result", "{si sO? ss? ss?}",
639 "status", status, "object", object, "error", error, "info", info);
642 static void hook_api_new_api_before(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *api, const char *info, int noconcurrency)
644 hook_api(closure, hookid, export, "new_api.before", "{ss ss? sb}",
645 "api", api, "info", info, "noconcurrency", noconcurrency);
648 static void hook_api_new_api_after(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *api)
650 hook_api(closure, hookid, export, "new_api.after", "{si ss}",
651 "status", result, "api", api);
654 static void hook_api_api_set_verbs_v2(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const struct afb_verb_v2 *verbs)
656 hook_api(closure, hookid, export, "set_verbs_v2", "{si}", "status", result);
659 static void hook_api_api_set_verbs_v3(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const struct afb_verb_v3 *verbs)
661 hook_api(closure, hookid, export, "set_verbs_v3", "{si}", "status", result);
665 static void hook_api_api_add_verb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *verb, const char *info, int glob)
667 hook_api(closure, hookid, export, "add_verb", "{si ss ss? sb}", "status", result, "verb", verb, "info", info, "glob", glob);
670 static void hook_api_api_del_verb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *verb)
672 hook_api(closure, hookid, export, "del_verb", "{si ss}", "status", result, "verb", verb);
675 static void hook_api_api_set_on_event(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result)
677 hook_api(closure, hookid, export, "set_on_event", "{si}", "status", result);
680 static void hook_api_api_set_on_init(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result)
682 hook_api(closure, hookid, export, "set_on_init", "{si}", "status", result);
685 static void hook_api_api_seal(void *closure, const struct afb_hookid *hookid, const struct afb_export *export)
687 hook_api(closure, hookid, export, "seal", NULL);
690 static void hook_api_event_handler_add(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *pattern)
692 hook_api(closure, hookid, export, "event_handler_add", "{si ss?}", "status", result, "pattern", pattern);
695 static void hook_api_event_handler_del(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *pattern)
697 hook_api(closure, hookid, export, "event_handler_del", "{si ss?}", "status", result, "pattern", pattern);
700 static void hook_api_class_provide(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *name)
702 hook_api(closure, hookid, export, "class_provide", "{si ss?}", "status", result, "name", name);
705 static void hook_api_class_require(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *name)
707 hook_api(closure, hookid, export, "class_require", "{si ss?}", "status", result, "name", name);
710 static void hook_api_delete_api(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result)
712 hook_api(closure, hookid, export, "delete_api", "{si}", "status", result);
715 static void hook_api_on_event_handler_before(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)
717 hook_api(closure, hookid, export, "on_event_handler.before",
718 "{ss ss sO?}", "pattern", pattern, "event", event, "data", object);
721 static void hook_api_on_event_handler_after(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)
723 hook_api(closure, hookid, export, "on_event_handler.after",
724 "{ss ss sO?}", "pattern", pattern, "event", event, "data", object);
727 static void hook_api_settings(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct json_object *object)
729 hook_api(closure, hookid, export, "settings", "{sO}", "settings", object);
732 static struct afb_hook_api_itf hook_api_itf = {
733 .hook_api_event_broadcast_before = hook_api_event_broadcast_before,
734 .hook_api_event_broadcast_after = hook_api_event_broadcast_after,
735 .hook_api_get_event_loop = hook_api_get_event_loop,
736 .hook_api_get_user_bus = hook_api_get_user_bus,
737 .hook_api_get_system_bus = hook_api_get_system_bus,
738 .hook_api_vverbose = hook_api_vverbose,
739 .hook_api_event_make = hook_api_event_make,
740 .hook_api_rootdir_get_fd = hook_api_rootdir_get_fd,
741 .hook_api_rootdir_open_locale = hook_api_rootdir_open_locale,
742 .hook_api_queue_job = hook_api_queue_job,
743 .hook_api_legacy_unstore_req = hook_api_unstore_req,
744 .hook_api_require_api = hook_api_require_api,
745 .hook_api_require_api_result = hook_api_require_api_result,
746 .hook_api_add_alias = hook_api_add_alias_cb,
747 .hook_api_start_before = hook_api_start_before,
748 .hook_api_start_after = hook_api_start_after,
749 .hook_api_on_event_before = hook_api_on_event_before,
750 .hook_api_on_event_after = hook_api_on_event_after,
751 .hook_api_call = hook_api_call,
752 .hook_api_call_result = hook_api_call_result,
753 .hook_api_callsync = hook_api_callsync,
754 .hook_api_callsync_result = hook_api_callsync_result,
755 .hook_api_new_api_before = hook_api_new_api_before,
756 .hook_api_new_api_after = hook_api_new_api_after,
757 .hook_api_api_set_verbs_v2 = hook_api_api_set_verbs_v2,
758 .hook_api_api_set_verbs_v3 = hook_api_api_set_verbs_v3,
759 .hook_api_api_add_verb = hook_api_api_add_verb,
760 .hook_api_api_del_verb = hook_api_api_del_verb,
761 .hook_api_api_set_on_event = hook_api_api_set_on_event,
762 .hook_api_api_set_on_init = hook_api_api_set_on_init,
763 .hook_api_api_seal = hook_api_api_seal,
764 .hook_api_event_handler_add = hook_api_event_handler_add,
765 .hook_api_event_handler_del = hook_api_event_handler_del,
766 .hook_api_class_provide = hook_api_class_provide,
767 .hook_api_class_require = hook_api_class_require,
768 .hook_api_delete_api = hook_api_delete_api,
769 .hook_api_on_event_handler_before = hook_api_on_event_handler_before,
770 .hook_api_on_event_handler_after = hook_api_on_event_handler_after,
771 .hook_api_settings = hook_api_settings,
774 /*******************************************************************************/
775 /***** trace the events *****/
776 /*******************************************************************************/
778 static void hook_evt(void *closure, const struct afb_hookid *hookid, const char *evt, int id, const char *action, const char *format, ...)
782 va_start(ap, format);
783 emit(closure, hookid, "event", "{si ss ss}", format, ap,
790 static void hook_evt_create(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
792 hook_evt(closure, hookid, evt, id, "create", NULL);
795 static void hook_evt_push_before(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj)
797 hook_evt(closure, hookid, evt, id, "push_before", "{sO*}", "data", obj);
801 static void hook_evt_push_after(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj, int result)
803 hook_evt(closure, hookid, evt, id, "push_after", "{sO* si}", "data", obj, "result", result);
806 static void hook_evt_broadcast_before(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj)
808 hook_evt(closure, hookid, evt, id, "broadcast_before", "{sO*}", "data", obj);
811 static void hook_evt_broadcast_after(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj, int result)
813 hook_evt(closure, hookid, evt, id, "broadcast_after", "{sO* si}", "data", obj, "result", result);
816 static void hook_evt_name(void *closure, const struct afb_hookid *hookid, const char *evt, int id, const char *result)
818 hook_evt(closure, hookid, evt, id, "name", "{ss}", "result", result);
821 static void hook_evt_addref(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
823 hook_evt(closure, hookid, evt, id, "addref", NULL);
826 static void hook_evt_unref(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
828 hook_evt(closure, hookid, evt, id, "unref", NULL);
831 static struct afb_hook_evt_itf hook_evt_itf = {
832 .hook_evt_create = hook_evt_create,
833 .hook_evt_push_before = hook_evt_push_before,
834 .hook_evt_push_after = hook_evt_push_after,
835 .hook_evt_broadcast_before = hook_evt_broadcast_before,
836 .hook_evt_broadcast_after = hook_evt_broadcast_after,
837 .hook_evt_name = hook_evt_name,
838 .hook_evt_addref = hook_evt_addref,
839 .hook_evt_unref = hook_evt_unref
842 /*******************************************************************************/
843 /***** trace the sessions *****/
844 /*******************************************************************************/
846 static void hook_session(void *closure, const struct afb_hookid *hookid, struct afb_session *session, const char *action, const char *format, ...)
850 va_start(ap, format);
851 emit(closure, hookid, "session", "{ss ss}", format, ap,
852 "uuid", afb_session_uuid(session),
857 static void hook_session_create(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
859 hook_session(closure, hookid, session, "create", NULL);
862 static void hook_session_close(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
864 hook_session(closure, hookid, session, "close", NULL);
867 static void hook_session_destroy(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
869 hook_session(closure, hookid, session, "destroy", NULL);
872 static void hook_session_addref(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
874 hook_session(closure, hookid, session, "addref", NULL);
877 static void hook_session_unref(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
879 hook_session(closure, hookid, session, "unref", NULL);
882 static struct afb_hook_session_itf hook_session_itf = {
883 .hook_session_create = hook_session_create,
884 .hook_session_close = hook_session_close,
885 .hook_session_destroy = hook_session_destroy,
886 .hook_session_addref = hook_session_addref,
887 .hook_session_unref = hook_session_unref
890 /*******************************************************************************/
891 /***** trace the globals *****/
892 /*******************************************************************************/
894 static void hook_global(void *closure, const struct afb_hookid *hookid, const char *action, const char *format, ...)
898 va_start(ap, format);
899 emit(closure, hookid, "global", "{ss}", format, ap, "action", action);
903 static void hook_global_vverbose(void *closure, const struct afb_hookid *hookid, int level, const char *file, int line, const char *function, const char *fmt, va_list args)
905 struct json_object *pos;
914 len = vasprintf(&msg, fmt, ap);
918 wrap_json_pack(&pos, "{ss si ss*}", "file", file, "line", line, "function", function);
920 hook_global(closure, hookid, "vverbose", "{si ss* ss? so*}",
922 "type", verbosity_level_name(level),
923 len < 0 ? "format" : "message", len < 0 ? fmt : msg,
929 static struct afb_hook_global_itf hook_global_itf = {
930 .hook_global_vverbose = hook_global_vverbose,
933 /*******************************************************************************/
934 /***** abstract types *****/
935 /*******************************************************************************/
941 void (*unref)(void*);
942 int (*get_flag)(const char*);
944 abstracting[Trace_Type_Count] =
949 .unref = (void(*)(void*))afb_hook_unref_xreq,
950 .get_flag = afb_hook_flags_xreq_from_text
955 .unref = (void(*)(void*))afb_hook_unref_api,
956 .get_flag = afb_hook_flags_api_from_text
961 .unref = (void(*)(void*))afb_hook_unref_evt,
962 .get_flag = afb_hook_flags_evt_from_text
964 [Trace_Type_Session] =
967 .unref = (void(*)(void*))afb_hook_unref_session,
968 .get_flag = afb_hook_flags_session_from_text
970 [Trace_Type_Global] =
973 .unref = (void(*)(void*))afb_hook_unref_global,
974 .get_flag = afb_hook_flags_global_from_text
976 #if !defined(REMOVE_LEGACY_TRACE)
977 [Trace_Legacy_Type_Ditf] =
980 .unref = (void(*)(void*))afb_hook_unref_api,
981 .get_flag = afb_hook_flags_legacy_ditf_from_text
983 [Trace_Legacy_Type_Svc] =
986 .unref = (void(*)(void*))afb_hook_unref_api,
987 .get_flag = afb_hook_flags_legacy_svc_from_text
992 /*******************************************************************************/
993 /***** handle trace data *****/
994 /*******************************************************************************/
996 /* drop hooks of 'trace' matching 'tag' and 'event' and 'session' */
997 static void trace_unhook(struct afb_trace *trace, struct tag *tag, struct event *event, struct afb_session *session)
1000 struct hook *hook, **prev;
1002 /* remove any event */
1003 for (i = 0 ; i < Trace_Type_Count ; i++) {
1004 prev = &trace->hooks[i];
1005 while ((hook = *prev)) {
1006 if ((tag && tag != hook->tag)
1007 || (event && event != hook->event)
1008 || (session && session != hook->session))
1012 abstracting[i].unref(hook->handler);
1019 /* cleanup: removes unused tags, events and sessions of the 'trace' */
1020 static void trace_cleanup(struct afb_trace *trace)
1024 struct tag *tag, **ptag;
1025 struct event *event, **pevent;
1028 ptag = &trace->tags;
1029 while ((tag = *ptag)) {
1030 /* search for tag */
1031 for (hook = NULL, i = 0 ; !hook && i < Trace_Type_Count ; i++)
1032 for (hook = trace->hooks[i] ; hook && hook->tag != tag ; hook = hook->next);
1033 /* keep or free whether used or not */
1042 pevent = &trace->events;
1043 while ((event = *pevent)) {
1044 /* search for event */
1045 for (hook = NULL, i = 0 ; !hook && i < Trace_Type_Count ; i++)
1046 for (hook = trace->hooks[i] ; hook && hook->event != event ; hook = hook->next);
1047 /* keep or free whether used or not */
1049 pevent = &event->next;
1051 *pevent = event->next;
1052 afb_evt_evtid_unref(event->evtid);
1059 * Get the tag of 'name' within 'trace'.
1060 * If 'alloc' isn't zero, create the tag and add it.
1062 static struct tag *trace_get_tag(struct afb_trace *trace, const char *name, int alloc)
1066 /* search the tag of 'name' */
1068 while (tag && strcmp(name, tag->tag))
1071 if (!tag && alloc) {
1072 /* creation if needed */
1073 tag = malloc(sizeof * tag + 1 + strlen(name));
1075 strcpy(tag->tag, name);
1076 tag->next = trace->tags;
1084 * Get the event of 'name' within 'trace'.
1085 * If 'alloc' isn't zero, create the event and add it.
1087 static struct event *trace_get_event(struct afb_trace *trace, const char *name, int alloc)
1089 struct event *event;
1091 /* search the event */
1092 event = trace->events;
1093 while (event && strcmp(afb_evt_evtid_name(event->evtid), name))
1094 event = event->next;
1096 if (!event && alloc) {
1097 event = malloc(sizeof * event);
1099 event->evtid = afb_evt_evtid_create2(trace->apiname, name);
1101 event->next = trace->events;
1102 trace->events = event;
1113 * called on session closing
1115 static void session_closed(void *item)
1117 struct cookie *cookie = item;
1119 pthread_mutex_lock(&cookie->trace->mutex);
1120 trace_unhook(cookie->trace, NULL, NULL, cookie->session);
1121 pthread_mutex_unlock(&cookie->trace->mutex);
1126 * records the cookie of session for tracking close
1128 static void *session_open(void *closure)
1130 struct cookie *param = closure, *cookie;
1131 cookie = malloc(sizeof *cookie);
1138 * Get the session of 'uuid' within 'trace'.
1139 * If 'alloc' isn't zero, create the session and add it.
1141 static struct afb_session *trace_get_session_by_uuid(struct afb_trace *trace, const char *uuid, int alloc)
1143 struct cookie cookie;
1146 cookie.session = afb_session_search(uuid);
1148 cookie.session = afb_session_get(uuid, AFB_SESSION_TIMEOUT_DEFAULT, NULL);
1149 if (cookie.session) {
1150 cookie.trace = trace;
1151 afb_session_cookie(cookie.session, cookie.trace, session_open, session_closed, &cookie, 0);
1154 return cookie.session;
1157 static struct hook *trace_make_detached_hook(struct afb_trace *trace, const char *event, const char *tag)
1161 tag = tag ?: DEFAULT_TAG_NAME;
1162 event = event ?: DEFAULT_EVENT_NAME;
1163 hook = malloc(sizeof *hook);
1165 hook->tag = trace_get_tag(trace, tag, 1);
1166 hook->event = trace_get_event(trace, event, 1);
1167 hook->session = NULL;
1168 hook->handler = NULL;
1173 static void trace_attach_hook(struct afb_trace *trace, struct hook *hook, enum trace_type type)
1175 hook->next = trace->hooks[type];
1176 trace->hooks[type] = hook;
1179 /*******************************************************************************/
1180 /***** handle client requests *****/
1181 /*******************************************************************************/
1185 struct afb_trace *trace;
1192 struct context *context;
1196 const char *apiname;
1197 const char *verbname;
1198 const char *pattern;
1199 int flags[Trace_Type_Count];
1202 static void addhook(struct desc *desc, enum trace_type type)
1205 struct afb_session *session;
1206 struct afb_session *bind;
1207 struct afb_trace *trace = desc->context->trace;
1209 /* check permission for bound traces */
1210 bind = trace->bound;
1212 if (type != Trace_Type_Xreq) {
1213 ctxt_error(&desc->context->errors, "tracing %s is forbidden", abstracting[type].name);
1217 ctxt_error(&desc->context->errors, "setting session is forbidden");
1222 /* allocate the hook */
1223 hook = trace_make_detached_hook(trace, desc->name, desc->tag);
1225 ctxt_error(&desc->context->errors, "allocation of hook failed");
1229 /* create the hook handler */
1231 case Trace_Type_Xreq:
1233 session = afb_session_addref(bind);
1235 session = trace_get_session_by_uuid(trace, desc->uuid, 1);
1237 ctxt_error(&desc->context->errors, "allocation of session failed");
1242 hook->handler = afb_hook_create_xreq(desc->apiname, desc->verbname, session,
1243 desc->flags[type], &hook_xreq_itf, hook);
1244 afb_session_unref(session);
1246 case Trace_Type_Api:
1247 hook->handler = afb_hook_create_api(desc->apiname, desc->flags[type], &hook_api_itf, hook);
1249 case Trace_Type_Evt:
1250 hook->handler = afb_hook_create_evt(desc->pattern, desc->flags[type], &hook_evt_itf, hook);
1252 case Trace_Type_Session:
1253 hook->handler = afb_hook_create_session(desc->uuid, desc->flags[type], &hook_session_itf, hook);
1255 case Trace_Type_Global:
1256 hook->handler = afb_hook_create_global(desc->flags[type], &hook_global_itf, hook);
1261 if (!hook->handler) {
1262 ctxt_error(&desc->context->errors, "creation of hook failed");
1267 /* attach and activate the hook */
1268 afb_req_subscribe(desc->context->req, afb_evt_event_x2_from_evtid(hook->event->evtid));
1269 trace_attach_hook(trace, hook, type);
1272 static void addhooks(struct desc *desc)
1276 #if !defined(REMOVE_LEGACY_TRACE)
1277 desc->flags[Trace_Type_Api] |= desc->flags[Trace_Legacy_Type_Ditf] | desc->flags[Trace_Legacy_Type_Svc];
1278 desc->flags[Trace_Legacy_Type_Ditf] = desc->flags[Trace_Legacy_Type_Svc] = 0;
1281 for (i = 0 ; i < Trace_Type_Count ; i++) {
1287 static void add_flags(void *closure, struct json_object *object, enum trace_type type)
1290 const char *name, *queried;
1291 struct desc *desc = closure;
1293 if (wrap_json_unpack(object, "s", &name))
1294 ctxt_error(&desc->context->errors, "unexpected %s value %s",
1295 abstracting[type].name,
1296 json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE));
1298 queried = (name[0] == '*' && !name[1]) ? "all" : name;
1299 value = abstracting[type].get_flag(queried);
1301 desc->flags[type] |= value;
1303 ctxt_error(&desc->context->errors, "unknown %s name %s",
1304 abstracting[type].name, name);
1308 static void add_xreq_flags(void *closure, struct json_object *object)
1310 add_flags(closure, object, Trace_Type_Xreq);
1313 #if !defined(REMOVE_LEGACY_TRACE)
1314 static void legacy_add_ditf_flags(void *closure, struct json_object *object)
1316 add_flags(closure, object, Trace_Legacy_Type_Ditf);
1319 static void legacy_add_svc_flags(void *closure, struct json_object *object)
1321 add_flags(closure, object, Trace_Legacy_Type_Svc);
1325 static void add_api_flags(void *closure, struct json_object *object)
1327 add_flags(closure, object, Trace_Type_Api);
1330 static void add_evt_flags(void *closure, struct json_object *object)
1332 add_flags(closure, object, Trace_Type_Evt);
1335 static void add_session_flags(void *closure, struct json_object *object)
1337 add_flags(closure, object, Trace_Type_Session);
1340 static void add_global_flags(void *closure, struct json_object *object)
1342 add_flags(closure, object, Trace_Type_Global);
1346 static void add(void *closure, struct json_object *object)
1350 struct json_object *request, *event, *sub, *global, *session, *api;
1351 #if !defined(REMOVE_LEGACY_TRACE)
1352 struct json_object *daemon, *service;
1355 memcpy (&desc, closure, sizeof desc);
1356 request = event = sub = global = session = api = NULL;
1357 #if !defined(REMOVE_LEGACY_TRACE)
1358 daemon = service = NULL;
1361 rc = wrap_json_unpack(object, "{s?s s?s s?s s?s s?s s?s s?o s?o s?o s?o s?o s?o s?o}",
1364 "apiname", &desc.apiname,
1365 "verbname", &desc.verbname,
1367 "pattern", &desc.pattern,
1369 "request", &request,
1370 #if !defined(REMOVE_LEGACY_TRACE)
1372 "service", &service,
1375 "session", &session,
1381 if (desc.apiname && desc.apiname[0] == '*' && !desc.apiname[1])
1382 desc.apiname = NULL;
1384 if (desc.verbname && desc.verbname[0] == '*' && !desc.verbname[1])
1385 desc.verbname = NULL;
1387 if (desc.uuid && desc.uuid[0] == '*' && !desc.uuid[1])
1390 /* get what is expected */
1392 wrap_json_optarray_for_all(request, add_xreq_flags, &desc);
1395 wrap_json_optarray_for_all(api, add_api_flags, &desc);
1397 #if !defined(REMOVE_LEGACY_TRACE)
1399 wrap_json_optarray_for_all(daemon, legacy_add_ditf_flags, &desc);
1402 wrap_json_optarray_for_all(service, legacy_add_svc_flags, &desc);
1406 wrap_json_optarray_for_all(event, add_evt_flags, &desc);
1409 wrap_json_optarray_for_all(session, add_session_flags, &desc);
1412 wrap_json_optarray_for_all(global, add_global_flags, &desc);
1416 wrap_json_optarray_for_all(sub, add, &desc);
1421 wrap_json_optarray_for_all(object, add_xreq_flags, &desc);
1426 /* drop hooks of given tag */
1427 static void drop_tag(void *closure, struct json_object *object)
1430 struct context *context = closure;
1434 rc = wrap_json_unpack(object, "s", &name);
1436 ctxt_error(&context->errors, "unexpected tag value %s", json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE));
1438 tag = trace_get_tag(context->trace, name, 0);
1440 ctxt_error(&context->errors, "tag %s not found", name);
1442 trace_unhook(context->trace, tag, NULL, NULL);
1446 /* drop hooks of given event */
1447 static void drop_event(void *closure, struct json_object *object)
1450 struct context *context = closure;
1451 struct event *event;
1454 rc = wrap_json_unpack(object, "s", &name);
1456 ctxt_error(&context->errors, "unexpected event value %s", json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE));
1458 event = trace_get_event(context->trace, name, 0);
1460 ctxt_error(&context->errors, "event %s not found", name);
1462 trace_unhook(context->trace, NULL, event, NULL);
1466 /* drop hooks of given session */
1467 static void drop_session(void *closure, struct json_object *object)
1470 struct context *context = closure;
1471 struct afb_session *session;
1474 rc = wrap_json_unpack(object, "s", &uuid);
1476 ctxt_error(&context->errors, "unexpected session value %s", json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE));
1478 session = trace_get_session_by_uuid(context->trace, uuid, 0);
1480 ctxt_error(&context->errors, "session %s not found", uuid);
1482 trace_unhook(context->trace, NULL, NULL, session);
1483 afb_session_unref(session);
1488 /*******************************************************************************/
1489 /***** public interface *****/
1490 /*******************************************************************************/
1492 /* allocates an afb_trace instance */
1493 struct afb_trace *afb_trace_create(const char *apiname, struct afb_session *bound)
1495 struct afb_trace *trace;
1499 trace = calloc(1, sizeof *trace);
1501 trace->refcount = 1;
1502 trace->bound = bound;
1503 trace->apiname = apiname;
1504 pthread_mutex_init(&trace->mutex, NULL);
1509 /* add a reference to the trace */
1510 void afb_trace_addref(struct afb_trace *trace)
1512 __atomic_add_fetch(&trace->refcount, 1, __ATOMIC_RELAXED);
1515 /* drop one reference to the trace */
1516 void afb_trace_unref(struct afb_trace *trace)
1518 if (trace && !__atomic_sub_fetch(&trace->refcount, 1, __ATOMIC_RELAXED)) {
1520 trace_unhook(trace, NULL, NULL, NULL);
1521 trace_cleanup(trace);
1522 pthread_mutex_destroy(&trace->mutex);
1528 int afb_trace_add(afb_req_t req, struct json_object *args, struct afb_trace *trace)
1530 struct context context;
1533 memset(&context, 0, sizeof context);
1534 context.trace = trace;
1537 memset(&desc, 0, sizeof desc);
1538 desc.context = &context;
1540 pthread_mutex_lock(&trace->mutex);
1541 wrap_json_optarray_for_all(args, add, &desc);
1542 pthread_mutex_unlock(&trace->mutex);
1544 if (!context.errors)
1547 afb_req_fail(req, "error-detected", context.errors);
1548 free(context.errors);
1553 int afb_trace_drop(afb_req_t req, struct json_object *args, struct afb_trace *trace)
1556 struct context context;
1557 struct json_object *tags, *events, *uuids;
1559 memset(&context, 0, sizeof context);
1560 context.trace = trace;
1563 /* special: boolean value */
1564 if (!wrap_json_unpack(args, "b", &rc)) {
1566 pthread_mutex_lock(&trace->mutex);
1567 trace_unhook(trace, NULL, NULL, NULL);
1568 trace_cleanup(trace);
1569 pthread_mutex_unlock(&trace->mutex);
1574 tags = events = uuids = NULL;
1575 rc = wrap_json_unpack(args, "{s?o s?o s?o}",
1580 if (rc < 0 || !(events || tags || uuids)) {
1581 afb_req_fail(req, "error-detected", "bad drop arguments");
1585 pthread_mutex_lock(&trace->mutex);
1588 wrap_json_optarray_for_all(tags, drop_tag, &context);
1591 wrap_json_optarray_for_all(events, drop_event, &context);
1594 wrap_json_optarray_for_all(uuids, drop_session, &context);
1596 trace_cleanup(trace);
1598 pthread_mutex_unlock(&trace->mutex);
1600 if (!context.errors)
1603 afb_req_fail(req, "error-detected", context.errors);
1604 free(context.errors);