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.
29 #include <json-c/json.h>
30 #if !defined(JSON_C_TO_STRING_NOSLASHESCAPE)
31 #define JSON_C_TO_STRING_NOSLASHESCAPE 0
34 #define AFB_BINDING_VERSION 3
35 #include <afb/afb-binding.h>
38 #include "afb-hook-flags.h"
40 #include "afb-session.h"
42 #include "afb-export.h"
44 #include "afb-session.h"
45 #include "afb-trace.h"
47 #include "wrap-json.h"
50 /*******************************************************************************/
51 /***** default names *****/
52 /*******************************************************************************/
54 #if !defined(DEFAULT_EVENT_NAME)
55 # define DEFAULT_EVENT_NAME "trace"
57 #if !defined(DEFAULT_TAG_NAME)
58 # define DEFAULT_TAG_NAME "trace"
61 /*******************************************************************************/
63 /*******************************************************************************/
67 struct tag *next; /* link to the next */
68 char tag[1]; /* name of the tag */
71 /* struct for events */
73 struct event *next; /* link to the next event */
74 struct afb_evtid *evtid; /* the event */
77 /* struct for sessions */
79 struct afb_session *session; /* the session */
80 struct afb_trace *trace; /* the tracer */
83 /* struct for recording hooks */
85 struct hook *next; /* link to next hook */
86 void *handler; /* the handler of the hook */
87 struct event *event; /* the associated event */
88 struct tag *tag; /* the associated tag */
89 struct afb_session *session; /* the associated session */
95 Trace_Type_Xreq, /* xreq hooks */
96 Trace_Type_Api, /* api hooks */
97 Trace_Type_Evt, /* evt hooks */
98 Trace_Type_Session, /* session hooks */
99 Trace_Type_Global, /* global hooks */
100 #if !defined(REMOVE_LEGACY_TRACE)
101 Trace_Legacy_Type_Ditf, /* export hooks */
102 Trace_Legacy_Type_Svc, /* export hooks */
104 Trace_Type_Count, /* count of types of hooks */
110 int refcount; /* reference count */
111 pthread_mutex_t mutex; /* concurrency management */
112 const char *apiname; /* api name for events */
113 struct afb_session *bound; /* bound to session */
114 struct event *events; /* list of events */
115 struct tag *tags; /* list of tags */
116 struct hook *hooks[Trace_Type_Count]; /* hooks */
119 /*******************************************************************************/
120 /***** utility functions *****/
121 /*******************************************************************************/
123 static void ctxt_error(char **errors, const char *format, ...)
131 va_start(ap, format);
132 len = vsnprintf(buffer, sizeof buffer, format, ap);
134 if (len > (int)(sizeof buffer - 2))
135 len = (int)(sizeof buffer - 2);
136 buffer[len++] = '\n';
140 sz = errs ? strlen(errs) : 0;
141 errs = realloc(errs, sz + (size_t)len);
143 memcpy(errs + sz, buffer, len);
149 static struct json_object *timestamp(const struct afb_hookid *hookid)
151 #if JSON_C_MAJOR_VERSION > 0 || JSON_C_MINOR_VERSION >= 12
154 snprintf(ts, sizeof ts, "%llu.%06lu",
155 (long long unsigned)hookid->time.tv_sec,
156 (long unsigned)((hookid->time.tv_nsec + 500) / 1000));
158 return json_object_new_double_s(0.0f, ts); /* the real value isn't used */
160 return json_object_new_double((double)hookid->time.tv_sec +
161 (double)hookid->time.tv_nsec * .000000001);
165 /* verbosity level name or NULL */
166 static const char *verbosity_level_name(int level)
168 static const char *names[] = {
176 return level >= Log_Level_Error && level <= Log_Level_Debug ? names[level - Log_Level_Error] : NULL;
180 static void emit(void *closure, const struct afb_hookid *hookid, const char *type, const char *fmt1, const char *fmt2, va_list ap2, ...)
182 struct hook *hook = closure;
183 struct json_object *data, *data1, *data2;
186 data1 = data2 = data = NULL;
188 wrap_json_vpack(&data1, fmt1, ap1);
191 wrap_json_vpack(&data2, fmt2, ap2);
193 wrap_json_pack(&data, "{so ss ss si so so*}",
194 "time", timestamp(hookid),
195 "tag", hook->tag->tag,
197 "id", (int)(hookid->id & INT_MAX),
201 afb_evt_evtid_push(hook->event->evtid, data);
204 /*******************************************************************************/
205 /***** trace the requests *****/
206 /*******************************************************************************/
208 static void hook_xreq(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *action, const char *format, ...)
210 struct json_object *cred = NULL;
211 const char *session = NULL;
214 if (xreq->context.session)
215 session = afb_session_uuid(xreq->context.session);
218 wrap_json_pack(&cred, "{si ss si si ss* ss*}",
219 "uid", (int)xreq->cred->uid,
220 "user", xreq->cred->user,
221 "gid", (int)xreq->cred->gid,
222 "pid", (int)xreq->cred->pid,
223 "label", xreq->cred->label,
226 va_start(ap, format);
227 emit(closure, hookid, "request", "{si ss ss ss so* ss*}", format, ap,
228 "index", xreq->hookindex,
229 "api", xreq->request.called_api,
230 "verb", xreq->request.called_verb,
237 static void hook_xreq_begin(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
239 hook_xreq(closure, hookid, xreq, "begin", "{sO?}",
240 "json", afb_xreq_unhooked_json((struct afb_xreq*)xreq));
243 static void hook_xreq_end(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
245 hook_xreq(closure, hookid, xreq, "end", NULL);
248 static void hook_xreq_json(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *obj)
250 hook_xreq(closure, hookid, xreq, "json", "{sO?}",
254 static void hook_xreq_get(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *name, struct afb_arg arg)
256 hook_xreq(closure, hookid, xreq, "get", "{ss? ss? ss? ss?}",
263 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)
265 hook_xreq(closure, hookid, xreq, "reply", "{sO? ss? ss?}",
271 static void hook_xreq_context_get(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, void *value)
273 hook_xreq(closure, hookid, xreq, "context_get", NULL);
276 static void hook_xreq_context_set(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, void *value, void (*free_value)(void*))
278 hook_xreq(closure, hookid, xreq, "context_set", NULL);
281 static void hook_xreq_addref(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
283 hook_xreq(closure, hookid, xreq, "addref", NULL);
286 static void hook_xreq_unref(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
288 hook_xreq(closure, hookid, xreq, "unref", NULL);
291 static void hook_xreq_session_close(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
293 hook_xreq(closure, hookid, xreq, "session_close", NULL);
296 static void hook_xreq_session_set_LOA(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, unsigned level, int result)
298 hook_xreq(closure, hookid, xreq, "session_set_LOA", "{si si}",
303 static void hook_xreq_subscribe(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_event_x2 *event, int result)
305 hook_xreq(closure, hookid, xreq, "subscribe", "{s{ss si} si}",
307 "name", afb_evt_event_x2_fullname(event),
308 "id", afb_evt_event_x2_id(event),
312 static void hook_xreq_unsubscribe(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_event_x2 *event, int result)
314 hook_xreq(closure, hookid, xreq, "unsubscribe", "{s{ss? si} si}",
316 "name", afb_evt_event_x2_fullname(event),
317 "id", afb_evt_event_x2_id(event),
321 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)
323 hook_xreq(closure, hookid, xreq, "subcall", "{ss? ss? sO?}",
329 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)
331 hook_xreq(closure, hookid, xreq, "subcall_result", "{sO? ss? ss?}",
337 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)
339 hook_xreq(closure, hookid, xreq, "subcallsync", "{ss? ss? sO?}",
345 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)
347 hook_xreq(closure, hookid, xreq, "subcallsync_result", "{si sO? ss? ss?}",
354 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)
356 struct json_object *pos;
365 len = vasprintf(&msg, fmt, ap);
369 wrap_json_pack(&pos, "{ss si ss*}", "file", file, "line", line, "function", func);
371 hook_xreq(closure, hookid, xreq, "vverbose", "{si ss* ss? so*}",
373 "type", verbosity_level_name(level),
374 len < 0 ? "format" : "message", len < 0 ? fmt : msg,
380 static void hook_xreq_store(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_stored_req *sreq)
382 hook_xreq(closure, hookid, xreq, "store", NULL);
385 static void hook_xreq_unstore(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
387 hook_xreq(closure, hookid, xreq, "unstore", NULL);
390 static void hook_xreq_has_permission(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *permission, int result)
392 hook_xreq(closure, hookid, xreq, "has_permission", "{ss sb}",
393 "permission", permission,
397 static void hook_xreq_get_application_id(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, char *result)
399 hook_xreq(closure, hookid, xreq, "get_application_id", "{ss?}",
403 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)
405 char pc[50], pf[50], pv[50], pr[50];
406 snprintf(pc, sizeof pc, "%p", create_value);
407 snprintf(pf, sizeof pf, "%p", free_value);
408 snprintf(pv, sizeof pv, "%p", create_closure);
409 snprintf(pr, sizeof pr, "%p", result);
410 hook_xreq(closure, hookid, xreq, "context_make", "{sb ss ss ss ss}",
418 static void hook_xreq_get_uid(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int result)
420 hook_xreq(closure, hookid, xreq, "get_uid", "{si}",
424 static void hook_xreq_get_client_info(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *result)
426 hook_xreq(closure, hookid, xreq, "get_client_info", "{sO}",
430 static struct afb_hook_xreq_itf hook_xreq_itf = {
431 .hook_xreq_begin = hook_xreq_begin,
432 .hook_xreq_end = hook_xreq_end,
433 .hook_xreq_json = hook_xreq_json,
434 .hook_xreq_get = hook_xreq_get,
435 .hook_xreq_reply = hook_xreq_reply,
436 .hook_xreq_legacy_context_get = hook_xreq_context_get,
437 .hook_xreq_legacy_context_set = hook_xreq_context_set,
438 .hook_xreq_addref = hook_xreq_addref,
439 .hook_xreq_unref = hook_xreq_unref,
440 .hook_xreq_session_close = hook_xreq_session_close,
441 .hook_xreq_session_set_LOA = hook_xreq_session_set_LOA,
442 .hook_xreq_subscribe = hook_xreq_subscribe,
443 .hook_xreq_unsubscribe = hook_xreq_unsubscribe,
444 .hook_xreq_subcall = hook_xreq_subcall,
445 .hook_xreq_subcall_result = hook_xreq_subcall_result,
446 .hook_xreq_subcallsync = hook_xreq_subcallsync,
447 .hook_xreq_subcallsync_result = hook_xreq_subcallsync_result,
448 .hook_xreq_vverbose = hook_xreq_vverbose,
449 .hook_xreq_legacy_store = hook_xreq_store,
450 .hook_xreq_legacy_unstore = hook_xreq_unstore,
451 .hook_xreq_has_permission = hook_xreq_has_permission,
452 .hook_xreq_get_application_id = hook_xreq_get_application_id,
453 .hook_xreq_context_make = hook_xreq_context_make,
454 .hook_xreq_get_uid = hook_xreq_get_uid,
455 .hook_xreq_get_client_info = hook_xreq_get_client_info,
458 /*******************************************************************************/
459 /***** trace the api interface *****/
460 /*******************************************************************************/
462 static void hook_api(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *action, const char *format, ...)
466 va_start(ap, format);
467 emit(closure, hookid, "api", "{ss ss}", format, ap,
468 "api", afb_export_apiname(export),
473 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)
475 hook_api(closure, hookid, export, "event_broadcast_before", "{ss sO?}",
476 "name", name, "data", object);
479 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)
481 hook_api(closure, hookid, export, "event_broadcast_after", "{ss sO? si}",
482 "name", name, "data", object, "result", result);
485 static void hook_api_get_event_loop(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_event *result)
487 hook_api(closure, hookid, export, "get_event_loop", NULL);
490 static void hook_api_get_user_bus(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_bus *result)
492 hook_api(closure, hookid, export, "get_user_bus", NULL);
495 static void hook_api_get_system_bus(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_bus *result)
497 hook_api(closure, hookid, export, "get_system_bus", NULL);
500 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)
502 struct json_object *pos;
511 len = vasprintf(&msg, fmt, ap);
515 wrap_json_pack(&pos, "{ss si ss*}", "file", file, "line", line, "function", function);
517 hook_api(closure, hookid, export, "vverbose", "{si ss* ss? so*}",
519 "type", verbosity_level_name(level),
520 len < 0 ? "format" : "message", len < 0 ? fmt : msg,
526 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)
528 hook_api(closure, hookid, export, "event_make", "{ss ss si}",
529 "name", name, "event", afb_evt_event_x2_fullname(result), "id", afb_evt_event_x2_id(result));
532 static void hook_api_rootdir_get_fd(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result)
534 char path[PATH_MAX], proc[100];
535 const char *key, *val;
539 snprintf(proc, sizeof proc, "/proc/self/fd/%d", result);
540 s = readlink(proc, path, sizeof path);
541 path[s < 0 ? 0 : s >= sizeof path ? sizeof path - 1 : s] = 0;
546 val = strerror(errno);
549 hook_api(closure, hookid, export, "rootdir_get_fd", "{ss}", key, val);
552 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)
554 char path[PATH_MAX], proc[100];
555 const char *key, *val;
559 snprintf(proc, sizeof proc, "/proc/self/fd/%d", result);
560 s = readlink(proc, path, sizeof path);
561 path[s < 0 ? 0 : s >= sizeof path ? sizeof path - 1 : s] = 0;
566 val = strerror(errno);
569 hook_api(closure, hookid, export, "rootdir_open_locale", "{ss si ss* ss}",
576 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)
578 hook_api(closure, hookid, export, "queue_job", "{ss}", "result", result);
581 static void hook_api_unstore_req(void * closure, const struct afb_hookid *hookid, const struct afb_export *export, struct afb_stored_req *sreq)
583 hook_api(closure, hookid, export, "unstore_req", NULL);
586 static void hook_api_require_api(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, int initialized)
588 hook_api(closure, hookid, export, "require_api", "{ss sb}", "name", name, "initialized", initialized);
591 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)
593 hook_api(closure, hookid, export, "require_api_result", "{ss sb si}", "name", name, "initialized", initialized, "result", result);
596 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)
598 hook_api(closure, hookid, export, "add_alias", "{si ss? ss}", "status", result, "api", api, "alias", alias);
601 static void hook_api_start_before(void *closure, const struct afb_hookid *hookid, const struct afb_export *export)
603 hook_api(closure, hookid, export, "start_before", NULL);
606 static void hook_api_start_after(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int status)
608 hook_api(closure, hookid, export, "start_after", "{si}", "result", status);
611 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)
613 hook_api(closure, hookid, export, "on_event_before", "{ss si sO*}",
614 "event", event, "id", evtid, "data", object);
617 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)
619 hook_api(closure, hookid, export, "on_event_after", "{ss si sO?}",
620 "event", event, "id", evtid, "data", object);
623 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)
625 hook_api(closure, hookid, export, "call", "{ss ss sO?}",
626 "api", api, "verb", verb, "args", args);
629 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)
631 hook_api(closure, hookid, export, "call_result", "{sO? ss? ss?}",
632 "object", object, "error", error, "info", info);
635 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)
637 hook_api(closure, hookid, export, "callsync", "{ss ss sO?}",
638 "api", api, "verb", verb, "args", args);
641 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)
643 hook_api(closure, hookid, export, "callsync_result", "{si sO? ss? ss?}",
644 "status", status, "object", object, "error", error, "info", info);
647 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)
649 hook_api(closure, hookid, export, "new_api.before", "{ss ss? sb}",
650 "api", api, "info", info, "noconcurrency", noconcurrency);
653 static void hook_api_new_api_after(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *api)
655 hook_api(closure, hookid, export, "new_api.after", "{si ss}",
656 "status", result, "api", api);
659 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)
661 hook_api(closure, hookid, export, "set_verbs_v2", "{si}", "status", result);
664 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)
666 hook_api(closure, hookid, export, "set_verbs_v3", "{si}", "status", result);
670 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)
672 hook_api(closure, hookid, export, "add_verb", "{si ss ss? sb}", "status", result, "verb", verb, "info", info, "glob", glob);
675 static void hook_api_api_del_verb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *verb)
677 hook_api(closure, hookid, export, "del_verb", "{si ss}", "status", result, "verb", verb);
680 static void hook_api_api_set_on_event(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result)
682 hook_api(closure, hookid, export, "set_on_event", "{si}", "status", result);
685 static void hook_api_api_set_on_init(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result)
687 hook_api(closure, hookid, export, "set_on_init", "{si}", "status", result);
690 static void hook_api_api_seal(void *closure, const struct afb_hookid *hookid, const struct afb_export *export)
692 hook_api(closure, hookid, export, "seal", NULL);
695 static void hook_api_event_handler_add(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_add", "{si ss?}", "status", result, "pattern", pattern);
700 static void hook_api_event_handler_del(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *pattern)
702 hook_api(closure, hookid, export, "event_handler_del", "{si ss?}", "status", result, "pattern", pattern);
705 static void hook_api_class_provide(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *name)
707 hook_api(closure, hookid, export, "class_provide", "{si ss?}", "status", result, "name", name);
710 static void hook_api_class_require(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *name)
712 hook_api(closure, hookid, export, "class_require", "{si ss?}", "status", result, "name", name);
715 static void hook_api_delete_api(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result)
717 hook_api(closure, hookid, export, "delete_api", "{si}", "status", result);
720 static struct afb_hook_api_itf hook_api_itf = {
721 .hook_api_event_broadcast_before = hook_api_event_broadcast_before,
722 .hook_api_event_broadcast_after = hook_api_event_broadcast_after,
723 .hook_api_get_event_loop = hook_api_get_event_loop,
724 .hook_api_get_user_bus = hook_api_get_user_bus,
725 .hook_api_get_system_bus = hook_api_get_system_bus,
726 .hook_api_vverbose = hook_api_vverbose,
727 .hook_api_event_make = hook_api_event_make,
728 .hook_api_rootdir_get_fd = hook_api_rootdir_get_fd,
729 .hook_api_rootdir_open_locale = hook_api_rootdir_open_locale,
730 .hook_api_queue_job = hook_api_queue_job,
731 .hook_api_legacy_unstore_req = hook_api_unstore_req,
732 .hook_api_require_api = hook_api_require_api,
733 .hook_api_require_api_result = hook_api_require_api_result,
734 .hook_api_add_alias = hook_api_add_alias_cb,
735 .hook_api_start_before = hook_api_start_before,
736 .hook_api_start_after = hook_api_start_after,
737 .hook_api_on_event_before = hook_api_on_event_before,
738 .hook_api_on_event_after = hook_api_on_event_after,
739 .hook_api_call = hook_api_call,
740 .hook_api_call_result = hook_api_call_result,
741 .hook_api_callsync = hook_api_callsync,
742 .hook_api_callsync_result = hook_api_callsync_result,
743 .hook_api_new_api_before = hook_api_new_api_before,
744 .hook_api_new_api_after = hook_api_new_api_after,
745 .hook_api_api_set_verbs_v2 = hook_api_api_set_verbs_v2,
746 .hook_api_api_set_verbs_v3 = hook_api_api_set_verbs_v3,
747 .hook_api_api_add_verb = hook_api_api_add_verb,
748 .hook_api_api_del_verb = hook_api_api_del_verb,
749 .hook_api_api_set_on_event = hook_api_api_set_on_event,
750 .hook_api_api_set_on_init = hook_api_api_set_on_init,
751 .hook_api_api_seal = hook_api_api_seal,
752 .hook_api_event_handler_add = hook_api_event_handler_add,
753 .hook_api_event_handler_del = hook_api_event_handler_del,
754 .hook_api_class_provide = hook_api_class_provide,
755 .hook_api_class_require = hook_api_class_require,
756 .hook_api_delete_api = hook_api_delete_api,
759 /*******************************************************************************/
760 /***** trace the events *****/
761 /*******************************************************************************/
763 static void hook_evt(void *closure, const struct afb_hookid *hookid, const char *evt, int id, const char *action, const char *format, ...)
767 va_start(ap, format);
768 emit(closure, hookid, "event", "{si ss ss}", format, ap,
775 static void hook_evt_create(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
777 hook_evt(closure, hookid, evt, id, "create", NULL);
780 static void hook_evt_push_before(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj)
782 hook_evt(closure, hookid, evt, id, "push_before", "{sO*}", "data", obj);
786 static void hook_evt_push_after(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj, int result)
788 hook_evt(closure, hookid, evt, id, "push_after", "{sO* si}", "data", obj, "result", result);
791 static void hook_evt_broadcast_before(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj)
793 hook_evt(closure, hookid, evt, id, "broadcast_before", "{sO*}", "data", obj);
796 static void hook_evt_broadcast_after(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj, int result)
798 hook_evt(closure, hookid, evt, id, "broadcast_after", "{sO* si}", "data", obj, "result", result);
801 static void hook_evt_name(void *closure, const struct afb_hookid *hookid, const char *evt, int id, const char *result)
803 hook_evt(closure, hookid, evt, id, "name", "{ss}", "result", result);
806 static void hook_evt_addref(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
808 hook_evt(closure, hookid, evt, id, "addref", NULL);
811 static void hook_evt_unref(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
813 hook_evt(closure, hookid, evt, id, "unref", NULL);
816 static struct afb_hook_evt_itf hook_evt_itf = {
817 .hook_evt_create = hook_evt_create,
818 .hook_evt_push_before = hook_evt_push_before,
819 .hook_evt_push_after = hook_evt_push_after,
820 .hook_evt_broadcast_before = hook_evt_broadcast_before,
821 .hook_evt_broadcast_after = hook_evt_broadcast_after,
822 .hook_evt_name = hook_evt_name,
823 .hook_evt_addref = hook_evt_addref,
824 .hook_evt_unref = hook_evt_unref
827 /*******************************************************************************/
828 /***** trace the sessions *****/
829 /*******************************************************************************/
831 static void hook_session(void *closure, const struct afb_hookid *hookid, struct afb_session *session, const char *action, const char *format, ...)
835 va_start(ap, format);
836 emit(closure, hookid, "session", "{ss ss}", format, ap,
837 "uuid", afb_session_uuid(session),
842 static void hook_session_create(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
844 hook_session(closure, hookid, session, "create", "{ss}", "token", afb_session_token(session));
847 static void hook_session_close(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
849 hook_session(closure, hookid, session, "close", NULL);
852 static void hook_session_destroy(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
854 hook_session(closure, hookid, session, "destroy", NULL);
857 static void hook_session_renew(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
859 hook_session(closure, hookid, session, "renew", "{ss}", "token", afb_session_token(session));
862 static void hook_session_addref(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
864 hook_session(closure, hookid, session, "addref", NULL);
867 static void hook_session_unref(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
869 hook_session(closure, hookid, session, "unref", NULL);
872 static struct afb_hook_session_itf hook_session_itf = {
873 .hook_session_create = hook_session_create,
874 .hook_session_close = hook_session_close,
875 .hook_session_destroy = hook_session_destroy,
876 .hook_session_renew = hook_session_renew,
877 .hook_session_addref = hook_session_addref,
878 .hook_session_unref = hook_session_unref
881 /*******************************************************************************/
882 /***** trace the globals *****/
883 /*******************************************************************************/
885 static void hook_global(void *closure, const struct afb_hookid *hookid, const char *action, const char *format, ...)
889 va_start(ap, format);
890 emit(closure, hookid, "global", "{ss}", format, ap, "action", action);
894 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)
896 struct json_object *pos;
905 len = vasprintf(&msg, fmt, ap);
909 wrap_json_pack(&pos, "{ss si ss*}", "file", file, "line", line, "function", function);
911 hook_global(closure, hookid, "vverbose", "{si ss* ss? so*}",
913 "type", verbosity_level_name(level),
914 len < 0 ? "format" : "message", len < 0 ? fmt : msg,
920 static struct afb_hook_global_itf hook_global_itf = {
921 .hook_global_vverbose = hook_global_vverbose,
924 /*******************************************************************************/
925 /***** abstract types *****/
926 /*******************************************************************************/
932 void (*unref)(void*);
933 int (*get_flag)(const char*);
935 abstracting[Trace_Type_Count] =
940 .unref = (void(*)(void*))afb_hook_unref_xreq,
941 .get_flag = afb_hook_flags_xreq_from_text
946 .unref = (void(*)(void*))afb_hook_unref_api,
947 .get_flag = afb_hook_flags_api_from_text
952 .unref = (void(*)(void*))afb_hook_unref_evt,
953 .get_flag = afb_hook_flags_evt_from_text
955 [Trace_Type_Session] =
958 .unref = (void(*)(void*))afb_hook_unref_session,
959 .get_flag = afb_hook_flags_session_from_text
961 [Trace_Type_Global] =
964 .unref = (void(*)(void*))afb_hook_unref_global,
965 .get_flag = afb_hook_flags_global_from_text
967 #if !defined(REMOVE_LEGACY_TRACE)
968 [Trace_Legacy_Type_Ditf] =
971 .unref = (void(*)(void*))afb_hook_unref_api,
972 .get_flag = afb_hook_flags_legacy_ditf_from_text
974 [Trace_Legacy_Type_Svc] =
977 .unref = (void(*)(void*))afb_hook_unref_api,
978 .get_flag = afb_hook_flags_legacy_svc_from_text
983 /*******************************************************************************/
984 /***** handle trace data *****/
985 /*******************************************************************************/
987 /* drop hooks of 'trace' matching 'tag' and 'event' and 'session' */
988 static void trace_unhook(struct afb_trace *trace, struct tag *tag, struct event *event, struct afb_session *session)
991 struct hook *hook, **prev;
993 /* remove any event */
994 for (i = 0 ; i < Trace_Type_Count ; i++) {
995 prev = &trace->hooks[i];
996 while ((hook = *prev)) {
997 if ((tag && tag != hook->tag)
998 || (event && event != hook->event)
999 || (session && session != hook->session))
1003 abstracting[i].unref(hook->handler);
1010 /* cleanup: removes unused tags, events and sessions of the 'trace' */
1011 static void trace_cleanup(struct afb_trace *trace)
1015 struct tag *tag, **ptag;
1016 struct event *event, **pevent;
1019 ptag = &trace->tags;
1020 while ((tag = *ptag)) {
1021 /* search for tag */
1022 for (hook = NULL, i = 0 ; !hook && i < Trace_Type_Count ; i++)
1023 for (hook = trace->hooks[i] ; hook && hook->tag != tag ; hook = hook->next);
1024 /* keep or free whether used or not */
1033 pevent = &trace->events;
1034 while ((event = *pevent)) {
1035 /* search for event */
1036 for (hook = NULL, i = 0 ; !hook && i < Trace_Type_Count ; i++)
1037 for (hook = trace->hooks[i] ; hook && hook->event != event ; hook = hook->next);
1038 /* keep or free whether used or not */
1040 pevent = &event->next;
1042 *pevent = event->next;
1043 afb_evt_evtid_unref(event->evtid);
1050 * Get the tag of 'name' within 'trace'.
1051 * If 'alloc' isn't zero, create the tag and add it.
1053 static struct tag *trace_get_tag(struct afb_trace *trace, const char *name, int alloc)
1057 /* search the tag of 'name' */
1059 while (tag && strcmp(name, tag->tag))
1062 if (!tag && alloc) {
1063 /* creation if needed */
1064 tag = malloc(sizeof * tag + strlen(name));
1066 strcpy(tag->tag, name);
1067 tag->next = trace->tags;
1075 * Get the event of 'name' within 'trace'.
1076 * If 'alloc' isn't zero, create the event and add it.
1078 static struct event *trace_get_event(struct afb_trace *trace, const char *name, int alloc)
1080 struct event *event;
1082 /* search the event */
1083 event = trace->events;
1084 while (event && strcmp(afb_evt_evtid_name(event->evtid), name))
1085 event = event->next;
1087 if (!event && alloc) {
1088 event = malloc(sizeof * event);
1090 event->evtid = afb_evt_evtid_create2(trace->apiname, name);
1092 event->next = trace->events;
1093 trace->events = event;
1104 * called on session closing
1106 static void session_closed(void *item)
1108 struct cookie *cookie = item;
1110 pthread_mutex_lock(&cookie->trace->mutex);
1111 trace_unhook(cookie->trace, NULL, NULL, cookie->session);
1112 pthread_mutex_unlock(&cookie->trace->mutex);
1117 * records the cookie of session for tracking close
1119 static void *session_open(void *closure)
1121 struct cookie *param = closure, *cookie;
1122 cookie = malloc(sizeof *cookie);
1129 * Get the session of 'uuid' within 'trace'.
1130 * If 'alloc' isn't zero, create the session and add it.
1132 static struct afb_session *trace_get_session_by_uuid(struct afb_trace *trace, const char *uuid, int alloc)
1134 struct cookie cookie;
1137 cookie.session = afb_session_search(uuid);
1139 cookie.session = afb_session_get(uuid, AFB_SESSION_TIMEOUT_DEFAULT, NULL);
1140 if (cookie.session) {
1141 cookie.trace = trace;
1142 afb_session_cookie(cookie.session, cookie.trace, session_open, session_closed, &cookie, 0);
1145 return cookie.session;
1148 static struct hook *trace_make_detached_hook(struct afb_trace *trace, const char *event, const char *tag)
1152 tag = tag ?: DEFAULT_TAG_NAME;
1153 event = event ?: DEFAULT_EVENT_NAME;
1154 hook = malloc(sizeof *hook);
1156 hook->tag = trace_get_tag(trace, tag, 1);
1157 hook->event = trace_get_event(trace, event, 1);
1158 hook->session = NULL;
1159 hook->handler = NULL;
1164 static void trace_attach_hook(struct afb_trace *trace, struct hook *hook, enum trace_type type)
1166 hook->next = trace->hooks[type];
1167 trace->hooks[type] = hook;
1170 /*******************************************************************************/
1171 /***** handle client requests *****/
1172 /*******************************************************************************/
1176 struct afb_trace *trace;
1183 struct context *context;
1187 const char *apiname;
1188 const char *verbname;
1189 const char *pattern;
1190 int flags[Trace_Type_Count];
1193 static void addhook(struct desc *desc, enum trace_type type)
1196 struct afb_session *session;
1197 struct afb_session *bind;
1198 struct afb_trace *trace = desc->context->trace;
1200 /* check permission for bound traces */
1201 bind = trace->bound;
1203 if (type != Trace_Type_Xreq) {
1204 ctxt_error(&desc->context->errors, "tracing %s is forbidden", abstracting[type].name);
1208 ctxt_error(&desc->context->errors, "setting session is forbidden");
1213 /* allocate the hook */
1214 hook = trace_make_detached_hook(trace, desc->name, desc->tag);
1216 ctxt_error(&desc->context->errors, "allocation of hook failed");
1220 /* create the hook handler */
1222 case Trace_Type_Xreq:
1224 session = afb_session_addref(bind);
1226 session = trace_get_session_by_uuid(trace, desc->uuid, 1);
1228 ctxt_error(&desc->context->errors, "allocation of session failed");
1233 hook->handler = afb_hook_create_xreq(desc->apiname, desc->verbname, session,
1234 desc->flags[type], &hook_xreq_itf, hook);
1235 afb_session_unref(session);
1237 case Trace_Type_Api:
1238 hook->handler = afb_hook_create_api(desc->apiname, desc->flags[type], &hook_api_itf, hook);
1240 case Trace_Type_Evt:
1241 hook->handler = afb_hook_create_evt(desc->pattern, desc->flags[type], &hook_evt_itf, hook);
1243 case Trace_Type_Session:
1244 hook->handler = afb_hook_create_session(desc->uuid, desc->flags[type], &hook_session_itf, hook);
1246 case Trace_Type_Global:
1247 hook->handler = afb_hook_create_global(desc->flags[type], &hook_global_itf, hook);
1252 if (!hook->handler) {
1253 ctxt_error(&desc->context->errors, "creation of hook failed");
1258 /* attach and activate the hook */
1259 afb_req_subscribe(desc->context->req, afb_evt_event_x2_from_evtid(hook->event->evtid));
1260 trace_attach_hook(trace, hook, type);
1263 static void addhooks(struct desc *desc)
1267 #if !defined(REMOVE_LEGACY_TRACE)
1268 desc->flags[Trace_Type_Api] |= desc->flags[Trace_Legacy_Type_Ditf] | desc->flags[Trace_Legacy_Type_Svc];
1269 desc->flags[Trace_Legacy_Type_Ditf] = desc->flags[Trace_Legacy_Type_Svc] = 0;
1272 for (i = 0 ; i < Trace_Type_Count ; i++) {
1278 static void add_flags(void *closure, struct json_object *object, enum trace_type type)
1281 const char *name, *queried;
1282 struct desc *desc = closure;
1284 if (wrap_json_unpack(object, "s", &name))
1285 ctxt_error(&desc->context->errors, "unexpected %s value %s",
1286 abstracting[type].name,
1287 json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE));
1289 queried = (name[0] == '*' && !name[1]) ? "all" : name;
1290 value = abstracting[type].get_flag(queried);
1292 desc->flags[type] |= value;
1294 ctxt_error(&desc->context->errors, "unknown %s name %s",
1295 abstracting[type].name, name);
1299 static void add_xreq_flags(void *closure, struct json_object *object)
1301 add_flags(closure, object, Trace_Type_Xreq);
1304 #if !defined(REMOVE_LEGACY_TRACE)
1305 static void legacy_add_ditf_flags(void *closure, struct json_object *object)
1307 add_flags(closure, object, Trace_Legacy_Type_Ditf);
1310 static void legacy_add_svc_flags(void *closure, struct json_object *object)
1312 add_flags(closure, object, Trace_Legacy_Type_Svc);
1316 static void add_api_flags(void *closure, struct json_object *object)
1318 add_flags(closure, object, Trace_Type_Api);
1321 static void add_evt_flags(void *closure, struct json_object *object)
1323 add_flags(closure, object, Trace_Type_Evt);
1326 static void add_session_flags(void *closure, struct json_object *object)
1328 add_flags(closure, object, Trace_Type_Session);
1331 static void add_global_flags(void *closure, struct json_object *object)
1333 add_flags(closure, object, Trace_Type_Global);
1337 static void add(void *closure, struct json_object *object)
1341 struct json_object *request, *event, *sub, *global, *session, *api;
1342 #if !defined(REMOVE_LEGACY_TRACE)
1343 struct json_object *daemon, *service;
1346 memcpy (&desc, closure, sizeof desc);
1347 request = event = sub = global = session = api = NULL;
1348 #if !defined(REMOVE_LEGACY_TRACE)
1349 daemon = service = NULL;
1352 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}",
1355 "apiname", &desc.apiname,
1356 "verbname", &desc.verbname,
1358 "pattern", &desc.pattern,
1360 "request", &request,
1361 #if !defined(REMOVE_LEGACY_TRACE)
1363 "service", &service,
1366 "session", &session,
1372 if (desc.apiname && desc.apiname[0] == '*' && !desc.apiname[1])
1373 desc.apiname = NULL;
1375 if (desc.verbname && desc.verbname[0] == '*' && !desc.verbname[1])
1376 desc.verbname = NULL;
1378 if (desc.uuid && desc.uuid[0] == '*' && !desc.uuid[1])
1381 /* get what is expected */
1383 wrap_json_optarray_for_all(request, add_xreq_flags, &desc);
1386 wrap_json_optarray_for_all(api, add_api_flags, &desc);
1388 #if !defined(REMOVE_LEGACY_TRACE)
1390 wrap_json_optarray_for_all(daemon, legacy_add_ditf_flags, &desc);
1393 wrap_json_optarray_for_all(service, legacy_add_svc_flags, &desc);
1397 wrap_json_optarray_for_all(event, add_evt_flags, &desc);
1400 wrap_json_optarray_for_all(session, add_session_flags, &desc);
1403 wrap_json_optarray_for_all(global, add_global_flags, &desc);
1407 wrap_json_optarray_for_all(sub, add, &desc);
1412 wrap_json_optarray_for_all(object, add_xreq_flags, &desc);
1417 /* drop hooks of given tag */
1418 static void drop_tag(void *closure, struct json_object *object)
1421 struct context *context = closure;
1425 rc = wrap_json_unpack(object, "s", &name);
1427 ctxt_error(&context->errors, "unexpected tag value %s", json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE));
1429 tag = trace_get_tag(context->trace, name, 0);
1431 ctxt_error(&context->errors, "tag %s not found", name);
1433 trace_unhook(context->trace, tag, NULL, NULL);
1437 /* drop hooks of given event */
1438 static void drop_event(void *closure, struct json_object *object)
1441 struct context *context = closure;
1442 struct event *event;
1445 rc = wrap_json_unpack(object, "s", &name);
1447 ctxt_error(&context->errors, "unexpected event value %s", json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE));
1449 event = trace_get_event(context->trace, name, 0);
1451 ctxt_error(&context->errors, "event %s not found", name);
1453 trace_unhook(context->trace, NULL, event, NULL);
1457 /* drop hooks of given session */
1458 static void drop_session(void *closure, struct json_object *object)
1461 struct context *context = closure;
1462 struct afb_session *session;
1465 rc = wrap_json_unpack(object, "s", &uuid);
1467 ctxt_error(&context->errors, "unexpected session value %s", json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE));
1469 session = trace_get_session_by_uuid(context->trace, uuid, 0);
1471 ctxt_error(&context->errors, "session %s not found", uuid);
1473 trace_unhook(context->trace, NULL, NULL, session);
1474 afb_session_unref(session);
1479 /*******************************************************************************/
1480 /***** public interface *****/
1481 /*******************************************************************************/
1483 /* allocates an afb_trace instance */
1484 struct afb_trace *afb_trace_create(const char *apiname, struct afb_session *bound)
1486 struct afb_trace *trace;
1490 trace = calloc(1, sizeof *trace);
1492 trace->refcount = 1;
1493 trace->bound = bound;
1494 trace->apiname = apiname;
1495 pthread_mutex_init(&trace->mutex, NULL);
1500 /* add a reference to the trace */
1501 void afb_trace_addref(struct afb_trace *trace)
1503 __atomic_add_fetch(&trace->refcount, 1, __ATOMIC_RELAXED);
1506 /* drop one reference to the trace */
1507 void afb_trace_unref(struct afb_trace *trace)
1509 if (trace && !__atomic_sub_fetch(&trace->refcount, 1, __ATOMIC_RELAXED)) {
1511 trace_unhook(trace, NULL, NULL, NULL);
1512 trace_cleanup(trace);
1513 pthread_mutex_destroy(&trace->mutex);
1519 int afb_trace_add(afb_req_t req, struct json_object *args, struct afb_trace *trace)
1521 struct context context;
1524 memset(&context, 0, sizeof context);
1525 context.trace = trace;
1528 memset(&desc, 0, sizeof desc);
1529 desc.context = &context;
1531 pthread_mutex_lock(&trace->mutex);
1532 wrap_json_optarray_for_all(args, add, &desc);
1533 pthread_mutex_unlock(&trace->mutex);
1535 if (!context.errors)
1538 afb_req_fail(req, "error-detected", context.errors);
1539 free(context.errors);
1544 int afb_trace_drop(afb_req_t req, struct json_object *args, struct afb_trace *trace)
1547 struct context context;
1548 struct json_object *tags, *events, *uuids;
1550 memset(&context, 0, sizeof context);
1551 context.trace = trace;
1554 /* special: boolean value */
1555 if (!wrap_json_unpack(args, "b", &rc)) {
1557 pthread_mutex_lock(&trace->mutex);
1558 trace_unhook(trace, NULL, NULL, NULL);
1559 trace_cleanup(trace);
1560 pthread_mutex_unlock(&trace->mutex);
1565 tags = events = uuids = NULL;
1566 rc = wrap_json_unpack(args, "{s?o s?o s?o}",
1571 if (rc < 0 || !(events || tags || uuids)) {
1572 afb_req_fail(req, "error-detected", "bad drop arguments");
1576 pthread_mutex_lock(&trace->mutex);
1579 wrap_json_optarray_for_all(tags, drop_tag, &context);
1582 wrap_json_optarray_for_all(events, drop_event, &context);
1585 wrap_json_optarray_for_all(uuids, drop_session, &context);
1587 trace_cleanup(trace);
1589 pthread_mutex_unlock(&trace->mutex);
1591 if (!context.errors)
1594 afb_req_fail(req, "error-detected", context.errors);
1595 free(context.errors);