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