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 #define AFB_BINDING_VERSION 0
31 #include <afb/afb-binding.h>
35 #include "afb-session.h"
37 #include "afb-export.h"
39 #include "afb-trace.h"
41 #include "wrap-json.h"
44 /*******************************************************************************/
45 /***** default names *****/
46 /*******************************************************************************/
48 #if !defined(DEFAULT_EVENT_NAME)
49 # define DEFAULT_EVENT_NAME "trace"
51 #if !defined(DEFAULT_TAG_NAME)
52 # define DEFAULT_TAG_NAME "trace"
55 /*******************************************************************************/
57 /*******************************************************************************/
59 /* structure for searching flags by names */
62 const char *name; /** the name */
63 int value; /** the value */
68 struct tag *next; /* link to the next */
69 char tag[1]; /* name of the tag */
72 /* struct for events */
74 struct event *next; /* link to the next event */
75 struct afb_evtid *evtid; /* the event */
78 /* struct for sessions */
80 struct session *next; /* link to the next session */
81 struct afb_session *session; /* the session */
82 struct afb_trace *trace; /* the tracer */
85 /* struct for recording hooks */
87 struct hook *next; /* link to next hook */
88 void *handler; /* the handler of the hook */
89 struct event *event; /* the associated event */
90 struct tag *tag; /* the associated tag */
91 struct session *session; /* the associated session */
97 Trace_Type_Xreq, /* xreq hooks */
98 Trace_Type_Ditf, /* export hooks */
99 Trace_Type_Svc, /* export hooks */
100 Trace_Type_Evt, /* evt hooks */
101 Trace_Type_Global, /* global hooks */
102 Trace_Type_Count /* count of types of hooks */
108 int refcount; /* reference count */
109 pthread_mutex_t mutex; /* concurrency management */
110 struct afb_daemon *daemon; /* daemon */
111 struct afb_session *bound; /* bound to session */
112 struct event *events; /* list of events */
113 struct tag *tags; /* list of tags */
114 struct session *sessions; /* list of tags */
115 struct hook *hooks[Trace_Type_Count]; /* hooks */
118 /*******************************************************************************/
119 /***** utility functions *****/
120 /*******************************************************************************/
122 static void ctxt_error(char **errors, const char *format, ...)
130 va_start(ap, format);
131 len = vsnprintf(buffer, sizeof buffer, format, ap);
133 if (len > (int)(sizeof buffer - 2))
134 len = (int)(sizeof buffer - 2);
135 buffer[len++] = '\n';
139 sz = errs ? strlen(errs) : 0;
140 errs = realloc(errs, sz + (size_t)len);
142 memcpy(errs + sz, buffer, len);
147 /* get the value of the flag of 'name' in the array 'flags' of 'count elements */
148 static int get_flag(const char *name, struct flag flags[], int count)
150 /* dichotomic search */
151 int lower = 0, upper = count;
152 while (lower < upper) {
153 int mid = (lower + upper) >> 1;
154 int cmp = strcmp(name, flags[mid].name);
156 return flags[mid].value;
166 static struct json_object *timestamp(const struct afb_hookid *hookid)
170 snprintf(ts, sizeof ts, "%llu.%06lu", (long long unsigned)hookid->time.tv_sec, (long unsigned)(hookid->time.tv_nsec / 1000));
171 return json_object_new_string(ts);
174 /* verbosity level name or NULL */
175 static const char *verbosity_level_name(int level)
177 static const char *names[] = {
185 return level >= Log_Level_Error && level <= Log_Level_Debug ? names[level - Log_Level_Error] : NULL;
189 static void emit(void *closure, const struct afb_hookid *hookid, const char *type, const char *fmt1, const char *fmt2, va_list ap2, ...)
191 struct hook *hook = closure;
192 struct json_object *data, *data1, *data2;
195 data1 = data2 = data = NULL;
197 wrap_json_vpack(&data1, fmt1, ap1);
200 wrap_json_vpack(&data2, fmt2, ap2);
202 wrap_json_pack(&data, "{so ss ss si so so*}",
203 "time", timestamp(hookid),
204 "tag", hook->tag->tag,
206 "id", (int)(hookid->id & INT_MAX),
210 afb_evt_evtid_push(hook->event->evtid, data);
213 /*******************************************************************************/
214 /***** trace the requests *****/
215 /*******************************************************************************/
217 static struct flag xreq_flags[] = { /* must be sorted by names */
218 { "addref", afb_hook_flag_req_addref },
219 { "all", afb_hook_flags_req_all },
220 { "args", afb_hook_flags_req_args },
221 { "begin", afb_hook_flag_req_begin },
222 { "common", afb_hook_flags_req_common },
223 { "context", afb_hook_flags_req_context },
224 { "context_get", afb_hook_flag_req_context_get },
225 { "context_make", afb_hook_flag_req_context_make },
226 { "context_set", afb_hook_flag_req_context_set },
227 { "end", afb_hook_flag_req_end },
228 { "event", afb_hook_flags_req_event },
229 { "extra", afb_hook_flags_req_extra },
230 { "fail", afb_hook_flag_req_fail },
231 { "get", afb_hook_flag_req_get },
232 { "get_application_id", afb_hook_flag_req_get_application_id },
233 { "has_permission", afb_hook_flag_req_has_permission },
234 { "json", afb_hook_flag_req_json },
235 { "life", afb_hook_flags_req_life },
236 { "ref", afb_hook_flags_req_ref },
237 { "result", afb_hook_flags_req_result },
238 { "security", afb_hook_flags_req_security },
239 { "session", afb_hook_flags_req_session },
240 { "session_close", afb_hook_flag_req_session_close },
241 { "session_set_LOA", afb_hook_flag_req_session_set_LOA },
242 { "store", afb_hook_flag_req_store },
243 { "stores", afb_hook_flags_req_stores },
244 { "subcall", afb_hook_flag_req_subcall },
245 { "subcall_req", afb_hook_flag_req_subcall_req },
246 { "subcall_req_result", afb_hook_flag_req_subcall_req_result },
247 { "subcall_result", afb_hook_flag_req_subcall_result },
248 { "subcalls", afb_hook_flags_req_subcalls },
249 { "subcallsync", afb_hook_flag_req_subcallsync },
250 { "subcallsync_result", afb_hook_flag_req_subcallsync_result },
251 { "subscribe", afb_hook_flag_req_subscribe },
252 { "success", afb_hook_flag_req_success },
253 { "unref", afb_hook_flag_req_unref },
254 { "unstore", afb_hook_flag_req_unstore },
255 { "unsubscribe", afb_hook_flag_req_unsubscribe },
256 { "vverbose", afb_hook_flag_req_vverbose },
259 /* get the xreq value for flag of 'name' */
260 static int get_xreq_flag(const char *name)
262 return get_flag(name, xreq_flags, (int)(sizeof xreq_flags / sizeof *xreq_flags));
265 static void hook_xreq(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *action, const char *format, ...)
267 struct json_object *cred = NULL;
268 const char *session = NULL;
271 if (xreq->context.session)
272 session = afb_session_uuid(xreq->context.session);
275 wrap_json_pack(&cred, "{si ss si si ss* ss*}",
276 "uid", (int)xreq->cred->uid,
277 "user", xreq->cred->user,
278 "gid", (int)xreq->cred->gid,
279 "pid", (int)xreq->cred->pid,
280 "label", xreq->cred->label,
283 va_start(ap, format);
284 emit(closure, hookid, "request", "{si ss ss ss so* ss*}", format, ap,
285 "index", xreq->hookindex,
286 "api", xreq->request.api,
287 "verb", xreq->request.verb,
294 static void hook_xreq_begin(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
296 hook_xreq(closure, hookid, xreq, "begin", NULL);
299 static void hook_xreq_end(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
301 hook_xreq(closure, hookid, xreq, "end", NULL);
304 static void hook_xreq_json(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *obj)
306 hook_xreq(closure, hookid, xreq, "json", "{sO?}",
310 static void hook_xreq_get(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *name, struct afb_arg arg)
312 hook_xreq(closure, hookid, xreq, "get", "{ss? ss? ss? ss?}",
319 static void hook_xreq_success(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *obj, const char *info)
321 hook_xreq(closure, hookid, xreq, "success", "{sO? ss?}",
326 static void hook_xreq_fail(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *status, const char *info)
328 hook_xreq(closure, hookid, xreq, "fail", "{ss? ss?}",
333 static void hook_xreq_context_get(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, void *value)
335 hook_xreq(closure, hookid, xreq, "context_get", NULL);
338 static void hook_xreq_context_set(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, void *value, void (*free_value)(void*))
340 hook_xreq(closure, hookid, xreq, "context_set", NULL);
343 static void hook_xreq_addref(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
345 hook_xreq(closure, hookid, xreq, "addref", NULL);
348 static void hook_xreq_unref(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
350 hook_xreq(closure, hookid, xreq, "unref", NULL);
353 static void hook_xreq_session_close(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
355 hook_xreq(closure, hookid, xreq, "session_close", NULL);
358 static void hook_xreq_session_set_LOA(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, unsigned level, int result)
360 hook_xreq(closure, hookid, xreq, "session_set_LOA", "{si si}",
365 static void hook_xreq_subscribe(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_eventid *eventid, int result)
367 hook_xreq(closure, hookid, xreq, "subscribe", "{s{ss si} si}",
369 "name", afb_evt_eventid_fullname(eventid),
370 "id", afb_evt_eventid_id(eventid),
374 static void hook_xreq_unsubscribe(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_eventid *eventid, int result)
376 hook_xreq(closure, hookid, xreq, "unsubscribe", "{s{ss? si} si}",
378 "name", afb_evt_eventid_fullname(eventid),
379 "id", afb_evt_eventid_id(eventid),
383 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)
385 hook_xreq(closure, hookid, xreq, "subcall", "{ss? ss? sO?}",
391 static void hook_xreq_subcall_result(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int status, struct json_object *result)
393 hook_xreq(closure, hookid, xreq, "subcall_result", "{si sO?}",
398 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)
400 hook_xreq(closure, hookid, xreq, "subcallsync", "{ss? ss? sO?}",
406 static void hook_xreq_subcallsync_result(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int status, struct json_object *result)
408 hook_xreq(closure, hookid, xreq, "subcallsync_result", "{si sO?}",
413 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)
415 struct json_object *pos;
424 len = vasprintf(&msg, fmt, ap);
428 wrap_json_pack(&pos, "{ss si ss*}", "file", file, "line", line, "function", func);
430 hook_xreq(closure, hookid, xreq, "vverbose", "{si ss* ss? so*}",
432 "type", verbosity_level_name(level),
433 len < 0 ? "format" : "message", len < 0 ? fmt : msg,
439 static void hook_xreq_store(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_stored_req *sreq)
441 hook_xreq(closure, hookid, xreq, "store", NULL);
444 static void hook_xreq_unstore(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
446 hook_xreq(closure, hookid, xreq, "unstore", NULL);
449 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)
451 hook_xreq(closure, hookid, xreq, "subcall_req", "{ss? ss? sO?}",
457 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)
459 hook_xreq(closure, hookid, xreq, "subcall_req_result", "{si sO?}",
464 static void hook_xreq_has_permission(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *permission, int result)
466 hook_xreq(closure, hookid, xreq, "has_permission", "{ss sb}",
467 "permission", permission,
471 static void hook_xreq_get_application_id(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, char *result)
473 hook_xreq(closure, hookid, xreq, "get_application_id", "{ss?}",
477 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)
479 char pc[50], pf[50], pv[50], pr[50];
480 snprintf(pc, sizeof pc, "%p", create_value);
481 snprintf(pf, sizeof pf, "%p", free_value);
482 snprintf(pv, sizeof pv, "%p", create_closure);
483 snprintf(pr, sizeof pr, "%p", result);
484 hook_xreq(closure, hookid, xreq, "context_make", "{sb ss ss ss ss}",
492 static struct afb_hook_xreq_itf hook_xreq_itf = {
493 .hook_xreq_begin = hook_xreq_begin,
494 .hook_xreq_end = hook_xreq_end,
495 .hook_xreq_json = hook_xreq_json,
496 .hook_xreq_get = hook_xreq_get,
497 .hook_xreq_success = hook_xreq_success,
498 .hook_xreq_fail = hook_xreq_fail,
499 .hook_xreq_context_get = hook_xreq_context_get,
500 .hook_xreq_context_set = hook_xreq_context_set,
501 .hook_xreq_addref = hook_xreq_addref,
502 .hook_xreq_unref = hook_xreq_unref,
503 .hook_xreq_session_close = hook_xreq_session_close,
504 .hook_xreq_session_set_LOA = hook_xreq_session_set_LOA,
505 .hook_xreq_subscribe = hook_xreq_subscribe,
506 .hook_xreq_unsubscribe = hook_xreq_unsubscribe,
507 .hook_xreq_subcall = hook_xreq_subcall,
508 .hook_xreq_subcall_result = hook_xreq_subcall_result,
509 .hook_xreq_subcallsync = hook_xreq_subcallsync,
510 .hook_xreq_subcallsync_result = hook_xreq_subcallsync_result,
511 .hook_xreq_vverbose = hook_xreq_vverbose,
512 .hook_xreq_store = hook_xreq_store,
513 .hook_xreq_unstore = hook_xreq_unstore,
514 .hook_xreq_subcall_req = hook_xreq_subcall_req,
515 .hook_xreq_subcall_req_result = hook_xreq_subcall_req_result,
516 .hook_xreq_has_permission = hook_xreq_has_permission,
517 .hook_xreq_get_application_id = hook_xreq_get_application_id,
518 .hook_xreq_context_make = hook_xreq_context_make
521 /*******************************************************************************/
522 /***** trace the daemon interface *****/
523 /*******************************************************************************/
525 static struct flag ditf_flags[] = { /* must be sorted by names */
526 { "all", afb_hook_flags_ditf_all },
527 { "common", afb_hook_flags_ditf_common },
528 { "event_broadcast_after", afb_hook_flag_ditf_event_broadcast_after },
529 { "event_broadcast_before", afb_hook_flag_ditf_event_broadcast_before },
530 { "event_make", afb_hook_flag_ditf_event_make },
531 { "extra", afb_hook_flags_ditf_extra },
532 { "get_event_loop", afb_hook_flag_ditf_get_event_loop },
533 { "get_system_bus", afb_hook_flag_ditf_get_system_bus },
534 { "get_user_bus", afb_hook_flag_ditf_get_user_bus },
535 { "queue_job", afb_hook_flag_ditf_queue_job },
536 { "require_api", afb_hook_flag_ditf_require_api },
537 { "require_api_result", afb_hook_flag_ditf_require_api_result },
538 { "rootdir_get_fd", afb_hook_flag_ditf_rootdir_get_fd },
539 { "rootdir_open_locale", afb_hook_flag_ditf_rootdir_open_locale },
540 { "unstore_req", afb_hook_flag_ditf_unstore_req },
541 { "vverbose", afb_hook_flag_ditf_vverbose },
544 /* get the export value for flag of 'name' */
545 static int get_ditf_flag(const char *name)
547 return get_flag(name, ditf_flags, (int)(sizeof ditf_flags / sizeof *ditf_flags));
551 static void hook_ditf(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *action, const char *format, ...)
555 va_start(ap, format);
556 emit(closure, hookid, "daemon", "{ss ss}", format, ap,
557 "api", afb_export_apiname(export),
562 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)
564 hook_ditf(closure, hookid, export, "event_broadcast_before", "{ss sO*}",
565 "name", name, "data", object);
568 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)
570 hook_ditf(closure, hookid, export, "event_broadcast_after", "{ss sO* si}",
571 "name", name, "data", object, "result", result);
574 static void hook_ditf_get_event_loop(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_event *result)
576 hook_ditf(closure, hookid, export, "get_event_loop", NULL);
579 static void hook_ditf_get_user_bus(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_bus *result)
581 hook_ditf(closure, hookid, export, "get_user_bus", NULL);
584 static void hook_ditf_get_system_bus(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_bus *result)
586 hook_ditf(closure, hookid, export, "get_system_bus", NULL);
589 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)
591 struct json_object *pos;
600 len = vasprintf(&msg, fmt, ap);
604 wrap_json_pack(&pos, "{ss si ss*}", "file", file, "line", line, "function", function);
606 hook_ditf(closure, hookid, export, "vverbose", "{si ss* ss? so*}",
608 "type", verbosity_level_name(level),
609 len < 0 ? "format" : "message", len < 0 ? fmt : msg,
615 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)
617 hook_ditf(closure, hookid, export, "event_make", "{ss ss si}",
618 "name", name, "event", afb_evt_eventid_fullname(result), "id", afb_evt_eventid_id(result));
621 static void hook_ditf_rootdir_get_fd(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result)
626 sprintf(path, "/proc/self/fd/%d", result);
627 readlink(path, path, sizeof path);
630 hook_ditf(closure, hookid, export, "rootdir_get_fd", "{ss}",
631 result < 0 ? "path" : "error",
632 result < 0 ? strerror(errno) : path);
635 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)
640 sprintf(path, "/proc/self/fd/%d", result);
641 readlink(path, path, sizeof path);
644 hook_ditf(closure, hookid, export, "rootdir_open_locale", "{ss si ss* ss}",
648 result < 0 ? "path" : "error",
649 result < 0 ? strerror(errno) : path);
652 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)
654 hook_ditf(closure, hookid, export, "queue_job", "{ss}", "result", result);
657 static void hook_ditf_unstore_req(void * closure, const struct afb_hookid *hookid, const struct afb_export *export, struct afb_stored_req *sreq)
659 hook_ditf(closure, hookid, export, "unstore_req", NULL);
662 static void hook_ditf_require_api(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, int initialized)
664 hook_ditf(closure, hookid, export, "require_api", "{ss sb}", "name", name, "initialized", initialized);
667 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)
669 hook_ditf(closure, hookid, export, "require_api_result", "{ss sb si}", "name", name, "initialized", initialized, "result", result);
672 static struct afb_hook_ditf_itf hook_ditf_itf = {
673 .hook_ditf_event_broadcast_before = hook_ditf_event_broadcast_before,
674 .hook_ditf_event_broadcast_after = hook_ditf_event_broadcast_after,
675 .hook_ditf_get_event_loop = hook_ditf_get_event_loop,
676 .hook_ditf_get_user_bus = hook_ditf_get_user_bus,
677 .hook_ditf_get_system_bus = hook_ditf_get_system_bus,
678 .hook_ditf_vverbose = hook_ditf_vverbose,
679 .hook_ditf_event_make = hook_ditf_event_make,
680 .hook_ditf_rootdir_get_fd = hook_ditf_rootdir_get_fd,
681 .hook_ditf_rootdir_open_locale = hook_ditf_rootdir_open_locale,
682 .hook_ditf_queue_job = hook_ditf_queue_job,
683 .hook_ditf_unstore_req = hook_ditf_unstore_req,
684 .hook_ditf_require_api = hook_ditf_require_api,
685 .hook_ditf_require_api_result = hook_ditf_require_api_result
688 /*******************************************************************************/
689 /***** trace the services *****/
690 /*******************************************************************************/
692 static struct flag svc_flags[] = { /* must be sorted by names */
693 { "all", afb_hook_flags_svc_all },
694 { "call", afb_hook_flag_svc_call },
695 { "call_result", afb_hook_flag_svc_call_result },
696 { "callsync", afb_hook_flag_svc_callsync },
697 { "callsync_result", afb_hook_flag_svc_callsync_result },
698 { "on_event_after", afb_hook_flag_svc_on_event_after },
699 { "on_event_before", afb_hook_flag_svc_on_event_before },
700 { "start_after", afb_hook_flag_svc_start_after },
701 { "start_before", afb_hook_flag_svc_start_before },
704 /* get the export value for flag of 'name' */
705 static int get_svc_flag(const char *name)
707 return get_flag(name, svc_flags, (int)(sizeof svc_flags / sizeof *svc_flags));
710 static void hook_svc(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *action, const char *format, ...)
714 va_start(ap, format);
715 emit(closure, hookid, "service", "{ss ss}", format, ap,
716 "api", afb_export_apiname(export),
721 static void hook_svc_start_before(void *closure, const struct afb_hookid *hookid, const struct afb_export *export)
723 hook_svc(closure, hookid, export, "start_before", NULL);
726 static void hook_svc_start_after(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int status)
728 hook_svc(closure, hookid, export, "start_after", "{si}", "result", status);
731 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)
733 hook_svc(closure, hookid, export, "on_event_before", "{ss si sO*}",
734 "event", event, "id", evtid, "data", object);
737 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)
739 hook_svc(closure, hookid, export, "on_event_after", "{ss si sO*}",
740 "event", event, "id", evtid, "data", object);
743 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)
745 hook_svc(closure, hookid, export, "call", "{ss ss sO*}",
746 "api", api, "verb", verb, "args", args);
749 static void hook_svc_call_result(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int status, struct json_object *result)
751 hook_svc(closure, hookid, export, "call_result", "{si sO*}",
752 "status", status, "result", result);
755 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)
757 hook_svc(closure, hookid, export, "callsync", "{ss ss sO*}",
758 "api", api, "verb", verb, "args", args);
761 static void hook_svc_callsync_result(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int status, struct json_object *result)
763 hook_svc(closure, hookid, export, "callsync_result", "{si sO*}",
764 "status", status, "result", result);
767 static struct afb_hook_svc_itf hook_svc_itf = {
768 .hook_svc_start_before = hook_svc_start_before,
769 .hook_svc_start_after = hook_svc_start_after,
770 .hook_svc_on_event_before = hook_svc_on_event_before,
771 .hook_svc_on_event_after = hook_svc_on_event_after,
772 .hook_svc_call = hook_svc_call,
773 .hook_svc_call_result = hook_svc_call_result,
774 .hook_svc_callsync = hook_svc_callsync,
775 .hook_svc_callsync_result = hook_svc_callsync_result
778 /*******************************************************************************/
779 /***** trace the events *****/
780 /*******************************************************************************/
782 static struct flag evt_flags[] = { /* must be sorted by names */
783 { "addref", afb_hook_flag_evt_addref },
784 { "all", afb_hook_flags_evt_all },
785 { "broadcast_after", afb_hook_flag_evt_broadcast_after },
786 { "broadcast_before", afb_hook_flag_evt_broadcast_before },
787 { "common", afb_hook_flags_evt_common },
788 { "create", afb_hook_flag_evt_create },
789 { "extra", afb_hook_flags_evt_extra },
790 { "name", afb_hook_flag_evt_name },
791 { "push_after", afb_hook_flag_evt_push_after },
792 { "push_before", afb_hook_flag_evt_push_before },
793 { "unref", afb_hook_flag_evt_unref },
796 /* get the evt value for flag of 'name' */
797 static int get_evt_flag(const char *name)
799 return get_flag(name, evt_flags, (int)(sizeof evt_flags / sizeof *evt_flags));
802 static void hook_evt(void *closure, const struct afb_hookid *hookid, const char *evt, int id, const char *action, const char *format, ...)
806 va_start(ap, format);
807 emit(closure, hookid, "event", "{si ss ss}", format, ap,
814 static void hook_evt_create(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
816 hook_evt(closure, hookid, evt, id, "create", NULL);
819 static void hook_evt_push_before(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj)
821 hook_evt(closure, hookid, evt, id, "push_before", "{sO*}", "data", obj);
825 static void hook_evt_push_after(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj, int result)
827 hook_evt(closure, hookid, evt, id, "push_after", "{sO* si}", "data", obj, "result", result);
830 static void hook_evt_broadcast_before(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj)
832 hook_evt(closure, hookid, evt, id, "broadcast_before", "{sO*}", "data", obj);
835 static void hook_evt_broadcast_after(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj, int result)
837 hook_evt(closure, hookid, evt, id, "broadcast_after", "{sO* si}", "data", obj, "result", result);
840 static void hook_evt_name(void *closure, const struct afb_hookid *hookid, const char *evt, int id, const char *result)
842 hook_evt(closure, hookid, evt, id, "name", "{ss}", "result", result);
845 static void hook_evt_addref(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
847 hook_evt(closure, hookid, evt, id, "addref", NULL);
850 static void hook_evt_unref(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
852 hook_evt(closure, hookid, evt, id, "unref", NULL);
855 static struct afb_hook_evt_itf hook_evt_itf = {
856 .hook_evt_create = hook_evt_create,
857 .hook_evt_push_before = hook_evt_push_before,
858 .hook_evt_push_after = hook_evt_push_after,
859 .hook_evt_broadcast_before = hook_evt_broadcast_before,
860 .hook_evt_broadcast_after = hook_evt_broadcast_after,
861 .hook_evt_name = hook_evt_name,
862 .hook_evt_addref = hook_evt_addref,
863 .hook_evt_unref = hook_evt_unref
866 /*******************************************************************************/
867 /***** trace the globals *****/
868 /*******************************************************************************/
870 static struct flag global_flags[] = { /* must be sorted by names */
871 { "all", afb_hook_flags_global_all },
872 { "vverbose", afb_hook_flag_global_vverbose },
875 /* get the global value for flag of 'name' */
876 static int get_global_flag(const char *name)
878 return get_flag(name, global_flags, (int)(sizeof global_flags / sizeof *global_flags));
881 static void hook_global(void *closure, const struct afb_hookid *hookid, const char *action, const char *format, ...)
885 va_start(ap, format);
886 emit(closure, hookid, "global", "{ss}", format, ap, "action", action);
890 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)
892 struct json_object *pos;
901 len = vasprintf(&msg, fmt, ap);
905 wrap_json_pack(&pos, "{ss si ss*}", "file", file, "line", line, "function", function);
907 hook_global(closure, hookid, "vverbose", "{si ss* ss? so*}",
909 "type", verbosity_level_name(level),
910 len < 0 ? "format" : "message", len < 0 ? fmt : msg,
916 static struct afb_hook_global_itf hook_global_itf = {
917 .hook_global_vverbose = hook_global_vverbose,
920 /*******************************************************************************/
921 /***** abstract types *****/
922 /*******************************************************************************/
928 void (*unref)(void*);
929 int (*get_flag)(const char*);
931 abstracting[Trace_Type_Count] =
936 .unref = (void(*)(void*))afb_hook_unref_xreq,
937 .get_flag = get_xreq_flag
942 .unref = (void(*)(void*))afb_hook_unref_ditf,
943 .get_flag = get_ditf_flag
948 .unref = (void(*)(void*))afb_hook_unref_svc,
949 .get_flag = get_svc_flag
954 .unref = (void(*)(void*))afb_hook_unref_evt,
955 .get_flag = get_evt_flag
957 [Trace_Type_Global] =
960 .unref = (void(*)(void*))afb_hook_unref_global,
961 .get_flag = get_global_flag
965 /*******************************************************************************/
966 /***** handle trace data *****/
967 /*******************************************************************************/
969 /* drop hooks of 'trace' matching 'tag' and 'event' and 'session' */
970 static void trace_unhook(struct afb_trace *trace, struct tag *tag, struct event *event, struct session *session)
973 struct hook *hook, **prev;
975 /* remove any event */
976 for (i = 0 ; i < Trace_Type_Count ; i++) {
977 prev = &trace->hooks[i];
978 while ((hook = *prev)) {
979 if ((tag && tag != hook->tag)
980 || (event && event != hook->event)
981 || (session && session != hook->session))
985 abstracting[i].unref(hook->handler);
992 /* cleanup: removes unused tags, events and sessions of the 'trace' */
993 static void trace_cleanup(struct afb_trace *trace)
997 struct tag *tag, **ptag;
998 struct event *event, **pevent;
999 struct session *session, **psession;
1001 /* clean sessions */
1002 psession = &trace->sessions;
1003 while ((session = *psession)) {
1004 /* search for session */
1005 for (hook = NULL, i = 0 ; !hook && i < Trace_Type_Count ; i++)
1006 for (hook = trace->hooks[i] ; hook && hook->session != session ; hook = hook->next);
1007 /* keep or free whether used or not */
1009 psession = &session->next;
1011 *psession = session->next;
1012 if (__atomic_exchange_n(&session->trace, NULL, __ATOMIC_RELAXED))
1013 afb_session_set_cookie(session->session, session, NULL, NULL);
1018 ptag = &trace->tags;
1019 while ((tag = *ptag)) {
1020 /* search for tag */
1021 for (hook = NULL, i = 0 ; !hook && i < Trace_Type_Count ; i++)
1022 for (hook = trace->hooks[i] ; hook && hook->tag != tag ; hook = hook->next);
1023 /* keep or free whether used or not */
1032 pevent = &trace->events;
1033 while ((event = *pevent)) {
1034 /* search for event */
1035 for (hook = NULL, i = 0 ; !hook && i < Trace_Type_Count ; i++)
1036 for (hook = trace->hooks[i] ; hook && hook->event != event ; hook = hook->next);
1037 /* keep or free whether used or not */
1039 pevent = &event->next;
1041 *pevent = event->next;
1042 afb_evt_evtid_unref(event->evtid);
1048 /* callback at end of traced session */
1049 static void free_session_cookie(void *cookie)
1051 struct session *session = cookie;
1052 struct afb_trace *trace = __atomic_exchange_n(&session->trace, NULL, __ATOMIC_RELAXED);
1054 pthread_mutex_lock(&trace->mutex);
1055 trace_unhook(trace, NULL, NULL, session);
1056 trace_cleanup(trace);
1057 pthread_mutex_unlock(&trace->mutex);
1062 * Get the tag of 'name' within 'trace'.
1063 * If 'alloc' isn't zero, create the tag and add it.
1065 static struct tag *trace_get_tag(struct afb_trace *trace, const char *name, int alloc)
1069 /* search the tag of 'name' */
1071 while (tag && strcmp(name, tag->tag))
1074 if (!tag && alloc) {
1075 /* creation if needed */
1076 tag = malloc(sizeof * tag + strlen(name));
1078 strcpy(tag->tag, name);
1079 tag->next = trace->tags;
1087 * Get the event of 'name' within 'trace'.
1088 * If 'alloc' isn't zero, create the event and add it.
1090 static struct event *trace_get_event(struct afb_trace *trace, const char *name, int alloc)
1093 struct event *event;
1095 /* search the event */
1096 event = trace->events;
1097 while (event && strcmp(afb_evt_evtid_name(event->evtid), name))
1098 event = event->next;
1100 if (!event && alloc) {
1101 event = malloc(sizeof * event);
1103 e = afb_daemon_make_event_v1(*trace->daemon, name);
1104 event->evtid = afb_evt_eventid_to_evtid(afb_event_to_eventid(e));
1106 event->next = trace->events;
1107 trace->events = event;
1118 * Get the session of 'value' within 'trace'.
1119 * If 'alloc' isn't zero, create the session and add it.
1121 static struct session *trace_get_session(struct afb_trace *trace, struct afb_session *value, int alloc)
1123 struct session *session;
1125 /* search the session */
1126 session = trace->sessions;
1127 while (session && session->session != value)
1128 session = session->next;
1130 if (!session && alloc) {
1131 session = malloc(sizeof * session);
1133 session->session = value;
1134 session->trace = NULL;
1135 session->next = trace->sessions;
1136 trace->sessions = session;
1143 * Get the session of 'uuid' within 'trace'.
1144 * If 'alloc' isn't zero, create the session and add it.
1146 static struct session *trace_get_session_by_uuid(struct afb_trace *trace, const char *uuid, int alloc)
1148 struct afb_session *session;
1151 session = afb_session_get(uuid, alloc ? &created : NULL);
1152 return session ? trace_get_session(trace, session, alloc) : NULL;
1155 static struct hook *trace_make_detached_hook(struct afb_trace *trace, const char *event, const char *tag)
1159 tag = tag ?: DEFAULT_TAG_NAME;
1160 event = event ?: DEFAULT_EVENT_NAME;
1161 hook = malloc(sizeof *hook);
1163 hook->tag = trace_get_tag(trace, tag, 1);
1164 hook->event = trace_get_event(trace, event, 1);
1165 hook->session = NULL;
1166 hook->handler = NULL;
1171 static void trace_attach_hook(struct afb_trace *trace, struct hook *hook, enum trace_type type)
1173 struct session *session = hook->session;
1174 hook->next = trace->hooks[type];
1175 trace->hooks[type] = hook;
1176 if (session && !session->trace) {
1177 session->trace = trace;
1178 afb_session_set_cookie(session->session, session, session, free_session_cookie);
1182 /*******************************************************************************/
1183 /***** handle client requests *****/
1184 /*******************************************************************************/
1188 struct afb_trace *trace;
1195 struct context *context;
1198 const char *session;
1201 const char *pattern;
1202 int flags[Trace_Type_Count];
1206 static void addhook(struct desc *desc, enum trace_type type)
1209 struct session *session;
1210 struct afb_session *bind;
1211 struct afb_trace *trace = desc->context->trace;
1213 /* check permission for bound traces */
1214 bind = trace->bound;
1216 if (type != Trace_Type_Xreq) {
1217 ctxt_error(&desc->context->errors, "tracing %s is forbidden", abstracting[type].name);
1220 if (desc->session) {
1221 ctxt_error(&desc->context->errors, "setting session is forbidden");
1226 /* allocate the hook */
1227 hook = trace_make_detached_hook(trace, desc->name, desc->tag);
1229 ctxt_error(&desc->context->errors, "allocation of hook failed");
1233 /* create the hook handler */
1235 case Trace_Type_Xreq:
1236 if (desc->session) {
1237 session = trace_get_session_by_uuid(trace, desc->session, 1);
1239 ctxt_error(&desc->context->errors, "allocation of session failed");
1243 bind = session->session;
1245 hook->handler = afb_hook_create_xreq(desc->api, desc->verb, bind,
1246 desc->flags[type], &hook_xreq_itf, hook);
1248 case Trace_Type_Ditf:
1249 hook->handler = afb_hook_create_ditf(desc->api, desc->flags[type], &hook_ditf_itf, hook);
1251 case Trace_Type_Svc:
1252 hook->handler = afb_hook_create_svc(desc->api, desc->flags[type], &hook_svc_itf, hook);
1254 case Trace_Type_Evt:
1255 hook->handler = afb_hook_create_evt(desc->pattern, desc->flags[type], &hook_evt_itf, hook);
1257 case Trace_Type_Global:
1258 hook->handler = afb_hook_create_global(desc->flags[type], &hook_global_itf, hook);
1263 if (!hook->handler) {
1264 ctxt_error(&desc->context->errors, "creation of hook failed");
1269 /* attach and activate the hook */
1270 afb_req_subscribe(desc->context->req, afb_evt_event_from_evtid(hook->event->evtid));
1271 trace_attach_hook(trace, hook, type);
1274 static void addhooks(struct desc *desc)
1278 for (i = 0 ; i < Trace_Type_Count ; i++) {
1284 static void add_flags(void *closure, struct json_object *object, enum trace_type type)
1287 const char *name, *queried;
1288 struct desc *desc = closure;
1290 if (wrap_json_unpack(object, "s", &name))
1291 ctxt_error(&desc->context->errors, "unexpected %s value %s",
1292 abstracting[type].name,
1293 json_object_to_json_string(object));
1295 queried = (name[0] == '*' && !name[1]) ? "all" : name;
1296 value = abstracting[type].get_flag(queried);
1298 desc->flags[type] |= value;
1300 ctxt_error(&desc->context->errors, "unknown %s name %s",
1301 abstracting[type].name, name);
1305 static void add_xreq_flags(void *closure, struct json_object *object)
1307 add_flags(closure, object, Trace_Type_Xreq);
1310 static void add_ditf_flags(void *closure, struct json_object *object)
1312 add_flags(closure, object, Trace_Type_Ditf);
1315 static void add_svc_flags(void *closure, struct json_object *object)
1317 add_flags(closure, object, Trace_Type_Svc);
1320 static void add_evt_flags(void *closure, struct json_object *object)
1322 add_flags(closure, object, Trace_Type_Evt);
1325 static void add_global_flags(void *closure, struct json_object *object)
1327 add_flags(closure, object, Trace_Type_Global);
1331 static void add(void *closure, struct json_object *object)
1335 struct json_object *request, *event, *daemon, *service, *sub, *global;
1337 memcpy (&desc, closure, sizeof desc);
1338 request = event = daemon = service = sub = global = NULL;
1340 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}",
1345 "session", &desc.session,
1346 "pattern", &desc.pattern,
1347 "request", &request,
1349 "service", &service,
1356 if (desc.api && desc.api[0] == '*' && !desc.api[1])
1359 if (desc.verb && desc.verb[0] == '*' && !desc.verb[1])
1362 if (desc.session && desc.session[0] == '*' && !desc.session[1])
1363 desc.session = NULL;
1365 /* get what is expected */
1367 wrap_json_optarray_for_all(request, add_xreq_flags, &desc);
1370 wrap_json_optarray_for_all(daemon, add_ditf_flags, &desc);
1373 wrap_json_optarray_for_all(service, add_svc_flags, &desc);
1376 wrap_json_optarray_for_all(event, add_evt_flags, &desc);
1379 wrap_json_optarray_for_all(global, add_global_flags, &desc);
1383 wrap_json_optarray_for_all(sub, add, &desc);
1388 wrap_json_optarray_for_all(object, add_xreq_flags, &desc);
1393 /* drop hooks of given tag */
1394 static void drop_tag(void *closure, struct json_object *object)
1397 struct context *context = closure;
1401 rc = wrap_json_unpack(object, "s", &name);
1403 ctxt_error(&context->errors, "unexpected tag value %s", json_object_to_json_string(object));
1405 tag = trace_get_tag(context->trace, name, 0);
1407 ctxt_error(&context->errors, "tag %s not found", name);
1409 trace_unhook(context->trace, tag, NULL, NULL);
1413 /* drop hooks of given event */
1414 static void drop_event(void *closure, struct json_object *object)
1417 struct context *context = closure;
1418 struct event *event;
1421 rc = wrap_json_unpack(object, "s", &name);
1423 ctxt_error(&context->errors, "unexpected event value %s", json_object_to_json_string(object));
1425 event = trace_get_event(context->trace, name, 0);
1427 ctxt_error(&context->errors, "event %s not found", name);
1429 trace_unhook(context->trace, NULL, event, NULL);
1433 /* drop hooks of given session */
1434 static void drop_session(void *closure, struct json_object *object)
1437 struct context *context = closure;
1438 struct session *session;
1441 rc = wrap_json_unpack(object, "s", &uuid);
1443 ctxt_error(&context->errors, "unexpected session value %s", json_object_to_json_string(object));
1445 session = trace_get_session_by_uuid(context->trace, uuid, 0);
1447 ctxt_error(&context->errors, "session %s not found", uuid);
1449 trace_unhook(context->trace, NULL, NULL, session);
1453 /*******************************************************************************/
1454 /***** public interface *****/
1455 /*******************************************************************************/
1457 /* allocates an afb_trace instance */
1458 struct afb_trace *afb_trace_create(struct afb_daemon *daemon, struct afb_session *bound)
1460 struct afb_trace *trace;
1464 trace = calloc(1, sizeof *trace);
1466 trace->refcount = 1;
1467 trace->bound = bound;
1468 trace->daemon = daemon;
1469 pthread_mutex_init(&trace->mutex, NULL);
1474 /* add a reference to the trace */
1475 void afb_trace_addref(struct afb_trace *trace)
1477 __atomic_add_fetch(&trace->refcount, 1, __ATOMIC_RELAXED);
1480 /* drop one reference to the trace */
1481 void afb_trace_unref(struct afb_trace *trace)
1483 if (trace && !__atomic_sub_fetch(&trace->refcount, 1, __ATOMIC_RELAXED)) {
1485 trace_unhook(trace, NULL, NULL, NULL);
1486 trace_cleanup(trace);
1487 pthread_mutex_destroy(&trace->mutex);
1493 int afb_trace_add(struct afb_req req, struct json_object *args, struct afb_trace *trace)
1495 struct context context;
1498 memset(&context, 0, sizeof context);
1499 context.trace = trace;
1502 memset(&desc, 0, sizeof desc);
1503 desc.context = &context;
1505 pthread_mutex_lock(&trace->mutex);
1506 wrap_json_optarray_for_all(args, add, &desc);
1507 pthread_mutex_unlock(&trace->mutex);
1509 if (!context.errors)
1512 afb_req_fail(req, "error-detected", context.errors);
1513 free(context.errors);
1518 extern int afb_trace_drop(struct afb_req req, struct json_object *args, struct afb_trace *trace)
1521 struct context context;
1522 struct json_object *tags, *events, *sessions;
1524 memset(&context, 0, sizeof context);
1525 context.trace = trace;
1528 /* special: boolean value */
1529 if (!wrap_json_unpack(args, "b", &rc)) {
1531 pthread_mutex_lock(&trace->mutex);
1532 trace_unhook(trace, NULL, NULL, NULL);
1533 trace_cleanup(trace);
1534 pthread_mutex_unlock(&trace->mutex);
1539 tags = events = sessions = NULL;
1540 rc = wrap_json_unpack(args, "{s?o s?o s?o}",
1543 "session", &sessions);
1545 if (rc < 0 || !(events || tags || sessions)) {
1546 afb_req_fail(req, "error-detected", "bad drop arguments");
1550 pthread_mutex_lock(&trace->mutex);
1553 wrap_json_optarray_for_all(tags, drop_tag, &context);
1556 wrap_json_optarray_for_all(events, drop_event, &context);
1559 wrap_json_optarray_for_all(sessions, drop_session, &context);
1561 trace_cleanup(trace);
1563 pthread_mutex_unlock(&trace->mutex);
1565 if (!context.errors)
1568 afb_req_fail(req, "error-detected", context.errors);
1569 free(context.errors);