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;
692 snprintf(proc, sizeof proc, "/proc/self/fd/%d", result);
693 readlink(proc, path, sizeof path);
698 val = strerror(errno);
701 hook_api(closure, hookid, export, "rootdir_get_fd", "{ss}", key, val);
704 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)
706 char path[PATH_MAX], proc[100];
707 const char *key, *val;
710 snprintf(proc, sizeof proc, "/proc/self/fd/%d", result);
711 readlink(proc, path, sizeof path);
716 val = strerror(errno);
719 hook_api(closure, hookid, export, "rootdir_open_locale", "{ss si ss* ss}",
726 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)
728 hook_api(closure, hookid, export, "queue_job", "{ss}", "result", result);
731 static void hook_api_unstore_req(void * closure, const struct afb_hookid *hookid, const struct afb_export *export, struct afb_stored_req *sreq)
733 hook_api(closure, hookid, export, "unstore_req", NULL);
736 static void hook_api_require_api(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, int initialized)
738 hook_api(closure, hookid, export, "require_api", "{ss sb}", "name", name, "initialized", initialized);
741 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)
743 hook_api(closure, hookid, export, "require_api_result", "{ss sb si}", "name", name, "initialized", initialized, "result", result);
746 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)
748 hook_api(closure, hookid, export, "add_alias", "{si ss? ss}", "status", result, "api", api, "alias", alias);
751 static void hook_api_start_before(void *closure, const struct afb_hookid *hookid, const struct afb_export *export)
753 hook_api(closure, hookid, export, "start_before", NULL);
756 static void hook_api_start_after(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int status)
758 hook_api(closure, hookid, export, "start_after", "{si}", "result", status);
761 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)
763 hook_api(closure, hookid, export, "on_event_before", "{ss si sO*}",
764 "event", event, "id", evtid, "data", object);
767 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)
769 hook_api(closure, hookid, export, "on_event_after", "{ss si sO?}",
770 "event", event, "id", evtid, "data", object);
773 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)
775 hook_api(closure, hookid, export, "call", "{ss ss sO?}",
776 "api", api, "verb", verb, "args", args);
779 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)
781 hook_api(closure, hookid, export, "call_result", "{sO? ss? ss?}",
782 "object", object, "error", error, "info", info);
785 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)
787 hook_api(closure, hookid, export, "callsync", "{ss ss sO?}",
788 "api", api, "verb", verb, "args", args);
791 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)
793 hook_api(closure, hookid, export, "callsync_result", "{si sO? ss? ss?}",
794 "status", status, "object", object, "error", error, "info", info);
797 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)
799 hook_api(closure, hookid, export, "new_api.before", "{ss ss? sb}",
800 "api", api, "info", info, "noconcurrency", noconcurrency);
803 static void hook_api_new_api_after(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *api)
805 hook_api(closure, hookid, export, "new_api.after", "{si ss}",
806 "status", result, "api", api);
809 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)
811 hook_api(closure, hookid, export, "set_verbs_v2", "{si}", "status", result);
814 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)
816 hook_api(closure, hookid, export, "set_verbs_v3", "{si}", "status", result);
820 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)
822 hook_api(closure, hookid, export, "add_verb", "{si ss ss? sb}", "status", result, "verb", verb, "info", info, "glob", glob);
825 static void hook_api_api_del_verb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *verb)
827 hook_api(closure, hookid, export, "del_verb", "{si ss}", "status", result, "verb", verb);
830 static void hook_api_api_set_on_event(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result)
832 hook_api(closure, hookid, export, "set_on_event", "{si}", "status", result);
835 static void hook_api_api_set_on_init(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result)
837 hook_api(closure, hookid, export, "set_on_init", "{si}", "status", result);
840 static void hook_api_api_seal(void *closure, const struct afb_hookid *hookid, const struct afb_export *export)
842 hook_api(closure, hookid, export, "seal", NULL);
845 static void hook_api_event_handler_add(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *pattern)
847 hook_api(closure, hookid, export, "event_handler_add", "{si ss?}", "status", result, "pattern", pattern);
850 static void hook_api_event_handler_del(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *pattern)
852 hook_api(closure, hookid, export, "event_handler_del", "{si ss?}", "status", result, "pattern", pattern);
855 static void hook_api_class_provide(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *name)
857 hook_api(closure, hookid, export, "class_provide", "{si ss?}", "status", result, "name", name);
860 static void hook_api_class_require(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *name)
862 hook_api(closure, hookid, export, "class_require", "{si ss?}", "status", result, "name", name);
865 static void hook_api_delete_api(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result)
867 hook_api(closure, hookid, export, "delete_api", "{si}", "status", result);
870 static struct afb_hook_api_itf hook_api_itf = {
871 .hook_api_event_broadcast_before = hook_api_event_broadcast_before,
872 .hook_api_event_broadcast_after = hook_api_event_broadcast_after,
873 .hook_api_get_event_loop = hook_api_get_event_loop,
874 .hook_api_get_user_bus = hook_api_get_user_bus,
875 .hook_api_get_system_bus = hook_api_get_system_bus,
876 .hook_api_vverbose = hook_api_vverbose,
877 .hook_api_event_make = hook_api_event_make,
878 .hook_api_rootdir_get_fd = hook_api_rootdir_get_fd,
879 .hook_api_rootdir_open_locale = hook_api_rootdir_open_locale,
880 .hook_api_queue_job = hook_api_queue_job,
881 .hook_api_legacy_unstore_req = hook_api_unstore_req,
882 .hook_api_require_api = hook_api_require_api,
883 .hook_api_require_api_result = hook_api_require_api_result,
884 .hook_api_add_alias = hook_api_add_alias_cb,
885 .hook_api_start_before = hook_api_start_before,
886 .hook_api_start_after = hook_api_start_after,
887 .hook_api_on_event_before = hook_api_on_event_before,
888 .hook_api_on_event_after = hook_api_on_event_after,
889 .hook_api_call = hook_api_call,
890 .hook_api_call_result = hook_api_call_result,
891 .hook_api_callsync = hook_api_callsync,
892 .hook_api_callsync_result = hook_api_callsync_result,
893 .hook_api_new_api_before = hook_api_new_api_before,
894 .hook_api_new_api_after = hook_api_new_api_after,
895 .hook_api_api_set_verbs_v2 = hook_api_api_set_verbs_v2,
896 .hook_api_api_set_verbs_v3 = hook_api_api_set_verbs_v3,
897 .hook_api_api_add_verb = hook_api_api_add_verb,
898 .hook_api_api_del_verb = hook_api_api_del_verb,
899 .hook_api_api_set_on_event = hook_api_api_set_on_event,
900 .hook_api_api_set_on_init = hook_api_api_set_on_init,
901 .hook_api_api_seal = hook_api_api_seal,
902 .hook_api_event_handler_add = hook_api_event_handler_add,
903 .hook_api_event_handler_del = hook_api_event_handler_del,
904 .hook_api_class_provide = hook_api_class_provide,
905 .hook_api_class_require = hook_api_class_require,
906 .hook_api_delete_api = hook_api_delete_api,
909 /*******************************************************************************/
910 /***** trace the events *****/
911 /*******************************************************************************/
913 static struct flag evt_flags[] = { /* must be sorted by names */
914 { "addref", afb_hook_flag_evt_addref },
915 { "all", afb_hook_flags_evt_all },
916 { "broadcast_after", afb_hook_flag_evt_broadcast_after },
917 { "broadcast_before", afb_hook_flag_evt_broadcast_before },
918 { "common", afb_hook_flags_evt_common },
919 { "create", afb_hook_flag_evt_create },
920 { "extra", afb_hook_flags_evt_extra },
921 { "name", afb_hook_flag_evt_name },
922 { "push_after", afb_hook_flag_evt_push_after },
923 { "push_before", afb_hook_flag_evt_push_before },
924 { "unref", afb_hook_flag_evt_unref },
927 /* get the evt value for flag of 'name' */
928 static int get_evt_flag(const char *name)
930 return get_flag(name, evt_flags, (int)(sizeof evt_flags / sizeof *evt_flags));
933 static void hook_evt(void *closure, const struct afb_hookid *hookid, const char *evt, int id, const char *action, const char *format, ...)
937 va_start(ap, format);
938 emit(closure, hookid, "event", "{si ss ss}", format, ap,
945 static void hook_evt_create(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
947 hook_evt(closure, hookid, evt, id, "create", NULL);
950 static void hook_evt_push_before(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj)
952 hook_evt(closure, hookid, evt, id, "push_before", "{sO*}", "data", obj);
956 static void hook_evt_push_after(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj, int result)
958 hook_evt(closure, hookid, evt, id, "push_after", "{sO* si}", "data", obj, "result", result);
961 static void hook_evt_broadcast_before(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj)
963 hook_evt(closure, hookid, evt, id, "broadcast_before", "{sO*}", "data", obj);
966 static void hook_evt_broadcast_after(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj, int result)
968 hook_evt(closure, hookid, evt, id, "broadcast_after", "{sO* si}", "data", obj, "result", result);
971 static void hook_evt_name(void *closure, const struct afb_hookid *hookid, const char *evt, int id, const char *result)
973 hook_evt(closure, hookid, evt, id, "name", "{ss}", "result", result);
976 static void hook_evt_addref(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
978 hook_evt(closure, hookid, evt, id, "addref", NULL);
981 static void hook_evt_unref(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
983 hook_evt(closure, hookid, evt, id, "unref", NULL);
986 static struct afb_hook_evt_itf hook_evt_itf = {
987 .hook_evt_create = hook_evt_create,
988 .hook_evt_push_before = hook_evt_push_before,
989 .hook_evt_push_after = hook_evt_push_after,
990 .hook_evt_broadcast_before = hook_evt_broadcast_before,
991 .hook_evt_broadcast_after = hook_evt_broadcast_after,
992 .hook_evt_name = hook_evt_name,
993 .hook_evt_addref = hook_evt_addref,
994 .hook_evt_unref = hook_evt_unref
997 /*******************************************************************************/
998 /***** trace the sessions *****/
999 /*******************************************************************************/
1001 static struct flag session_flags[] = { /* must be sorted by names */
1002 { "addref", afb_hook_flag_session_addref },
1003 { "all", afb_hook_flags_session_all },
1004 { "close", afb_hook_flag_session_close },
1005 { "common", afb_hook_flags_session_common },
1006 { "create", afb_hook_flag_session_create },
1007 { "destroy", afb_hook_flag_session_destroy },
1008 { "renew", afb_hook_flag_session_renew },
1009 { "unref", afb_hook_flag_session_unref },
1012 /* get the session value for flag of 'name' */
1013 static int get_session_flag(const char *name)
1015 return get_flag(name, session_flags, (int)(sizeof session_flags / sizeof *session_flags));
1018 static void hook_session(void *closure, const struct afb_hookid *hookid, struct afb_session *session, const char *action, const char *format, ...)
1022 va_start(ap, format);
1023 emit(closure, hookid, "session", "{ss ss}", format, ap,
1029 static void hook_session_create(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1031 hook_session(closure, hookid, session, "create", "{ss}", "token", afb_session_token(session));
1034 static void hook_session_close(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1036 hook_session(closure, hookid, session, "close", NULL);
1039 static void hook_session_destroy(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1041 hook_session(closure, hookid, session, "destroy", NULL);
1044 static void hook_session_renew(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1046 hook_session(closure, hookid, session, "renew", "{ss}", "token", afb_session_token(session));
1049 static void hook_session_addref(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1051 hook_session(closure, hookid, session, "addref", NULL);
1054 static void hook_session_unref(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1056 hook_session(closure, hookid, session, "unref", NULL);
1059 static struct afb_hook_session_itf hook_session_itf = {
1060 .hook_session_create = hook_session_create,
1061 .hook_session_close = hook_session_close,
1062 .hook_session_destroy = hook_session_destroy,
1063 .hook_session_renew = hook_session_renew,
1064 .hook_session_addref = hook_session_addref,
1065 .hook_session_unref = hook_session_unref
1068 /*******************************************************************************/
1069 /***** trace the globals *****/
1070 /*******************************************************************************/
1072 static struct flag global_flags[] = { /* must be sorted by names */
1073 { "all", afb_hook_flags_global_all },
1074 { "vverbose", afb_hook_flag_global_vverbose },
1077 /* get the global value for flag of 'name' */
1078 static int get_global_flag(const char *name)
1080 return get_flag(name, global_flags, (int)(sizeof global_flags / sizeof *global_flags));
1083 static void hook_global(void *closure, const struct afb_hookid *hookid, const char *action, const char *format, ...)
1087 va_start(ap, format);
1088 emit(closure, hookid, "global", "{ss}", format, ap, "action", action);
1092 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)
1094 struct json_object *pos;
1103 len = vasprintf(&msg, fmt, ap);
1107 wrap_json_pack(&pos, "{ss si ss*}", "file", file, "line", line, "function", function);
1109 hook_global(closure, hookid, "vverbose", "{si ss* ss? so*}",
1111 "type", verbosity_level_name(level),
1112 len < 0 ? "format" : "message", len < 0 ? fmt : msg,
1118 static struct afb_hook_global_itf hook_global_itf = {
1119 .hook_global_vverbose = hook_global_vverbose,
1122 /*******************************************************************************/
1123 /***** abstract types *****/
1124 /*******************************************************************************/
1130 void (*unref)(void*);
1131 int (*get_flag)(const char*);
1133 abstracting[Trace_Type_Count] =
1138 .unref = (void(*)(void*))afb_hook_unref_xreq,
1139 .get_flag = get_xreq_flag
1144 .unref = (void(*)(void*))afb_hook_unref_api,
1145 .get_flag = get_api_flag
1150 .unref = (void(*)(void*))afb_hook_unref_evt,
1151 .get_flag = get_evt_flag
1153 [Trace_Type_Session] =
1156 .unref = (void(*)(void*))afb_hook_unref_session,
1157 .get_flag = get_session_flag
1159 [Trace_Type_Global] =
1162 .unref = (void(*)(void*))afb_hook_unref_global,
1163 .get_flag = get_global_flag
1165 #if !defined(REMOVE_LEGACY_TRACE)
1166 [Trace_Legacy_Type_Ditf] =
1169 .unref = (void(*)(void*))afb_hook_unref_api,
1170 .get_flag = get_legacy_ditf_flag
1172 [Trace_Legacy_Type_Svc] =
1175 .unref = (void(*)(void*))afb_hook_unref_api,
1176 .get_flag = get_legacy_svc_flag
1181 /*******************************************************************************/
1182 /***** handle trace data *****/
1183 /*******************************************************************************/
1185 /* drop hooks of 'trace' matching 'tag' and 'event' and 'session' */
1186 static void trace_unhook(struct afb_trace *trace, struct tag *tag, struct event *event, struct afb_session *session)
1189 struct hook *hook, **prev;
1191 /* remove any event */
1192 for (i = 0 ; i < Trace_Type_Count ; i++) {
1193 prev = &trace->hooks[i];
1194 while ((hook = *prev)) {
1195 if ((tag && tag != hook->tag)
1196 || (event && event != hook->event)
1197 || (session && session != hook->session))
1201 abstracting[i].unref(hook->handler);
1208 /* cleanup: removes unused tags, events and sessions of the 'trace' */
1209 static void trace_cleanup(struct afb_trace *trace)
1213 struct tag *tag, **ptag;
1214 struct event *event, **pevent;
1217 ptag = &trace->tags;
1218 while ((tag = *ptag)) {
1219 /* search for tag */
1220 for (hook = NULL, i = 0 ; !hook && i < Trace_Type_Count ; i++)
1221 for (hook = trace->hooks[i] ; hook && hook->tag != tag ; hook = hook->next);
1222 /* keep or free whether used or not */
1231 pevent = &trace->events;
1232 while ((event = *pevent)) {
1233 /* search for event */
1234 for (hook = NULL, i = 0 ; !hook && i < Trace_Type_Count ; i++)
1235 for (hook = trace->hooks[i] ; hook && hook->event != event ; hook = hook->next);
1236 /* keep or free whether used or not */
1238 pevent = &event->next;
1240 *pevent = event->next;
1241 afb_evt_evtid_unref(event->evtid);
1248 * Get the tag of 'name' within 'trace'.
1249 * If 'alloc' isn't zero, create the tag and add it.
1251 static struct tag *trace_get_tag(struct afb_trace *trace, const char *name, int alloc)
1255 /* search the tag of 'name' */
1257 while (tag && strcmp(name, tag->tag))
1260 if (!tag && alloc) {
1261 /* creation if needed */
1262 tag = malloc(sizeof * tag + strlen(name));
1264 strcpy(tag->tag, name);
1265 tag->next = trace->tags;
1273 * Get the event of 'name' within 'trace'.
1274 * If 'alloc' isn't zero, create the event and add it.
1276 static struct event *trace_get_event(struct afb_trace *trace, const char *name, int alloc)
1278 struct event *event;
1280 /* search the event */
1281 event = trace->events;
1282 while (event && strcmp(afb_evt_evtid_name(event->evtid), name))
1283 event = event->next;
1285 if (!event && alloc) {
1286 event = malloc(sizeof * event);
1288 event->evtid = afb_evt_evtid_create2(trace->apiname, name);
1290 event->next = trace->events;
1291 trace->events = event;
1302 * called on session closing
1304 static void session_closed(void *item)
1306 struct cookie *cookie = item;
1308 pthread_mutex_lock(&cookie->trace->mutex);
1309 trace_unhook(cookie->trace, NULL, NULL, cookie->session);
1310 pthread_mutex_unlock(&cookie->trace->mutex);
1315 * records the cookie of session for tracking close
1317 static void *session_open(void *closure)
1319 struct cookie *param = closure, *cookie;
1320 cookie = malloc(sizeof *cookie);
1327 * Get the session of 'uuid' within 'trace'.
1328 * If 'alloc' isn't zero, create the session and add it.
1330 static struct afb_session *trace_get_session_by_uuid(struct afb_trace *trace, const char *uuid, int alloc)
1332 struct cookie cookie;
1335 cookie.session = afb_session_search(uuid);
1337 cookie.session = afb_session_get(uuid, AFB_SESSION_TIMEOUT_DEFAULT, NULL);
1338 if (cookie.session) {
1339 cookie.trace = trace;
1340 afb_session_cookie(cookie.session, cookie.trace, session_open, session_closed, &cookie, 0);
1343 return cookie.session;
1346 static struct hook *trace_make_detached_hook(struct afb_trace *trace, const char *event, const char *tag)
1350 tag = tag ?: DEFAULT_TAG_NAME;
1351 event = event ?: DEFAULT_EVENT_NAME;
1352 hook = malloc(sizeof *hook);
1354 hook->tag = trace_get_tag(trace, tag, 1);
1355 hook->event = trace_get_event(trace, event, 1);
1356 hook->session = NULL;
1357 hook->handler = NULL;
1362 static void trace_attach_hook(struct afb_trace *trace, struct hook *hook, enum trace_type type)
1364 hook->next = trace->hooks[type];
1365 trace->hooks[type] = hook;
1368 /*******************************************************************************/
1369 /***** handle client requests *****/
1370 /*******************************************************************************/
1374 struct afb_trace *trace;
1381 struct context *context;
1385 const char *apiname;
1386 const char *verbname;
1387 const char *pattern;
1388 int flags[Trace_Type_Count];
1391 static void addhook(struct desc *desc, enum trace_type type)
1394 struct afb_session *session;
1395 struct afb_session *bind;
1396 struct afb_trace *trace = desc->context->trace;
1398 /* check permission for bound traces */
1399 bind = trace->bound;
1401 if (type != Trace_Type_Xreq) {
1402 ctxt_error(&desc->context->errors, "tracing %s is forbidden", abstracting[type].name);
1406 ctxt_error(&desc->context->errors, "setting session is forbidden");
1411 /* allocate the hook */
1412 hook = trace_make_detached_hook(trace, desc->name, desc->tag);
1414 ctxt_error(&desc->context->errors, "allocation of hook failed");
1418 /* create the hook handler */
1420 case Trace_Type_Xreq:
1422 session = afb_session_addref(bind);
1424 session = trace_get_session_by_uuid(trace, desc->uuid, 1);
1426 ctxt_error(&desc->context->errors, "allocation of session failed");
1431 hook->handler = afb_hook_create_xreq(desc->apiname, desc->verbname, session,
1432 desc->flags[type], &hook_xreq_itf, hook);
1433 afb_session_unref(session);
1435 case Trace_Type_Api:
1436 hook->handler = afb_hook_create_api(desc->apiname, desc->flags[type], &hook_api_itf, hook);
1438 case Trace_Type_Evt:
1439 hook->handler = afb_hook_create_evt(desc->pattern, desc->flags[type], &hook_evt_itf, hook);
1441 case Trace_Type_Session:
1442 hook->handler = afb_hook_create_session(desc->uuid, desc->flags[type], &hook_session_itf, hook);
1444 case Trace_Type_Global:
1445 hook->handler = afb_hook_create_global(desc->flags[type], &hook_global_itf, hook);
1450 if (!hook->handler) {
1451 ctxt_error(&desc->context->errors, "creation of hook failed");
1456 /* attach and activate the hook */
1457 afb_req_subscribe(desc->context->req, afb_evt_event_x2_from_evtid(hook->event->evtid));
1458 trace_attach_hook(trace, hook, type);
1461 static void addhooks(struct desc *desc)
1465 #if !defined(REMOVE_LEGACY_TRACE)
1466 desc->flags[Trace_Type_Api] |= desc->flags[Trace_Legacy_Type_Ditf] | desc->flags[Trace_Legacy_Type_Svc];
1467 desc->flags[Trace_Legacy_Type_Ditf] = desc->flags[Trace_Legacy_Type_Svc] = 0;
1470 for (i = 0 ; i < Trace_Type_Count ; i++) {
1476 static void add_flags(void *closure, struct json_object *object, enum trace_type type)
1479 const char *name, *queried;
1480 struct desc *desc = closure;
1482 if (wrap_json_unpack(object, "s", &name))
1483 ctxt_error(&desc->context->errors, "unexpected %s value %s",
1484 abstracting[type].name,
1485 json_object_to_json_string(object));
1487 queried = (name[0] == '*' && !name[1]) ? "all" : name;
1488 value = abstracting[type].get_flag(queried);
1490 desc->flags[type] |= value;
1492 ctxt_error(&desc->context->errors, "unknown %s name %s",
1493 abstracting[type].name, name);
1497 static void add_xreq_flags(void *closure, struct json_object *object)
1499 add_flags(closure, object, Trace_Type_Xreq);
1502 #if !defined(REMOVE_LEGACY_TRACE)
1503 static void legacy_add_ditf_flags(void *closure, struct json_object *object)
1505 add_flags(closure, object, Trace_Legacy_Type_Ditf);
1508 static void legacy_add_svc_flags(void *closure, struct json_object *object)
1510 add_flags(closure, object, Trace_Legacy_Type_Svc);
1514 static void add_api_flags(void *closure, struct json_object *object)
1516 add_flags(closure, object, Trace_Type_Api);
1519 static void add_evt_flags(void *closure, struct json_object *object)
1521 add_flags(closure, object, Trace_Type_Evt);
1524 static void add_session_flags(void *closure, struct json_object *object)
1526 add_flags(closure, object, Trace_Type_Session);
1529 static void add_global_flags(void *closure, struct json_object *object)
1531 add_flags(closure, object, Trace_Type_Global);
1535 static void add(void *closure, struct json_object *object)
1539 struct json_object *request, *event, *sub, *global, *session, *api;
1540 #if !defined(REMOVE_LEGACY_TRACE)
1541 struct json_object *daemon, *service;
1544 memcpy (&desc, closure, sizeof desc);
1545 request = event = sub = global = session = api = NULL;
1546 #if !defined(REMOVE_LEGACY_TRACE)
1547 daemon = service = NULL;
1550 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}",
1553 "apiname", &desc.apiname,
1554 "verbname", &desc.verbname,
1556 "pattern", &desc.pattern,
1558 "request", &request,
1559 #if !defined(REMOVE_LEGACY_TRACE)
1561 "service", &service,
1564 "session", &session,
1570 if (desc.apiname && desc.apiname[0] == '*' && !desc.apiname[1])
1571 desc.apiname = NULL;
1573 if (desc.verbname && desc.verbname[0] == '*' && !desc.verbname[1])
1574 desc.verbname = NULL;
1576 if (desc.uuid && desc.uuid[0] == '*' && !desc.uuid[1])
1579 /* get what is expected */
1581 wrap_json_optarray_for_all(request, add_xreq_flags, &desc);
1584 wrap_json_optarray_for_all(api, add_api_flags, &desc);
1586 #if !defined(REMOVE_LEGACY_TRACE)
1588 wrap_json_optarray_for_all(daemon, legacy_add_ditf_flags, &desc);
1591 wrap_json_optarray_for_all(service, legacy_add_svc_flags, &desc);
1595 wrap_json_optarray_for_all(event, add_evt_flags, &desc);
1598 wrap_json_optarray_for_all(event, add_session_flags, &desc);
1601 wrap_json_optarray_for_all(global, add_global_flags, &desc);
1605 wrap_json_optarray_for_all(sub, add, &desc);
1610 wrap_json_optarray_for_all(object, add_xreq_flags, &desc);
1615 /* drop hooks of given tag */
1616 static void drop_tag(void *closure, struct json_object *object)
1619 struct context *context = closure;
1623 rc = wrap_json_unpack(object, "s", &name);
1625 ctxt_error(&context->errors, "unexpected tag value %s", json_object_to_json_string(object));
1627 tag = trace_get_tag(context->trace, name, 0);
1629 ctxt_error(&context->errors, "tag %s not found", name);
1631 trace_unhook(context->trace, tag, NULL, NULL);
1635 /* drop hooks of given event */
1636 static void drop_event(void *closure, struct json_object *object)
1639 struct context *context = closure;
1640 struct event *event;
1643 rc = wrap_json_unpack(object, "s", &name);
1645 ctxt_error(&context->errors, "unexpected event value %s", json_object_to_json_string(object));
1647 event = trace_get_event(context->trace, name, 0);
1649 ctxt_error(&context->errors, "event %s not found", name);
1651 trace_unhook(context->trace, NULL, event, NULL);
1655 /* drop hooks of given session */
1656 static void drop_session(void *closure, struct json_object *object)
1659 struct context *context = closure;
1660 struct afb_session *session;
1663 rc = wrap_json_unpack(object, "s", &uuid);
1665 ctxt_error(&context->errors, "unexpected session value %s", json_object_to_json_string(object));
1667 session = trace_get_session_by_uuid(context->trace, uuid, 0);
1669 ctxt_error(&context->errors, "session %s not found", uuid);
1671 trace_unhook(context->trace, NULL, NULL, session);
1672 afb_session_unref(session);
1677 /*******************************************************************************/
1678 /***** public interface *****/
1679 /*******************************************************************************/
1681 /* allocates an afb_trace instance */
1682 struct afb_trace *afb_trace_create(const char *apiname, struct afb_session *bound)
1684 struct afb_trace *trace;
1688 trace = calloc(1, sizeof *trace);
1690 trace->refcount = 1;
1691 trace->bound = bound;
1692 trace->apiname = apiname;
1693 pthread_mutex_init(&trace->mutex, NULL);
1698 /* add a reference to the trace */
1699 void afb_trace_addref(struct afb_trace *trace)
1701 __atomic_add_fetch(&trace->refcount, 1, __ATOMIC_RELAXED);
1704 /* drop one reference to the trace */
1705 void afb_trace_unref(struct afb_trace *trace)
1707 if (trace && !__atomic_sub_fetch(&trace->refcount, 1, __ATOMIC_RELAXED)) {
1709 trace_unhook(trace, NULL, NULL, NULL);
1710 trace_cleanup(trace);
1711 pthread_mutex_destroy(&trace->mutex);
1717 int afb_trace_add(afb_req_t req, struct json_object *args, struct afb_trace *trace)
1719 struct context context;
1722 memset(&context, 0, sizeof context);
1723 context.trace = trace;
1726 memset(&desc, 0, sizeof desc);
1727 desc.context = &context;
1729 pthread_mutex_lock(&trace->mutex);
1730 wrap_json_optarray_for_all(args, add, &desc);
1731 pthread_mutex_unlock(&trace->mutex);
1733 if (!context.errors)
1736 afb_req_fail(req, "error-detected", context.errors);
1737 free(context.errors);
1742 extern int afb_trace_drop(afb_req_t req, struct json_object *args, struct afb_trace *trace)
1745 struct context context;
1746 struct json_object *tags, *events, *uuids;
1748 memset(&context, 0, sizeof context);
1749 context.trace = trace;
1752 /* special: boolean value */
1753 if (!wrap_json_unpack(args, "b", &rc)) {
1755 pthread_mutex_lock(&trace->mutex);
1756 trace_unhook(trace, NULL, NULL, NULL);
1757 trace_cleanup(trace);
1758 pthread_mutex_unlock(&trace->mutex);
1763 tags = events = uuids = NULL;
1764 rc = wrap_json_unpack(args, "{s?o s?o s?o}",
1769 if (rc < 0 || !(events || tags || uuids)) {
1770 afb_req_fail(req, "error-detected", "bad drop arguments");
1774 pthread_mutex_lock(&trace->mutex);
1777 wrap_json_optarray_for_all(tags, drop_tag, &context);
1780 wrap_json_optarray_for_all(events, drop_event, &context);
1783 wrap_json_optarray_for_all(uuids, drop_session, &context);
1785 trace_cleanup(trace);
1787 pthread_mutex_unlock(&trace->mutex);
1789 if (!context.errors)
1792 afb_req_fail(req, "error-detected", context.errors);
1793 free(context.errors);