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