2 * Copyright (C) 2016, 2017 "IoT.bzh"
3 * Author José Bollo <jose.bollo@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
29 #include <json-c/json.h>
31 #define AFB_BINDING_VERSION 0
32 #include <afb/afb-binding.h>
36 #include "afb-session.h"
38 #include "afb-export.h"
40 #include "afb-session.h"
41 #include "afb-trace.h"
43 #include "wrap-json.h"
46 /*******************************************************************************/
47 /***** default names *****/
48 /*******************************************************************************/
50 #if !defined(DEFAULT_EVENT_NAME)
51 # define DEFAULT_EVENT_NAME "trace"
53 #if !defined(DEFAULT_TAG_NAME)
54 # define DEFAULT_TAG_NAME "trace"
57 /*******************************************************************************/
59 /*******************************************************************************/
61 /* structure for searching flags by names */
64 const char *name; /** the name */
65 int value; /** the value */
70 struct tag *next; /* link to the next */
71 char tag[1]; /* 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_Ditf, /* export hooks */
100 Trace_Type_Svc, /* export hooks */
101 Trace_Type_Evt, /* evt hooks */
102 Trace_Type_Session, /* session hooks */
103 Trace_Type_Global, /* global 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);
148 /* get the value of the flag of 'name' in the array 'flags' of 'count elements */
149 static int get_flag(const char *name, struct flag flags[], int count)
151 /* dichotomic search */
152 int lower = 0, upper = count;
153 while (lower < upper) {
154 int mid = (lower + upper) >> 1;
155 int cmp = strcmp(name, flags[mid].name);
157 return flags[mid].value;
167 static struct json_object *timestamp(const struct afb_hookid *hookid)
171 snprintf(ts, sizeof ts, "%llu.%06lu", (long long unsigned)hookid->time.tv_sec, (long unsigned)(hookid->time.tv_nsec / 1000));
172 return json_object_new_string(ts);
175 /* verbosity level name or NULL */
176 static const char *verbosity_level_name(int level)
178 static const char *names[] = {
186 return level >= Log_Level_Error && level <= Log_Level_Debug ? names[level - Log_Level_Error] : NULL;
190 static void emit(void *closure, const struct afb_hookid *hookid, const char *type, const char *fmt1, const char *fmt2, va_list ap2, ...)
192 struct hook *hook = closure;
193 struct json_object *data, *data1, *data2;
196 data1 = data2 = data = NULL;
198 wrap_json_vpack(&data1, fmt1, ap1);
201 wrap_json_vpack(&data2, fmt2, ap2);
203 wrap_json_pack(&data, "{so ss ss si so so*}",
204 "time", timestamp(hookid),
205 "tag", hook->tag->tag,
207 "id", (int)(hookid->id & INT_MAX),
211 afb_evt_evtid_push(hook->event->evtid, data);
214 /*******************************************************************************/
215 /***** trace the requests *****/
216 /*******************************************************************************/
218 static struct flag xreq_flags[] = { /* must be sorted by names */
219 { "addref", afb_hook_flag_req_addref },
220 { "all", afb_hook_flags_req_all },
221 { "args", afb_hook_flags_req_args },
222 { "begin", afb_hook_flag_req_begin },
223 { "common", afb_hook_flags_req_common },
224 { "context", afb_hook_flags_req_context },
225 { "context_get", afb_hook_flag_req_context_get },
226 { "context_make", afb_hook_flag_req_context_make },
227 { "context_set", afb_hook_flag_req_context_set },
228 { "end", afb_hook_flag_req_end },
229 { "event", afb_hook_flags_req_event },
230 { "extra", afb_hook_flags_req_extra },
231 { "fail", afb_hook_flag_req_fail },
232 { "get", afb_hook_flag_req_get },
233 { "get_application_id", afb_hook_flag_req_get_application_id },
234 { "get_uid", afb_hook_flag_req_get_uid },
235 { "has_permission", afb_hook_flag_req_has_permission },
236 { "json", afb_hook_flag_req_json },
237 { "life", afb_hook_flags_req_life },
238 { "ref", afb_hook_flags_req_ref },
239 { "result", afb_hook_flags_req_result },
240 { "security", afb_hook_flags_req_security },
241 { "session", afb_hook_flags_req_session },
242 { "session_close", afb_hook_flag_req_session_close },
243 { "session_set_LOA", afb_hook_flag_req_session_set_LOA },
244 { "store", afb_hook_flag_req_store },
245 { "stores", afb_hook_flags_req_stores },
246 { "subcall", afb_hook_flag_req_subcall },
247 { "subcall_req", afb_hook_flag_req_subcall_req },
248 { "subcall_req_result", afb_hook_flag_req_subcall_req_result },
249 { "subcall_result", afb_hook_flag_req_subcall_result },
250 { "subcalls", afb_hook_flags_req_subcalls },
251 { "subcallsync", afb_hook_flag_req_subcallsync },
252 { "subcallsync_result", afb_hook_flag_req_subcallsync_result },
253 { "subscribe", afb_hook_flag_req_subscribe },
254 { "success", afb_hook_flag_req_success },
255 { "unref", afb_hook_flag_req_unref },
256 { "unstore", afb_hook_flag_req_unstore },
257 { "unsubscribe", afb_hook_flag_req_unsubscribe },
258 { "vverbose", afb_hook_flag_req_vverbose },
261 /* get the xreq value for flag of 'name' */
262 static int get_xreq_flag(const char *name)
264 return get_flag(name, xreq_flags, (int)(sizeof xreq_flags / sizeof *xreq_flags));
267 static void hook_xreq(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *action, const char *format, ...)
269 struct json_object *cred = NULL;
270 const char *session = NULL;
273 if (xreq->context.session)
274 session = afb_session_uuid(xreq->context.session);
277 wrap_json_pack(&cred, "{si ss si si ss* ss*}",
278 "uid", (int)xreq->cred->uid,
279 "user", xreq->cred->user,
280 "gid", (int)xreq->cred->gid,
281 "pid", (int)xreq->cred->pid,
282 "label", xreq->cred->label,
285 va_start(ap, format);
286 emit(closure, hookid, "request", "{si ss ss ss so* ss*}", format, ap,
287 "index", xreq->hookindex,
288 "api", xreq->request.api,
289 "verb", xreq->request.verb,
296 static void hook_xreq_begin(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
298 hook_xreq(closure, hookid, xreq, "begin", NULL);
301 static void hook_xreq_end(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
303 hook_xreq(closure, hookid, xreq, "end", NULL);
306 static void hook_xreq_json(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *obj)
308 hook_xreq(closure, hookid, xreq, "json", "{sO?}",
312 static void hook_xreq_get(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *name, struct afb_arg arg)
314 hook_xreq(closure, hookid, xreq, "get", "{ss? ss? ss? ss?}",
321 static void hook_xreq_success(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *obj, const char *info)
323 hook_xreq(closure, hookid, xreq, "success", "{sO? ss?}",
328 static void hook_xreq_fail(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *status, const char *info)
330 hook_xreq(closure, hookid, xreq, "fail", "{ss? ss?}",
335 static void hook_xreq_context_get(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, void *value)
337 hook_xreq(closure, hookid, xreq, "context_get", NULL);
340 static void hook_xreq_context_set(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, void *value, void (*free_value)(void*))
342 hook_xreq(closure, hookid, xreq, "context_set", NULL);
345 static void hook_xreq_addref(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
347 hook_xreq(closure, hookid, xreq, "addref", NULL);
350 static void hook_xreq_unref(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
352 hook_xreq(closure, hookid, xreq, "unref", NULL);
355 static void hook_xreq_session_close(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
357 hook_xreq(closure, hookid, xreq, "session_close", NULL);
360 static void hook_xreq_session_set_LOA(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, unsigned level, int result)
362 hook_xreq(closure, hookid, xreq, "session_set_LOA", "{si si}",
367 static void hook_xreq_subscribe(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_eventid *eventid, int result)
369 hook_xreq(closure, hookid, xreq, "subscribe", "{s{ss si} si}",
371 "name", afb_evt_eventid_fullname(eventid),
372 "id", afb_evt_eventid_id(eventid),
376 static void hook_xreq_unsubscribe(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_eventid *eventid, int result)
378 hook_xreq(closure, hookid, xreq, "unsubscribe", "{s{ss? si} si}",
380 "name", afb_evt_eventid_fullname(eventid),
381 "id", afb_evt_eventid_id(eventid),
385 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)
387 hook_xreq(closure, hookid, xreq, "subcall", "{ss? ss? sO?}",
393 static void hook_xreq_subcall_result(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int status, struct json_object *result)
395 hook_xreq(closure, hookid, xreq, "subcall_result", "{si sO?}",
400 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)
402 hook_xreq(closure, hookid, xreq, "subcallsync", "{ss? ss? sO?}",
408 static void hook_xreq_subcallsync_result(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int status, struct json_object *result)
410 hook_xreq(closure, hookid, xreq, "subcallsync_result", "{si sO?}",
415 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)
417 struct json_object *pos;
426 len = vasprintf(&msg, fmt, ap);
430 wrap_json_pack(&pos, "{ss si ss*}", "file", file, "line", line, "function", func);
432 hook_xreq(closure, hookid, xreq, "vverbose", "{si ss* ss? so*}",
434 "type", verbosity_level_name(level),
435 len < 0 ? "format" : "message", len < 0 ? fmt : msg,
441 static void hook_xreq_store(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_stored_req *sreq)
443 hook_xreq(closure, hookid, xreq, "store", NULL);
446 static void hook_xreq_unstore(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
448 hook_xreq(closure, hookid, xreq, "unstore", NULL);
451 static void hook_xreq_subcall_req(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
453 hook_xreq(closure, hookid, xreq, "subcall_req", "{ss? ss? sO?}",
459 static void hook_xreq_subcall_req_result(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int status, struct json_object *result)
461 hook_xreq(closure, hookid, xreq, "subcall_req_result", "{si sO?}",
466 static void hook_xreq_has_permission(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *permission, int result)
468 hook_xreq(closure, hookid, xreq, "has_permission", "{ss sb}",
469 "permission", permission,
473 static void hook_xreq_get_application_id(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, char *result)
475 hook_xreq(closure, hookid, xreq, "get_application_id", "{ss?}",
479 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)
481 char pc[50], pf[50], pv[50], pr[50];
482 snprintf(pc, sizeof pc, "%p", create_value);
483 snprintf(pf, sizeof pf, "%p", free_value);
484 snprintf(pv, sizeof pv, "%p", create_closure);
485 snprintf(pr, sizeof pr, "%p", result);
486 hook_xreq(closure, hookid, xreq, "context_make", "{sb ss ss ss ss}",
494 static void hook_xreq_get_uid(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int result)
496 hook_xreq(closure, hookid, xreq, "get_uid", "{si}",
500 static struct afb_hook_xreq_itf hook_xreq_itf = {
501 .hook_xreq_begin = hook_xreq_begin,
502 .hook_xreq_end = hook_xreq_end,
503 .hook_xreq_json = hook_xreq_json,
504 .hook_xreq_get = hook_xreq_get,
505 .hook_xreq_success = hook_xreq_success,
506 .hook_xreq_fail = hook_xreq_fail,
507 .hook_xreq_context_get = hook_xreq_context_get,
508 .hook_xreq_context_set = hook_xreq_context_set,
509 .hook_xreq_addref = hook_xreq_addref,
510 .hook_xreq_unref = hook_xreq_unref,
511 .hook_xreq_session_close = hook_xreq_session_close,
512 .hook_xreq_session_set_LOA = hook_xreq_session_set_LOA,
513 .hook_xreq_subscribe = hook_xreq_subscribe,
514 .hook_xreq_unsubscribe = hook_xreq_unsubscribe,
515 .hook_xreq_subcall = hook_xreq_subcall,
516 .hook_xreq_subcall_result = hook_xreq_subcall_result,
517 .hook_xreq_subcallsync = hook_xreq_subcallsync,
518 .hook_xreq_subcallsync_result = hook_xreq_subcallsync_result,
519 .hook_xreq_vverbose = hook_xreq_vverbose,
520 .hook_xreq_store = hook_xreq_store,
521 .hook_xreq_unstore = hook_xreq_unstore,
522 .hook_xreq_subcall_req = hook_xreq_subcall_req,
523 .hook_xreq_subcall_req_result = hook_xreq_subcall_req_result,
524 .hook_xreq_has_permission = hook_xreq_has_permission,
525 .hook_xreq_get_application_id = hook_xreq_get_application_id,
526 .hook_xreq_context_make = hook_xreq_context_make,
527 .hook_xreq_get_uid = hook_xreq_get_uid,
530 /*******************************************************************************/
531 /***** trace the daemon interface *****/
532 /*******************************************************************************/
534 static struct flag ditf_flags[] = { /* must be sorted by names */
535 { "all", afb_hook_flags_ditf_all },
536 { "common", afb_hook_flags_ditf_common },
537 { "event_broadcast_after", afb_hook_flag_ditf_event_broadcast_after },
538 { "event_broadcast_before", afb_hook_flag_ditf_event_broadcast_before },
539 { "event_make", afb_hook_flag_ditf_event_make },
540 { "extra", afb_hook_flags_ditf_extra },
541 { "get_event_loop", afb_hook_flag_ditf_get_event_loop },
542 { "get_system_bus", afb_hook_flag_ditf_get_system_bus },
543 { "get_user_bus", afb_hook_flag_ditf_get_user_bus },
544 { "queue_job", afb_hook_flag_ditf_queue_job },
545 { "require_api", afb_hook_flag_ditf_require_api },
546 { "require_api_result", afb_hook_flag_ditf_require_api_result },
547 { "rootdir_get_fd", afb_hook_flag_ditf_rootdir_get_fd },
548 { "rootdir_open_locale", afb_hook_flag_ditf_rootdir_open_locale },
549 { "unstore_req", afb_hook_flag_ditf_unstore_req },
550 { "vverbose", afb_hook_flag_ditf_vverbose },
553 /* get the export value for flag of 'name' */
554 static int get_ditf_flag(const char *name)
556 return get_flag(name, ditf_flags, (int)(sizeof ditf_flags / sizeof *ditf_flags));
560 static void hook_ditf(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *action, const char *format, ...)
564 va_start(ap, format);
565 emit(closure, hookid, "daemon", "{ss ss}", format, ap,
566 "api", afb_export_apiname(export),
571 static void hook_ditf_event_broadcast_before(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, struct json_object *object)
573 hook_ditf(closure, hookid, export, "event_broadcast_before", "{ss sO*}",
574 "name", name, "data", object);
577 static void hook_ditf_event_broadcast_after(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, struct json_object *object, int result)
579 hook_ditf(closure, hookid, export, "event_broadcast_after", "{ss sO* si}",
580 "name", name, "data", object, "result", result);
583 static void hook_ditf_get_event_loop(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_event *result)
585 hook_ditf(closure, hookid, export, "get_event_loop", NULL);
588 static void hook_ditf_get_user_bus(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_bus *result)
590 hook_ditf(closure, hookid, export, "get_user_bus", NULL);
593 static void hook_ditf_get_system_bus(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_bus *result)
595 hook_ditf(closure, hookid, export, "get_system_bus", NULL);
598 static void hook_ditf_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)
600 struct json_object *pos;
609 len = vasprintf(&msg, fmt, ap);
613 wrap_json_pack(&pos, "{ss si ss*}", "file", file, "line", line, "function", function);
615 hook_ditf(closure, hookid, export, "vverbose", "{si ss* ss? so*}",
617 "type", verbosity_level_name(level),
618 len < 0 ? "format" : "message", len < 0 ? fmt : msg,
624 static void hook_ditf_event_make(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, struct afb_eventid *result)
626 hook_ditf(closure, hookid, export, "event_make", "{ss ss si}",
627 "name", name, "event", afb_evt_eventid_fullname(result), "id", afb_evt_eventid_id(result));
630 static void hook_ditf_rootdir_get_fd(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result)
635 sprintf(path, "/proc/self/fd/%d", result);
636 readlink(path, path, sizeof path);
639 hook_ditf(closure, hookid, export, "rootdir_get_fd", "{ss}",
640 result < 0 ? "path" : "error",
641 result < 0 ? strerror(errno) : path);
644 static void hook_ditf_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)
649 sprintf(path, "/proc/self/fd/%d", result);
650 readlink(path, path, sizeof path);
653 hook_ditf(closure, hookid, export, "rootdir_open_locale", "{ss si ss* ss}",
657 result < 0 ? "path" : "error",
658 result < 0 ? strerror(errno) : path);
661 static void hook_ditf_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)
663 hook_ditf(closure, hookid, export, "queue_job", "{ss}", "result", result);
666 static void hook_ditf_unstore_req(void * closure, const struct afb_hookid *hookid, const struct afb_export *export, struct afb_stored_req *sreq)
668 hook_ditf(closure, hookid, export, "unstore_req", NULL);
671 static void hook_ditf_require_api(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, int initialized)
673 hook_ditf(closure, hookid, export, "require_api", "{ss sb}", "name", name, "initialized", initialized);
676 static void hook_ditf_require_api_result(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, int initialized, int result)
678 hook_ditf(closure, hookid, export, "require_api_result", "{ss sb si}", "name", name, "initialized", initialized, "result", result);
681 static struct afb_hook_ditf_itf hook_ditf_itf = {
682 .hook_ditf_event_broadcast_before = hook_ditf_event_broadcast_before,
683 .hook_ditf_event_broadcast_after = hook_ditf_event_broadcast_after,
684 .hook_ditf_get_event_loop = hook_ditf_get_event_loop,
685 .hook_ditf_get_user_bus = hook_ditf_get_user_bus,
686 .hook_ditf_get_system_bus = hook_ditf_get_system_bus,
687 .hook_ditf_vverbose = hook_ditf_vverbose,
688 .hook_ditf_event_make = hook_ditf_event_make,
689 .hook_ditf_rootdir_get_fd = hook_ditf_rootdir_get_fd,
690 .hook_ditf_rootdir_open_locale = hook_ditf_rootdir_open_locale,
691 .hook_ditf_queue_job = hook_ditf_queue_job,
692 .hook_ditf_unstore_req = hook_ditf_unstore_req,
693 .hook_ditf_require_api = hook_ditf_require_api,
694 .hook_ditf_require_api_result = hook_ditf_require_api_result
697 /*******************************************************************************/
698 /***** trace the services *****/
699 /*******************************************************************************/
701 static struct flag svc_flags[] = { /* must be sorted by names */
702 { "all", afb_hook_flags_svc_all },
703 { "call", afb_hook_flag_svc_call },
704 { "call_result", afb_hook_flag_svc_call_result },
705 { "callsync", afb_hook_flag_svc_callsync },
706 { "callsync_result", afb_hook_flag_svc_callsync_result },
707 { "on_event_after", afb_hook_flag_svc_on_event_after },
708 { "on_event_before", afb_hook_flag_svc_on_event_before },
709 { "start_after", afb_hook_flag_svc_start_after },
710 { "start_before", afb_hook_flag_svc_start_before },
713 /* get the export value for flag of 'name' */
714 static int get_svc_flag(const char *name)
716 return get_flag(name, svc_flags, (int)(sizeof svc_flags / sizeof *svc_flags));
719 static void hook_svc(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *action, const char *format, ...)
723 va_start(ap, format);
724 emit(closure, hookid, "service", "{ss ss}", format, ap,
725 "api", afb_export_apiname(export),
730 static void hook_svc_start_before(void *closure, const struct afb_hookid *hookid, const struct afb_export *export)
732 hook_svc(closure, hookid, export, "start_before", NULL);
735 static void hook_svc_start_after(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int status)
737 hook_svc(closure, hookid, export, "start_after", "{si}", "result", status);
740 static void hook_svc_on_event_before(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *event, int evtid, struct json_object *object)
742 hook_svc(closure, hookid, export, "on_event_before", "{ss si sO*}",
743 "event", event, "id", evtid, "data", object);
746 static void hook_svc_on_event_after(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *event, int evtid, struct json_object *object)
748 hook_svc(closure, hookid, export, "on_event_after", "{ss si sO*}",
749 "event", event, "id", evtid, "data", object);
752 static void hook_svc_call(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *api, const char *verb, struct json_object *args)
754 hook_svc(closure, hookid, export, "call", "{ss ss sO*}",
755 "api", api, "verb", verb, "args", args);
758 static void hook_svc_call_result(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int status, struct json_object *result)
760 hook_svc(closure, hookid, export, "call_result", "{si sO*}",
761 "status", status, "result", result);
764 static void hook_svc_callsync(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *api, const char *verb, struct json_object *args)
766 hook_svc(closure, hookid, export, "callsync", "{ss ss sO*}",
767 "api", api, "verb", verb, "args", args);
770 static void hook_svc_callsync_result(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int status, struct json_object *result)
772 hook_svc(closure, hookid, export, "callsync_result", "{si sO*}",
773 "status", status, "result", result);
776 static struct afb_hook_svc_itf hook_svc_itf = {
777 .hook_svc_start_before = hook_svc_start_before,
778 .hook_svc_start_after = hook_svc_start_after,
779 .hook_svc_on_event_before = hook_svc_on_event_before,
780 .hook_svc_on_event_after = hook_svc_on_event_after,
781 .hook_svc_call = hook_svc_call,
782 .hook_svc_call_result = hook_svc_call_result,
783 .hook_svc_callsync = hook_svc_callsync,
784 .hook_svc_callsync_result = hook_svc_callsync_result
787 /*******************************************************************************/
788 /***** trace the events *****/
789 /*******************************************************************************/
791 static struct flag evt_flags[] = { /* must be sorted by names */
792 { "addref", afb_hook_flag_evt_addref },
793 { "all", afb_hook_flags_evt_all },
794 { "broadcast_after", afb_hook_flag_evt_broadcast_after },
795 { "broadcast_before", afb_hook_flag_evt_broadcast_before },
796 { "common", afb_hook_flags_evt_common },
797 { "create", afb_hook_flag_evt_create },
798 { "extra", afb_hook_flags_evt_extra },
799 { "name", afb_hook_flag_evt_name },
800 { "push_after", afb_hook_flag_evt_push_after },
801 { "push_before", afb_hook_flag_evt_push_before },
802 { "unref", afb_hook_flag_evt_unref },
805 /* get the evt value for flag of 'name' */
806 static int get_evt_flag(const char *name)
808 return get_flag(name, evt_flags, (int)(sizeof evt_flags / sizeof *evt_flags));
811 static void hook_evt(void *closure, const struct afb_hookid *hookid, const char *evt, int id, const char *action, const char *format, ...)
815 va_start(ap, format);
816 emit(closure, hookid, "event", "{si ss ss}", format, ap,
823 static void hook_evt_create(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
825 hook_evt(closure, hookid, evt, id, "create", NULL);
828 static void hook_evt_push_before(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj)
830 hook_evt(closure, hookid, evt, id, "push_before", "{sO*}", "data", obj);
834 static void hook_evt_push_after(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj, int result)
836 hook_evt(closure, hookid, evt, id, "push_after", "{sO* si}", "data", obj, "result", result);
839 static void hook_evt_broadcast_before(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj)
841 hook_evt(closure, hookid, evt, id, "broadcast_before", "{sO*}", "data", obj);
844 static void hook_evt_broadcast_after(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj, int result)
846 hook_evt(closure, hookid, evt, id, "broadcast_after", "{sO* si}", "data", obj, "result", result);
849 static void hook_evt_name(void *closure, const struct afb_hookid *hookid, const char *evt, int id, const char *result)
851 hook_evt(closure, hookid, evt, id, "name", "{ss}", "result", result);
854 static void hook_evt_addref(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
856 hook_evt(closure, hookid, evt, id, "addref", NULL);
859 static void hook_evt_unref(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
861 hook_evt(closure, hookid, evt, id, "unref", NULL);
864 static struct afb_hook_evt_itf hook_evt_itf = {
865 .hook_evt_create = hook_evt_create,
866 .hook_evt_push_before = hook_evt_push_before,
867 .hook_evt_push_after = hook_evt_push_after,
868 .hook_evt_broadcast_before = hook_evt_broadcast_before,
869 .hook_evt_broadcast_after = hook_evt_broadcast_after,
870 .hook_evt_name = hook_evt_name,
871 .hook_evt_addref = hook_evt_addref,
872 .hook_evt_unref = hook_evt_unref
875 /*******************************************************************************/
876 /***** trace the sessions *****/
877 /*******************************************************************************/
879 static struct flag session_flags[] = { /* must be sorted by names */
880 { "addref", afb_hook_flag_session_addref },
881 { "all", afb_hook_flags_session_all },
882 { "close", afb_hook_flag_session_close },
883 { "common", afb_hook_flags_session_common },
884 { "create", afb_hook_flag_session_create },
885 { "destroy", afb_hook_flag_session_destroy },
886 { "renew", afb_hook_flag_session_renew },
887 { "unref", afb_hook_flag_session_unref },
890 /* get the session value for flag of 'name' */
891 static int get_session_flag(const char *name)
893 return get_flag(name, session_flags, (int)(sizeof session_flags / sizeof *session_flags));
896 static void hook_session(void *closure, const struct afb_hookid *hookid, struct afb_session *session, const char *action, const char *format, ...)
900 va_start(ap, format);
901 emit(closure, hookid, "session", "{ss ss}", format, ap,
907 static void hook_session_create(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
909 hook_session(closure, hookid, session, "create", "{ss}", "token", afb_session_token(session));
912 static void hook_session_close(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
914 hook_session(closure, hookid, session, "close", NULL);
917 static void hook_session_destroy(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
919 hook_session(closure, hookid, session, "destroy", NULL);
922 static void hook_session_renew(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
924 hook_session(closure, hookid, session, "renew", "{ss}", "token", afb_session_token(session));
927 static void hook_session_addref(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
929 hook_session(closure, hookid, session, "addref", NULL);
932 static void hook_session_unref(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
934 hook_session(closure, hookid, session, "unref", NULL);
937 static struct afb_hook_session_itf hook_session_itf = {
938 .hook_session_create = hook_session_create,
939 .hook_session_close = hook_session_close,
940 .hook_session_destroy = hook_session_destroy,
941 .hook_session_renew = hook_session_renew,
942 .hook_session_addref = hook_session_addref,
943 .hook_session_unref = hook_session_unref
946 /*******************************************************************************/
947 /***** trace the globals *****/
948 /*******************************************************************************/
950 static struct flag global_flags[] = { /* must be sorted by names */
951 { "all", afb_hook_flags_global_all },
952 { "vverbose", afb_hook_flag_global_vverbose },
955 /* get the global value for flag of 'name' */
956 static int get_global_flag(const char *name)
958 return get_flag(name, global_flags, (int)(sizeof global_flags / sizeof *global_flags));
961 static void hook_global(void *closure, const struct afb_hookid *hookid, const char *action, const char *format, ...)
965 va_start(ap, format);
966 emit(closure, hookid, "global", "{ss}", format, ap, "action", action);
970 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)
972 struct json_object *pos;
981 len = vasprintf(&msg, fmt, ap);
985 wrap_json_pack(&pos, "{ss si ss*}", "file", file, "line", line, "function", function);
987 hook_global(closure, hookid, "vverbose", "{si ss* ss? so*}",
989 "type", verbosity_level_name(level),
990 len < 0 ? "format" : "message", len < 0 ? fmt : msg,
996 static struct afb_hook_global_itf hook_global_itf = {
997 .hook_global_vverbose = hook_global_vverbose,
1000 /*******************************************************************************/
1001 /***** abstract types *****/
1002 /*******************************************************************************/
1008 void (*unref)(void*);
1009 int (*get_flag)(const char*);
1011 abstracting[Trace_Type_Count] =
1016 .unref = (void(*)(void*))afb_hook_unref_xreq,
1017 .get_flag = get_xreq_flag
1022 .unref = (void(*)(void*))afb_hook_unref_ditf,
1023 .get_flag = get_ditf_flag
1028 .unref = (void(*)(void*))afb_hook_unref_svc,
1029 .get_flag = get_svc_flag
1034 .unref = (void(*)(void*))afb_hook_unref_evt,
1035 .get_flag = get_evt_flag
1037 [Trace_Type_Session] =
1040 .unref = (void(*)(void*))afb_hook_unref_session,
1041 .get_flag = get_session_flag
1043 [Trace_Type_Global] =
1046 .unref = (void(*)(void*))afb_hook_unref_global,
1047 .get_flag = get_global_flag
1051 /*******************************************************************************/
1052 /***** handle trace data *****/
1053 /*******************************************************************************/
1055 /* drop hooks of 'trace' matching 'tag' and 'event' and 'session' */
1056 static void trace_unhook(struct afb_trace *trace, struct tag *tag, struct event *event, struct afb_session *session)
1059 struct hook *hook, **prev;
1061 /* remove any event */
1062 for (i = 0 ; i < Trace_Type_Count ; i++) {
1063 prev = &trace->hooks[i];
1064 while ((hook = *prev)) {
1065 if ((tag && tag != hook->tag)
1066 || (event && event != hook->event)
1067 || (session && session != hook->session))
1071 abstracting[i].unref(hook->handler);
1078 /* cleanup: removes unused tags, events and sessions of the 'trace' */
1079 static void trace_cleanup(struct afb_trace *trace)
1083 struct tag *tag, **ptag;
1084 struct event *event, **pevent;
1087 ptag = &trace->tags;
1088 while ((tag = *ptag)) {
1089 /* search for tag */
1090 for (hook = NULL, i = 0 ; !hook && i < Trace_Type_Count ; i++)
1091 for (hook = trace->hooks[i] ; hook && hook->tag != tag ; hook = hook->next);
1092 /* keep or free whether used or not */
1101 pevent = &trace->events;
1102 while ((event = *pevent)) {
1103 /* search for event */
1104 for (hook = NULL, i = 0 ; !hook && i < Trace_Type_Count ; i++)
1105 for (hook = trace->hooks[i] ; hook && hook->event != event ; hook = hook->next);
1106 /* keep or free whether used or not */
1108 pevent = &event->next;
1110 *pevent = event->next;
1111 afb_evt_evtid_unref(event->evtid);
1118 * Get the tag of 'name' within 'trace'.
1119 * If 'alloc' isn't zero, create the tag and add it.
1121 static struct tag *trace_get_tag(struct afb_trace *trace, const char *name, int alloc)
1125 /* search the tag of 'name' */
1127 while (tag && strcmp(name, tag->tag))
1130 if (!tag && alloc) {
1131 /* creation if needed */
1132 tag = malloc(sizeof * tag + strlen(name));
1134 strcpy(tag->tag, name);
1135 tag->next = trace->tags;
1143 * Get the event of 'name' within 'trace'.
1144 * If 'alloc' isn't zero, create the event and add it.
1146 static struct event *trace_get_event(struct afb_trace *trace, const char *name, int alloc)
1148 struct event *event;
1150 /* search the event */
1151 event = trace->events;
1152 while (event && strcmp(afb_evt_evtid_name(event->evtid), name))
1153 event = event->next;
1155 if (!event && alloc) {
1156 event = malloc(sizeof * event);
1158 event->evtid = afb_evt_evtid_create2(trace->apiname, name);
1160 event->next = trace->events;
1161 trace->events = event;
1172 * called on session closing
1174 static void session_closed(void *item)
1176 struct cookie *cookie = item;
1178 pthread_mutex_lock(&cookie->trace->mutex);
1179 trace_unhook(cookie->trace, NULL, NULL, cookie->session);
1180 pthread_mutex_unlock(&cookie->trace->mutex);
1185 * records the cookie of session for tracking close
1187 static void *session_open(void *closure)
1189 struct cookie *param = closure, *cookie;
1190 cookie = malloc(sizeof *cookie);
1197 * Get the session of 'uuid' within 'trace'.
1198 * If 'alloc' isn't zero, create the session and add it.
1200 static struct afb_session *trace_get_session_by_uuid(struct afb_trace *trace, const char *uuid, int alloc)
1202 struct cookie cookie;
1205 cookie.session = afb_session_search(uuid);
1207 cookie.session = afb_session_get(uuid, AFB_SESSION_TIMEOUT_DEFAULT, NULL);
1208 if (cookie.session) {
1209 cookie.trace = trace;
1210 afb_session_cookie(cookie.session, cookie.trace, session_open, session_closed, &cookie, 0);
1213 return cookie.session;
1216 static struct hook *trace_make_detached_hook(struct afb_trace *trace, const char *event, const char *tag)
1220 tag = tag ?: DEFAULT_TAG_NAME;
1221 event = event ?: DEFAULT_EVENT_NAME;
1222 hook = malloc(sizeof *hook);
1224 hook->tag = trace_get_tag(trace, tag, 1);
1225 hook->event = trace_get_event(trace, event, 1);
1226 hook->session = NULL;
1227 hook->handler = NULL;
1232 static void trace_attach_hook(struct afb_trace *trace, struct hook *hook, enum trace_type type)
1234 hook->next = trace->hooks[type];
1235 trace->hooks[type] = hook;
1238 /*******************************************************************************/
1239 /***** handle client requests *****/
1240 /*******************************************************************************/
1244 struct afb_trace *trace;
1251 struct context *context;
1257 const char *pattern;
1258 int flags[Trace_Type_Count];
1262 static void addhook(struct desc *desc, enum trace_type type)
1265 struct afb_session *session;
1266 struct afb_session *bind;
1267 struct afb_trace *trace = desc->context->trace;
1269 /* check permission for bound traces */
1270 bind = trace->bound;
1272 if (type != Trace_Type_Xreq) {
1273 ctxt_error(&desc->context->errors, "tracing %s is forbidden", abstracting[type].name);
1277 ctxt_error(&desc->context->errors, "setting session is forbidden");
1282 /* allocate the hook */
1283 hook = trace_make_detached_hook(trace, desc->name, desc->tag);
1285 ctxt_error(&desc->context->errors, "allocation of hook failed");
1289 /* create the hook handler */
1291 case Trace_Type_Xreq:
1293 session = afb_session_addref(bind);
1295 session = trace_get_session_by_uuid(trace, desc->uuid, 1);
1297 ctxt_error(&desc->context->errors, "allocation of session failed");
1302 hook->handler = afb_hook_create_xreq(desc->api, desc->verb, session,
1303 desc->flags[type], &hook_xreq_itf, hook);
1304 afb_session_unref(session);
1306 case Trace_Type_Ditf:
1307 hook->handler = afb_hook_create_ditf(desc->api, desc->flags[type], &hook_ditf_itf, hook);
1309 case Trace_Type_Svc:
1310 hook->handler = afb_hook_create_svc(desc->api, desc->flags[type], &hook_svc_itf, hook);
1312 case Trace_Type_Evt:
1313 hook->handler = afb_hook_create_evt(desc->pattern, desc->flags[type], &hook_evt_itf, hook);
1315 case Trace_Type_Session:
1316 hook->handler = afb_hook_create_session(desc->uuid, desc->flags[type], &hook_session_itf, hook);
1318 case Trace_Type_Global:
1319 hook->handler = afb_hook_create_global(desc->flags[type], &hook_global_itf, hook);
1324 if (!hook->handler) {
1325 ctxt_error(&desc->context->errors, "creation of hook failed");
1330 /* attach and activate the hook */
1331 afb_req_subscribe(desc->context->req, afb_evt_event_from_evtid(hook->event->evtid));
1332 trace_attach_hook(trace, hook, type);
1335 static void addhooks(struct desc *desc)
1339 for (i = 0 ; i < Trace_Type_Count ; i++) {
1345 static void add_flags(void *closure, struct json_object *object, enum trace_type type)
1348 const char *name, *queried;
1349 struct desc *desc = closure;
1351 if (wrap_json_unpack(object, "s", &name))
1352 ctxt_error(&desc->context->errors, "unexpected %s value %s",
1353 abstracting[type].name,
1354 json_object_to_json_string(object));
1356 queried = (name[0] == '*' && !name[1]) ? "all" : name;
1357 value = abstracting[type].get_flag(queried);
1359 desc->flags[type] |= value;
1361 ctxt_error(&desc->context->errors, "unknown %s name %s",
1362 abstracting[type].name, name);
1366 static void add_xreq_flags(void *closure, struct json_object *object)
1368 add_flags(closure, object, Trace_Type_Xreq);
1371 static void add_ditf_flags(void *closure, struct json_object *object)
1373 add_flags(closure, object, Trace_Type_Ditf);
1376 static void add_svc_flags(void *closure, struct json_object *object)
1378 add_flags(closure, object, Trace_Type_Svc);
1381 static void add_evt_flags(void *closure, struct json_object *object)
1383 add_flags(closure, object, Trace_Type_Evt);
1386 static void add_session_flags(void *closure, struct json_object *object)
1388 add_flags(closure, object, Trace_Type_Session);
1391 static void add_global_flags(void *closure, struct json_object *object)
1393 add_flags(closure, object, Trace_Type_Global);
1397 static void add(void *closure, struct json_object *object)
1401 struct json_object *request, *event, *daemon, *service, *sub, *global, *session;
1403 memcpy (&desc, closure, sizeof desc);
1404 request = event = daemon = service = sub = global = NULL;
1406 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}",
1412 "pattern", &desc.pattern,
1413 "request", &request,
1415 "service", &service,
1417 "session", &session,
1423 if (desc.api && desc.api[0] == '*' && !desc.api[1])
1426 if (desc.verb && desc.verb[0] == '*' && !desc.verb[1])
1429 if (desc.uuid && desc.uuid[0] == '*' && !desc.uuid[1])
1432 /* get what is expected */
1434 wrap_json_optarray_for_all(request, add_xreq_flags, &desc);
1437 wrap_json_optarray_for_all(daemon, add_ditf_flags, &desc);
1440 wrap_json_optarray_for_all(service, add_svc_flags, &desc);
1443 wrap_json_optarray_for_all(event, add_evt_flags, &desc);
1446 wrap_json_optarray_for_all(event, add_session_flags, &desc);
1449 wrap_json_optarray_for_all(global, add_global_flags, &desc);
1453 wrap_json_optarray_for_all(sub, add, &desc);
1458 wrap_json_optarray_for_all(object, add_xreq_flags, &desc);
1463 /* drop hooks of given tag */
1464 static void drop_tag(void *closure, struct json_object *object)
1467 struct context *context = closure;
1471 rc = wrap_json_unpack(object, "s", &name);
1473 ctxt_error(&context->errors, "unexpected tag value %s", json_object_to_json_string(object));
1475 tag = trace_get_tag(context->trace, name, 0);
1477 ctxt_error(&context->errors, "tag %s not found", name);
1479 trace_unhook(context->trace, tag, NULL, NULL);
1483 /* drop hooks of given event */
1484 static void drop_event(void *closure, struct json_object *object)
1487 struct context *context = closure;
1488 struct event *event;
1491 rc = wrap_json_unpack(object, "s", &name);
1493 ctxt_error(&context->errors, "unexpected event value %s", json_object_to_json_string(object));
1495 event = trace_get_event(context->trace, name, 0);
1497 ctxt_error(&context->errors, "event %s not found", name);
1499 trace_unhook(context->trace, NULL, event, NULL);
1503 /* drop hooks of given session */
1504 static void drop_session(void *closure, struct json_object *object)
1507 struct context *context = closure;
1508 struct afb_session *session;
1511 rc = wrap_json_unpack(object, "s", &uuid);
1513 ctxt_error(&context->errors, "unexpected session value %s", json_object_to_json_string(object));
1515 session = trace_get_session_by_uuid(context->trace, uuid, 0);
1517 ctxt_error(&context->errors, "session %s not found", uuid);
1519 trace_unhook(context->trace, NULL, NULL, session);
1520 afb_session_unref(session);
1525 /*******************************************************************************/
1526 /***** public interface *****/
1527 /*******************************************************************************/
1529 /* allocates an afb_trace instance */
1530 struct afb_trace *afb_trace_create(const char *apiname, struct afb_session *bound)
1532 struct afb_trace *trace;
1536 trace = calloc(1, sizeof *trace);
1538 trace->refcount = 1;
1539 trace->bound = bound;
1540 trace->apiname = apiname;
1541 pthread_mutex_init(&trace->mutex, NULL);
1546 /* add a reference to the trace */
1547 void afb_trace_addref(struct afb_trace *trace)
1549 __atomic_add_fetch(&trace->refcount, 1, __ATOMIC_RELAXED);
1552 /* drop one reference to the trace */
1553 void afb_trace_unref(struct afb_trace *trace)
1555 if (trace && !__atomic_sub_fetch(&trace->refcount, 1, __ATOMIC_RELAXED)) {
1557 trace_unhook(trace, NULL, NULL, NULL);
1558 trace_cleanup(trace);
1559 pthread_mutex_destroy(&trace->mutex);
1565 int afb_trace_add(struct afb_req req, struct json_object *args, struct afb_trace *trace)
1567 struct context context;
1570 memset(&context, 0, sizeof context);
1571 context.trace = trace;
1574 memset(&desc, 0, sizeof desc);
1575 desc.context = &context;
1577 pthread_mutex_lock(&trace->mutex);
1578 wrap_json_optarray_for_all(args, add, &desc);
1579 pthread_mutex_unlock(&trace->mutex);
1581 if (!context.errors)
1584 afb_req_fail(req, "error-detected", context.errors);
1585 free(context.errors);
1590 extern int afb_trace_drop(struct afb_req req, struct json_object *args, struct afb_trace *trace)
1593 struct context context;
1594 struct json_object *tags, *events, *uuids;
1596 memset(&context, 0, sizeof context);
1597 context.trace = trace;
1600 /* special: boolean value */
1601 if (!wrap_json_unpack(args, "b", &rc)) {
1603 pthread_mutex_lock(&trace->mutex);
1604 trace_unhook(trace, NULL, NULL, NULL);
1605 trace_cleanup(trace);
1606 pthread_mutex_unlock(&trace->mutex);
1611 tags = events = uuids = NULL;
1612 rc = wrap_json_unpack(args, "{s?o s?o s?o}",
1617 if (rc < 0 || !(events || tags || uuids)) {
1618 afb_req_fail(req, "error-detected", "bad drop arguments");
1622 pthread_mutex_lock(&trace->mutex);
1625 wrap_json_optarray_for_all(tags, drop_tag, &context);
1628 wrap_json_optarray_for_all(events, drop_event, &context);
1631 wrap_json_optarray_for_all(uuids, drop_session, &context);
1633 trace_cleanup(trace);
1635 pthread_mutex_unlock(&trace->mutex);
1637 if (!context.errors)
1640 afb_req_fail(req, "error-detected", context.errors);
1641 free(context.errors);