Add support for L4Re Virtio Sockets
[src/app-framework-binder.git] / src / afb-hook.c
1 /*
2  * Copyright (C) 2016-2019 "IoT.bzh"
3  * Author José Bollo <jose.bollo@iot.bzh>
4  *
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
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #if WITH_AFB_HOOK  /***********************************************************/
19
20 #define _GNU_SOURCE
21
22 #include <limits.h>
23 #include <stdio.h>
24 #include <stdarg.h>
25 #include <string.h>
26 #include <pthread.h>
27 #include <unistd.h>
28 #include <sys/uio.h>
29
30 #include <json-c/json.h>
31 #if !defined(JSON_C_TO_STRING_NOSLASHESCAPE)
32 #define JSON_C_TO_STRING_NOSLASHESCAPE 0
33 #endif
34
35 #include <afb/afb-req-x1.h>
36 #include <afb/afb-event-x2.h>
37
38 #include "afb-context.h"
39 #include "afb-hook.h"
40 #include "afb-session.h"
41 #include "afb-cred.h"
42 #include "afb-xreq.h"
43 #include "afb-export.h"
44 #include "afb-evt.h"
45 #include "afb-api.h"
46 #include "afb-msg-json.h"
47
48 #include "globmatch.h"
49 #include "verbose.h"
50
51 #define MATCH(pattern,string)   (\
52                 pattern \
53                         ? !fnmatch((pattern),(string),FNM_CASEFOLD|FNM_EXTMATCH|FNM_PERIOD) \
54                         : afb_api_is_public(string))
55
56 #define MATCH_API(pattern,string)       MATCH(pattern,string)
57 #define MATCH_VERB(pattern,string)      MATCH(pattern,string)
58 #define MATCH_EVENT(pattern,string)     MATCH(pattern,string)
59 #define MATCH_SESSION(pattern,string)   MATCH(pattern,string)
60
61 /**
62  * Definition of a hook for xreq
63  */
64 struct afb_hook_xreq {
65         struct afb_hook_xreq *next; /**< next hook */
66         unsigned refcount; /**< reference count */
67         unsigned flags; /**< hook flags */
68         char *api; /**< api hooked or NULL for any */
69         char *verb; /**< verb hooked or NULL for any */
70         struct afb_session *session; /**< session hooked or NULL if any */
71         struct afb_hook_xreq_itf *itf; /**< interface of hook */
72         void *closure; /**< closure for callbacks */
73 };
74
75 /**
76  * Definition of a hook for export
77  */
78 struct afb_hook_api {
79         struct afb_hook_api *next; /**< next hook */
80         unsigned refcount; /**< reference count */
81         unsigned flags; /**< hook flags */
82         char *api; /**< api hooked or NULL for any */
83         struct afb_hook_api_itf *itf; /**< interface of hook */
84         void *closure; /**< closure for callbacks */
85 };
86
87 /**
88  * Definition of a hook for evt
89  */
90 struct afb_hook_evt {
91         struct afb_hook_evt *next; /**< next hook */
92         unsigned refcount; /**< reference count */
93         unsigned flags; /**< hook flags */
94         char *pattern; /**< event pattern name hooked or NULL for any */
95         struct afb_hook_evt_itf *itf; /**< interface of hook */
96         void *closure; /**< closure for callbacks */
97 };
98
99 /**
100  * Definition of a hook for session
101  */
102 struct afb_hook_session {
103         struct afb_hook_session *next; /**< next hook */
104         unsigned refcount; /**< reference count */
105         unsigned flags; /**< hook flags */
106         char *pattern; /**< event pattern name hooked or NULL for any */
107         struct afb_hook_session_itf *itf; /**< interface of hook */
108         void *closure; /**< closure for callbacks */
109 };
110
111 /**
112  * Definition of a hook for global
113  */
114 struct afb_hook_global {
115         struct afb_hook_global *next; /**< next hook */
116         unsigned refcount; /**< reference count */
117         unsigned flags; /**< hook flags */
118         struct afb_hook_global_itf *itf; /**< interface of hook */
119         void *closure; /**< closure for callbacks */
120 };
121
122 /* synchronization across threads */
123 static pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
124 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
125
126 /* list of hooks for xreq */
127 static struct afb_hook_xreq *list_of_xreq_hooks = NULL;
128
129 /* list of hooks for export */
130 static struct afb_hook_api *list_of_api_hooks = NULL;
131
132 /* list of hooks for evt */
133 static struct afb_hook_evt *list_of_evt_hooks = NULL;
134
135 /* list of hooks for session */
136 static struct afb_hook_session *list_of_session_hooks = NULL;
137
138 /* list of hooks for global */
139 static struct afb_hook_global *list_of_global_hooks = NULL;
140
141 /* hook id */
142 static unsigned next_hookid = 0;
143
144 /******************************************************************************
145  * section: hook id
146  *****************************************************************************/
147 static void init_hookid(struct afb_hookid *hookid)
148 {
149         hookid->id = __atomic_add_fetch(&next_hookid, 1, __ATOMIC_RELAXED);
150         clock_gettime(CLOCK_REALTIME, &hookid->time);
151 }
152
153 /******************************************************************************
154  * section: default callbacks for tracing requests
155  *****************************************************************************/
156
157 static char *_pbuf_(const char *fmt, va_list args, char **palloc, char *sbuf, size_t szsbuf, size_t *outlen)
158 {
159         int rc;
160         va_list cp;
161
162         *palloc = NULL;
163         va_copy(cp, args);
164         rc = vsnprintf(sbuf, szsbuf, fmt, args);
165         if ((size_t)rc >= szsbuf) {
166                 sbuf[szsbuf-1] = 0;
167                 sbuf[szsbuf-2] = sbuf[szsbuf-3] = sbuf[szsbuf-4] = '.';
168                 rc = vasprintf(palloc, fmt, cp);
169                 if (rc >= 0)
170                         sbuf = *palloc;
171         }
172         va_end(cp);
173         if (rc >= 0 && outlen)
174                 *outlen = (size_t)rc;
175         return sbuf;
176 }
177
178 static void _hook_(const char *fmt1, const char *fmt2, va_list arg2, ...)
179 {
180         static const char chars[] = "HOOK: [] \n";
181         char *mem1, *mem2, buf1[256], buf2[2000];
182         struct iovec iov[5];
183         va_list arg1;
184
185         /* "HOOK: [" */
186         iov[0].iov_base = (void*)&chars[0];
187         iov[0].iov_len = 7;
188
189         /* fmt1 ... */
190         va_start(arg1, arg2);
191         iov[1].iov_base = _pbuf_(fmt1, arg1, &mem1, buf1, sizeof buf1, &iov[1].iov_len);
192         va_end(arg1);
193
194         /* "] " */
195         iov[2].iov_base = (void*)&chars[7];
196         iov[2].iov_len = 2;
197
198         /* fmt2 arg2 */
199         iov[3].iov_base = _pbuf_(fmt2, arg2, &mem2, buf2, sizeof buf2, &iov[3].iov_len);
200
201         /* "\n" */
202         iov[4].iov_base = (void*)&chars[9];
203         iov[4].iov_len = 1;
204
205         (void)writev(2, iov, 5);
206
207         free(mem1);
208         free(mem2);
209 }
210
211 static void _hook_xreq_(const struct afb_xreq *xreq, const char *format, ...)
212 {
213         va_list ap;
214         va_start(ap, format);
215         _hook_("xreq-%06d:%s/%s", format, ap, xreq->hookindex, xreq->request.called_api, xreq->request.called_verb);
216         va_end(ap);
217 }
218
219 static void hook_xreq_begin_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
220 {
221         if (!xreq->cred)
222                 _hook_xreq_(xreq, "BEGIN");
223         else
224                 _hook_xreq_(xreq, "BEGIN uid=%d=%s gid=%d pid=%d label=%s id=%s",
225                         (int)xreq->cred->uid,
226                         xreq->cred->user,
227                         (int)xreq->cred->gid,
228                         (int)xreq->cred->pid,
229                         xreq->cred->label?:"(null)",
230                         xreq->cred->id?:"(null)"
231                 );
232 }
233
234 static void hook_xreq_end_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
235 {
236         _hook_xreq_(xreq, "END");
237 }
238
239 static void hook_xreq_json_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *obj)
240 {
241         _hook_xreq_(xreq, "json() -> %s", json_object_to_json_string_ext(obj, JSON_C_TO_STRING_NOSLASHESCAPE));
242 }
243
244 static void hook_xreq_get_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *name, struct afb_arg arg)
245 {
246         _hook_xreq_(xreq, "get(%s) -> { name: %s, value: %s, path: %s }", name, arg.name, arg.value, arg.path);
247 }
248
249 static void hook_xreq_reply_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *obj, const char *error, const char *info)
250 {
251         _hook_xreq_(xreq, "reply[%s](%s, %s)", error?:"success", json_object_to_json_string_ext(obj, JSON_C_TO_STRING_NOSLASHESCAPE), info);
252 }
253
254 static void hook_xreq_legacy_context_get_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, void *value)
255 {
256         _hook_xreq_(xreq, "context_get() -> %p", value);
257 }
258
259 static void hook_xreq_legacy_context_set_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, void *value, void (*free_value)(void*))
260 {
261         _hook_xreq_(xreq, "context_set(%p, %p)", value, free_value);
262 }
263
264 static void hook_xreq_addref_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
265 {
266         _hook_xreq_(xreq, "addref()");
267 }
268
269 static void hook_xreq_unref_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
270 {
271         _hook_xreq_(xreq, "unref()");
272 }
273
274 static void hook_xreq_session_close_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
275 {
276         _hook_xreq_(xreq, "session_close()");
277 }
278
279 static void hook_xreq_session_set_LOA_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, unsigned level, int result)
280 {
281         _hook_xreq_(xreq, "session_set_LOA(%u) -> %d", level, result);
282 }
283
284 static void hook_xreq_subscribe_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_event_x2 *event_x2, int result)
285 {
286         _hook_xreq_(xreq, "subscribe(%s:%d) -> %d", afb_evt_event_x2_fullname(event_x2), afb_evt_event_x2_id(event_x2), result);
287 }
288
289 static void hook_xreq_unsubscribe_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_event_x2 *event_x2, int result)
290 {
291         _hook_xreq_(xreq, "unsubscribe(%s:%d) -> %d", afb_evt_event_x2_fullname(event_x2), afb_evt_event_x2_id(event_x2), result);
292 }
293
294 static void hook_xreq_subcall_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
295 {
296         _hook_xreq_(xreq, "subcall(%s/%s, %s) ...", api, verb, json_object_to_json_string_ext(args, JSON_C_TO_STRING_NOSLASHESCAPE));
297 }
298
299 static void hook_xreq_subcall_result_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *object, const char *error, const char *info)
300 {
301         _hook_xreq_(xreq, "    ...subcall... [%s] -> %s (%s)", error?:"success", json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE), info?:"");
302 }
303
304 static void hook_xreq_subcallsync_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
305 {
306         _hook_xreq_(xreq, "subcallsync(%s/%s, %s) ...", api, verb, json_object_to_json_string_ext(args, JSON_C_TO_STRING_NOSLASHESCAPE));
307 }
308
309 static void hook_xreq_subcallsync_result_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int status, struct json_object *object, const char *error, const char *info)
310 {
311         _hook_xreq_(xreq, "    ...subcallsync... %d [%s] -> %s (%s)", status, error?:"success", json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE), info?:"");
312 }
313
314 static void hook_xreq_vverbose_cb(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)
315 {
316         int len;
317         char *msg;
318         va_list ap;
319
320         va_copy(ap, args);
321         len = vasprintf(&msg, fmt, ap);
322         va_end(ap);
323
324         if (len < 0)
325                 _hook_xreq_(xreq, "vverbose(%d:%s, %s, %d, %s) -> %s ? ? ?", level, verbose_name_of_level(level), file, line, func, fmt);
326         else {
327                 _hook_xreq_(xreq, "vverbose(%d:%s, %s, %d, %s) -> %s", level, verbose_name_of_level(level), file, line, func, msg);
328                 free(msg);
329         }
330 }
331
332 static void hook_xreq_legacy_store_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_stored_req *sreq)
333 {
334         _hook_xreq_(xreq, "store() -> %p", sreq);
335 }
336
337 static void hook_xreq_legacy_unstore_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
338 {
339         _hook_xreq_(xreq, "unstore()");
340 }
341
342 static void hook_xreq_has_permission_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *permission, int result)
343 {
344         _hook_xreq_(xreq, "has_permission(%s) -> %d", permission, result);
345 }
346
347 static void hook_xreq_get_application_id_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, char *result)
348 {
349         _hook_xreq_(xreq, "get_application_id() -> %s", result);
350 }
351
352 static void hook_xreq_context_make_cb(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)
353 {
354         _hook_xreq_(xreq, "context_make(replace=%s, %p, %p, %p) -> %p", replace?"yes":"no", create_value, free_value, create_closure, result);
355 }
356
357 static void hook_xreq_get_uid_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int result)
358 {
359         _hook_xreq_(xreq, "get_uid() -> %d", result);
360 }
361
362 static void hook_xreq_get_client_info_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *result)
363 {
364         _hook_xreq_(xreq, "get_client_info() -> %s", json_object_to_json_string_ext(result, JSON_C_TO_STRING_NOSLASHESCAPE));
365 }
366
367 static struct afb_hook_xreq_itf hook_xreq_default_itf = {
368         .hook_xreq_begin = hook_xreq_begin_cb,
369         .hook_xreq_end = hook_xreq_end_cb,
370         .hook_xreq_json = hook_xreq_json_cb,
371         .hook_xreq_get = hook_xreq_get_cb,
372         .hook_xreq_reply = hook_xreq_reply_cb,
373         .hook_xreq_legacy_context_get = hook_xreq_legacy_context_get_cb,
374         .hook_xreq_legacy_context_set = hook_xreq_legacy_context_set_cb,
375         .hook_xreq_addref = hook_xreq_addref_cb,
376         .hook_xreq_unref = hook_xreq_unref_cb,
377         .hook_xreq_session_close = hook_xreq_session_close_cb,
378         .hook_xreq_session_set_LOA = hook_xreq_session_set_LOA_cb,
379         .hook_xreq_subscribe = hook_xreq_subscribe_cb,
380         .hook_xreq_unsubscribe = hook_xreq_unsubscribe_cb,
381         .hook_xreq_subcall = hook_xreq_subcall_cb,
382         .hook_xreq_subcall_result = hook_xreq_subcall_result_cb,
383         .hook_xreq_subcallsync = hook_xreq_subcallsync_cb,
384         .hook_xreq_subcallsync_result = hook_xreq_subcallsync_result_cb,
385         .hook_xreq_vverbose = hook_xreq_vverbose_cb,
386         .hook_xreq_legacy_store = hook_xreq_legacy_store_cb,
387         .hook_xreq_legacy_unstore = hook_xreq_legacy_unstore_cb,
388         .hook_xreq_has_permission = hook_xreq_has_permission_cb,
389         .hook_xreq_get_application_id = hook_xreq_get_application_id_cb,
390         .hook_xreq_context_make = hook_xreq_context_make_cb,
391         .hook_xreq_get_uid = hook_xreq_get_uid_cb,
392         .hook_xreq_get_client_info = hook_xreq_get_client_info_cb,
393 };
394
395 /******************************************************************************
396  * section: hooks for tracing requests
397  *****************************************************************************/
398
399 #define _HOOK_XREQ_2_(flag,func,...)   \
400         struct afb_hook_xreq *hook; \
401         struct afb_hookid hookid; \
402         pthread_rwlock_rdlock(&rwlock); \
403         init_hookid(&hookid); \
404         hook = list_of_xreq_hooks; \
405         while (hook) { \
406                 if (hook->refcount \
407                  && hook->itf->hook_xreq_##func \
408                  && (hook->flags & afb_hook_flag_req_##flag) != 0 \
409                  && (!hook->session || hook->session == xreq->context.session) \
410                  && MATCH_API(hook->api, xreq->request.called_api) \
411                  && MATCH_VERB(hook->verb, xreq->request.called_verb)) { \
412                         hook->itf->hook_xreq_##func(hook->closure, &hookid, __VA_ARGS__); \
413                 } \
414                 hook = hook->next; \
415         } \
416         pthread_rwlock_unlock(&rwlock);
417
418 #define _HOOK_XREQ_(what,...)   _HOOK_XREQ_2_(what,what,__VA_ARGS__)
419
420 void afb_hook_xreq_begin(const struct afb_xreq *xreq)
421 {
422         _HOOK_XREQ_(begin, xreq);
423 }
424
425 void afb_hook_xreq_end(const struct afb_xreq *xreq)
426 {
427         _HOOK_XREQ_(end, xreq);
428 }
429
430 struct json_object *afb_hook_xreq_json(const struct afb_xreq *xreq, struct json_object *obj)
431 {
432         _HOOK_XREQ_(json, xreq, obj);
433         return obj;
434 }
435
436 struct afb_arg afb_hook_xreq_get(const struct afb_xreq *xreq, const char *name, struct afb_arg arg)
437 {
438         _HOOK_XREQ_(get, xreq, name, arg);
439         return arg;
440 }
441
442 void afb_hook_xreq_reply(const struct afb_xreq *xreq, struct json_object *obj, const char *error, const char *info)
443 {
444         _HOOK_XREQ_(reply, xreq, obj, error, info);
445 }
446
447 void *afb_hook_xreq_legacy_context_get(const struct afb_xreq *xreq, void *value)
448 {
449         _HOOK_XREQ_(legacy_context_get, xreq, value);
450         return value;
451 }
452
453 void afb_hook_xreq_legacy_context_set(const struct afb_xreq *xreq, void *value, void (*free_value)(void*))
454 {
455         _HOOK_XREQ_(legacy_context_set, xreq, value, free_value);
456 }
457
458 void afb_hook_xreq_addref(const struct afb_xreq *xreq)
459 {
460         _HOOK_XREQ_(addref, xreq);
461 }
462
463 void afb_hook_xreq_unref(const struct afb_xreq *xreq)
464 {
465         _HOOK_XREQ_(unref, xreq);
466 }
467
468 void afb_hook_xreq_session_close(const struct afb_xreq *xreq)
469 {
470         _HOOK_XREQ_(session_close, xreq);
471 }
472
473 int afb_hook_xreq_session_set_LOA(const struct afb_xreq *xreq, unsigned level, int result)
474 {
475         _HOOK_XREQ_(session_set_LOA, xreq, level, result);
476         return result;
477 }
478
479 int afb_hook_xreq_subscribe(const struct afb_xreq *xreq, struct afb_event_x2 *event_x2, int result)
480 {
481         _HOOK_XREQ_(subscribe, xreq, event_x2, result);
482         return result;
483 }
484
485 int afb_hook_xreq_unsubscribe(const struct afb_xreq *xreq, struct afb_event_x2 *event_x2, int result)
486 {
487         _HOOK_XREQ_(unsubscribe, xreq, event_x2, result);
488         return result;
489 }
490
491 void afb_hook_xreq_subcall(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, int flags)
492 {
493         _HOOK_XREQ_(subcall, xreq, api, verb, args);
494 }
495
496 void afb_hook_xreq_subcall_result(const struct afb_xreq *xreq, struct json_object *object, const char *error, const char *info)
497 {
498         _HOOK_XREQ_(subcall_result, xreq, object, error, info);
499 }
500
501 void afb_hook_xreq_subcallsync(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, int flags)
502 {
503         _HOOK_XREQ_(subcallsync, xreq, api, verb, args);
504 }
505
506 int  afb_hook_xreq_subcallsync_result(const struct afb_xreq *xreq, int status, struct json_object *object, const char *error, const char *info)
507 {
508         _HOOK_XREQ_(subcallsync_result, xreq, status, object, error, info);
509         return status;
510 }
511
512 void afb_hook_xreq_vverbose(const struct afb_xreq *xreq, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
513 {
514         _HOOK_XREQ_(vverbose, xreq, level, file ?: "?", line, func ?: "?", fmt, args);
515 }
516
517 void afb_hook_xreq_legacy_store(const struct afb_xreq *xreq, struct afb_stored_req *sreq)
518 {
519         _HOOK_XREQ_(legacy_store, xreq, sreq);
520 }
521
522 void afb_hook_xreq_legacy_unstore(const struct afb_xreq *xreq)
523 {
524         _HOOK_XREQ_(legacy_unstore, xreq);
525 }
526
527 int afb_hook_xreq_has_permission(const struct afb_xreq *xreq, const char *permission, int result)
528 {
529         _HOOK_XREQ_(has_permission, xreq, permission, result);
530         return result;
531 }
532
533 char *afb_hook_xreq_get_application_id(const struct afb_xreq *xreq, char *result)
534 {
535         _HOOK_XREQ_(get_application_id, xreq, result);
536         return result;
537 }
538
539 void *afb_hook_xreq_context_make(const struct afb_xreq *xreq, int replace, void *(*create_value)(void*), void (*free_value)(void*), void *create_closure, void *result)
540 {
541         _HOOK_XREQ_(context_make, xreq, replace, create_value, free_value, create_closure, result);
542         return result;
543 }
544
545 int afb_hook_xreq_get_uid(const struct afb_xreq *xreq, int result)
546 {
547         _HOOK_XREQ_(get_uid, xreq, result);
548         return result;
549 }
550
551 struct json_object *afb_hook_xreq_get_client_info(const struct afb_xreq *xreq, struct json_object *result)
552 {
553         _HOOK_XREQ_(get_client_info, xreq, result);
554         return result;
555 }
556
557 /******************************************************************************
558  * section: hooking xreqs
559  *****************************************************************************/
560
561 void afb_hook_init_xreq(struct afb_xreq *xreq)
562 {
563         static int reqindex = 0;
564
565         int f, flags;
566         int add, x;
567         struct afb_hook_xreq *hook;
568
569         /* scan hook list to get the expected flags */
570         flags = 0;
571         pthread_rwlock_rdlock(&rwlock);
572         hook = list_of_xreq_hooks;
573         while (hook) {
574                 f = hook->flags & afb_hook_flags_req_all;
575                 add = f != 0
576                    && (!hook->session || hook->session == xreq->context.session)
577                    && MATCH_API(hook->api, xreq->request.called_api)
578                    && MATCH_VERB(hook->verb, xreq->request.called_verb);
579                 if (add)
580                         flags |= f;
581                 hook = hook->next;
582         }
583         pthread_rwlock_unlock(&rwlock);
584
585         /* store the hooking data */
586         xreq->hookflags = flags;
587         if (flags) {
588                 do {
589                         x = __atomic_load_n(&reqindex, __ATOMIC_RELAXED);
590                         xreq->hookindex = (x + 1) % 1000000 ?: 1;
591                 } while (x != __atomic_exchange_n(&reqindex, xreq->hookindex, __ATOMIC_RELAXED));
592         }
593 }
594
595 struct afb_hook_xreq *afb_hook_create_xreq(const char *api, const char *verb, struct afb_session *session, int flags, struct afb_hook_xreq_itf *itf, void *closure)
596 {
597         struct afb_hook_xreq *hook;
598
599         /* alloc the result */
600         hook = calloc(1, sizeof *hook);
601         if (hook == NULL)
602                 return NULL;
603
604         /* get a copy of the names */
605         hook->api = api ? strdup(api) : NULL;
606         hook->verb = verb ? strdup(verb) : NULL;
607         if ((api && !hook->api) || (verb && !hook->verb)) {
608                 free(hook->api);
609                 free(hook->verb);
610                 free(hook);
611                 return NULL;
612         }
613
614         /* initialise the rest */
615         hook->session = session;
616         if (session)
617                 afb_session_addref(session);
618         hook->refcount = 1;
619         hook->flags = flags;
620         hook->itf = itf ? itf : &hook_xreq_default_itf;
621         hook->closure = closure;
622
623         /* record the hook */
624         pthread_mutex_lock(&mutex);
625         hook->next = list_of_xreq_hooks;
626         list_of_xreq_hooks = hook;
627         pthread_mutex_unlock(&mutex);
628
629         /* returns it */
630         return hook;
631 }
632
633 struct afb_hook_xreq *afb_hook_addref_xreq(struct afb_hook_xreq *hook)
634 {
635         __atomic_add_fetch(&hook->refcount, 1, __ATOMIC_RELAXED);
636         return hook;
637 }
638
639 static void hook_clean_xreq()
640 {
641         struct afb_hook_xreq **prv, *hook, *head;
642
643         if (pthread_rwlock_trywrlock(&rwlock) == 0) {
644                 /* unlink under mutex */
645                 head = NULL;
646                 pthread_mutex_lock(&mutex);
647                 prv = &list_of_xreq_hooks;
648                 while ((hook = *prv)) {
649                         if (hook->refcount)
650                                 prv = &(*prv)->next;
651                         else {
652                                 *prv = hook->next;
653                                 hook->next = head;
654                                 head = hook;
655                         }
656                 }
657                 pthread_mutex_unlock(&mutex);
658                 pthread_rwlock_unlock(&rwlock);
659
660                 /* free found hooks */
661                 while((hook = head)) {
662                         head = hook->next;
663                         free(hook->api);
664                         free(hook->verb);
665                         if (hook->session)
666                                 afb_session_unref(hook->session);
667                         free(hook);
668                 }
669         }
670 }
671
672 void afb_hook_unref_xreq(struct afb_hook_xreq *hook)
673 {
674         if (hook && !__atomic_sub_fetch(&hook->refcount, 1, __ATOMIC_RELAXED))
675                 hook_clean_xreq();
676 }
677
678 /******************************************************************************
679  * section: default callbacks for tracing daemon interface
680  *****************************************************************************/
681
682 static void _hook_api_(const struct afb_export *export, const char *format, ...)
683 {
684         va_list ap;
685         va_start(ap, format);
686         _hook_("api-%s", format, ap, afb_export_apiname(export));
687         va_end(ap);
688 }
689
690 static void hook_api_event_broadcast_before_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, struct json_object *object)
691 {
692         _hook_api_(export, "event_broadcast.before(%s, %s)....", name, json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE));
693 }
694
695 static void hook_api_event_broadcast_after_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, struct json_object *object, int result)
696 {
697         _hook_api_(export, "event_broadcast.after(%s, %s) -> %d", name, json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE), result);
698 }
699
700 static void hook_api_get_event_loop_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_event *result)
701 {
702         _hook_api_(export, "get_event_loop() -> %p", result);
703 }
704
705 static void hook_api_get_user_bus_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_bus *result)
706 {
707         _hook_api_(export, "get_user_bus() -> %p", result);
708 }
709
710 static void hook_api_get_system_bus_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_bus *result)
711 {
712         _hook_api_(export, "get_system_bus() -> %p", result);
713 }
714
715 static void hook_api_vverbose_cb(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)
716 {
717         int len;
718         char *msg;
719         va_list ap;
720
721         va_copy(ap, args);
722         len = vasprintf(&msg, fmt, ap);
723         va_end(ap);
724
725         if (len < 0)
726                 _hook_api_(export, "vverbose(%d:%s, %s, %d, %s) -> %s ? ? ?", level, verbose_name_of_level(level), file, line, function, fmt);
727         else {
728                 _hook_api_(export, "vverbose(%d:%s, %s, %d, %s) -> %s", level, verbose_name_of_level(level), file, line, function, msg);
729                 free(msg);
730         }
731 }
732
733 static void hook_api_event_make_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, struct afb_event_x2 *result)
734 {
735         _hook_api_(export, "event_make(%s) -> %s:%d", name, afb_evt_event_x2_fullname(result), afb_evt_event_x2_id(result));
736 }
737
738 static void hook_api_rootdir_get_fd_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result)
739 {
740         char path[PATH_MAX], proc[100];
741         ssize_t s;
742
743         if (result < 0)
744                 _hook_api_(export, "rootdir_get_fd() -> %d, %m", result);
745         else {
746                 snprintf(proc, sizeof proc, "/proc/self/fd/%d", result);
747                 s = readlink(proc, path, sizeof path);
748                 path[s < 0 ? 0 : s >= sizeof path ? sizeof path - 1 : s] = 0;
749                 _hook_api_(export, "rootdir_get_fd() -> %d = %s", result, path);
750         }
751 }
752
753 static void hook_api_rootdir_open_locale_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *filename, int flags, const char *locale, int result)
754 {
755         char path[PATH_MAX], proc[100];
756         ssize_t s;
757
758         if (!locale)
759                 locale = "(null)";
760         if (result < 0)
761                 _hook_api_(export, "rootdir_open_locale(%s, %d, %s) -> %d, %m", filename, flags, locale, result);
762         else {
763                 snprintf(proc, sizeof proc, "/proc/self/fd/%d", result);
764                 s = readlink(proc, path, sizeof path);
765                 path[s < 0 ? 0 : s >= sizeof path ? sizeof path - 1 : s] = 0;
766                 _hook_api_(export, "rootdir_open_locale(%s, %d, %s) -> %d = %s", filename, flags, locale, result, path);
767         }
768 }
769
770 static void hook_api_queue_job_cb(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)
771 {
772         _hook_api_(export, "queue_job(%p, %p, %p, %d) -> %d", callback, argument, group, timeout, result);
773 }
774
775 static void hook_api_unstore_req_cb(void *closure, const struct afb_hookid *hookid,  const struct afb_export *export, struct afb_stored_req *sreq)
776 {
777         _hook_api_(export, "unstore_req(%p)", sreq);
778 }
779
780 static void hook_api_require_api_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, int initialized)
781 {
782         _hook_api_(export, "require_api(%s, %d)...", name, initialized);
783 }
784
785 static void hook_api_require_api_result_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, int initialized, int result)
786 {
787         _hook_api_(export, "...require_api(%s, %d) -> %d", name, initialized, result);
788 }
789
790 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)
791 {
792         _hook_api_(export, "add_alias(%s -> %s) -> %d", api, alias?:"<null>", result);
793 }
794
795 static void hook_api_start_before_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export)
796 {
797         _hook_api_(export, "start.before");
798 }
799
800 static void hook_api_start_after_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int status)
801 {
802         _hook_api_(export, "start.after -> %d", status);
803 }
804
805 static void hook_api_on_event_before_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *event, int event_x2, struct json_object *object)
806 {
807         _hook_api_(export, "on_event.before(%s, %d, %s)", event, event_x2, json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE));
808 }
809
810 static void hook_api_on_event_after_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *event, int event_x2, struct json_object *object)
811 {
812         _hook_api_(export, "on_event.after(%s, %d, %s)", event, event_x2, json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE));
813 }
814
815 static void hook_api_call_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *api, const char *verb, struct json_object *args)
816 {
817         _hook_api_(export, "call(%s/%s, %s) ...", api, verb, json_object_to_json_string_ext(args, JSON_C_TO_STRING_NOSLASHESCAPE));
818 }
819
820 static void hook_api_call_result_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct json_object *object, const char *error, const char *info)
821 {
822         _hook_api_(export, "    ...call... [%s] -> %s (%s)", error?:"success", json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE), info?:"");
823 }
824
825 static void hook_api_callsync_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *api, const char *verb, struct json_object *args)
826 {
827         _hook_api_(export, "callsync(%s/%s, %s) ...", api, verb, json_object_to_json_string_ext(args, JSON_C_TO_STRING_NOSLASHESCAPE));
828 }
829
830 static void hook_api_callsync_result_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int status, struct json_object *object, const char *error, const char *info)
831 {
832         _hook_api_(export, "    ...callsync... %d [%s] -> %s (%s)", status, error?:"success", json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE), info?:"");
833 }
834
835 static void hook_api_new_api_before_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *api, const char *info, int noconcurrency)
836 {
837         _hook_api_(export, "new_api.before %s (%s)%s ...", api, info?:"", noconcurrency?" no-concurrency" : "");
838 }
839
840 static void hook_api_new_api_after_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *api)
841 {
842         _hook_api_(export, "... new_api.after %s -> %s (%d)", api, result >= 0 ? "OK" : "ERROR", result);
843 }
844
845 static void hook_api_api_set_verbs_v2_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const struct afb_verb_v2 *verbs)
846 {
847         _hook_api_(export, "set_verbs_v2 -> %s (%d)", result >= 0 ? "OK" : "ERROR", result);
848 }
849
850 static void hook_api_api_set_verbs_v3_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const struct afb_verb_v3 *verbs)
851 {
852         _hook_api_(export, "set_verbs_v3 -> %s (%d)", result >= 0 ? "OK" : "ERROR", result);
853 }
854
855 static void hook_api_api_add_verb_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *verb, const char *info, int glob)
856 {
857         _hook_api_(export, "add_verb(%s%s [%s]) -> %s (%d)", verb, glob?" (GLOB)":"", info?:"", result >= 0 ? "OK" : "ERROR", result);
858 }
859
860 static void hook_api_api_del_verb_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *verb)
861 {
862         _hook_api_(export, "del_verb(%s) -> %s (%d)", verb, result >= 0 ? "OK" : "ERROR", result);
863 }
864
865 static void hook_api_api_set_on_event_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result)
866 {
867         _hook_api_(export, "set_on_event -> %s (%d)", result >= 0 ? "OK" : "ERROR", result);
868 }
869
870 static void hook_api_api_set_on_init_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result)
871 {
872         _hook_api_(export, "set_on_init -> %s (%d)", result >= 0 ? "OK" : "ERROR", result);
873 }
874
875 static void hook_api_api_seal_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export)
876 {
877         _hook_api_(export, "seal");
878 }
879
880 static void hook_api_event_handler_add_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *pattern)
881 {
882         _hook_api_(export, "event_handler_add(%s) -> %s (%d)", pattern, result >= 0 ? "OK" : "ERROR", result);
883 }
884
885 static void hook_api_event_handler_del_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *pattern)
886 {
887         _hook_api_(export, "event_handler_del(%s) -> %s (%d)", pattern, result >= 0 ? "OK" : "ERROR", result);
888 }
889
890 static void hook_api_class_provide_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *name)
891 {
892         _hook_api_(export, "class_provide(%s) -> %s (%d)", name, result >= 0 ? "OK" : "ERROR", result);
893 }
894
895 static void hook_api_class_require_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result, const char *name)
896 {
897         _hook_api_(export, "class_require(%s) -> %s (%d)", name, result >= 0 ? "OK" : "ERROR", result);
898 }
899
900 static void hook_api_delete_api_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result)
901 {
902         _hook_api_(export, "delete_api -> %s (%d)", result >= 0 ? "OK" : "ERROR", result);
903 }
904
905 static void hook_api_on_event_handler_before_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *event, int event_x2, struct json_object *object, const char *pattern)
906 {
907         _hook_api_(export, "on_event_handler[%s].before(%s, %d, %s)", pattern, event, event_x2, json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE));
908 }
909
910 static void hook_api_on_event_handler_after_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *event, int event_x2, struct json_object *object, const char *pattern)
911 {
912         _hook_api_(export, "on_event_handler[%s].after(%s, %d, %s)", pattern, event, event_x2, json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE));
913 }
914
915 static void hook_api_settings_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct json_object *object)
916 {
917         _hook_api_(export, "settings -> %s", json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE));
918 }
919
920 static struct afb_hook_api_itf hook_api_default_itf = {
921         .hook_api_event_broadcast_before = hook_api_event_broadcast_before_cb,
922         .hook_api_event_broadcast_after = hook_api_event_broadcast_after_cb,
923         .hook_api_get_event_loop = hook_api_get_event_loop_cb,
924         .hook_api_get_user_bus = hook_api_get_user_bus_cb,
925         .hook_api_get_system_bus = hook_api_get_system_bus_cb,
926         .hook_api_vverbose = hook_api_vverbose_cb,
927         .hook_api_event_make = hook_api_event_make_cb,
928         .hook_api_rootdir_get_fd = hook_api_rootdir_get_fd_cb,
929         .hook_api_rootdir_open_locale = hook_api_rootdir_open_locale_cb,
930         .hook_api_queue_job = hook_api_queue_job_cb,
931         .hook_api_legacy_unstore_req = hook_api_unstore_req_cb,
932         .hook_api_require_api = hook_api_require_api_cb,
933         .hook_api_require_api_result = hook_api_require_api_result_cb,
934         .hook_api_add_alias = hook_api_add_alias_cb,
935         .hook_api_start_before = hook_api_start_before_cb,
936         .hook_api_start_after = hook_api_start_after_cb,
937         .hook_api_on_event_before = hook_api_on_event_before_cb,
938         .hook_api_on_event_after = hook_api_on_event_after_cb,
939         .hook_api_call = hook_api_call_cb,
940         .hook_api_call_result = hook_api_call_result_cb,
941         .hook_api_callsync = hook_api_callsync_cb,
942         .hook_api_callsync_result = hook_api_callsync_result_cb,
943         .hook_api_new_api_before = hook_api_new_api_before_cb,
944         .hook_api_new_api_after = hook_api_new_api_after_cb,
945         .hook_api_api_set_verbs_v2 = hook_api_api_set_verbs_v2_cb,
946         .hook_api_api_set_verbs_v3 = hook_api_api_set_verbs_v3_cb,
947         .hook_api_api_add_verb = hook_api_api_add_verb_cb,
948         .hook_api_api_del_verb = hook_api_api_del_verb_cb,
949         .hook_api_api_set_on_event = hook_api_api_set_on_event_cb,
950         .hook_api_api_set_on_init = hook_api_api_set_on_init_cb,
951         .hook_api_api_seal = hook_api_api_seal_cb,
952         .hook_api_event_handler_add = hook_api_event_handler_add_cb,
953         .hook_api_event_handler_del = hook_api_event_handler_del_cb,
954         .hook_api_class_provide = hook_api_class_provide_cb,
955         .hook_api_class_require = hook_api_class_require_cb,
956         .hook_api_delete_api = hook_api_delete_api_cb,
957         .hook_api_on_event_handler_before = hook_api_on_event_handler_before_cb,
958         .hook_api_on_event_handler_after = hook_api_on_event_handler_after_cb,
959         .hook_api_settings = hook_api_settings_cb,
960 };
961
962 /******************************************************************************
963  * section: hooks for tracing daemon interface (export)
964  *****************************************************************************/
965
966 #define _HOOK_API_2_(flag,func,...)   \
967         struct afb_hook_api *hook; \
968         struct afb_hookid hookid; \
969         const char *apiname = afb_export_apiname(export); \
970         pthread_rwlock_rdlock(&rwlock); \
971         init_hookid(&hookid); \
972         hook = list_of_api_hooks; \
973         while (hook) { \
974                 if (hook->refcount \
975                  && hook->itf->hook_api_##func \
976                  && (hook->flags & afb_hook_flag_api_##flag) != 0 \
977                  && MATCH_API(hook->api, apiname)) { \
978                         hook->itf->hook_api_##func(hook->closure, &hookid, __VA_ARGS__); \
979                 } \
980                 hook = hook->next; \
981         } \
982         pthread_rwlock_unlock(&rwlock);
983
984 #define _HOOK_API_(what,...)   _HOOK_API_2_(what,what,__VA_ARGS__)
985
986 void afb_hook_api_event_broadcast_before(const struct afb_export *export, const char *name, struct json_object *object)
987 {
988         _HOOK_API_2_(event_broadcast, event_broadcast_before, export, name, object);
989 }
990
991 int afb_hook_api_event_broadcast_after(const struct afb_export *export, const char *name, struct json_object *object, int result)
992 {
993         _HOOK_API_2_(event_broadcast, event_broadcast_after, export, name, object, result);
994         return result;
995 }
996
997 struct sd_event *afb_hook_api_get_event_loop(const struct afb_export *export, struct sd_event *result)
998 {
999         _HOOK_API_(get_event_loop, export, result);
1000         return result;
1001 }
1002
1003 struct sd_bus *afb_hook_api_get_user_bus(const struct afb_export *export, struct sd_bus *result)
1004 {
1005         _HOOK_API_(get_user_bus, export, result);
1006         return result;
1007 }
1008
1009 struct sd_bus *afb_hook_api_get_system_bus(const struct afb_export *export, struct sd_bus *result)
1010 {
1011         _HOOK_API_(get_system_bus, export, result);
1012         return result;
1013 }
1014
1015 void afb_hook_api_vverbose(const struct afb_export *export, int level, const char *file, int line, const char *function, const char *fmt, va_list args)
1016 {
1017         _HOOK_API_(vverbose, export, level, file, line, function, fmt, args);
1018 }
1019
1020 struct afb_event_x2 *afb_hook_api_event_make(const struct afb_export *export, const char *name, struct afb_event_x2 *result)
1021 {
1022         _HOOK_API_(event_make, export, name, result);
1023         return result;
1024 }
1025
1026 int afb_hook_api_rootdir_get_fd(const struct afb_export *export, int result)
1027 {
1028         _HOOK_API_(rootdir_get_fd, export, result);
1029         return result;
1030 }
1031
1032 int afb_hook_api_rootdir_open_locale(const struct afb_export *export, const char *filename, int flags, const char *locale, int result)
1033 {
1034         _HOOK_API_(rootdir_open_locale, export, filename, flags, locale, result);
1035         return result;
1036 }
1037
1038 int afb_hook_api_queue_job(const struct afb_export *export, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout, int result)
1039 {
1040         _HOOK_API_(queue_job, export, callback, argument, group, timeout, result);
1041         return result;
1042 }
1043
1044 void afb_hook_api_legacy_unstore_req(const struct afb_export *export, struct afb_stored_req *sreq)
1045 {
1046         _HOOK_API_(legacy_unstore_req, export, sreq);
1047 }
1048
1049 void afb_hook_api_require_api(const struct afb_export *export, const char *name, int initialized)
1050 {
1051         _HOOK_API_(require_api, export, name, initialized);
1052 }
1053
1054 int afb_hook_api_require_api_result(const struct afb_export *export, const char *name, int initialized, int result)
1055 {
1056         _HOOK_API_2_(require_api, require_api_result, export, name, initialized, result);
1057         return result;
1058 }
1059
1060 int afb_hook_api_add_alias(const struct afb_export *export, const char *api, const char *alias, int result)
1061 {
1062         _HOOK_API_(add_alias, export, api, alias, result);
1063         return result;
1064 }
1065
1066 void afb_hook_api_start_before(const struct afb_export *export)
1067 {
1068         _HOOK_API_2_(start, start_before, export);
1069 }
1070
1071 int afb_hook_api_start_after(const struct afb_export *export, int status)
1072 {
1073         _HOOK_API_2_(start, start_after, export, status);
1074         return status;
1075 }
1076
1077 void afb_hook_api_on_event_before(const struct afb_export *export, const char *event, int event_x2, struct json_object *object)
1078 {
1079         _HOOK_API_2_(on_event, on_event_before, export, event, event_x2, object);
1080 }
1081
1082 void afb_hook_api_on_event_after(const struct afb_export *export, const char *event, int event_x2, struct json_object *object)
1083 {
1084         _HOOK_API_2_(on_event, on_event_after, export, event, event_x2, object);
1085 }
1086
1087 void afb_hook_api_call(const struct afb_export *export, const char *api, const char *verb, struct json_object *args)
1088 {
1089         _HOOK_API_(call, export, api, verb, args);
1090 }
1091
1092 void afb_hook_api_call_result(const struct afb_export *export, struct json_object *object, const char*error, const char *info)
1093 {
1094         _HOOK_API_2_(call, call_result, export, object, error, info);
1095
1096 }
1097
1098 void afb_hook_api_callsync(const struct afb_export *export, const char *api, const char *verb, struct json_object *args)
1099 {
1100         _HOOK_API_(callsync, export, api, verb, args);
1101 }
1102
1103 int afb_hook_api_callsync_result(const struct afb_export *export, int status, struct json_object *object, const char *error, const char *info)
1104 {
1105         _HOOK_API_2_(callsync, callsync_result, export, status, object, error, info);
1106         return status;
1107 }
1108
1109 void afb_hook_api_new_api_before(const struct afb_export *export, const char *api, const char *info, int noconcurrency)
1110 {
1111         _HOOK_API_2_(new_api, new_api_before, export, api, info, noconcurrency);
1112 }
1113
1114 int afb_hook_api_new_api_after(const struct afb_export *export, int result, const char *api)
1115 {
1116         _HOOK_API_2_(new_api, new_api_after, export, result, api);
1117         return result;
1118 }
1119
1120 int afb_hook_api_api_set_verbs_v2(const struct afb_export *export, int result, const struct afb_verb_v2 *verbs)
1121 {
1122         _HOOK_API_2_(api_set_verbs, api_set_verbs_v2, export, result, verbs);
1123         return result;
1124 }
1125
1126 int afb_hook_api_api_set_verbs_v3(const struct afb_export *export, int result, const struct afb_verb_v3 *verbs)
1127 {
1128         _HOOK_API_2_(api_set_verbs, api_set_verbs_v3, export, result, verbs);
1129         return result;
1130 }
1131
1132 int afb_hook_api_api_add_verb(const struct afb_export *export, int result, const char *verb, const char *info, int glob)
1133 {
1134         _HOOK_API_(api_add_verb, export, result, verb, info, glob);
1135         return result;
1136 }
1137
1138 int afb_hook_api_api_del_verb(const struct afb_export *export, int result, const char *verb)
1139 {
1140         _HOOK_API_(api_del_verb, export, result, verb);
1141         return result;
1142 }
1143
1144 int afb_hook_api_api_set_on_event(const struct afb_export *export, int result)
1145 {
1146         _HOOK_API_(api_set_on_event, export, result);
1147         return result;
1148 }
1149
1150 int afb_hook_api_api_set_on_init(const struct afb_export *export, int result)
1151 {
1152         _HOOK_API_(api_set_on_init, export, result);
1153         return result;
1154 }
1155
1156 void afb_hook_api_api_seal(const struct afb_export *export)
1157 {
1158         _HOOK_API_(api_seal, export);
1159 }
1160
1161 int afb_hook_api_event_handler_add(const struct afb_export *export, int result, const char *pattern)
1162 {
1163         _HOOK_API_(event_handler_add, export, result, pattern);
1164         return result;
1165 }
1166 int afb_hook_api_event_handler_del(const struct afb_export *export, int result, const char *pattern)
1167 {
1168         _HOOK_API_(event_handler_del, export, result, pattern);
1169         return result;
1170 }
1171 int afb_hook_api_class_provide(const struct afb_export *export, int result, const char *name)
1172 {
1173         _HOOK_API_(class_provide, export, result, name);
1174         return result;
1175 }
1176 int afb_hook_api_class_require(const struct afb_export *export, int result, const char *name)
1177 {
1178         _HOOK_API_(class_require, export, result, name);
1179         return result;
1180 }
1181
1182 int afb_hook_api_delete_api(const struct afb_export *export, int result)
1183 {
1184         _HOOK_API_(delete_api, export, result);
1185         return result;
1186 }
1187
1188 void afb_hook_api_on_event_handler_before(const struct afb_export *export, const char *event, int event_x2, struct json_object *object, const char *pattern)
1189 {
1190         _HOOK_API_2_(on_event_handler, on_event_handler_before, export, event, event_x2, object, pattern);
1191 }
1192
1193 void afb_hook_api_on_event_handler_after(const struct afb_export *export, const char *event, int event_x2, struct json_object *object, const char *pattern)
1194 {
1195         _HOOK_API_2_(on_event_handler, on_event_handler_after, export, event, event_x2, object, pattern);
1196 }
1197
1198 struct json_object *afb_hook_api_settings(const struct afb_export *export, struct json_object *object)
1199 {
1200         _HOOK_API_(settings, export, object);
1201         return object;
1202 }
1203
1204 /******************************************************************************
1205  * section: hooking export
1206  *****************************************************************************/
1207
1208 int afb_hook_flags_api(const char *api)
1209 {
1210         int flags;
1211         struct afb_hook_api *hook;
1212
1213         flags = 0;
1214         pthread_rwlock_rdlock(&rwlock);
1215         hook = list_of_api_hooks;
1216         while (hook) {
1217                 if (!api || MATCH_API(hook->api, api))
1218                         flags |= hook->flags;
1219                 hook = hook->next;
1220         }
1221         pthread_rwlock_unlock(&rwlock);
1222         return flags;
1223 }
1224
1225 struct afb_hook_api *afb_hook_create_api(const char *api, int flags, struct afb_hook_api_itf *itf, void *closure)
1226 {
1227         struct afb_hook_api *hook;
1228
1229         /* alloc the result */
1230         hook = calloc(1, sizeof *hook);
1231         if (hook == NULL)
1232                 return NULL;
1233
1234         /* get a copy of the names */
1235         hook->api = api ? strdup(api) : NULL;
1236         if (api && !hook->api) {
1237                 free(hook);
1238                 return NULL;
1239         }
1240
1241         /* initialise the rest */
1242         hook->refcount = 1;
1243         hook->flags = flags;
1244         hook->itf = itf ? itf : &hook_api_default_itf;
1245         hook->closure = closure;
1246
1247         /* record the hook */
1248         pthread_mutex_lock(&mutex);
1249         hook->next = list_of_api_hooks;
1250         list_of_api_hooks = hook;
1251         pthread_mutex_unlock(&mutex);
1252
1253         /* returns it */
1254         return hook;
1255 }
1256
1257 struct afb_hook_api *afb_hook_addref_api(struct afb_hook_api *hook)
1258 {
1259         __atomic_add_fetch(&hook->refcount, 1, __ATOMIC_RELAXED);
1260         return hook;
1261 }
1262
1263 static void hook_clean_api()
1264 {
1265         struct afb_hook_api **prv, *hook, *head;
1266
1267         if (pthread_rwlock_trywrlock(&rwlock) == 0) {
1268                 /* unlink under mutex */
1269                 head = NULL;
1270                 pthread_mutex_lock(&mutex);
1271                 prv = &list_of_api_hooks;
1272                 while ((hook = *prv)) {
1273                         if (hook->refcount)
1274                                 prv = &(*prv)->next;
1275                         else {
1276                                 *prv = hook->next;
1277                                 hook->next = head;
1278                                 head = hook;
1279                         }
1280                 }
1281                 pthread_mutex_unlock(&mutex);
1282                 pthread_rwlock_unlock(&rwlock);
1283
1284                 /* free found hooks */
1285                 while((hook = head)) {
1286                         head = hook->next;
1287                         free(hook->api);
1288                         free(hook);
1289                 }
1290         }
1291 }
1292
1293 void afb_hook_unref_api(struct afb_hook_api *hook)
1294 {
1295         if (hook && !__atomic_sub_fetch(&hook->refcount, 1, __ATOMIC_RELAXED))
1296                 hook_clean_api();
1297 }
1298
1299 /******************************************************************************
1300  * section: default callbacks for tracing service interface (evt)
1301  *****************************************************************************/
1302
1303 static void _hook_evt_(const char *evt, int id, const char *format, ...)
1304 {
1305         va_list ap;
1306         va_start(ap, format);
1307         _hook_("evt-%s:%d", format, ap, evt, id);
1308         va_end(ap);
1309 }
1310
1311 static void hook_evt_create_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
1312 {
1313         _hook_evt_(evt, id, "create");
1314 }
1315
1316 static void hook_evt_push_before_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj)
1317 {
1318         _hook_evt_(evt, id, "push.before(%s)", json_object_to_json_string_ext(obj, JSON_C_TO_STRING_NOSLASHESCAPE));
1319 }
1320
1321
1322 static void hook_evt_push_after_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj, int result)
1323 {
1324         _hook_evt_(evt, id, "push.after(%s) -> %d", json_object_to_json_string_ext(obj, JSON_C_TO_STRING_NOSLASHESCAPE), result);
1325 }
1326
1327 static void hook_evt_broadcast_before_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj)
1328 {
1329         _hook_evt_(evt, id, "broadcast.before(%s)", json_object_to_json_string_ext(obj, JSON_C_TO_STRING_NOSLASHESCAPE));
1330 }
1331
1332 static void hook_evt_broadcast_after_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj, int result)
1333 {
1334         _hook_evt_(evt, id, "broadcast.after(%s) -> %d", json_object_to_json_string_ext(obj, JSON_C_TO_STRING_NOSLASHESCAPE), result);
1335 }
1336
1337 static void hook_evt_name_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, const char *result)
1338 {
1339         _hook_evt_(evt, id, "name -> %s", result);
1340 }
1341
1342 static void hook_evt_addref_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
1343 {
1344         _hook_evt_(evt, id, "addref");
1345 }
1346
1347 static void hook_evt_unref_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
1348 {
1349         _hook_evt_(evt, id, "unref");
1350 }
1351
1352 static struct afb_hook_evt_itf hook_evt_default_itf = {
1353         .hook_evt_create = hook_evt_create_cb,
1354         .hook_evt_push_before = hook_evt_push_before_cb,
1355         .hook_evt_push_after = hook_evt_push_after_cb,
1356         .hook_evt_broadcast_before = hook_evt_broadcast_before_cb,
1357         .hook_evt_broadcast_after = hook_evt_broadcast_after_cb,
1358         .hook_evt_name = hook_evt_name_cb,
1359         .hook_evt_addref = hook_evt_addref_cb,
1360         .hook_evt_unref = hook_evt_unref_cb
1361 };
1362
1363 /******************************************************************************
1364  * section: hooks for tracing events interface (evt)
1365  *****************************************************************************/
1366
1367 #define _HOOK_EVT_(what,...)   \
1368         struct afb_hook_evt *hook; \
1369         struct afb_hookid hookid; \
1370         pthread_rwlock_rdlock(&rwlock); \
1371         init_hookid(&hookid); \
1372         hook = list_of_evt_hooks; \
1373         while (hook) { \
1374                 if (hook->refcount \
1375                  && hook->itf->hook_evt_##what \
1376                  && (hook->flags & afb_hook_flag_evt_##what) != 0 \
1377                  && MATCH_EVENT(hook->pattern, evt)) { \
1378                         hook->itf->hook_evt_##what(hook->closure, &hookid, __VA_ARGS__); \
1379                 } \
1380                 hook = hook->next; \
1381         } \
1382         pthread_rwlock_unlock(&rwlock);
1383
1384 void afb_hook_evt_create(const char *evt, int id)
1385 {
1386         _HOOK_EVT_(create, evt, id);
1387 }
1388
1389 void afb_hook_evt_push_before(const char *evt, int id, struct json_object *obj)
1390 {
1391         _HOOK_EVT_(push_before, evt, id, obj);
1392 }
1393
1394 int afb_hook_evt_push_after(const char *evt, int id, struct json_object *obj, int result)
1395 {
1396         _HOOK_EVT_(push_after, evt, id, obj, result);
1397         return result;
1398 }
1399
1400 void afb_hook_evt_broadcast_before(const char *evt, int id, struct json_object *obj)
1401 {
1402         _HOOK_EVT_(broadcast_before, evt, id, obj);
1403 }
1404
1405 int afb_hook_evt_broadcast_after(const char *evt, int id, struct json_object *obj, int result)
1406 {
1407         _HOOK_EVT_(broadcast_after, evt, id, obj, result);
1408         return result;
1409 }
1410
1411 void afb_hook_evt_name(const char *evt, int id, const char *result)
1412 {
1413         _HOOK_EVT_(name, evt, id, result);
1414 }
1415
1416 void afb_hook_evt_addref(const char *evt, int id)
1417 {
1418         _HOOK_EVT_(addref, evt, id);
1419 }
1420
1421 void afb_hook_evt_unref(const char *evt, int id)
1422 {
1423         _HOOK_EVT_(unref, evt, id);
1424 }
1425
1426 /******************************************************************************
1427  * section: hooking services (evt)
1428  *****************************************************************************/
1429
1430 int afb_hook_flags_evt(const char *name)
1431 {
1432         int flags;
1433         struct afb_hook_evt *hook;
1434
1435         flags = 0;
1436         pthread_rwlock_rdlock(&rwlock);
1437         hook = list_of_evt_hooks;
1438         while (hook) {
1439                 if (!name || MATCH_EVENT(hook->pattern, name))
1440                         flags |= hook->flags;
1441                 hook = hook->next;
1442         }
1443         pthread_rwlock_unlock(&rwlock);
1444         return flags;
1445 }
1446
1447 struct afb_hook_evt *afb_hook_create_evt(const char *pattern, int flags, struct afb_hook_evt_itf *itf, void *closure)
1448 {
1449         struct afb_hook_evt *hook;
1450
1451         /* alloc the result */
1452         hook = calloc(1, sizeof *hook);
1453         if (hook == NULL)
1454                 return NULL;
1455
1456         /* get a copy of the names */
1457         hook->pattern = pattern ? strdup(pattern) : NULL;
1458         if (pattern && !hook->pattern) {
1459                 free(hook);
1460                 return NULL;
1461         }
1462
1463         /* initialise the rest */
1464         hook->refcount = 1;
1465         hook->flags = flags;
1466         hook->itf = itf ? itf : &hook_evt_default_itf;
1467         hook->closure = closure;
1468
1469         /* record the hook */
1470         pthread_mutex_lock(&mutex);
1471         hook->next = list_of_evt_hooks;
1472         list_of_evt_hooks = hook;
1473         pthread_mutex_unlock(&mutex);
1474
1475         /* returns it */
1476         return hook;
1477 }
1478
1479 struct afb_hook_evt *afb_hook_addref_evt(struct afb_hook_evt *hook)
1480 {
1481         __atomic_add_fetch(&hook->refcount, 1, __ATOMIC_RELAXED);
1482         return hook;
1483 }
1484
1485 static void hook_clean_evt()
1486 {
1487         struct afb_hook_evt **prv, *hook, *head;
1488
1489         if (pthread_rwlock_trywrlock(&rwlock) == 0) {
1490                 /* unlink under mutex */
1491                 head = NULL;
1492                 pthread_mutex_lock(&mutex);
1493                 prv = &list_of_evt_hooks;
1494                 while ((hook = *prv)) {
1495                         if (hook->refcount)
1496                                 prv = &(*prv)->next;
1497                         else {
1498                                 *prv = hook->next;
1499                                 hook->next = head;
1500                                 head = hook;
1501                         }
1502                 }
1503                 pthread_mutex_unlock(&mutex);
1504                 pthread_rwlock_unlock(&rwlock);
1505
1506                 /* free found hooks */
1507                 while((hook = head)) {
1508                         head = hook->next;
1509                         free(hook->pattern);
1510                         free(hook);
1511                 }
1512         }
1513 }
1514
1515 void afb_hook_unref_evt(struct afb_hook_evt *hook)
1516 {
1517         if (hook && !__atomic_sub_fetch(&hook->refcount, 1, __ATOMIC_RELAXED))
1518                 hook_clean_evt();
1519 }
1520
1521 /******************************************************************************
1522  * section: default callbacks for sessions (session)
1523  *****************************************************************************/
1524
1525 static void _hook_session_(struct afb_session *session, const char *format, ...)
1526 {
1527         va_list ap;
1528         va_start(ap, format);
1529         _hook_("session-%s", format, ap, afb_session_uuid(session));
1530         va_end(ap);
1531 }
1532
1533 static void hook_session_create_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1534 {
1535         _hook_session_(session, "create -> token=%s", afb_session_token(session));
1536 }
1537
1538 static void hook_session_close_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1539 {
1540         _hook_session_(session, "close");
1541 }
1542
1543 static void hook_session_destroy_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1544 {
1545         _hook_session_(session, "destroy");
1546 }
1547
1548 static void hook_session_renew_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1549 {
1550         _hook_session_(session, "renew -> token=%s", afb_session_token(session));
1551 }
1552
1553 static void hook_session_addref_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1554 {
1555         _hook_session_(session, "addref");
1556 }
1557
1558 static void hook_session_unref_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session)
1559 {
1560         _hook_session_(session, "unref");
1561 }
1562
1563 static struct afb_hook_session_itf hook_session_default_itf = {
1564         .hook_session_create = hook_session_create_cb,
1565         .hook_session_close = hook_session_close_cb,
1566         .hook_session_destroy = hook_session_destroy_cb,
1567         .hook_session_renew = hook_session_renew_cb,
1568         .hook_session_addref = hook_session_addref_cb,
1569         .hook_session_unref = hook_session_unref_cb
1570 };
1571
1572 /******************************************************************************
1573  * section: hooks for tracing sessions (session)
1574  *****************************************************************************/
1575
1576 #define _HOOK_SESSION_(what,...)   \
1577         struct afb_hook_session *hook; \
1578         struct afb_hookid hookid; \
1579         const char *sessid = 0; \
1580         pthread_rwlock_rdlock(&rwlock); \
1581         init_hookid(&hookid); \
1582         hook = list_of_session_hooks; \
1583         while (hook) { \
1584                 if (hook->refcount \
1585                  && hook->itf->hook_session_##what \
1586                  && (hook->flags & afb_hook_flag_session_##what) != 0 \
1587                  && MATCH_SESSION(hook->pattern, (sessid?:(sessid=afb_session_uuid(session))))) { \
1588                         hook->itf->hook_session_##what(hook->closure, &hookid, __VA_ARGS__); \
1589                 } \
1590                 hook = hook->next; \
1591         } \
1592         pthread_rwlock_unlock(&rwlock);
1593
1594 void afb_hook_session_create(struct afb_session *session)
1595 {
1596         _HOOK_SESSION_(create, session);
1597 }
1598
1599 void afb_hook_session_close(struct afb_session *session)
1600 {
1601         _HOOK_SESSION_(close, session);
1602 }
1603
1604 void afb_hook_session_destroy(struct afb_session *session)
1605 {
1606         _HOOK_SESSION_(destroy, session);
1607 }
1608
1609 void afb_hook_session_renew(struct afb_session *session)
1610 {
1611         _HOOK_SESSION_(renew, session);
1612 }
1613
1614 void afb_hook_session_addref(struct afb_session *session)
1615 {
1616         _HOOK_SESSION_(addref, session);
1617 }
1618
1619 void afb_hook_session_unref(struct afb_session *session)
1620 {
1621         _HOOK_SESSION_(unref, session);
1622 }
1623
1624
1625 /******************************************************************************
1626  * section: hooking sessions (session)
1627  *****************************************************************************/
1628
1629 struct afb_hook_session *afb_hook_create_session(const char *pattern, int flags, struct afb_hook_session_itf *itf, void *closure)
1630 {
1631         struct afb_hook_session *hook;
1632
1633         /* alloc the result */
1634         hook = calloc(1, sizeof *hook);
1635         if (hook == NULL)
1636                 return NULL;
1637
1638         /* get a copy of the names */
1639         hook->pattern = pattern ? strdup(pattern) : NULL;
1640         if (pattern && !hook->pattern) {
1641                 free(hook);
1642                 return NULL;
1643         }
1644
1645         /* initialise the rest */
1646         hook->refcount = 1;
1647         hook->flags = flags;
1648         hook->itf = itf ? itf : &hook_session_default_itf;
1649         hook->closure = closure;
1650
1651         /* record the hook */
1652         pthread_mutex_lock(&mutex);
1653         hook->next = list_of_session_hooks;
1654         list_of_session_hooks = hook;
1655         pthread_mutex_unlock(&mutex);
1656
1657         /* returns it */
1658         return hook;
1659 }
1660
1661 struct afb_hook_session *afb_hook_addref_session(struct afb_hook_session *hook)
1662 {
1663         __atomic_add_fetch(&hook->refcount, 1, __ATOMIC_RELAXED);
1664         return hook;
1665 }
1666
1667 static void hook_clean_session()
1668 {
1669         struct afb_hook_session **prv, *hook, *head;
1670
1671         if (pthread_rwlock_trywrlock(&rwlock) == 0) {
1672                 /* unlink under mutex */
1673                 head = NULL;
1674                 pthread_mutex_lock(&mutex);
1675                 prv = &list_of_session_hooks;
1676                 while ((hook = *prv)) {
1677                         if (hook->refcount)
1678                                 prv = &(*prv)->next;
1679                         else {
1680                                 *prv = hook->next;
1681                                 hook->next = head;
1682                                 head = hook;
1683                         }
1684                 }
1685                 pthread_mutex_unlock(&mutex);
1686                 pthread_rwlock_unlock(&rwlock);
1687
1688                 /* free found hooks */
1689                 while((hook = head)) {
1690                         head = hook->next;
1691                         free(hook->pattern);
1692                         free(hook);
1693                 }
1694         }
1695 }
1696
1697 void afb_hook_unref_session(struct afb_hook_session *hook)
1698 {
1699         if (hook && !__atomic_sub_fetch(&hook->refcount, 1, __ATOMIC_RELAXED))
1700                 hook_clean_session();
1701 }
1702
1703 /******************************************************************************
1704  * section: default callbacks for globals (global)
1705  *****************************************************************************/
1706
1707 static void _hook_global_(const char *format, ...)
1708 {
1709         va_list ap;
1710         va_start(ap, format);
1711         _hook_("global", format, ap);
1712         va_end(ap);
1713 }
1714
1715 static void hook_global_vverbose_cb(void *closure, const struct afb_hookid *hookid, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
1716 {
1717         int len;
1718         char *msg;
1719         va_list ap;
1720
1721         va_copy(ap, args);
1722         len = vasprintf(&msg, fmt, ap);
1723         va_end(ap);
1724
1725         if (len < 0)
1726                 _hook_global_("vverbose(%d:%s, %s, %d, %s) -> %s ? ? ?", level, verbose_name_of_level(level), file, line, func, fmt);
1727         else {
1728                 _hook_global_("vverbose(%d:%s, %s, %d, %s) -> %s", level, verbose_name_of_level(level), file, line, func, msg);
1729                 free(msg);
1730         }
1731 }
1732
1733 static struct afb_hook_global_itf hook_global_default_itf = {
1734         .hook_global_vverbose = hook_global_vverbose_cb
1735 };
1736
1737 /******************************************************************************
1738  * section: hooks for tracing globals (global)
1739  *****************************************************************************/
1740
1741 #define _HOOK_GLOBAL_(what,...)   \
1742         struct afb_hook_global *hook; \
1743         struct afb_hookid hookid; \
1744         pthread_rwlock_rdlock(&rwlock); \
1745         init_hookid(&hookid); \
1746         hook = list_of_global_hooks; \
1747         while (hook) { \
1748                 if (hook->refcount \
1749                  && hook->itf->hook_global_##what \
1750                  && (hook->flags & afb_hook_flag_global_##what) != 0) { \
1751                         hook->itf->hook_global_##what(hook->closure, &hookid, __VA_ARGS__); \
1752                 } \
1753                 hook = hook->next; \
1754         } \
1755         pthread_rwlock_unlock(&rwlock);
1756
1757 static void afb_hook_global_vverbose(int level, const char *file, int line, const char *func, const char *fmt, va_list args)
1758 {
1759         _HOOK_GLOBAL_(vverbose, level, file ?: "?", line, func ?: "?", fmt ?: "", args);
1760 }
1761
1762 /******************************************************************************
1763  * section: hooking globals (global)
1764  *****************************************************************************/
1765
1766 static void update_global()
1767 {
1768         struct afb_hook_global *hook;
1769         int flags = 0;
1770
1771         pthread_rwlock_rdlock(&rwlock);
1772         hook = list_of_global_hooks;
1773         while (hook) {
1774                 if (hook->refcount)
1775                         flags = hook->flags;
1776                 hook = hook->next;
1777         }
1778         verbose_observer = (flags & afb_hook_flag_global_vverbose) ? afb_hook_global_vverbose : NULL;
1779         pthread_rwlock_unlock(&rwlock);
1780 }
1781
1782 struct afb_hook_global *afb_hook_create_global(int flags, struct afb_hook_global_itf *itf, void *closure)
1783 {
1784         struct afb_hook_global *hook;
1785
1786         /* alloc the result */
1787         hook = calloc(1, sizeof *hook);
1788         if (hook == NULL)
1789                 return NULL;
1790
1791         /* initialise the rest */
1792         hook->refcount = 1;
1793         hook->flags = flags;
1794         hook->itf = itf ? itf : &hook_global_default_itf;
1795         hook->closure = closure;
1796
1797         /* record the hook */
1798         pthread_mutex_lock(&mutex);
1799         hook->next = list_of_global_hooks;
1800         list_of_global_hooks = hook;
1801         pthread_mutex_unlock(&mutex);
1802
1803         /* update hooking */
1804         update_global();
1805
1806         /* returns it */
1807         return hook;
1808 }
1809
1810 struct afb_hook_global *afb_hook_addref_global(struct afb_hook_global *hook)
1811 {
1812         __atomic_add_fetch(&hook->refcount, 1, __ATOMIC_RELAXED);
1813         return hook;
1814 }
1815
1816 static void hook_clean_global()
1817 {
1818         struct afb_hook_global **prv, *hook, *head;
1819
1820         if (pthread_rwlock_trywrlock(&rwlock) == 0) {
1821                 /* unlink under mutex */
1822                 head = NULL;
1823                 pthread_mutex_lock(&mutex);
1824                 prv = &list_of_global_hooks;
1825                 while ((hook = *prv)) {
1826                         if (hook->refcount)
1827                                 prv = &(*prv)->next;
1828                         else {
1829                                 *prv = hook->next;
1830                                 hook->next = head;
1831                                 head = hook;
1832                         }
1833                 }
1834                 pthread_mutex_unlock(&mutex);
1835                 pthread_rwlock_unlock(&rwlock);
1836
1837                 /* free found hooks */
1838                 while((hook = head)) {
1839                         head = hook->next;
1840                         free(hook);
1841                 }
1842         }
1843 }
1844
1845 void afb_hook_unref_global(struct afb_hook_global *hook)
1846 {
1847         if (hook && !__atomic_sub_fetch(&hook->refcount, 1, __ATOMIC_RELAXED)) {
1848                 /* update hooking */
1849                 update_global();
1850                 hook_clean_global();
1851         }
1852 }
1853
1854 #endif /* WITH_AFB_HOOK *******************************************************/