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>
30 #include <afb/afb-binding-v2.h>
34 #include "afb-session.h"
36 #include "afb-export.h"
38 #include "afb-trace.h"
40 #include "wrap-json.h"
43 /*******************************************************************************/
44 /***** default names *****/
45 /*******************************************************************************/
47 #if !defined(DEFAULT_EVENT_NAME)
48 # define DEFAULT_EVENT_NAME "trace"
50 #if !defined(DEFAULT_TAG_NAME)
51 # define DEFAULT_TAG_NAME "trace"
54 /*******************************************************************************/
56 /*******************************************************************************/
58 /* structure for searching flags by names */
61 const char *name; /** the name */
62 int value; /** the value */
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_event event; /* the event */
77 /* struct for sessions */
79 struct session *next; /* link to the next session */
80 struct afb_session *session; /* the session */
81 struct afb_trace *trace; /* the tracer */
84 /* struct for recording hooks */
86 struct hook *next; /* link to next hook */
87 void *handler; /* the handler of the hook */
88 struct event *event; /* the associated event */
89 struct tag *tag; /* the associated tag */
90 struct session *session; /* the associated session */
96 Trace_Type_Xreq, /* xreq hooks */
97 Trace_Type_Ditf, /* export hooks */
98 Trace_Type_Svc, /* export hooks */
99 Trace_Type_Evt, /* evt hooks */
100 Trace_Type_Global, /* global hooks */
101 Trace_Type_Count /* count of types of hooks */
107 int refcount; /* reference count */
108 pthread_mutex_t mutex; /* concurrency management */
109 struct afb_daemon *daemon; /* daemon */
110 struct afb_session *bound; /* bound to session */
111 struct event *events; /* list of events */
112 struct tag *tags; /* list of tags */
113 struct session *sessions; /* list of tags */
114 struct hook *hooks[Trace_Type_Count]; /* hooks */
117 /*******************************************************************************/
118 /***** utility functions *****/
119 /*******************************************************************************/
121 static void ctxt_error(char **errors, const char *format, ...)
129 va_start(ap, format);
130 len = vsnprintf(buffer, sizeof buffer, format, ap);
132 if (len > (int)(sizeof buffer - 2))
133 len = (int)(sizeof buffer - 2);
134 buffer[len++] = '\n';
138 sz = errs ? strlen(errs) : 0;
139 errs = realloc(errs, sz + (size_t)len);
141 memcpy(errs + sz, buffer, len);
146 /* get the value of the flag of 'name' in the array 'flags' of 'count elements */
147 static int get_flag(const char *name, struct flag flags[], int count)
149 /* dichotomic search */
150 int lower = 0, upper = count;
151 while (lower < upper) {
152 int mid = (lower + upper) >> 1;
153 int cmp = strcmp(name, flags[mid].name);
155 return flags[mid].value;
165 static struct json_object *timestamp(const struct afb_hookid *hookid)
169 snprintf(ts, sizeof ts, "%llu.%06lu", (long long unsigned)hookid->time.tv_sec, (long unsigned)(hookid->time.tv_nsec / 1000));
170 return json_object_new_string(ts);
173 /* verbosity level name or NULL */
174 static const char *verbosity_level_name(int level)
176 static const char *names[] = {
184 return level >= Log_Level_Error && level <= Log_Level_Debug ? names[level - Log_Level_Error] : NULL;
188 static void emit(void *closure, const struct afb_hookid *hookid, const char *type, const char *fmt1, const char *fmt2, va_list ap2, ...)
190 struct hook *hook = closure;
191 struct json_object *event, *data1, *data2;
194 data1 = data2 = event = NULL;
196 wrap_json_vpack(&data1, fmt1, ap1);
199 wrap_json_vpack(&data2, fmt2, ap2);
201 wrap_json_pack(&event, "{so ss ss si so so*}",
202 "time", timestamp(hookid),
203 "tag", hook->tag->tag,
205 "id", (int)(hookid->id & INT_MAX),
209 afb_evt_unhooked_push(hook->event->event, event);
212 /*******************************************************************************/
213 /***** trace the requests *****/
214 /*******************************************************************************/
216 static struct flag xreq_flags[] = { /* must be sorted by names */
217 { "addref", afb_hook_flag_req_addref },
218 { "all", afb_hook_flags_req_all },
219 { "args", afb_hook_flags_req_args },
220 { "begin", afb_hook_flag_req_begin },
221 { "common", afb_hook_flags_req_common },
222 { "context", afb_hook_flags_req_context },
223 { "context_get", afb_hook_flag_req_context_get },
224 { "context_set", afb_hook_flag_req_context_set },
225 { "end", afb_hook_flag_req_end },
226 { "event", afb_hook_flags_req_event },
227 { "extra", afb_hook_flags_req_extra },
228 { "fail", afb_hook_flag_req_fail },
229 { "get", afb_hook_flag_req_get },
230 { "get_application_id", afb_hook_flag_req_get_application_id },
231 { "has_permission", afb_hook_flag_req_has_permission },
232 { "json", afb_hook_flag_req_json },
233 { "life", afb_hook_flags_req_life },
234 { "ref", afb_hook_flags_req_ref },
235 { "result", afb_hook_flags_req_result },
236 { "security", afb_hook_flags_req_security },
237 { "session", afb_hook_flags_req_session },
238 { "session_close", afb_hook_flag_req_session_close },
239 { "session_set_LOA", afb_hook_flag_req_session_set_LOA },
240 { "store", afb_hook_flag_req_store },
241 { "stores", afb_hook_flags_req_stores },
242 { "subcall", afb_hook_flag_req_subcall },
243 { "subcall_req", afb_hook_flag_req_subcall_req },
244 { "subcall_req_result", afb_hook_flag_req_subcall_req_result },
245 { "subcall_result", afb_hook_flag_req_subcall_result },
246 { "subcalls", afb_hook_flags_req_subcalls },
247 { "subcallsync", afb_hook_flag_req_subcallsync },
248 { "subcallsync_result", afb_hook_flag_req_subcallsync_result },
249 { "subscribe", afb_hook_flag_req_subscribe },
250 { "success", afb_hook_flag_req_success },
251 { "unref", afb_hook_flag_req_unref },
252 { "unstore", afb_hook_flag_req_unstore },
253 { "unsubscribe", afb_hook_flag_req_unsubscribe },
254 { "vverbose", afb_hook_flag_req_vverbose },
257 /* get the xreq value for flag of 'name' */
258 static int get_xreq_flag(const char *name)
260 return get_flag(name, xreq_flags, (int)(sizeof xreq_flags / sizeof *xreq_flags));
263 static void hook_xreq(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *action, const char *format, ...)
265 struct json_object *cred = NULL;
266 const char *session = NULL;
269 if (xreq->context.session)
270 session = afb_session_uuid(xreq->context.session);
273 wrap_json_pack(&cred, "{si ss si si ss* ss*}",
274 "uid", (int)xreq->cred->uid,
275 "user", xreq->cred->user,
276 "gid", (int)xreq->cred->gid,
277 "pid", (int)xreq->cred->pid,
278 "label", xreq->cred->label,
281 va_start(ap, format);
282 emit(closure, hookid, "request", "{si ss ss ss so* ss*}", format, ap,
283 "index", xreq->hookindex,
292 static void hook_xreq_begin(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
294 hook_xreq(closure, hookid, xreq, "begin", NULL);
297 static void hook_xreq_end(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
299 hook_xreq(closure, hookid, xreq, "end", NULL);
302 static void hook_xreq_json(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *obj)
304 hook_xreq(closure, hookid, xreq, "json", "{sO?}",
308 static void hook_xreq_get(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *name, struct afb_arg arg)
310 hook_xreq(closure, hookid, xreq, "get", "{ss? ss? ss? ss?}",
317 static void hook_xreq_success(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *obj, const char *info)
319 hook_xreq(closure, hookid, xreq, "success", "{sO? ss?}",
324 static void hook_xreq_fail(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *status, const char *info)
326 hook_xreq(closure, hookid, xreq, "fail", "{ss? ss?}",
331 static void hook_xreq_context_get(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, void *value)
333 hook_xreq(closure, hookid, xreq, "context_get", NULL);
336 static void hook_xreq_context_set(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, void *value, void (*free_value)(void*))
338 hook_xreq(closure, hookid, xreq, "context_set", NULL);
341 static void hook_xreq_addref(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
343 hook_xreq(closure, hookid, xreq, "addref", NULL);
346 static void hook_xreq_unref(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
348 hook_xreq(closure, hookid, xreq, "unref", NULL);
351 static void hook_xreq_session_close(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
353 hook_xreq(closure, hookid, xreq, "session_close", NULL);
356 static void hook_xreq_session_set_LOA(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, unsigned level, int result)
358 hook_xreq(closure, hookid, xreq, "session_set_LOA", "{si si}",
363 static void hook_xreq_subscribe(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_event event, int result)
365 hook_xreq(closure, hookid, xreq, "subscribe", "{s{ss si} si}",
367 "name", afb_evt_event_name(event),
368 "id", afb_evt_event_id(event),
372 static void hook_xreq_unsubscribe(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_event event, int result)
374 hook_xreq(closure, hookid, xreq, "unsubscribe", "{s{ss? si} si}",
376 "name", afb_evt_event_name(event),
377 "id", afb_evt_event_id(event),
381 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)
383 hook_xreq(closure, hookid, xreq, "subcall", "{ss? ss? sO?}",
389 static void hook_xreq_subcall_result(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int status, struct json_object *result)
391 hook_xreq(closure, hookid, xreq, "subcall_result", "{si sO?}",
396 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)
398 hook_xreq(closure, hookid, xreq, "subcallsync", "{ss? ss? sO?}",
404 static void hook_xreq_subcallsync_result(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int status, struct json_object *result)
406 hook_xreq(closure, hookid, xreq, "subcallsync_result", "{si sO?}",
411 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)
413 struct json_object *pos;
422 len = vasprintf(&msg, fmt, ap);
426 wrap_json_pack(&pos, "{ss si ss*}", "file", file, "line", line, "function", func);
428 hook_xreq(closure, hookid, xreq, "vverbose", "{si ss* ss? so*}",
430 "type", verbosity_level_name(level),
431 len < 0 ? "format" : "message", len < 0 ? fmt : msg,
437 static void hook_xreq_store(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_stored_req *sreq)
439 hook_xreq(closure, hookid, xreq, "store", NULL);
442 static void hook_xreq_unstore(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
444 hook_xreq(closure, hookid, xreq, "unstore", NULL);
447 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)
449 hook_xreq(closure, hookid, xreq, "subcall_req", "{ss? ss? sO?}",
455 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)
457 hook_xreq(closure, hookid, xreq, "subcall_req_result", "{si sO?}",
462 static void hook_xreq_has_permission(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *permission, int result)
464 hook_xreq(closure, hookid, xreq, "has_permission", "{ss sb}",
465 "permission", permission,
469 static void hook_xreq_get_application_id(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, char *result)
471 hook_xreq(closure, hookid, xreq, "get_application_id", "{ss?}",
475 static struct afb_hook_xreq_itf hook_xreq_itf = {
476 .hook_xreq_begin = hook_xreq_begin,
477 .hook_xreq_end = hook_xreq_end,
478 .hook_xreq_json = hook_xreq_json,
479 .hook_xreq_get = hook_xreq_get,
480 .hook_xreq_success = hook_xreq_success,
481 .hook_xreq_fail = hook_xreq_fail,
482 .hook_xreq_context_get = hook_xreq_context_get,
483 .hook_xreq_context_set = hook_xreq_context_set,
484 .hook_xreq_addref = hook_xreq_addref,
485 .hook_xreq_unref = hook_xreq_unref,
486 .hook_xreq_session_close = hook_xreq_session_close,
487 .hook_xreq_session_set_LOA = hook_xreq_session_set_LOA,
488 .hook_xreq_subscribe = hook_xreq_subscribe,
489 .hook_xreq_unsubscribe = hook_xreq_unsubscribe,
490 .hook_xreq_subcall = hook_xreq_subcall,
491 .hook_xreq_subcall_result = hook_xreq_subcall_result,
492 .hook_xreq_subcallsync = hook_xreq_subcallsync,
493 .hook_xreq_subcallsync_result = hook_xreq_subcallsync_result,
494 .hook_xreq_vverbose = hook_xreq_vverbose,
495 .hook_xreq_store = hook_xreq_store,
496 .hook_xreq_unstore = hook_xreq_unstore,
497 .hook_xreq_subcall_req = hook_xreq_subcall_req,
498 .hook_xreq_subcall_req_result = hook_xreq_subcall_req_result,
499 .hook_xreq_has_permission = hook_xreq_has_permission,
500 .hook_xreq_get_application_id = hook_xreq_get_application_id
503 /*******************************************************************************/
504 /***** trace the daemon interface *****/
505 /*******************************************************************************/
507 static struct flag ditf_flags[] = { /* must be sorted by names */
508 { "all", afb_hook_flags_ditf_all },
509 { "common", afb_hook_flags_ditf_common },
510 { "event_broadcast_after", afb_hook_flag_ditf_event_broadcast_after },
511 { "event_broadcast_before", afb_hook_flag_ditf_event_broadcast_before },
512 { "event_make", afb_hook_flag_ditf_event_make },
513 { "extra", afb_hook_flags_ditf_extra },
514 { "get_event_loop", afb_hook_flag_ditf_get_event_loop },
515 { "get_system_bus", afb_hook_flag_ditf_get_system_bus },
516 { "get_user_bus", afb_hook_flag_ditf_get_user_bus },
517 { "queue_job", afb_hook_flag_ditf_queue_job },
518 { "require_api", afb_hook_flag_ditf_require_api },
519 { "require_api_result", afb_hook_flag_ditf_require_api_result },
520 { "rootdir_get_fd", afb_hook_flag_ditf_rootdir_get_fd },
521 { "rootdir_open_locale", afb_hook_flag_ditf_rootdir_open_locale },
522 { "unstore_req", afb_hook_flag_ditf_unstore_req },
523 { "vverbose", afb_hook_flag_ditf_vverbose },
526 /* get the export value for flag of 'name' */
527 static int get_ditf_flag(const char *name)
529 return get_flag(name, ditf_flags, (int)(sizeof ditf_flags / sizeof *ditf_flags));
533 static void hook_ditf(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *action, const char *format, ...)
537 va_start(ap, format);
538 emit(closure, hookid, "daemon", "{ss ss}", format, ap,
539 "api", afb_export_apiname(export),
544 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)
546 hook_ditf(closure, hookid, export, "event_broadcast_before", "{ss sO*}",
547 "name", name, "data", object);
550 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)
552 hook_ditf(closure, hookid, export, "event_broadcast_after", "{ss sO* si}",
553 "name", name, "data", object, "result", result);
556 static void hook_ditf_get_event_loop(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_event *result)
558 hook_ditf(closure, hookid, export, "get_event_loop", NULL);
561 static void hook_ditf_get_user_bus(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_bus *result)
563 hook_ditf(closure, hookid, export, "get_user_bus", NULL);
566 static void hook_ditf_get_system_bus(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_bus *result)
568 hook_ditf(closure, hookid, export, "get_system_bus", NULL);
571 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)
573 struct json_object *pos;
582 len = vasprintf(&msg, fmt, ap);
586 wrap_json_pack(&pos, "{ss si ss*}", "file", file, "line", line, "function", function);
588 hook_ditf(closure, hookid, export, "vverbose", "{si ss* ss? so*}",
590 "type", verbosity_level_name(level),
591 len < 0 ? "format" : "message", len < 0 ? fmt : msg,
597 static void hook_ditf_event_make(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, struct afb_event result)
599 hook_ditf(closure, hookid, export, "event_make", "{ss ss si}",
600 "name", name, "event", afb_evt_event_name(result), "id", afb_evt_event_id(result));
603 static void hook_ditf_rootdir_get_fd(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result)
608 sprintf(path, "/proc/self/fd/%d", result);
609 readlink(path, path, sizeof path);
612 hook_ditf(closure, hookid, export, "rootdir_get_fd", "{ss}",
613 result < 0 ? "path" : "error",
614 result < 0 ? strerror(errno) : path);
617 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)
622 sprintf(path, "/proc/self/fd/%d", result);
623 readlink(path, path, sizeof path);
626 hook_ditf(closure, hookid, export, "rootdir_open_locale", "{ss si ss* ss}",
630 result < 0 ? "path" : "error",
631 result < 0 ? strerror(errno) : path);
634 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)
636 hook_ditf(closure, hookid, export, "queue_job", "{ss}", "result", result);
639 static void hook_ditf_unstore_req(void * closure, const struct afb_hookid *hookid, const struct afb_export *export, struct afb_stored_req *sreq)
641 hook_ditf(closure, hookid, export, "unstore_req", NULL);
644 static void hook_ditf_require_api(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, int initialized)
646 hook_ditf(closure, hookid, export, "require_api", "{ss sb}", "name", name, "initialized", initialized);
649 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)
651 hook_ditf(closure, hookid, export, "require_api_result", "{ss sb si}", "name", name, "initialized", initialized, "result", result);
654 static struct afb_hook_ditf_itf hook_ditf_itf = {
655 .hook_ditf_event_broadcast_before = hook_ditf_event_broadcast_before,
656 .hook_ditf_event_broadcast_after = hook_ditf_event_broadcast_after,
657 .hook_ditf_get_event_loop = hook_ditf_get_event_loop,
658 .hook_ditf_get_user_bus = hook_ditf_get_user_bus,
659 .hook_ditf_get_system_bus = hook_ditf_get_system_bus,
660 .hook_ditf_vverbose = hook_ditf_vverbose,
661 .hook_ditf_event_make = hook_ditf_event_make,
662 .hook_ditf_rootdir_get_fd = hook_ditf_rootdir_get_fd,
663 .hook_ditf_rootdir_open_locale = hook_ditf_rootdir_open_locale,
664 .hook_ditf_queue_job = hook_ditf_queue_job,
665 .hook_ditf_unstore_req = hook_ditf_unstore_req,
666 .hook_ditf_require_api = hook_ditf_require_api,
667 .hook_ditf_require_api_result = hook_ditf_require_api_result
670 /*******************************************************************************/
671 /***** trace the services *****/
672 /*******************************************************************************/
674 static struct flag svc_flags[] = { /* must be sorted by names */
675 { "all", afb_hook_flags_svc_all },
676 { "call", afb_hook_flag_svc_call },
677 { "call_result", afb_hook_flag_svc_call_result },
678 { "callsync", afb_hook_flag_svc_callsync },
679 { "callsync_result", afb_hook_flag_svc_callsync_result },
680 { "on_event_after", afb_hook_flag_svc_on_event_after },
681 { "on_event_before", afb_hook_flag_svc_on_event_before },
682 { "start_after", afb_hook_flag_svc_start_after },
683 { "start_before", afb_hook_flag_svc_start_before },
686 /* get the export value for flag of 'name' */
687 static int get_svc_flag(const char *name)
689 return get_flag(name, svc_flags, (int)(sizeof svc_flags / sizeof *svc_flags));
692 static void hook_svc(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *action, const char *format, ...)
696 va_start(ap, format);
697 emit(closure, hookid, "service", "{ss ss}", format, ap,
698 "api", afb_export_apiname(export),
703 static void hook_svc_start_before(void *closure, const struct afb_hookid *hookid, const struct afb_export *export)
705 hook_svc(closure, hookid, export, "start_before", NULL);
708 static void hook_svc_start_after(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int status)
710 hook_svc(closure, hookid, export, "start_after", "{si}", "result", status);
713 static void hook_svc_on_event_before(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *event, int eventid, struct json_object *object)
715 hook_svc(closure, hookid, export, "on_event_before", "{ss si sO*}",
716 "event", event, "id", eventid, "data", object);
719 static void hook_svc_on_event_after(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *event, int eventid, struct json_object *object)
721 hook_svc(closure, hookid, export, "on_event_after", "{ss si sO*}",
722 "event", event, "id", eventid, "data", object);
725 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)
727 hook_svc(closure, hookid, export, "call", "{ss ss sO*}",
728 "api", api, "verb", verb, "args", args);
731 static void hook_svc_call_result(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int status, struct json_object *result)
733 hook_svc(closure, hookid, export, "call_result", "{si sO*}",
734 "status", status, "result", result);
737 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)
739 hook_svc(closure, hookid, export, "callsync", "{ss ss sO*}",
740 "api", api, "verb", verb, "args", args);
743 static void hook_svc_callsync_result(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int status, struct json_object *result)
745 hook_svc(closure, hookid, export, "callsync_result", "{si sO*}",
746 "status", status, "result", result);
749 static struct afb_hook_svc_itf hook_svc_itf = {
750 .hook_svc_start_before = hook_svc_start_before,
751 .hook_svc_start_after = hook_svc_start_after,
752 .hook_svc_on_event_before = hook_svc_on_event_before,
753 .hook_svc_on_event_after = hook_svc_on_event_after,
754 .hook_svc_call = hook_svc_call,
755 .hook_svc_call_result = hook_svc_call_result,
756 .hook_svc_callsync = hook_svc_callsync,
757 .hook_svc_callsync_result = hook_svc_callsync_result
760 /*******************************************************************************/
761 /***** trace the events *****/
762 /*******************************************************************************/
764 static struct flag evt_flags[] = { /* must be sorted by names */
765 { "all", afb_hook_flags_evt_all },
766 { "broadcast_after", afb_hook_flag_evt_broadcast_after },
767 { "broadcast_before", afb_hook_flag_evt_broadcast_before },
768 { "common", afb_hook_flags_evt_common },
769 { "create", afb_hook_flag_evt_create },
770 { "drop", afb_hook_flag_evt_drop },
771 { "extra", afb_hook_flags_evt_extra },
772 { "name", afb_hook_flag_evt_name },
773 { "push_after", afb_hook_flag_evt_push_after },
774 { "push_before", afb_hook_flag_evt_push_before },
777 /* get the evt value for flag of 'name' */
778 static int get_evt_flag(const char *name)
780 return get_flag(name, evt_flags, (int)(sizeof evt_flags / sizeof *evt_flags));
783 static void hook_evt(void *closure, const struct afb_hookid *hookid, const char *evt, int id, const char *action, const char *format, ...)
787 va_start(ap, format);
788 emit(closure, hookid, "event", "{si ss ss}", format, ap,
795 static void hook_evt_create(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
797 hook_evt(closure, hookid, evt, id, "create", NULL);
800 static void hook_evt_push_before(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj)
802 hook_evt(closure, hookid, evt, id, "push_before", "{sO*}", "data", obj);
806 static void hook_evt_push_after(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj, int result)
808 hook_evt(closure, hookid, evt, id, "push_after", "{sO* si}", "data", obj, "result", result);
811 static void hook_evt_broadcast_before(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj)
813 hook_evt(closure, hookid, evt, id, "broadcast_before", "{sO*}", "data", obj);
816 static void hook_evt_broadcast_after(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj, int result)
818 hook_evt(closure, hookid, evt, id, "broadcast_after", "{sO* si}", "data", obj, "result", result);
821 static void hook_evt_name(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
823 hook_evt(closure, hookid, evt, id, "name", NULL);
826 static void hook_evt_drop(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
828 hook_evt(closure, hookid, evt, id, "drop", NULL);
831 static struct afb_hook_evt_itf hook_evt_itf = {
832 .hook_evt_create = hook_evt_create,
833 .hook_evt_push_before = hook_evt_push_before,
834 .hook_evt_push_after = hook_evt_push_after,
835 .hook_evt_broadcast_before = hook_evt_broadcast_before,
836 .hook_evt_broadcast_after = hook_evt_broadcast_after,
837 .hook_evt_name = hook_evt_name,
838 .hook_evt_drop = hook_evt_drop
841 /*******************************************************************************/
842 /***** trace the globals *****/
843 /*******************************************************************************/
845 static struct flag global_flags[] = { /* must be sorted by names */
846 { "all", afb_hook_flags_global_all },
847 { "vverbose", afb_hook_flag_global_vverbose },
850 /* get the global value for flag of 'name' */
851 static int get_global_flag(const char *name)
853 return get_flag(name, global_flags, (int)(sizeof global_flags / sizeof *global_flags));
856 static void hook_global(void *closure, const struct afb_hookid *hookid, const char *action, const char *format, ...)
860 va_start(ap, format);
861 emit(closure, hookid, "global", "{ss}", format, ap, "action", action);
865 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)
867 struct json_object *pos;
876 len = vasprintf(&msg, fmt, ap);
880 wrap_json_pack(&pos, "{ss si ss*}", "file", file, "line", line, "function", function);
882 hook_global(closure, hookid, "vverbose", "{si ss* ss? so*}",
884 "type", verbosity_level_name(level),
885 len < 0 ? "format" : "message", len < 0 ? fmt : msg,
891 static struct afb_hook_global_itf hook_global_itf = {
892 .hook_global_vverbose = hook_global_vverbose,
895 /*******************************************************************************/
896 /***** abstract types *****/
897 /*******************************************************************************/
903 void (*unref)(void*);
904 int (*get_flag)(const char*);
906 abstracting[Trace_Type_Count] =
911 .unref = (void(*)(void*))afb_hook_unref_xreq,
912 .get_flag = get_xreq_flag
917 .unref = (void(*)(void*))afb_hook_unref_ditf,
918 .get_flag = get_ditf_flag
923 .unref = (void(*)(void*))afb_hook_unref_svc,
924 .get_flag = get_svc_flag
929 .unref = (void(*)(void*))afb_hook_unref_evt,
930 .get_flag = get_evt_flag
932 [Trace_Type_Global] =
935 .unref = (void(*)(void*))afb_hook_unref_global,
936 .get_flag = get_global_flag
940 /*******************************************************************************/
941 /***** handle trace data *****/
942 /*******************************************************************************/
944 /* drop hooks of 'trace' matching 'tag' and 'event' and 'session' */
945 static void trace_unhook(struct afb_trace *trace, struct tag *tag, struct event *event, struct session *session)
948 struct hook *hook, **prev;
950 /* remove any event */
951 for (i = 0 ; i < Trace_Type_Count ; i++) {
952 prev = &trace->hooks[i];
953 while ((hook = *prev)) {
954 if ((tag && tag != hook->tag)
955 || (event && event != hook->event)
956 || (session && session != hook->session))
960 abstracting[i].unref(hook->handler);
967 /* cleanup: removes unused tags, events and sessions of the 'trace' */
968 static void trace_cleanup(struct afb_trace *trace)
972 struct tag *tag, **ptag;
973 struct event *event, **pevent;
974 struct session *session, **psession;
977 psession = &trace->sessions;
978 while ((session = *psession)) {
979 /* search for session */
980 for (hook = NULL, i = 0 ; !hook && i < Trace_Type_Count ; i++)
981 for (hook = trace->hooks[i] ; hook && hook->session != session ; hook = hook->next);
982 /* keep or free whether used or not */
984 psession = &session->next;
986 *psession = session->next;
987 if (__atomic_exchange_n(&session->trace, NULL, __ATOMIC_RELAXED))
988 afb_session_set_cookie(session->session, session, NULL, NULL);
994 while ((tag = *ptag)) {
996 for (hook = NULL, i = 0 ; !hook && i < Trace_Type_Count ; i++)
997 for (hook = trace->hooks[i] ; hook && hook->tag != tag ; hook = hook->next);
998 /* keep or free whether used or not */
1007 pevent = &trace->events;
1008 while ((event = *pevent)) {
1009 /* search for event */
1010 for (hook = NULL, i = 0 ; !hook && i < Trace_Type_Count ; i++)
1011 for (hook = trace->hooks[i] ; hook && hook->event != event ; hook = hook->next);
1012 /* keep or free whether used or not */
1014 pevent = &event->next;
1016 *pevent = event->next;
1017 afb_event_drop(event->event);
1023 /* callback at end of traced session */
1024 static void free_session_cookie(void *cookie)
1026 struct session *session = cookie;
1027 struct afb_trace *trace = __atomic_exchange_n(&session->trace, NULL, __ATOMIC_RELAXED);
1029 pthread_mutex_lock(&trace->mutex);
1030 trace_unhook(trace, NULL, NULL, session);
1031 trace_cleanup(trace);
1032 pthread_mutex_unlock(&trace->mutex);
1037 * Get the tag of 'name' within 'trace'.
1038 * If 'alloc' isn't zero, create the tag and add it.
1040 static struct tag *trace_get_tag(struct afb_trace *trace, const char *name, int alloc)
1044 /* search the tag of 'name' */
1046 while (tag && strcmp(name, tag->tag))
1049 if (!tag && alloc) {
1050 /* creation if needed */
1051 tag = malloc(sizeof * tag + strlen(name));
1053 strcpy(tag->tag, name);
1054 tag->next = trace->tags;
1062 * Get the event of 'name' within 'trace'.
1063 * If 'alloc' isn't zero, create the event and add it.
1065 static struct event *trace_get_event(struct afb_trace *trace, const char *name, int alloc)
1067 struct event *event;
1069 /* search the event */
1070 event = trace->events;
1071 while (event && strcmp(afb_event_name(event->event), name))
1072 event = event->next;
1074 if (!event && alloc) {
1075 event = malloc(sizeof * event);
1077 event->event = trace->daemon->itf->event_make(trace->daemon->closure, name);
1078 if (afb_event_is_valid(event->event)) {
1079 event->next = trace->events;
1080 trace->events = event;
1091 * Get the session of 'value' within 'trace'.
1092 * If 'alloc' isn't zero, create the session and add it.
1094 static struct session *trace_get_session(struct afb_trace *trace, struct afb_session *value, int alloc)
1096 struct session *session;
1098 /* search the session */
1099 session = trace->sessions;
1100 while (session && session->session != value)
1101 session = session->next;
1103 if (!session && alloc) {
1104 session = malloc(sizeof * session);
1106 session->session = value;
1107 session->trace = NULL;
1108 session->next = trace->sessions;
1109 trace->sessions = session;
1116 * Get the session of 'uuid' within 'trace'.
1117 * If 'alloc' isn't zero, create the session and add it.
1119 static struct session *trace_get_session_by_uuid(struct afb_trace *trace, const char *uuid, int alloc)
1121 struct afb_session *session;
1124 session = afb_session_get(uuid, alloc ? &created : NULL);
1125 return session ? trace_get_session(trace, session, alloc) : NULL;
1128 static struct hook *trace_make_detached_hook(struct afb_trace *trace, const char *event, const char *tag)
1132 tag = tag ?: DEFAULT_TAG_NAME;
1133 event = event ?: DEFAULT_EVENT_NAME;
1134 hook = malloc(sizeof *hook);
1136 hook->tag = trace_get_tag(trace, tag, 1);
1137 hook->event = trace_get_event(trace, event, 1);
1138 hook->session = NULL;
1139 hook->handler = NULL;
1144 static void trace_attach_hook(struct afb_trace *trace, struct hook *hook, enum trace_type type)
1146 struct session *session = hook->session;
1147 hook->next = trace->hooks[type];
1148 trace->hooks[type] = hook;
1149 if (session && !session->trace) {
1150 session->trace = trace;
1151 afb_session_set_cookie(session->session, session, session, free_session_cookie);
1155 /*******************************************************************************/
1156 /***** handle client requests *****/
1157 /*******************************************************************************/
1161 struct afb_trace *trace;
1168 struct context *context;
1171 const char *session;
1174 const char *pattern;
1175 int flags[Trace_Type_Count];
1179 static void addhook(struct desc *desc, enum trace_type type)
1182 struct session *session;
1183 struct afb_session *bind;
1184 struct afb_trace *trace = desc->context->trace;
1186 /* check permission for bound traces */
1187 bind = trace->bound;
1189 if (type != Trace_Type_Xreq) {
1190 ctxt_error(&desc->context->errors, "tracing %s is forbidden", abstracting[type].name);
1193 if (desc->session) {
1194 ctxt_error(&desc->context->errors, "setting session is forbidden");
1199 /* allocate the hook */
1200 hook = trace_make_detached_hook(trace, desc->name, desc->tag);
1202 ctxt_error(&desc->context->errors, "allocation of hook failed");
1206 /* create the hook handler */
1208 case Trace_Type_Xreq:
1209 if (desc->session) {
1210 session = trace_get_session_by_uuid(trace, desc->session, 1);
1212 ctxt_error(&desc->context->errors, "allocation of session failed");
1216 bind = session->session;
1218 hook->handler = afb_hook_create_xreq(desc->api, desc->verb, bind,
1219 desc->flags[type], &hook_xreq_itf, hook);
1221 case Trace_Type_Ditf:
1222 hook->handler = afb_hook_create_ditf(desc->api, desc->flags[type], &hook_ditf_itf, hook);
1224 case Trace_Type_Svc:
1225 hook->handler = afb_hook_create_svc(desc->api, desc->flags[type], &hook_svc_itf, hook);
1227 case Trace_Type_Evt:
1228 hook->handler = afb_hook_create_evt(desc->pattern, desc->flags[type], &hook_evt_itf, hook);
1230 case Trace_Type_Global:
1231 hook->handler = afb_hook_create_global(desc->flags[type], &hook_global_itf, hook);
1236 if (!hook->handler) {
1237 ctxt_error(&desc->context->errors, "creation of hook failed");
1242 /* attach and activate the hook */
1243 afb_req_subscribe(desc->context->req, hook->event->event);
1244 trace_attach_hook(trace, hook, type);
1247 static void addhooks(struct desc *desc)
1251 for (i = 0 ; i < Trace_Type_Count ; i++) {
1257 static void add_flags(void *closure, struct json_object *object, enum trace_type type)
1260 const char *name, *queried;
1261 struct desc *desc = closure;
1263 if (wrap_json_unpack(object, "s", &name))
1264 ctxt_error(&desc->context->errors, "unexpected %s value %s",
1265 abstracting[type].name,
1266 json_object_to_json_string(object));
1268 queried = (name[0] == '*' && !name[1]) ? "all" : name;
1269 value = abstracting[type].get_flag(queried);
1271 desc->flags[type] |= value;
1273 ctxt_error(&desc->context->errors, "unknown %s name %s",
1274 abstracting[type].name, name);
1278 static void add_xreq_flags(void *closure, struct json_object *object)
1280 add_flags(closure, object, Trace_Type_Xreq);
1283 static void add_ditf_flags(void *closure, struct json_object *object)
1285 add_flags(closure, object, Trace_Type_Ditf);
1288 static void add_svc_flags(void *closure, struct json_object *object)
1290 add_flags(closure, object, Trace_Type_Svc);
1293 static void add_evt_flags(void *closure, struct json_object *object)
1295 add_flags(closure, object, Trace_Type_Evt);
1298 static void add_global_flags(void *closure, struct json_object *object)
1300 add_flags(closure, object, Trace_Type_Global);
1304 static void add(void *closure, struct json_object *object)
1308 struct json_object *request, *event, *daemon, *service, *sub, *global;
1310 memcpy (&desc, closure, sizeof desc);
1311 request = event = daemon = service = sub = global = NULL;
1313 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}",
1318 "session", &desc.session,
1319 "pattern", &desc.pattern,
1320 "request", &request,
1322 "service", &service,
1329 if (desc.api && desc.api[0] == '*' && !desc.api[1])
1332 if (desc.verb && desc.verb[0] == '*' && !desc.verb[1])
1335 if (desc.session && desc.session[0] == '*' && !desc.session[1])
1336 desc.session = NULL;
1338 /* get what is expected */
1340 wrap_json_optarray_for_all(request, add_xreq_flags, &desc);
1343 wrap_json_optarray_for_all(daemon, add_ditf_flags, &desc);
1346 wrap_json_optarray_for_all(service, add_svc_flags, &desc);
1349 wrap_json_optarray_for_all(event, add_evt_flags, &desc);
1352 wrap_json_optarray_for_all(global, add_global_flags, &desc);
1356 wrap_json_optarray_for_all(sub, add, &desc);
1361 wrap_json_optarray_for_all(object, add_xreq_flags, &desc);
1366 /* drop hooks of given tag */
1367 static void drop_tag(void *closure, struct json_object *object)
1370 struct context *context = closure;
1374 rc = wrap_json_unpack(object, "s", &name);
1376 ctxt_error(&context->errors, "unexpected tag value %s", json_object_to_json_string(object));
1378 tag = trace_get_tag(context->trace, name, 0);
1380 ctxt_error(&context->errors, "tag %s not found", name);
1382 trace_unhook(context->trace, tag, NULL, NULL);
1386 /* drop hooks of given event */
1387 static void drop_event(void *closure, struct json_object *object)
1390 struct context *context = closure;
1391 struct event *event;
1394 rc = wrap_json_unpack(object, "s", &name);
1396 ctxt_error(&context->errors, "unexpected event value %s", json_object_to_json_string(object));
1398 event = trace_get_event(context->trace, name, 0);
1400 ctxt_error(&context->errors, "event %s not found", name);
1402 trace_unhook(context->trace, NULL, event, NULL);
1406 /* drop hooks of given session */
1407 static void drop_session(void *closure, struct json_object *object)
1410 struct context *context = closure;
1411 struct session *session;
1414 rc = wrap_json_unpack(object, "s", &uuid);
1416 ctxt_error(&context->errors, "unexpected session value %s", json_object_to_json_string(object));
1418 session = trace_get_session_by_uuid(context->trace, uuid, 0);
1420 ctxt_error(&context->errors, "session %s not found", uuid);
1422 trace_unhook(context->trace, NULL, NULL, session);
1426 /*******************************************************************************/
1427 /***** public interface *****/
1428 /*******************************************************************************/
1430 /* allocates an afb_trace instance */
1431 struct afb_trace *afb_trace_create(struct afb_daemon *daemon, struct afb_session *bound)
1433 struct afb_trace *trace;
1437 trace = calloc(1, sizeof *trace);
1439 trace->refcount = 1;
1440 trace->bound = bound;
1441 trace->daemon = daemon;
1442 pthread_mutex_init(&trace->mutex, NULL);
1447 /* add a reference to the trace */
1448 void afb_trace_addref(struct afb_trace *trace)
1450 __atomic_add_fetch(&trace->refcount, 1, __ATOMIC_RELAXED);
1453 /* drop one reference to the trace */
1454 void afb_trace_unref(struct afb_trace *trace)
1456 if (trace && !__atomic_sub_fetch(&trace->refcount, 1, __ATOMIC_RELAXED)) {
1458 trace_unhook(trace, NULL, NULL, NULL);
1459 trace_cleanup(trace);
1460 pthread_mutex_destroy(&trace->mutex);
1466 int afb_trace_add(struct afb_req req, struct json_object *args, struct afb_trace *trace)
1468 struct context context;
1471 memset(&context, 0, sizeof context);
1472 context.trace = trace;
1475 memset(&desc, 0, sizeof desc);
1476 desc.context = &context;
1478 pthread_mutex_lock(&trace->mutex);
1479 wrap_json_optarray_for_all(args, add, &desc);
1480 pthread_mutex_unlock(&trace->mutex);
1482 if (!context.errors)
1485 afb_req_fail(req, "error-detected", context.errors);
1486 free(context.errors);
1491 extern int afb_trace_drop(struct afb_req req, struct json_object *args, struct afb_trace *trace)
1494 struct context context;
1495 struct json_object *tags, *events, *sessions;
1497 memset(&context, 0, sizeof context);
1498 context.trace = trace;
1501 /* special: boolean value */
1502 if (!wrap_json_unpack(args, "b", &rc)) {
1504 pthread_mutex_lock(&trace->mutex);
1505 trace_unhook(trace, NULL, NULL, NULL);
1506 trace_cleanup(trace);
1507 pthread_mutex_unlock(&trace->mutex);
1512 tags = events = sessions = NULL;
1513 rc = wrap_json_unpack(args, "{s?o s?o s?o}",
1516 "session", &sessions);
1518 if (rc < 0 || !(events || tags || sessions)) {
1519 afb_req_fail(req, "error-detected", "bad drop arguments");
1523 pthread_mutex_lock(&trace->mutex);
1526 wrap_json_optarray_for_all(tags, drop_tag, &context);
1529 wrap_json_optarray_for_all(events, drop_event, &context);
1532 wrap_json_optarray_for_all(sessions, drop_session, &context);
1534 trace_cleanup(trace);
1536 pthread_mutex_unlock(&trace->mutex);
1538 if (!context.errors)
1541 afb_req_fail(req, "error-detected", context.errors);
1542 free(context.errors);