2 * Copyright (C) 2016, 2017, 2018 "IoT.bzh"
3 * Author José Bollo <jose.bollo@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
29 #include <json-c/json.h>
31 #define AFB_BINDING_VERSION 3
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_Api, /* api hooks */
100 Trace_Type_Evt, /* evt hooks */
101 Trace_Type_Session, /* session hooks */
102 Trace_Type_Global, /* global hooks */
103 #if !defined(REMOVE_LEGACY_TRACE)
104 Trace_Legacy_Type_Ditf, /* export hooks */
105 Trace_Legacy_Type_Svc, /* export hooks */
107 Trace_Type_Count, /* count of types of hooks */
113 int refcount; /* reference count */
114 pthread_mutex_t mutex; /* concurrency management */
115 const char *apiname; /* api name for events */
116 struct afb_session *bound; /* bound to session */
117 struct event *events; /* list of events */
118 struct tag *tags; /* list of tags */
119 struct hook *hooks[Trace_Type_Count]; /* hooks */
122 /*******************************************************************************/
123 /***** utility functions *****/
124 /*******************************************************************************/
126 static void ctxt_error(char **errors, const char *format, ...)
134 va_start(ap, format);
135 len = vsnprintf(buffer, sizeof buffer, format, ap);
137 if (len > (int)(sizeof buffer - 2))
138 len = (int)(sizeof buffer - 2);
139 buffer[len++] = '\n';
143 sz = errs ? strlen(errs) : 0;
144 errs = realloc(errs, sz + (size_t)len);
146 memcpy(errs + sz, buffer, len);
151 /* get the value of the flag of 'name' in the array 'flags' of 'count elements */
152 static int get_flag(const char *name, struct flag flags[], int count)
154 /* dichotomic search */
155 int lower = 0, upper = count;
156 while (lower < upper) {
157 int mid = (lower + upper) >> 1;
158 int cmp = strcmp(name, flags[mid].name);
160 return flags[mid].value;
170 static struct json_object *timestamp(const struct afb_hookid *hookid)
174 snprintf(ts, sizeof ts, "%llu.%06lu",
175 (long long unsigned)hookid->time.tv_sec,
176 (long unsigned)((hookid->time.tv_nsec + 500) / 1000));
178 return json_object_new_double_s(0.0f, ts); /* the real value isn't used */
180 return json_object_new_string(ts);
181 return json_object_new_double_s(0f, ts); /* the real value isn't used */
185 /* verbosity level name or NULL */
186 static const char *verbosity_level_name(int level)
188 static const char *names[] = {
196 return level >= Log_Level_Error && level <= Log_Level_Debug ? names[level - Log_Level_Error] : NULL;
200 static void emit(void *closure, const struct afb_hookid *hookid, const char *type, const char *fmt1, const char *fmt2, va_list ap2, ...)
202 struct hook *hook = closure;
203 struct json_object *data, *data1, *data2;
206 data1 = data2 = data = NULL;
208 wrap_json_vpack(&data1, fmt1, ap1);
211 wrap_json_vpack(&data2, fmt2, ap2);
213 wrap_json_pack(&data, "{so ss ss si so so*}",
214 "time", timestamp(hookid),
215 "tag", hook->tag->tag,
217 "id", (int)(hookid->id & INT_MAX),
221 afb_evt_evtid_push(hook->event->evtid, data);
224 /*******************************************************************************/
225 /***** trace the requests *****/
226 /*******************************************************************************/
228 static struct flag xreq_flags[] = { /* must be sorted by names */
229 { "addref", afb_hook_flag_req_addref },
230 { "all", afb_hook_flags_req_all },
231 { "args", afb_hook_flags_req_args },
232 { "begin", afb_hook_flag_req_begin },
233 { "common", afb_hook_flags_req_common },
234 { "context", afb_hook_flags_req_context },
235 { "context_get", afb_hook_flag_req_legacy_context_get },
236 { "context_make", afb_hook_flag_req_context_make },
237 { "context_set", afb_hook_flag_req_legacy_context_set },
238 { "end", afb_hook_flag_req_end },
239 { "event", afb_hook_flags_req_event },
240 { "extra", afb_hook_flags_req_extra },
241 { "get", afb_hook_flag_req_get },
242 { "get_application_id", afb_hook_flag_req_get_application_id },
243 { "get_client_info", afb_hook_flag_req_get_client_info },
244 { "get_uid", afb_hook_flag_req_get_uid },
245 { "has_permission", afb_hook_flag_req_has_permission },
246 { "json", afb_hook_flag_req_json },
247 { "life", afb_hook_flags_req_life },
248 { "ref", afb_hook_flags_req_ref },
249 { "reply", afb_hook_flag_req_reply },
250 { "security", afb_hook_flags_req_security },
251 { "session", afb_hook_flags_req_session },
252 { "session_close", afb_hook_flag_req_session_close },
253 { "session_set_LOA", afb_hook_flag_req_session_set_LOA },
254 { "store", afb_hook_flag_req_legacy_store },
255 { "stores", afb_hook_flags_req_stores },
256 { "subcall", afb_hook_flag_req_subcall },
257 { "subcall_result", afb_hook_flag_req_subcall_result },
258 { "subcalls", afb_hook_flags_req_subcalls },
259 { "subcallsync", afb_hook_flag_req_subcallsync },
260 { "subcallsync_result", afb_hook_flag_req_subcallsync_result },
261 { "subscribe", afb_hook_flag_req_subscribe },
262 { "unref", afb_hook_flag_req_unref },
263 { "unstore", afb_hook_flag_req_legacy_unstore },
264 { "unsubscribe", afb_hook_flag_req_unsubscribe },
265 { "vverbose", afb_hook_flag_req_vverbose },
268 /* get the xreq value for flag of 'name' */
269 static int get_xreq_flag(const char *name)
271 return get_flag(name, xreq_flags, (int)(sizeof xreq_flags / sizeof *xreq_flags));
274 static void hook_xreq(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *action, const char *format, ...)
276 struct json_object *cred = NULL;
277 const char *session = NULL;
280 if (xreq->context.session)
281 session = afb_session_uuid(xreq->context.session);
284 wrap_json_pack(&cred, "{si ss si si ss* ss*}",
285 "uid", (int)xreq->cred->uid,
286 "user", xreq->cred->user,
287 "gid", (int)xreq->cred->gid,
288 "pid", (int)xreq->cred->pid,
289 "label", xreq->cred->label,
292 va_start(ap, format);
293 emit(closure, hookid, "request", "{si ss ss ss so* ss*}", format, ap,
294 "index", xreq->hookindex,
295 "api", xreq->request.called_api,
296 "verb", xreq->request.called_verb,
303 static void hook_xreq_begin(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
305 hook_xreq(closure, hookid, xreq, "begin", "{sO?}",
306 "json", afb_xreq_unhooked_json((struct afb_xreq*)xreq));
309 static void hook_xreq_end(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
311 hook_xreq(closure, hookid, xreq, "end", NULL);
314 static void hook_xreq_json(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *obj)
316 hook_xreq(closure, hookid, xreq, "json", "{sO?}",
320 static void hook_xreq_get(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *name, struct afb_arg arg)
322 hook_xreq(closure, hookid, xreq, "get", "{ss? ss? ss? ss?}",
329 static void hook_xreq_reply(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *obj, const char *error, const char *info)
331 hook_xreq(closure, hookid, xreq, "reply", "{sO? ss? ss?}",
337 static void hook_xreq_context_get(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, void *value)
339 hook_xreq(closure, hookid, xreq, "context_get", NULL);
342 static void hook_xreq_context_set(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, void *value, void (*free_value)(void*))
344 hook_xreq(closure, hookid, xreq, "context_set", NULL);
347 static void hook_xreq_addref(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
349 hook_xreq(closure, hookid, xreq, "addref", NULL);
352 static void hook_xreq_unref(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
354 hook_xreq(closure, hookid, xreq, "unref", NULL);
357 static void hook_xreq_session_close(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
359 hook_xreq(closure, hookid, xreq, "session_close", NULL);
362 static void hook_xreq_session_set_LOA(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, unsigned level, int result)
364 hook_xreq(closure, hookid, xreq, "session_set_LOA", "{si si}",
369 static void hook_xreq_subscribe(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_event_x2 *event, int result)
371 hook_xreq(closure, hookid, xreq, "subscribe", "{s{ss si} si}",
373 "name", afb_evt_event_x2_fullname(event),
374 "id", afb_evt_event_x2_id(event),
378 static void hook_xreq_unsubscribe(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_event_x2 *event, int result)
380 hook_xreq(closure, hookid, xreq, "unsubscribe", "{s{ss? si} si}",
382 "name", afb_evt_event_x2_fullname(event),
383 "id", afb_evt_event_x2_id(event),
387 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)
389 hook_xreq(closure, hookid, xreq, "subcall", "{ss? ss? sO?}",
395 static void hook_xreq_subcall_result(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *object, const char *error, const char *info)
397 hook_xreq(closure, hookid, xreq, "subcall_result", "{sO? ss? ss?}",
403 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)
405 hook_xreq(closure, hookid, xreq, "subcallsync", "{ss? ss? sO?}",
411 static void hook_xreq_subcallsync_result(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int status, struct json_object *object, const char *error, const char *info)
413 hook_xreq(closure, hookid, xreq, "subcallsync_result", "{si sO? ss? ss?}",
420 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)
422 struct json_object *pos;
431 len = vasprintf(&msg, fmt, ap);
435 wrap_json_pack(&pos, "{ss si ss*}", "file", file, "line", line, "function", func);
437 hook_xreq(closure, hookid, xreq, "vverbose", "{si ss* ss? so*}",
439 "type", verbosity_level_name(level),
440 len < 0 ? "format" : "message", len < 0 ? fmt : msg,
446 static void hook_xreq_store(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_stored_req *sreq)
448 hook_xreq(closure, hookid, xreq, "store", NULL);
451 static void hook_xreq_unstore(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
453 hook_xreq(closure, hookid, xreq, "unstore", NULL);
456 static void hook_xreq_has_permission(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *permission, int result)
458 hook_xreq(closure, hookid, xreq, "has_permission", "{ss sb}",
459 "permission", permission,
463 static void hook_xreq_get_application_id(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, char *result)
465 hook_xreq(closure, hookid, xreq, "get_application_id", "{ss?}",
469 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)
471 char pc[50], pf[50], pv[50], pr[50];
472 snprintf(pc, sizeof pc, "%p", create_value);
473 snprintf(pf, sizeof pf, "%p", free_value);
474 snprintf(pv, sizeof pv, "%p", create_closure);
475 snprintf(pr, sizeof pr, "%p", result);
476 hook_xreq(closure, hookid, xreq, "context_make", "{sb ss ss ss ss}",
484 static void hook_xreq_get_uid(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int result)
486 hook_xreq(closure, hookid, xreq, "get_uid", "{si}",
490 static void hook_xreq_get_client_info(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *result)
492 hook_xreq(closure, hookid, xreq, "get_client_info", "{sO}",
496 static struct afb_hook_xreq_itf hook_xreq_itf = {
497 .hook_xreq_begin = hook_xreq_begin,
498 .hook_xreq_end = hook_xreq_end,
499 .hook_xreq_json = hook_xreq_json,
500 .hook_xreq_get = hook_xreq_get,
501 .hook_xreq_reply = hook_xreq_reply,
502 .hook_xreq_legacy_context_get = hook_xreq_context_get,
503 .hook_xreq_legacy_context_set = hook_xreq_context_set,
504 .hook_xreq_addref = hook_xreq_addref,
505 .hook_xreq_unref = hook_xreq_unref,
506 .hook_xreq_session_close = hook_xreq_session_close,
507 .hook_xreq_session_set_LOA = hook_xreq_session_set_LOA,
508 .hook_xreq_subscribe = hook_xreq_subscribe,
509 .hook_xreq_unsubscribe = hook_xreq_unsubscribe,
510 .hook_xreq_subcall = hook_xreq_subcall,
511 .hook_xreq_subcall_result = hook_xreq_subcall_result,
512 .hook_xreq_subcallsync = hook_xreq_subcallsync,
513 .hook_xreq_subcallsync_result = hook_xreq_subcallsync_result,
514 .hook_xreq_vverbose = hook_xreq_vverbose,
515 .hook_xreq_legacy_store = hook_xreq_store,
516 .hook_xreq_legacy_unstore = hook_xreq_unstore,
517 .hook_xreq_has_permission = hook_xreq_has_permission,
518 .hook_xreq_get_application_id = hook_xreq_get_application_id,
519 .hook_xreq_context_make = hook_xreq_context_make,
520 .hook_xreq_get_uid = hook_xreq_get_uid,
521 .hook_xreq_get_client_info = hook_xreq_get_client_info,
524 /*******************************************************************************/
525 /***** trace the api interface *****/
526 /*******************************************************************************/
528 #if !defined(REMOVE_LEGACY_TRACE)
529 static struct flag legacy_ditf_flags[] = { /* must be sorted by names */
530 { "all", afb_hook_flags_api_ditf_all },
531 { "common", afb_hook_flags_api_ditf_common },
532 { "event_broadcast_after", afb_hook_flag_api_event_broadcast },
533 { "event_broadcast_before", afb_hook_flag_api_event_broadcast },
534 { "event_make", afb_hook_flag_api_event_make },
535 { "extra", afb_hook_flags_api_ditf_extra },
536 { "get_event_loop", afb_hook_flag_api_get_event_loop },
537 { "get_system_bus", afb_hook_flag_api_get_system_bus },
538 { "get_user_bus", afb_hook_flag_api_get_user_bus },
539 { "queue_job", afb_hook_flag_api_queue_job },
540 { "require_api", afb_hook_flag_api_require_api },
541 { "require_api_result", afb_hook_flag_api_require_api },
542 { "rootdir_get_fd", afb_hook_flag_api_rootdir_get_fd },
543 { "rootdir_open_locale", afb_hook_flag_api_rootdir_open_locale },
544 { "unstore_req", afb_hook_flag_api_legacy_unstore_req },
545 { "vverbose", afb_hook_flag_api_vverbose },
548 static struct flag legacy_svc_flags[] = { /* must be sorted by names */
549 { "all", afb_hook_flags_api_svc_all },
550 { "call", afb_hook_flag_api_call },
551 { "call_result", afb_hook_flag_api_call },
552 { "callsync", afb_hook_flag_api_callsync },
553 { "callsync_result", afb_hook_flag_api_callsync },
554 { "on_event_after", afb_hook_flag_api_on_event },
555 { "on_event_before", afb_hook_flag_api_on_event },
556 { "start_after", afb_hook_flag_api_start },
557 { "start_before", afb_hook_flag_api_start },
560 /* get the export value for flag of 'name' */
561 static int get_legacy_ditf_flag(const char *name)
563 return get_flag(name, legacy_ditf_flags, (int)(sizeof legacy_ditf_flags / sizeof *legacy_ditf_flags));
566 /* get the export value for flag of 'name' */
567 static int get_legacy_svc_flag(const char *name)
569 return get_flag(name, legacy_svc_flags, (int)(sizeof legacy_svc_flags / sizeof *legacy_svc_flags));
573 static struct flag api_flags[] = { /* must be sorted by names */
574 { "add_alias", afb_hook_flag_api_add_alias },
575 { "all", afb_hook_flags_api_all },
576 { "api_add_verb", afb_hook_flag_api_api_add_verb },
577 { "api", afb_hook_flags_api_api },
578 { "api_del_verb", afb_hook_flag_api_api_del_verb },
579 { "api_seal", afb_hook_flag_api_api_seal },
580 { "api_set_on_event", afb_hook_flag_api_api_set_on_event },
581 { "api_set_on_init", afb_hook_flag_api_api_set_on_init },
582 { "api_set_verbs", afb_hook_flag_api_api_set_verbs },
583 { "call", afb_hook_flag_api_call },
584 { "callsync", afb_hook_flag_api_callsync },
585 { "class_provide", afb_hook_flag_api_class_provide },
586 { "class_require", afb_hook_flag_api_class_require },
587 { "common", afb_hook_flags_api_common },
588 { "delete_api", afb_hook_flag_api_delete_api },
589 { "event", afb_hook_flags_api_event },
590 { "event_broadcast", afb_hook_flag_api_event_broadcast },
591 { "event_handler_add", afb_hook_flag_api_event_handler_add },
592 { "event_handler_del", afb_hook_flag_api_event_handler_del },
593 { "event_make", afb_hook_flag_api_event_make },
594 { "extra", afb_hook_flags_api_extra },
595 { "get_event_loop", afb_hook_flag_api_get_event_loop },
596 { "get_system_bus", afb_hook_flag_api_get_system_bus },
597 { "get_user_bus", afb_hook_flag_api_get_user_bus },
598 { "legacy_unstore_req", afb_hook_flag_api_legacy_unstore_req },
599 { "new_api", afb_hook_flag_api_new_api },
600 { "on_event", afb_hook_flag_api_on_event },
601 { "on_event_handler", afb_hook_flag_api_on_event_handler },
602 { "queue_job", afb_hook_flag_api_queue_job },
603 { "require_api", afb_hook_flag_api_require_api },
604 { "rootdir_get_fd", afb_hook_flag_api_rootdir_get_fd },
605 { "rootdir_open_locale",afb_hook_flag_api_rootdir_open_locale },
606 { "start", afb_hook_flag_api_start },
607 { "vverbose", afb_hook_flag_api_vverbose },
610 /* get the export value for flag of 'name' */
611 static int get_api_flag(const char *name)
613 return get_flag(name, api_flags, (int)(sizeof api_flags / sizeof *api_flags));
616 static void hook_api(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *action, const char *format, ...)
620 va_start(ap, format);
621 emit(closure, hookid, "api", "{ss ss}", format, ap,
622 "api", afb_export_apiname(export),
627 static void hook_api_event_broadcast_before(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, struct json_object *object)
629 hook_api(closure, hookid, export, "event_broadcast_before", "{ss sO?}",
630 "name", name, "data", object);
633 static void hook_api_event_broadcast_after(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, struct json_object *object, int result)
635 hook_api(closure, hookid, export, "event_broadcast_after", "{ss sO? si}",
636 "name", name, "data", object, "result", result);
639 static void hook_api_get_event_loop(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_event *result)
641 hook_api(closure, hookid, export, "get_event_loop", NULL);
644 static void hook_api_get_user_bus(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_bus *result)
646 hook_api(closure, hookid, export, "get_user_bus", NULL);
649 static void hook_api_get_system_bus(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_bus *result)
651 hook_api(closure, hookid, export, "get_system_bus", NULL);
654 static void hook_api_vverbose(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int level, const char *file, int line, const char *function, const char *fmt, va_list args)
656 struct json_object *pos;
665 len = vasprintf(&msg, fmt, ap);
669 wrap_json_pack(&pos, "{ss si ss*}", "file", file, "line", line, "function", function);
671 hook_api(closure, hookid, export, "vverbose", "{si ss* ss? so*}",
673 "type", verbosity_level_name(level),
674 len < 0 ? "format" : "message", len < 0 ? fmt : msg,
680 static void hook_api_event_make(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, struct afb_event_x2 *result)
682 hook_api(closure, hookid, export, "event_make", "{ss ss si}",
683 "name", name, "event", afb_evt_event_x2_fullname(result), "id", afb_evt_event_x2_id(result));
686 static void hook_api_rootdir_get_fd(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result)
688 char path[PATH_MAX], proc[100];
689 const char *key, *val;
693 snprintf(proc, sizeof proc, "/proc/self/fd/%d", result);
694 s = readlink(proc, path, sizeof path);
695 path[s < 0 ? 0 : s >= sizeof path ? sizeof path - 1 : s] = 0;
700 val = strerror(errno);
703 hook_api(closure, hookid, export, "rootdir_get_fd", "{ss}", key, val);
706 static void hook_api_rootdir_open_locale(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *filename, int flags, const char *locale, int result)
708 char path[PATH_MAX], proc[100];
709 const char *key, *val;
713 snprintf(proc, sizeof proc, "/proc/self/fd/%d", result);
714 s = readlink(proc, path, sizeof path);
715 path[s < 0 ? 0 : s >= sizeof path ? sizeof path - 1 : s] = 0;
720 val = strerror(errno);
723 hook_api(closure, hookid, export, "rootdir_open_locale", "{ss si ss* ss}",
730 static void hook_api_queue_job(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout, int result)
732 hook_api(closure, hookid, export, "queue_job", "{ss}", "result", result);
735 static void hook_api_unstore_req(void * closure, const struct afb_hookid *hookid, const struct afb_export *export, struct afb_stored_req *sreq)
737 hook_api(closure, hookid, export, "unstore_req", NULL);
740 static void hook_api_require_api(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, int initialized)
742 hook_api(closure, hookid, export, "require_api", "{ss sb}", "name", name, "initialized", initialized);
745 static void hook_api_require_api_result(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, int initialized, int result)
747 hook_api(closure, hookid, export, "require_api_result", "{ss sb si}", "name", name, "initialized", initialized, "result", result);
750 static void hook_api_add_alias_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *api, const char *alias, int result)
752 hook_api(closure, hookid, export, "add_alias", "{si ss? ss}", "status", result, "api", api, "alias", alias);
755 static void hook_api_start_before(void *closure, const struct afb_hookid *hookid, const struct afb_export *export)
757 hook_api(closure, hookid, export, "start_before", NULL);
760 static void hook_api_start_after(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int status)
762 hook_api(closure, hookid, export, "start_after", "{si}", "result", status);
765 static void hook_api_on_event_before(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *event, int evtid, struct json_object *object)
767 hook_api(closure, hookid, export, "on_event_before", "{ss si sO*}",
768 "event", event, "id", evtid, "data", object);
771 static void hook_api_on_event_after(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *event, int evtid, struct json_object *object)
773 hook_api(closure, hookid, export, "on_event_after", "{ss si sO?}",
774 "event", event, "id", evtid, "data", object);
777 static void hook_api_call(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *api, const char *verb, struct json_object *args)
779 hook_api(closure, hookid, export, "call", "{ss ss sO?}",
780 "api", api, "verb", verb, "args", args);
783 static void hook_api_call_result(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct json_object *object, const char *error, const char *info)
785 hook_api(closure, hookid, export, "call_result", "{sO? ss? ss?}",
786 "object", object, "error", error, "info", info);
789 static void hook_api_callsync(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *api, const char *verb, struct json_object *args)
791 hook_api(closure, hookid, export, "callsync", "{ss ss sO?}",
792 "api", api, "verb", verb, "args", args);
795 static void hook_api_callsync_result(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int status, struct json_object *object, const char *error, const char *info)
797 hook_api(closure, hookid, export, "callsync_result", "{si sO? ss? ss?}",
798 "status", status, "object", object, "error", error, "info", info);
801 static void hook_api_new_api_before(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *api, const char *info, int noconcurrency)
803 hook_api(closure, hookid, export, "new_api.before", "{ss ss? sb}",
804 "api", api, "info", info, "noconcurrency", noconcurrency);
807 static void hook_api_new_api_after(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *api)
809 hook_api(closure, hookid, export, "new_api.after", "{si ss}",
810 "status", result, "api", api);
813 static void hook_api_api_set_verbs_v2(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const struct afb_verb_v2 *verbs)
815 hook_api(closure, hookid, export, "set_verbs_v2", "{si}", "status", result);
818 static void hook_api_api_set_verbs_v3(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const struct afb_verb_v3 *verbs)
820 hook_api(closure, hookid, export, "set_verbs_v3", "{si}", "status", result);
824 static void hook_api_api_add_verb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *verb, const char *info, int glob)
826 hook_api(closure, hookid, export, "add_verb", "{si ss ss? sb}", "status", result, "verb", verb, "info", info, "glob", glob);
829 static void hook_api_api_del_verb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *verb)
831 hook_api(closure, hookid, export, "del_verb", "{si ss}", "status", result, "verb", verb);
834 static void hook_api_api_set_on_event(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result)
836 hook_api(closure, hookid, export, "set_on_event", "{si}", "status", result);
839 static void hook_api_api_set_on_init(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result)
841 hook_api(closure, hookid, export, "set_on_init", "{si}", "status", result);
844 static void hook_api_api_seal(void *closure, const struct afb_hookid *hookid, const struct afb_export *export)
846 hook_api(closure, hookid, export, "seal", NULL);
849 static void hook_api_event_handler_add(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *pattern)
851 hook_api(closure, hookid, export, "event_handler_add", "{si ss?}", "status", result, "pattern", pattern);
854 static void hook_api_event_handler_del(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *pattern)
856 hook_api(closure, hookid, export, "event_handler_del", "{si ss?}", "status", result, "pattern", pattern);
859 static void hook_api_class_provide(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *name)
861 hook_api(closure, hookid, export, "class_provide", "{si ss?}", "status", result, "name", name);
864 static void hook_api_class_require(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *name)
866 hook_api(closure, hookid, export, "class_require", "{si ss?}", "status", result, "name", name);
869 static void hook_api_delete_api(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result)
871 hook_api(closure, hookid, export, "delete_api", "{si}", "status", result);
874 static struct afb_hook_api_itf hook_api_itf = {
875 .hook_api_event_broadcast_before = hook_api_event_broadcast_before,
876 .hook_api_event_broadcast_after = hook_api_event_broadcast_after,
877 .hook_api_get_event_loop = hook_api_get_event_loop,
878 .hook_api_get_user_bus = hook_api_get_user_bus,
879 .hook_api_get_system_bus = hook_api_get_system_bus,
880 .hook_api_vverbose = hook_api_vverbose,
881 .hook_api_event_make = hook_api_event_make,
882 .hook_api_rootdir_get_fd = hook_api_rootdir_get_fd,
883 .hook_api_rootdir_open_locale = hook_api_rootdir_open_locale,
884 .hook_api_queue_job = hook_api_queue_job,
885 .hook_api_legacy_unstore_req = hook_api_unstore_req,
886 .hook_api_require_api = hook_api_require_api,
887 .hook_api_require_api_result = hook_api_require_api_result,
888 .hook_api_add_alias = hook_api_add_alias_cb,
889 .hook_api_start_before = hook_api_start_before,
890 .hook_api_start_after = hook_api_start_after,
891 .hook_api_on_event_before = hook_api_on_event_before,
892 .hook_api_on_event_after = hook_api_on_event_after,
893 .hook_api_call = hook_api_call,
894 .hook_api_call_result = hook_api_call_result,
895 .hook_api_callsync = hook_api_callsync,
896 .hook_api_callsync_result = hook_api_callsync_result,
897 .hook_api_new_api_before = hook_api_new_api_before,
898 .hook_api_new_api_after = hook_api_new_api_after,
899 .hook_api_api_set_verbs_v2 = hook_api_api_set_verbs_v2,
900 .hook_api_api_set_verbs_v3 = hook_api_api_set_verbs_v3,
901 .hook_api_api_add_verb = hook_api_api_add_verb,
902 .hook_api_api_del_verb = hook_api_api_del_verb,
903 .hook_api_api_set_on_event = hook_api_api_set_on_event,
904 .hook_api_api_set_on_init = hook_api_api_set_on_init,
905 .hook_api_api_seal = hook_api_api_seal,
906 .hook_api_event_handler_add = hook_api_event_handler_add,
907 .hook_api_event_handler_del = hook_api_event_handler_del,
908 .hook_api_class_provide = hook_api_class_provide,
909 .hook_api_class_require = hook_api_class_require,
910 .hook_api_delete_api = hook_api_delete_api,
913 /*******************************************************************************/
914 /***** trace the events *****/
915 /*******************************************************************************/
917 static struct flag evt_flags[] = { /* must be sorted by names */
918 { "addref", afb_hook_flag_evt_addref },
919 { "all", afb_hook_flags_evt_all },
920 { "broadcast_after", afb_hook_flag_evt_broadcast_after },
921 { "broadcast_before", afb_hook_flag_evt_broadcast_before },
922 { "common", afb_hook_flags_evt_common },
923 { "create", afb_hook_flag_evt_create },
924 { "extra", afb_hook_flags_evt_extra },
925 { "name", afb_hook_flag_evt_name },
926 { "push_after", afb_hook_flag_evt_push_after },
927 { "push_before", afb_hook_flag_evt_push_before },
928 { "unref", afb_hook_flag_evt_unref },
931 /* get the evt value for flag of 'name' */
932 static int get_evt_flag(const char *name)
934 return get_flag(name, evt_flags, (int)(sizeof evt_flags / sizeof *evt_flags));
937 static void hook_evt(void *closure, const struct afb_hookid *hookid, const char *evt, int id, const char *action, const char *format, ...)
941 va_start(ap, format);
942 emit(closure, hookid, "event", "{si ss ss}", format, ap,
949 static void hook_evt_create(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
951 hook_evt(closure, hookid, evt, id, "create", NULL);
954 static void hook_evt_push_before(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj)
956 hook_evt(closure, hookid, evt, id, "push_before", "{sO*}", "data", obj);
960 static void hook_evt_push_after(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj, int result)
962 hook_evt(closure, hookid, evt, id, "push_after", "{sO* si}", "data", obj, "result", result);
965 static void hook_evt_broadcast_before(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj)
967 hook_evt(closure, hookid, evt, id, "broadcast_before", "{sO*}", "data", obj);
970 static void hook_evt_broadcast_after(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj, int result)
972 hook_evt(closure, hookid, evt, id, "broadcast_after", "{sO* si}", "data", obj, "result", result);
975 static void hook_evt_name(void *closure, const struct afb_hookid *hookid, const char *evt, int id, const char *result)
977 hook_evt(closure, hookid, evt, id, "name", "{ss}", "result", result);
980 static void hook_evt_addref(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
982 hook_evt(closure, hookid, evt, id, "addref", NULL);
985 static void hook_evt_unref(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
987 hook_evt(closure, hookid, evt, id, "unref", NULL);
990 static struct afb_hook_evt_itf hook_evt_itf = {
991 .hook_evt_create = hook_evt_create,
992 .hook_evt_push_before = hook_evt_push_before,
993 .hook_evt_push_after = hook_evt_push_after,
994 .hook_evt_broadcast_before = hook_evt_broadcast_before,
995 .hook_evt_broadcast_after = hook_evt_broadcast_after,
996 .hook_evt_name = hook_evt_name,
997 .hook_evt_addref = hook_evt_addref,
998 .hook_evt_unref = hook_evt_unref
1001 /*******************************************************************************/
1002 /***** trace the sessions *****/
1003 /*******************************************************************************/
1005 static struct flag session_flags[] = { /* must be sorted by names */
1006 { "addref", afb_hook_flag_session_addref },
1007 { "all", afb_hook_flags_session_all },
1008 { "close", afb_hook_flag_session_close },
1009 { "common", afb_hook_flags_session_common },
1010 { "create", afb_hook_flag_session_create },
1011 { "destroy", afb_hook_flag_session_destroy },
1012 { "renew", afb_hook_flag_session_renew },
1013 { "unref", afb_hook_flag_session_unref },
1016 /* get the session value for flag of 'name' */
1017 static int get_session_flag(const char *name)
1019 return get_flag(name, session_flags, (int)(sizeof session_flags / sizeof *session_flags));
1022 static void hook_session(void *closure, const struct afb_hookid *hookid, struct afb_session *session, const char *action, const char *format, ...)
1026 va_start(ap, format);
1027 emit(closure, hookid, "session", "{ss ss}", format, ap,
1033 static void hook_session_create(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1035 hook_session(closure, hookid, session, "create", "{ss}", "token", afb_session_token(session));
1038 static void hook_session_close(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1040 hook_session(closure, hookid, session, "close", NULL);
1043 static void hook_session_destroy(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1045 hook_session(closure, hookid, session, "destroy", NULL);
1048 static void hook_session_renew(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1050 hook_session(closure, hookid, session, "renew", "{ss}", "token", afb_session_token(session));
1053 static void hook_session_addref(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1055 hook_session(closure, hookid, session, "addref", NULL);
1058 static void hook_session_unref(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1060 hook_session(closure, hookid, session, "unref", NULL);
1063 static struct afb_hook_session_itf hook_session_itf = {
1064 .hook_session_create = hook_session_create,
1065 .hook_session_close = hook_session_close,
1066 .hook_session_destroy = hook_session_destroy,
1067 .hook_session_renew = hook_session_renew,
1068 .hook_session_addref = hook_session_addref,
1069 .hook_session_unref = hook_session_unref
1072 /*******************************************************************************/
1073 /***** trace the globals *****/
1074 /*******************************************************************************/
1076 static struct flag global_flags[] = { /* must be sorted by names */
1077 { "all", afb_hook_flags_global_all },
1078 { "vverbose", afb_hook_flag_global_vverbose },
1081 /* get the global value for flag of 'name' */
1082 static int get_global_flag(const char *name)
1084 return get_flag(name, global_flags, (int)(sizeof global_flags / sizeof *global_flags));
1087 static void hook_global(void *closure, const struct afb_hookid *hookid, const char *action, const char *format, ...)
1091 va_start(ap, format);
1092 emit(closure, hookid, "global", "{ss}", format, ap, "action", action);
1096 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)
1098 struct json_object *pos;
1107 len = vasprintf(&msg, fmt, ap);
1111 wrap_json_pack(&pos, "{ss si ss*}", "file", file, "line", line, "function", function);
1113 hook_global(closure, hookid, "vverbose", "{si ss* ss? so*}",
1115 "type", verbosity_level_name(level),
1116 len < 0 ? "format" : "message", len < 0 ? fmt : msg,
1122 static struct afb_hook_global_itf hook_global_itf = {
1123 .hook_global_vverbose = hook_global_vverbose,
1126 /*******************************************************************************/
1127 /***** abstract types *****/
1128 /*******************************************************************************/
1134 void (*unref)(void*);
1135 int (*get_flag)(const char*);
1137 abstracting[Trace_Type_Count] =
1142 .unref = (void(*)(void*))afb_hook_unref_xreq,
1143 .get_flag = get_xreq_flag
1148 .unref = (void(*)(void*))afb_hook_unref_api,
1149 .get_flag = get_api_flag
1154 .unref = (void(*)(void*))afb_hook_unref_evt,
1155 .get_flag = get_evt_flag
1157 [Trace_Type_Session] =
1160 .unref = (void(*)(void*))afb_hook_unref_session,
1161 .get_flag = get_session_flag
1163 [Trace_Type_Global] =
1166 .unref = (void(*)(void*))afb_hook_unref_global,
1167 .get_flag = get_global_flag
1169 #if !defined(REMOVE_LEGACY_TRACE)
1170 [Trace_Legacy_Type_Ditf] =
1173 .unref = (void(*)(void*))afb_hook_unref_api,
1174 .get_flag = get_legacy_ditf_flag
1176 [Trace_Legacy_Type_Svc] =
1179 .unref = (void(*)(void*))afb_hook_unref_api,
1180 .get_flag = get_legacy_svc_flag
1185 /*******************************************************************************/
1186 /***** handle trace data *****/
1187 /*******************************************************************************/
1189 /* drop hooks of 'trace' matching 'tag' and 'event' and 'session' */
1190 static void trace_unhook(struct afb_trace *trace, struct tag *tag, struct event *event, struct afb_session *session)
1193 struct hook *hook, **prev;
1195 /* remove any event */
1196 for (i = 0 ; i < Trace_Type_Count ; i++) {
1197 prev = &trace->hooks[i];
1198 while ((hook = *prev)) {
1199 if ((tag && tag != hook->tag)
1200 || (event && event != hook->event)
1201 || (session && session != hook->session))
1205 abstracting[i].unref(hook->handler);
1212 /* cleanup: removes unused tags, events and sessions of the 'trace' */
1213 static void trace_cleanup(struct afb_trace *trace)
1217 struct tag *tag, **ptag;
1218 struct event *event, **pevent;
1221 ptag = &trace->tags;
1222 while ((tag = *ptag)) {
1223 /* search for tag */
1224 for (hook = NULL, i = 0 ; !hook && i < Trace_Type_Count ; i++)
1225 for (hook = trace->hooks[i] ; hook && hook->tag != tag ; hook = hook->next);
1226 /* keep or free whether used or not */
1235 pevent = &trace->events;
1236 while ((event = *pevent)) {
1237 /* search for event */
1238 for (hook = NULL, i = 0 ; !hook && i < Trace_Type_Count ; i++)
1239 for (hook = trace->hooks[i] ; hook && hook->event != event ; hook = hook->next);
1240 /* keep or free whether used or not */
1242 pevent = &event->next;
1244 *pevent = event->next;
1245 afb_evt_evtid_unref(event->evtid);
1252 * Get the tag of 'name' within 'trace'.
1253 * If 'alloc' isn't zero, create the tag and add it.
1255 static struct tag *trace_get_tag(struct afb_trace *trace, const char *name, int alloc)
1259 /* search the tag of 'name' */
1261 while (tag && strcmp(name, tag->tag))
1264 if (!tag && alloc) {
1265 /* creation if needed */
1266 tag = malloc(sizeof * tag + strlen(name));
1268 strcpy(tag->tag, name);
1269 tag->next = trace->tags;
1277 * Get the event of 'name' within 'trace'.
1278 * If 'alloc' isn't zero, create the event and add it.
1280 static struct event *trace_get_event(struct afb_trace *trace, const char *name, int alloc)
1282 struct event *event;
1284 /* search the event */
1285 event = trace->events;
1286 while (event && strcmp(afb_evt_evtid_name(event->evtid), name))
1287 event = event->next;
1289 if (!event && alloc) {
1290 event = malloc(sizeof * event);
1292 event->evtid = afb_evt_evtid_create2(trace->apiname, name);
1294 event->next = trace->events;
1295 trace->events = event;
1306 * called on session closing
1308 static void session_closed(void *item)
1310 struct cookie *cookie = item;
1312 pthread_mutex_lock(&cookie->trace->mutex);
1313 trace_unhook(cookie->trace, NULL, NULL, cookie->session);
1314 pthread_mutex_unlock(&cookie->trace->mutex);
1319 * records the cookie of session for tracking close
1321 static void *session_open(void *closure)
1323 struct cookie *param = closure, *cookie;
1324 cookie = malloc(sizeof *cookie);
1331 * Get the session of 'uuid' within 'trace'.
1332 * If 'alloc' isn't zero, create the session and add it.
1334 static struct afb_session *trace_get_session_by_uuid(struct afb_trace *trace, const char *uuid, int alloc)
1336 struct cookie cookie;
1339 cookie.session = afb_session_search(uuid);
1341 cookie.session = afb_session_get(uuid, AFB_SESSION_TIMEOUT_DEFAULT, NULL);
1342 if (cookie.session) {
1343 cookie.trace = trace;
1344 afb_session_cookie(cookie.session, cookie.trace, session_open, session_closed, &cookie, 0);
1347 return cookie.session;
1350 static struct hook *trace_make_detached_hook(struct afb_trace *trace, const char *event, const char *tag)
1354 tag = tag ?: DEFAULT_TAG_NAME;
1355 event = event ?: DEFAULT_EVENT_NAME;
1356 hook = malloc(sizeof *hook);
1358 hook->tag = trace_get_tag(trace, tag, 1);
1359 hook->event = trace_get_event(trace, event, 1);
1360 hook->session = NULL;
1361 hook->handler = NULL;
1366 static void trace_attach_hook(struct afb_trace *trace, struct hook *hook, enum trace_type type)
1368 hook->next = trace->hooks[type];
1369 trace->hooks[type] = hook;
1372 /*******************************************************************************/
1373 /***** handle client requests *****/
1374 /*******************************************************************************/
1378 struct afb_trace *trace;
1385 struct context *context;
1389 const char *apiname;
1390 const char *verbname;
1391 const char *pattern;
1392 int flags[Trace_Type_Count];
1395 static void addhook(struct desc *desc, enum trace_type type)
1398 struct afb_session *session;
1399 struct afb_session *bind;
1400 struct afb_trace *trace = desc->context->trace;
1402 /* check permission for bound traces */
1403 bind = trace->bound;
1405 if (type != Trace_Type_Xreq) {
1406 ctxt_error(&desc->context->errors, "tracing %s is forbidden", abstracting[type].name);
1410 ctxt_error(&desc->context->errors, "setting session is forbidden");
1415 /* allocate the hook */
1416 hook = trace_make_detached_hook(trace, desc->name, desc->tag);
1418 ctxt_error(&desc->context->errors, "allocation of hook failed");
1422 /* create the hook handler */
1424 case Trace_Type_Xreq:
1426 session = afb_session_addref(bind);
1428 session = trace_get_session_by_uuid(trace, desc->uuid, 1);
1430 ctxt_error(&desc->context->errors, "allocation of session failed");
1435 hook->handler = afb_hook_create_xreq(desc->apiname, desc->verbname, session,
1436 desc->flags[type], &hook_xreq_itf, hook);
1437 afb_session_unref(session);
1439 case Trace_Type_Api:
1440 hook->handler = afb_hook_create_api(desc->apiname, desc->flags[type], &hook_api_itf, hook);
1442 case Trace_Type_Evt:
1443 hook->handler = afb_hook_create_evt(desc->pattern, desc->flags[type], &hook_evt_itf, hook);
1445 case Trace_Type_Session:
1446 hook->handler = afb_hook_create_session(desc->uuid, desc->flags[type], &hook_session_itf, hook);
1448 case Trace_Type_Global:
1449 hook->handler = afb_hook_create_global(desc->flags[type], &hook_global_itf, hook);
1454 if (!hook->handler) {
1455 ctxt_error(&desc->context->errors, "creation of hook failed");
1460 /* attach and activate the hook */
1461 afb_req_subscribe(desc->context->req, afb_evt_event_x2_from_evtid(hook->event->evtid));
1462 trace_attach_hook(trace, hook, type);
1465 static void addhooks(struct desc *desc)
1469 #if !defined(REMOVE_LEGACY_TRACE)
1470 desc->flags[Trace_Type_Api] |= desc->flags[Trace_Legacy_Type_Ditf] | desc->flags[Trace_Legacy_Type_Svc];
1471 desc->flags[Trace_Legacy_Type_Ditf] = desc->flags[Trace_Legacy_Type_Svc] = 0;
1474 for (i = 0 ; i < Trace_Type_Count ; i++) {
1480 static void add_flags(void *closure, struct json_object *object, enum trace_type type)
1483 const char *name, *queried;
1484 struct desc *desc = closure;
1486 if (wrap_json_unpack(object, "s", &name))
1487 ctxt_error(&desc->context->errors, "unexpected %s value %s",
1488 abstracting[type].name,
1489 json_object_to_json_string(object));
1491 queried = (name[0] == '*' && !name[1]) ? "all" : name;
1492 value = abstracting[type].get_flag(queried);
1494 desc->flags[type] |= value;
1496 ctxt_error(&desc->context->errors, "unknown %s name %s",
1497 abstracting[type].name, name);
1501 static void add_xreq_flags(void *closure, struct json_object *object)
1503 add_flags(closure, object, Trace_Type_Xreq);
1506 #if !defined(REMOVE_LEGACY_TRACE)
1507 static void legacy_add_ditf_flags(void *closure, struct json_object *object)
1509 add_flags(closure, object, Trace_Legacy_Type_Ditf);
1512 static void legacy_add_svc_flags(void *closure, struct json_object *object)
1514 add_flags(closure, object, Trace_Legacy_Type_Svc);
1518 static void add_api_flags(void *closure, struct json_object *object)
1520 add_flags(closure, object, Trace_Type_Api);
1523 static void add_evt_flags(void *closure, struct json_object *object)
1525 add_flags(closure, object, Trace_Type_Evt);
1528 static void add_session_flags(void *closure, struct json_object *object)
1530 add_flags(closure, object, Trace_Type_Session);
1533 static void add_global_flags(void *closure, struct json_object *object)
1535 add_flags(closure, object, Trace_Type_Global);
1539 static void add(void *closure, struct json_object *object)
1543 struct json_object *request, *event, *sub, *global, *session, *api;
1544 #if !defined(REMOVE_LEGACY_TRACE)
1545 struct json_object *daemon, *service;
1548 memcpy (&desc, closure, sizeof desc);
1549 request = event = sub = global = session = api = NULL;
1550 #if !defined(REMOVE_LEGACY_TRACE)
1551 daemon = service = NULL;
1554 rc = wrap_json_unpack(object, "{s?s s?s s?s s?s s?s s?s s?o s?o s?o s?o s?o s?o s?o}",
1557 "apiname", &desc.apiname,
1558 "verbname", &desc.verbname,
1560 "pattern", &desc.pattern,
1562 "request", &request,
1563 #if !defined(REMOVE_LEGACY_TRACE)
1565 "service", &service,
1568 "session", &session,
1574 if (desc.apiname && desc.apiname[0] == '*' && !desc.apiname[1])
1575 desc.apiname = NULL;
1577 if (desc.verbname && desc.verbname[0] == '*' && !desc.verbname[1])
1578 desc.verbname = NULL;
1580 if (desc.uuid && desc.uuid[0] == '*' && !desc.uuid[1])
1583 /* get what is expected */
1585 wrap_json_optarray_for_all(request, add_xreq_flags, &desc);
1588 wrap_json_optarray_for_all(api, add_api_flags, &desc);
1590 #if !defined(REMOVE_LEGACY_TRACE)
1592 wrap_json_optarray_for_all(daemon, legacy_add_ditf_flags, &desc);
1595 wrap_json_optarray_for_all(service, legacy_add_svc_flags, &desc);
1599 wrap_json_optarray_for_all(event, add_evt_flags, &desc);
1602 wrap_json_optarray_for_all(event, add_session_flags, &desc);
1605 wrap_json_optarray_for_all(global, add_global_flags, &desc);
1609 wrap_json_optarray_for_all(sub, add, &desc);
1614 wrap_json_optarray_for_all(object, add_xreq_flags, &desc);
1619 /* drop hooks of given tag */
1620 static void drop_tag(void *closure, struct json_object *object)
1623 struct context *context = closure;
1627 rc = wrap_json_unpack(object, "s", &name);
1629 ctxt_error(&context->errors, "unexpected tag value %s", json_object_to_json_string(object));
1631 tag = trace_get_tag(context->trace, name, 0);
1633 ctxt_error(&context->errors, "tag %s not found", name);
1635 trace_unhook(context->trace, tag, NULL, NULL);
1639 /* drop hooks of given event */
1640 static void drop_event(void *closure, struct json_object *object)
1643 struct context *context = closure;
1644 struct event *event;
1647 rc = wrap_json_unpack(object, "s", &name);
1649 ctxt_error(&context->errors, "unexpected event value %s", json_object_to_json_string(object));
1651 event = trace_get_event(context->trace, name, 0);
1653 ctxt_error(&context->errors, "event %s not found", name);
1655 trace_unhook(context->trace, NULL, event, NULL);
1659 /* drop hooks of given session */
1660 static void drop_session(void *closure, struct json_object *object)
1663 struct context *context = closure;
1664 struct afb_session *session;
1667 rc = wrap_json_unpack(object, "s", &uuid);
1669 ctxt_error(&context->errors, "unexpected session value %s", json_object_to_json_string(object));
1671 session = trace_get_session_by_uuid(context->trace, uuid, 0);
1673 ctxt_error(&context->errors, "session %s not found", uuid);
1675 trace_unhook(context->trace, NULL, NULL, session);
1676 afb_session_unref(session);
1681 /*******************************************************************************/
1682 /***** public interface *****/
1683 /*******************************************************************************/
1685 /* allocates an afb_trace instance */
1686 struct afb_trace *afb_trace_create(const char *apiname, struct afb_session *bound)
1688 struct afb_trace *trace;
1692 trace = calloc(1, sizeof *trace);
1694 trace->refcount = 1;
1695 trace->bound = bound;
1696 trace->apiname = apiname;
1697 pthread_mutex_init(&trace->mutex, NULL);
1702 /* add a reference to the trace */
1703 void afb_trace_addref(struct afb_trace *trace)
1705 __atomic_add_fetch(&trace->refcount, 1, __ATOMIC_RELAXED);
1708 /* drop one reference to the trace */
1709 void afb_trace_unref(struct afb_trace *trace)
1711 if (trace && !__atomic_sub_fetch(&trace->refcount, 1, __ATOMIC_RELAXED)) {
1713 trace_unhook(trace, NULL, NULL, NULL);
1714 trace_cleanup(trace);
1715 pthread_mutex_destroy(&trace->mutex);
1721 int afb_trace_add(afb_req_t req, struct json_object *args, struct afb_trace *trace)
1723 struct context context;
1726 memset(&context, 0, sizeof context);
1727 context.trace = trace;
1730 memset(&desc, 0, sizeof desc);
1731 desc.context = &context;
1733 pthread_mutex_lock(&trace->mutex);
1734 wrap_json_optarray_for_all(args, add, &desc);
1735 pthread_mutex_unlock(&trace->mutex);
1737 if (!context.errors)
1740 afb_req_fail(req, "error-detected", context.errors);
1741 free(context.errors);
1746 int afb_trace_drop(afb_req_t req, struct json_object *args, struct afb_trace *trace)
1749 struct context context;
1750 struct json_object *tags, *events, *uuids;
1752 memset(&context, 0, sizeof context);
1753 context.trace = trace;
1756 /* special: boolean value */
1757 if (!wrap_json_unpack(args, "b", &rc)) {
1759 pthread_mutex_lock(&trace->mutex);
1760 trace_unhook(trace, NULL, NULL, NULL);
1761 trace_cleanup(trace);
1762 pthread_mutex_unlock(&trace->mutex);
1767 tags = events = uuids = NULL;
1768 rc = wrap_json_unpack(args, "{s?o s?o s?o}",
1773 if (rc < 0 || !(events || tags || uuids)) {
1774 afb_req_fail(req, "error-detected", "bad drop arguments");
1778 pthread_mutex_lock(&trace->mutex);
1781 wrap_json_optarray_for_all(tags, drop_tag, &context);
1784 wrap_json_optarray_for_all(events, drop_event, &context);
1787 wrap_json_optarray_for_all(uuids, drop_session, &context);
1789 trace_cleanup(trace);
1791 pthread_mutex_unlock(&trace->mutex);
1793 if (!context.errors)
1796 afb_req_fail(req, "error-detected", context.errors);
1797 free(context.errors);