Add the function afb_req_get_uid
[src/app-framework-binder.git] / src / afb-hook.c
1 /*
2  * Copyright (C) 2016, 2017 "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 <fnmatch.h>
27 #include <sys/uio.h>
28
29 #include <json-c/json.h>
30
31 #include <afb/afb-req.h>
32 #include <afb/afb-event.h>
33
34 #include "afb-context.h"
35 #include "afb-hook.h"
36 #include "afb-session.h"
37 #include "afb-cred.h"
38 #include "afb-xreq.h"
39 #include "afb-export.h"
40 #include "afb-evt.h"
41 #include "verbose.h"
42
43 /**
44  * Definition of a hook for xreq
45  */
46 struct afb_hook_xreq {
47         struct afb_hook_xreq *next; /**< next hook */
48         unsigned refcount; /**< reference count */
49         char *api; /**< api hooked or NULL for any */
50         char *verb; /**< verb hooked or NULL for any */
51         struct afb_session *session; /**< session hooked or NULL if any */
52         unsigned flags; /**< hook flags */
53         struct afb_hook_xreq_itf *itf; /**< interface of hook */
54         void *closure; /**< closure for callbacks */
55 };
56
57 /**
58  * Definition of a hook for export
59  */
60 struct afb_hook_ditf {
61         struct afb_hook_ditf *next; /**< next hook */
62         unsigned refcount; /**< reference count */
63         char *api; /**< api hooked or NULL for any */
64         unsigned flags; /**< hook flags */
65         struct afb_hook_ditf_itf *itf; /**< interface of hook */
66         void *closure; /**< closure for callbacks */
67 };
68
69 /**
70  * Definition of a hook for export
71  */
72 struct afb_hook_svc {
73         struct afb_hook_svc *next; /**< next hook */
74         unsigned refcount; /**< reference count */
75         char *api; /**< api hooked or NULL for any */
76         unsigned flags; /**< hook flags */
77         struct afb_hook_svc_itf *itf; /**< interface of hook */
78         void *closure; /**< closure for callbacks */
79 };
80
81 /**
82  * Definition of a hook for evt
83  */
84 struct afb_hook_evt {
85         struct afb_hook_evt *next; /**< next hook */
86         unsigned refcount; /**< reference count */
87         char *pattern; /**< event pattern name hooked or NULL for any */
88         unsigned flags; /**< hook flags */
89         struct afb_hook_evt_itf *itf; /**< interface of hook */
90         void *closure; /**< closure for callbacks */
91 };
92
93 /**
94  * Definition of a hook for global
95  */
96 struct afb_hook_global {
97         struct afb_hook_global *next; /**< next hook */
98         unsigned refcount; /**< reference count */
99         unsigned flags; /**< hook flags */
100         struct afb_hook_global_itf *itf; /**< interface of hook */
101         void *closure; /**< closure for callbacks */
102 };
103
104 /* synchronisation across threads */
105 static pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
106
107 /* list of hooks for xreq */
108 static struct afb_hook_xreq *list_of_xreq_hooks = NULL;
109
110 /* list of hooks for export */
111 static struct afb_hook_ditf *list_of_ditf_hooks = NULL;
112
113 /* list of hooks for export */
114 static struct afb_hook_svc *list_of_svc_hooks = NULL;
115
116 /* list of hooks for evt */
117 static struct afb_hook_evt *list_of_evt_hooks = NULL;
118
119 /* list of hooks for global */
120 static struct afb_hook_global *list_of_global_hooks = NULL;
121
122 /* hook id */
123 static unsigned next_hookid = 0;
124
125 /******************************************************************************
126  * section: hook id
127  *****************************************************************************/
128 static void init_hookid(struct afb_hookid *hookid)
129 {
130         hookid->id = __atomic_add_fetch(&next_hookid, 1, __ATOMIC_RELAXED);
131         clock_gettime(CLOCK_MONOTONIC, &hookid->time);
132 }
133
134 /******************************************************************************
135  * section: default callbacks for tracing requests
136  *****************************************************************************/
137
138 static char *_pbuf_(const char *fmt, va_list args, char **palloc, char *sbuf, size_t szsbuf, size_t *outlen)
139 {
140         int rc;
141         va_list cp;
142
143         *palloc = NULL;
144         va_copy(cp, args);
145         rc = vsnprintf(sbuf, szsbuf, fmt, args);
146         if ((size_t)rc >= szsbuf) {
147                 sbuf[szsbuf-1] = 0;
148                 sbuf[szsbuf-2] = sbuf[szsbuf-3] = sbuf[szsbuf-4] = '.';
149                 rc = vasprintf(palloc, fmt, cp);
150                 if (rc >= 0)
151                         sbuf = *palloc;
152         }
153         va_end(cp);
154         if (rc >= 0 && outlen)
155                 *outlen = (size_t)rc;
156         return sbuf;
157 }
158
159 #if 0 /* old behaviour: use NOTICE */
160 static void _hook_(const char *fmt1, const char *fmt2, va_list arg2, ...)
161 {
162         char *tag, *data, *mem1, *mem2, buf1[256], buf2[2000];
163         va_list arg1;
164
165         data = _pbuf_(fmt2, arg2, &mem2, buf2, sizeof buf2, NULL);
166
167         va_start(arg1, arg2);
168         tag = _pbuf_(fmt1, arg1, &mem1, buf1, sizeof buf1, NULL);
169         va_end(arg1);
170
171         NOTICE("[HOOK %s] %s", tag, data);
172
173         free(mem1);
174         free(mem2);
175 }
176 #else /* new behaviour: emits directly to stderr */
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         iov[0].iov_base = (void*)&chars[0];
185         iov[0].iov_len = 7;
186
187         va_start(arg1, arg2);
188         iov[1].iov_base = _pbuf_(fmt1, arg1, &mem1, buf1, sizeof buf1, &iov[1].iov_len);
189         va_end(arg1);
190
191         iov[2].iov_base = (void*)&chars[7];
192         iov[2].iov_len = 2;
193
194         iov[3].iov_base = _pbuf_(fmt2, arg2, &mem2, buf2, sizeof buf2, &iov[3].iov_len);
195
196         iov[4].iov_base = (void*)&chars[9];
197         iov[4].iov_len = 1;
198
199         writev(2, iov, 5);
200
201         free(mem1);
202         free(mem2);
203 }
204 #endif
205
206 static void _hook_xreq_(const struct afb_xreq *xreq, const char *format, ...)
207 {
208         va_list ap;
209         va_start(ap, format);
210         _hook_("xreq-%06d:%s/%s", format, ap, xreq->hookindex, xreq->request.api, xreq->request.verb);
211         va_end(ap);
212 }
213
214 static void hook_xreq_begin_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
215 {
216         if (!xreq->cred)
217                 _hook_xreq_(xreq, "BEGIN");
218         else
219                 _hook_xreq_(xreq, "BEGIN uid=%d=%s gid=%d pid=%d label=%s id=%s",
220                         (int)xreq->cred->uid,
221                         xreq->cred->user,
222                         (int)xreq->cred->gid,
223                         (int)xreq->cred->pid,
224                         xreq->cred->label?:"(null)",
225                         xreq->cred->id?:"(null)"
226                 );
227 }
228
229 static void hook_xreq_end_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
230 {
231         _hook_xreq_(xreq, "END");
232 }
233
234 static void hook_xreq_json_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *obj)
235 {
236         _hook_xreq_(xreq, "json() -> %s", json_object_to_json_string(obj));
237 }
238
239 static void hook_xreq_get_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *name, struct afb_arg arg)
240 {
241         _hook_xreq_(xreq, "get(%s) -> { name: %s, value: %s, path: %s }", name, arg.name, arg.value, arg.path);
242 }
243
244 static void hook_xreq_success_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *obj, const char *info)
245 {
246         _hook_xreq_(xreq, "success(%s, %s)", json_object_to_json_string(obj), info);
247 }
248
249 static void hook_xreq_fail_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *status, const char *info)
250 {
251         _hook_xreq_(xreq, "fail(%s, %s)", status, info);
252 }
253
254 static void hook_xreq_context_get_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, void *value)
255 {
256         _hook_xreq_(xreq, "context_get() -> %p", value);
257 }
258
259 static void hook_xreq_context_set_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, void *value, void (*free_value)(void*))
260 {
261         _hook_xreq_(xreq, "context_set(%p, %p)", value, free_value);
262 }
263
264 static void hook_xreq_addref_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
265 {
266         _hook_xreq_(xreq, "addref()");
267 }
268
269 static void hook_xreq_unref_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
270 {
271         _hook_xreq_(xreq, "unref()");
272 }
273
274 static void hook_xreq_session_close_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
275 {
276         _hook_xreq_(xreq, "session_close()");
277 }
278
279 static void hook_xreq_session_set_LOA_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, unsigned level, int result)
280 {
281         _hook_xreq_(xreq, "session_set_LOA(%u) -> %d", level, result);
282 }
283
284 static void hook_xreq_subscribe_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_eventid *eventid, int result)
285 {
286         _hook_xreq_(xreq, "subscribe(%s:%d) -> %d", afb_evt_eventid_fullname(eventid), afb_evt_eventid_id(eventid), result);
287 }
288
289 static void hook_xreq_unsubscribe_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_eventid *eventid, int result)
290 {
291         _hook_xreq_(xreq, "unsubscribe(%s:%d) -> %d", afb_evt_eventid_fullname(eventid), afb_evt_eventid_id(eventid), result);
292 }
293
294 static void hook_xreq_subcall_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
295 {
296         _hook_xreq_(xreq, "subcall(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
297 }
298
299 static void hook_xreq_subcall_result_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int status, struct json_object *result)
300 {
301         _hook_xreq_(xreq, "    ...subcall... -> %d: %s", status, json_object_to_json_string(result));
302 }
303
304 static void hook_xreq_subcallsync_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
305 {
306         _hook_xreq_(xreq, "subcallsync(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
307 }
308
309 static void hook_xreq_subcallsync_result_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int status, struct json_object *result)
310 {
311         _hook_xreq_(xreq, "    ...subcallsync... -> %d: %s", status, json_object_to_json_string(result));
312 }
313
314 static void hook_xreq_vverbose_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
315 {
316         int len;
317         char *msg;
318         va_list ap;
319
320         va_copy(ap, args);
321         len = vasprintf(&msg, fmt, ap);
322         va_end(ap);
323
324         if (len < 0)
325                 _hook_xreq_(xreq, "vverbose(%d, %s, %d, %s) -> %s ? ? ?", level, file, line, func, fmt);
326         else {
327                 _hook_xreq_(xreq, "vverbose(%d, %s, %d, %s) -> %s", level, file, line, func, msg);
328                 free(msg);
329         }
330 }
331
332 static void hook_xreq_store_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct afb_stored_req *sreq)
333 {
334         _hook_xreq_(xreq, "store() -> %p", sreq);
335 }
336
337 static void hook_xreq_unstore_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq)
338 {
339         _hook_xreq_(xreq, "unstore()");
340 }
341
342 static void hook_xreq_subcall_req_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
343 {
344         _hook_xreq_(xreq, "subcall_req(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
345 }
346
347 static void hook_xreq_subcall_req_result_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int status, struct json_object *result)
348 {
349         _hook_xreq_(xreq, "    ...subcall_req... -> %d: %s", status, json_object_to_json_string(result));
350 }
351
352 static void hook_xreq_has_permission_default_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_default_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_default_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_default_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 struct afb_hook_xreq_itf hook_xreq_default_itf = {
373         .hook_xreq_begin = hook_xreq_begin_default_cb,
374         .hook_xreq_end = hook_xreq_end_default_cb,
375         .hook_xreq_json = hook_xreq_json_default_cb,
376         .hook_xreq_get = hook_xreq_get_default_cb,
377         .hook_xreq_success = hook_xreq_success_default_cb,
378         .hook_xreq_fail = hook_xreq_fail_default_cb,
379         .hook_xreq_context_get = hook_xreq_context_get_default_cb,
380         .hook_xreq_context_set = hook_xreq_context_set_default_cb,
381         .hook_xreq_addref = hook_xreq_addref_default_cb,
382         .hook_xreq_unref = hook_xreq_unref_default_cb,
383         .hook_xreq_session_close = hook_xreq_session_close_default_cb,
384         .hook_xreq_session_set_LOA = hook_xreq_session_set_LOA_default_cb,
385         .hook_xreq_subscribe = hook_xreq_subscribe_default_cb,
386         .hook_xreq_unsubscribe = hook_xreq_unsubscribe_default_cb,
387         .hook_xreq_subcall = hook_xreq_subcall_default_cb,
388         .hook_xreq_subcall_result = hook_xreq_subcall_result_default_cb,
389         .hook_xreq_subcallsync = hook_xreq_subcallsync_default_cb,
390         .hook_xreq_subcallsync_result = hook_xreq_subcallsync_result_default_cb,
391         .hook_xreq_vverbose = hook_xreq_vverbose_default_cb,
392         .hook_xreq_store = hook_xreq_store_default_cb,
393         .hook_xreq_unstore = hook_xreq_unstore_default_cb,
394         .hook_xreq_subcall_req = hook_xreq_subcall_req_default_cb,
395         .hook_xreq_subcall_req_result = hook_xreq_subcall_req_result_default_cb,
396         .hook_xreq_has_permission = hook_xreq_has_permission_default_cb,
397         .hook_xreq_get_application_id = hook_xreq_get_application_id_default_cb,
398         .hook_xreq_context_make = hook_xreq_context_make_default_cb,
399         .hook_xreq_get_uid = hook_xreq_get_uid_default_cb,
400 };
401
402 /******************************************************************************
403  * section: hooks for tracing requests
404  *****************************************************************************/
405
406 #define _HOOK_XREQ_(what,...)   \
407         struct afb_hook_xreq *hook; \
408         struct afb_hookid hookid; \
409         pthread_rwlock_rdlock(&rwlock); \
410         init_hookid(&hookid); \
411         hook = list_of_xreq_hooks; \
412         while (hook) { \
413                 if (hook->itf->hook_xreq_##what \
414                  && (hook->flags & afb_hook_flag_req_##what) != 0 \
415                  && (!hook->session || hook->session == xreq->context.session) \
416                  && (!hook->api || !strcasecmp(hook->api, xreq->request.api)) \
417                  && (!hook->verb || !strcasecmp(hook->verb, xreq->request.verb))) { \
418                         hook->itf->hook_xreq_##what(hook->closure, &hookid, __VA_ARGS__); \
419                 } \
420                 hook = hook->next; \
421         } \
422         pthread_rwlock_unlock(&rwlock);
423
424
425 void afb_hook_xreq_begin(const struct afb_xreq *xreq)
426 {
427         _HOOK_XREQ_(begin, xreq);
428 }
429
430 void afb_hook_xreq_end(const struct afb_xreq *xreq)
431 {
432         _HOOK_XREQ_(end, xreq);
433 }
434
435 struct json_object *afb_hook_xreq_json(const struct afb_xreq *xreq, struct json_object *obj)
436 {
437         _HOOK_XREQ_(json, xreq, obj);
438         return obj;
439 }
440
441 struct afb_arg afb_hook_xreq_get(const struct afb_xreq *xreq, const char *name, struct afb_arg arg)
442 {
443         _HOOK_XREQ_(get, xreq, name, arg);
444         return arg;
445 }
446
447 void afb_hook_xreq_success(const struct afb_xreq *xreq, struct json_object *obj, const char *info)
448 {
449         _HOOK_XREQ_(success, xreq, obj, info);
450 }
451
452 void afb_hook_xreq_fail(const struct afb_xreq *xreq, const char *status, const char *info)
453 {
454         _HOOK_XREQ_(fail, xreq, status, info);
455 }
456
457 void *afb_hook_xreq_context_get(const struct afb_xreq *xreq, void *value)
458 {
459         _HOOK_XREQ_(context_get, xreq, value);
460         return value;
461 }
462
463 void afb_hook_xreq_context_set(const struct afb_xreq *xreq, void *value, void (*free_value)(void*))
464 {
465         _HOOK_XREQ_(context_set, xreq, value, free_value);
466 }
467
468 void afb_hook_xreq_addref(const struct afb_xreq *xreq)
469 {
470         _HOOK_XREQ_(addref, xreq);
471 }
472
473 void afb_hook_xreq_unref(const struct afb_xreq *xreq)
474 {
475         _HOOK_XREQ_(unref, xreq);
476 }
477
478 void afb_hook_xreq_session_close(const struct afb_xreq *xreq)
479 {
480         _HOOK_XREQ_(session_close, xreq);
481 }
482
483 int afb_hook_xreq_session_set_LOA(const struct afb_xreq *xreq, unsigned level, int result)
484 {
485         _HOOK_XREQ_(session_set_LOA, xreq, level, result);
486         return result;
487 }
488
489 int afb_hook_xreq_subscribe(const struct afb_xreq *xreq, struct afb_eventid *eventid, int result)
490 {
491         _HOOK_XREQ_(subscribe, xreq, eventid, result);
492         return result;
493 }
494
495 int afb_hook_xreq_unsubscribe(const struct afb_xreq *xreq, struct afb_eventid *eventid, int result)
496 {
497         _HOOK_XREQ_(unsubscribe, xreq, eventid, result);
498         return result;
499 }
500
501 void afb_hook_xreq_subcall(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
502 {
503         _HOOK_XREQ_(subcall, xreq, api, verb, args);
504 }
505
506 void afb_hook_xreq_subcall_result(const struct afb_xreq *xreq, int status, struct json_object *result)
507 {
508         _HOOK_XREQ_(subcall_result, xreq, status, result);
509 }
510
511 void afb_hook_xreq_subcallsync(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
512 {
513         _HOOK_XREQ_(subcallsync, xreq, api, verb, args);
514 }
515
516 int afb_hook_xreq_subcallsync_result(const struct afb_xreq *xreq, int status, struct json_object *result)
517 {
518         _HOOK_XREQ_(subcallsync_result, xreq, status, result);
519         return status;
520 }
521
522 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)
523 {
524         _HOOK_XREQ_(vverbose, xreq, level, file ?: "?", line, func ?: "?", fmt, args);
525 }
526
527 void afb_hook_xreq_store(const struct afb_xreq *xreq, struct afb_stored_req *sreq)
528 {
529         _HOOK_XREQ_(store, xreq, sreq);
530 }
531
532 void afb_hook_xreq_unstore(const struct afb_xreq *xreq)
533 {
534         _HOOK_XREQ_(unstore, xreq);
535 }
536
537 void afb_hook_xreq_subcall_req(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
538 {
539         _HOOK_XREQ_(subcall_req, xreq, api, verb, args);
540 }
541
542 void afb_hook_xreq_subcall_req_result(const struct afb_xreq *xreq, int status, struct json_object *result)
543 {
544         _HOOK_XREQ_(subcall_req_result, xreq, status, result);
545 }
546
547 int afb_hook_xreq_has_permission(const struct afb_xreq *xreq, const char *permission, int result)
548 {
549         _HOOK_XREQ_(has_permission, xreq, permission, result);
550         return result;
551 }
552
553 char *afb_hook_xreq_get_application_id(const struct afb_xreq *xreq, char *result)
554 {
555         _HOOK_XREQ_(get_application_id, xreq, result);
556         return result;
557 }
558
559 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)
560 {
561         _HOOK_XREQ_(context_make, xreq, replace, create_value, free_value, create_closure, result);
562         return result;
563 }
564
565 int afb_hook_xreq_get_uid(const struct afb_xreq *xreq, int result)
566 {
567         _HOOK_XREQ_(get_uid, xreq, result);
568         return result;
569 }
570
571 /******************************************************************************
572  * section: hooking xreqs
573  *****************************************************************************/
574
575 void afb_hook_init_xreq(struct afb_xreq *xreq)
576 {
577         static int reqindex;
578
579         int f, flags;
580         int add;
581         struct afb_hook_xreq *hook;
582
583         /* scan hook list to get the expected flags */
584         flags = 0;
585         pthread_rwlock_rdlock(&rwlock);
586         hook = list_of_xreq_hooks;
587         while (hook) {
588                 f = hook->flags & afb_hook_flags_req_all;
589                 add = f != 0
590                    && (!hook->session || hook->session == xreq->context.session)
591                    && (!hook->api || !strcasecmp(hook->api, xreq->request.api))
592                    && (!hook->verb || !strcasecmp(hook->verb, xreq->request.verb));
593                 if (add)
594                         flags |= f;
595                 hook = hook->next;
596         }
597         pthread_rwlock_unlock(&rwlock);
598
599         /* store the hooking data */
600         xreq->hookflags = flags;
601         if (flags) {
602                 pthread_rwlock_wrlock(&rwlock);
603                 if (++reqindex < 0)
604                         reqindex = 1;
605                 xreq->hookindex = reqindex;
606                 pthread_rwlock_unlock(&rwlock);
607         }
608 }
609
610 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)
611 {
612         struct afb_hook_xreq *hook;
613
614         /* alloc the result */
615         hook = calloc(1, sizeof *hook);
616         if (hook == NULL)
617                 return NULL;
618
619         /* get a copy of the names */
620         hook->api = api ? strdup(api) : NULL;
621         hook->verb = verb ? strdup(verb) : NULL;
622         if ((api && !hook->api) || (verb && !hook->verb)) {
623                 free(hook->api);
624                 free(hook->verb);
625                 free(hook);
626                 return NULL;
627         }
628
629         /* initialise the rest */
630         hook->session = session;
631         if (session)
632                 afb_session_addref(session);
633         hook->refcount = 1;
634         hook->flags = flags;
635         hook->itf = itf ? itf : &hook_xreq_default_itf;
636         hook->closure = closure;
637
638         /* record the hook */
639         pthread_rwlock_wrlock(&rwlock);
640         hook->next = list_of_xreq_hooks;
641         list_of_xreq_hooks = hook;
642         pthread_rwlock_unlock(&rwlock);
643
644         /* returns it */
645         return hook;
646 }
647
648 struct afb_hook_xreq *afb_hook_addref_xreq(struct afb_hook_xreq *hook)
649 {
650         pthread_rwlock_wrlock(&rwlock);
651         hook->refcount++;
652         pthread_rwlock_unlock(&rwlock);
653         return hook;
654 }
655
656 void afb_hook_unref_xreq(struct afb_hook_xreq *hook)
657 {
658         struct afb_hook_xreq **prv;
659
660         if (hook) {
661                 pthread_rwlock_wrlock(&rwlock);
662                 if (--hook->refcount)
663                         hook = NULL;
664                 else {
665                         /* unlink */
666                         prv = &list_of_xreq_hooks;
667                         while (*prv && *prv != hook)
668                                 prv = &(*prv)->next;
669                         if(*prv)
670                                 *prv = hook->next;
671                 }
672                 pthread_rwlock_unlock(&rwlock);
673                 if (hook) {
674                         /* free */
675                         free(hook->api);
676                         free(hook->verb);
677                         if (hook->session)
678                                 afb_session_unref(hook->session);
679                         free(hook);
680                 }
681         }
682 }
683
684 /******************************************************************************
685  * section: default callbacks for tracing daemon interface
686  *****************************************************************************/
687
688 static void _hook_ditf_(const struct afb_export *export, const char *format, ...)
689 {
690         va_list ap;
691         va_start(ap, format);
692         _hook_("export-%s", format, ap, afb_export_apiname(export));
693         va_end(ap);
694 }
695
696 static void hook_ditf_event_broadcast_before_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, struct json_object *object)
697 {
698         _hook_ditf_(export, "event_broadcast.before(%s, %s)....", name, json_object_to_json_string(object));
699 }
700
701 static void hook_ditf_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)
702 {
703         _hook_ditf_(export, "event_broadcast.after(%s, %s) -> %d", name, json_object_to_json_string(object), result);
704 }
705
706 static void hook_ditf_get_event_loop_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_event *result)
707 {
708         _hook_ditf_(export, "get_event_loop() -> %p", result);
709 }
710
711 static void hook_ditf_get_user_bus_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_bus *result)
712 {
713         _hook_ditf_(export, "get_user_bus() -> %p", result);
714 }
715
716 static void hook_ditf_get_system_bus_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_bus *result)
717 {
718         _hook_ditf_(export, "get_system_bus() -> %p", result);
719 }
720
721 static void hook_ditf_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)
722 {
723         int len;
724         char *msg;
725         va_list ap;
726
727         va_copy(ap, args);
728         len = vasprintf(&msg, fmt, ap);
729         va_end(ap);
730
731         if (len < 0)
732                 _hook_ditf_(export, "vverbose(%d, %s, %d, %s) -> %s ? ? ?", level, file, line, function, fmt);
733         else {
734                 _hook_ditf_(export, "vverbose(%d, %s, %d, %s) -> %s", level, file, line, function, msg);
735                 free(msg);
736         }
737 }
738
739 static void hook_ditf_event_make_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, struct afb_eventid *result)
740 {
741         _hook_ditf_(export, "event_make(%s) -> %s:%d", name, afb_evt_eventid_fullname(result), afb_evt_eventid_id(result));
742 }
743
744 static void hook_ditf_rootdir_get_fd_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result)
745 {
746         char path[PATH_MAX];
747         if (result < 0)
748                 _hook_ditf_(export, "rootdir_get_fd() -> %d, %m", result);
749         else {
750                 sprintf(path, "/proc/self/fd/%d", result);
751                 readlink(path, path, sizeof path);
752                 _hook_ditf_(export, "rootdir_get_fd() -> %d = %s", result, path);
753         }
754 }
755
756 static void hook_ditf_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)
757 {
758         char path[PATH_MAX];
759         if (!locale)
760                 locale = "(null)";
761         if (result < 0)
762                 _hook_ditf_(export, "rootdir_open_locale(%s, %d, %s) -> %d, %m", filename, flags, locale, result);
763         else {
764                 sprintf(path, "/proc/self/fd/%d", result);
765                 readlink(path, path, sizeof path);
766                 _hook_ditf_(export, "rootdir_open_locale(%s, %d, %s) -> %d = %s", filename, flags, locale, result, path);
767         }
768 }
769
770 static void hook_ditf_queue_job_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout, int result)
771 {
772         _hook_ditf_(export, "queue_job(%p, %p, %p, %d) -> %d", callback, argument, group, timeout, result);
773 }
774
775 static void hook_ditf_unstore_req_cb(void *closure, const struct afb_hookid *hookid,  const struct afb_export *export, struct afb_stored_req *sreq)
776 {
777         _hook_ditf_(export, "unstore_req(%p)", sreq);
778 }
779
780 static void hook_ditf_require_api_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, int initialized)
781 {
782         _hook_ditf_(export, "require_api(%s, %d)...", name, initialized);
783 }
784
785 static void hook_ditf_require_api_result_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, int initialized, int result)
786 {
787         _hook_ditf_(export, "...require_api(%s, %d) -> %d", name, initialized, result);
788 }
789
790 static void hook_ditf_rename_api_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *oldname, const char *newname, int result)
791 {
792         _hook_ditf_(export, "rename_api(%s -> %s) -> %d", oldname, newname, result);
793 }
794
795 static struct afb_hook_ditf_itf hook_ditf_default_itf = {
796         .hook_ditf_event_broadcast_before = hook_ditf_event_broadcast_before_cb,
797         .hook_ditf_event_broadcast_after = hook_ditf_event_broadcast_after_cb,
798         .hook_ditf_get_event_loop = hook_ditf_get_event_loop_cb,
799         .hook_ditf_get_user_bus = hook_ditf_get_user_bus_cb,
800         .hook_ditf_get_system_bus = hook_ditf_get_system_bus_cb,
801         .hook_ditf_vverbose = hook_ditf_vverbose_cb,
802         .hook_ditf_event_make = hook_ditf_event_make_cb,
803         .hook_ditf_rootdir_get_fd = hook_ditf_rootdir_get_fd_cb,
804         .hook_ditf_rootdir_open_locale = hook_ditf_rootdir_open_locale_cb,
805         .hook_ditf_queue_job = hook_ditf_queue_job_cb,
806         .hook_ditf_unstore_req = hook_ditf_unstore_req_cb,
807         .hook_ditf_require_api = hook_ditf_require_api_cb,
808         .hook_ditf_require_api_result = hook_ditf_require_api_result_cb,
809         .hook_ditf_rename_api = hook_ditf_rename_api_cb
810 };
811
812 /******************************************************************************
813  * section: hooks for tracing daemon interface (export)
814  *****************************************************************************/
815
816 #define _HOOK_DITF_(what,...)   \
817         struct afb_hook_ditf *hook; \
818         struct afb_hookid hookid; \
819         const char *apiname = afb_export_apiname(export); \
820         pthread_rwlock_rdlock(&rwlock); \
821         init_hookid(&hookid); \
822         hook = list_of_ditf_hooks; \
823         while (hook) { \
824                 if (hook->itf->hook_ditf_##what \
825                  && (hook->flags & afb_hook_flag_ditf_##what) != 0 \
826                  && (!hook->api || !strcasecmp(hook->api, apiname))) { \
827                         hook->itf->hook_ditf_##what(hook->closure, &hookid, __VA_ARGS__); \
828                 } \
829                 hook = hook->next; \
830         } \
831         pthread_rwlock_unlock(&rwlock);
832
833 void afb_hook_ditf_event_broadcast_before(const struct afb_export *export, const char *name, struct json_object *object)
834 {
835         _HOOK_DITF_(event_broadcast_before, export, name, object);
836 }
837
838 int afb_hook_ditf_event_broadcast_after(const struct afb_export *export, const char *name, struct json_object *object, int result)
839 {
840         _HOOK_DITF_(event_broadcast_after, export, name, object, result);
841         return result;
842 }
843
844 struct sd_event *afb_hook_ditf_get_event_loop(const struct afb_export *export, struct sd_event *result)
845 {
846         _HOOK_DITF_(get_event_loop, export, result);
847         return result;
848 }
849
850 struct sd_bus *afb_hook_ditf_get_user_bus(const struct afb_export *export, struct sd_bus *result)
851 {
852         _HOOK_DITF_(get_user_bus, export, result);
853         return result;
854 }
855
856 struct sd_bus *afb_hook_ditf_get_system_bus(const struct afb_export *export, struct sd_bus *result)
857 {
858         _HOOK_DITF_(get_system_bus, export, result);
859         return result;
860 }
861
862 void afb_hook_ditf_vverbose(const struct afb_export *export, int level, const char *file, int line, const char *function, const char *fmt, va_list args)
863 {
864         _HOOK_DITF_(vverbose, export, level, file, line, function, fmt, args);
865 }
866
867 struct afb_eventid *afb_hook_ditf_event_make(const struct afb_export *export, const char *name, struct afb_eventid *result)
868 {
869         _HOOK_DITF_(event_make, export, name, result);
870         return result;
871 }
872
873 int afb_hook_ditf_rootdir_get_fd(const struct afb_export *export, int result)
874 {
875         _HOOK_DITF_(rootdir_get_fd, export, result);
876         return result;
877 }
878
879 int afb_hook_ditf_rootdir_open_locale(const struct afb_export *export, const char *filename, int flags, const char *locale, int result)
880 {
881         _HOOK_DITF_(rootdir_open_locale, export, filename, flags, locale, result);
882         return result;
883 }
884
885 int afb_hook_ditf_queue_job(const struct afb_export *export, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout, int result)
886 {
887         _HOOK_DITF_(queue_job, export, callback, argument, group, timeout, result);
888         return result;
889 }
890
891 void afb_hook_ditf_unstore_req(const struct afb_export *export, struct afb_stored_req *sreq)
892 {
893         _HOOK_DITF_(unstore_req, export, sreq);
894 }
895
896 void afb_hook_ditf_require_api(const struct afb_export *export, const char *name, int initialized)
897 {
898         _HOOK_DITF_(require_api, export, name, initialized);
899 }
900
901 int afb_hook_ditf_require_api_result(const struct afb_export *export, const char *name, int initialized, int result)
902 {
903         _HOOK_DITF_(require_api_result, export, name, initialized, result);
904         return result;
905 }
906
907 int afb_hook_ditf_rename_api(const struct afb_export *export, const char *oldname, const char *newname, int result)
908 {
909         _HOOK_DITF_(rename_api, export, oldname, newname, result);
910         return result;
911 }
912
913 /******************************************************************************
914  * section: hooking export
915  *****************************************************************************/
916
917 int afb_hook_flags_ditf(const char *api)
918 {
919         int flags;
920         struct afb_hook_ditf *hook;
921
922         pthread_rwlock_rdlock(&rwlock);
923         flags = 0;
924         hook = list_of_ditf_hooks;
925         while (hook) {
926                 if (!api || !hook->api || !strcasecmp(hook->api, api))
927                         flags |= hook->flags;
928                 hook = hook->next;
929         }
930         pthread_rwlock_unlock(&rwlock);
931         return flags;
932 }
933
934 struct afb_hook_ditf *afb_hook_create_ditf(const char *api, int flags, struct afb_hook_ditf_itf *itf, void *closure)
935 {
936         struct afb_hook_ditf *hook;
937
938         /* alloc the result */
939         hook = calloc(1, sizeof *hook);
940         if (hook == NULL)
941                 return NULL;
942
943         /* get a copy of the names */
944         hook->api = api ? strdup(api) : NULL;
945         if (api && !hook->api) {
946                 free(hook);
947                 return NULL;
948         }
949
950         /* initialise the rest */
951         hook->refcount = 1;
952         hook->flags = flags;
953         hook->itf = itf ? itf : &hook_ditf_default_itf;
954         hook->closure = closure;
955
956         /* record the hook */
957         pthread_rwlock_wrlock(&rwlock);
958         hook->next = list_of_ditf_hooks;
959         list_of_ditf_hooks = hook;
960         pthread_rwlock_unlock(&rwlock);
961
962         /* returns it */
963         return hook;
964 }
965
966 struct afb_hook_ditf *afb_hook_addref_ditf(struct afb_hook_ditf *hook)
967 {
968         pthread_rwlock_wrlock(&rwlock);
969         hook->refcount++;
970         pthread_rwlock_unlock(&rwlock);
971         return hook;
972 }
973
974 void afb_hook_unref_ditf(struct afb_hook_ditf *hook)
975 {
976         struct afb_hook_ditf **prv;
977
978         if (hook) {
979                 pthread_rwlock_wrlock(&rwlock);
980                 if (--hook->refcount)
981                         hook = NULL;
982                 else {
983                         /* unlink */
984                         prv = &list_of_ditf_hooks;
985                         while (*prv && *prv != hook)
986                                 prv = &(*prv)->next;
987                         if(*prv)
988                                 *prv = hook->next;
989                 }
990                 pthread_rwlock_unlock(&rwlock);
991                 if (hook) {
992                         /* free */
993                         free(hook->api);
994                         free(hook);
995                 }
996         }
997 }
998
999 /******************************************************************************
1000  * section: default callbacks for tracing service interface (export)
1001  *****************************************************************************/
1002
1003 static void _hook_svc_(const struct afb_export *export, const char *format, ...)
1004 {
1005         va_list ap;
1006         va_start(ap, format);
1007         _hook_("export-%s", format, ap, afb_export_apiname(export));
1008         va_end(ap);
1009 }
1010
1011 static void hook_svc_start_before_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export)
1012 {
1013         _hook_svc_(export, "start.before");
1014 }
1015
1016 static void hook_svc_start_after_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int status)
1017 {
1018         _hook_svc_(export, "start.after -> %d", status);
1019 }
1020
1021 static void hook_svc_on_event_before_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *event, int eventid, struct json_object *object)
1022 {
1023         _hook_svc_(export, "on_event.before(%s, %d, %s)", event, eventid, json_object_to_json_string(object));
1024 }
1025
1026 static void hook_svc_on_event_after_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *event, int eventid, struct json_object *object)
1027 {
1028         _hook_svc_(export, "on_event.after(%s, %d, %s)", event, eventid, json_object_to_json_string(object));
1029 }
1030
1031 static void hook_svc_call_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *api, const char *verb, struct json_object *args)
1032 {
1033         _hook_svc_(export, "call(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
1034 }
1035
1036 static void hook_svc_call_result_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int status, struct json_object *result)
1037 {
1038         _hook_svc_(export, "    ...call... -> %d: %s", status, json_object_to_json_string(result));
1039 }
1040
1041 static void hook_svc_callsync_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *api, const char *verb, struct json_object *args)
1042 {
1043         _hook_svc_(export, "callsync(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
1044 }
1045
1046 static void hook_svc_callsync_result_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int status, struct json_object *result)
1047 {
1048         _hook_svc_(export, "    ...callsync... -> %d: %s", status, json_object_to_json_string(result));
1049 }
1050
1051 static struct afb_hook_svc_itf hook_svc_default_itf = {
1052         .hook_svc_start_before = hook_svc_start_before_default_cb,
1053         .hook_svc_start_after = hook_svc_start_after_default_cb,
1054         .hook_svc_on_event_before = hook_svc_on_event_before_default_cb,
1055         .hook_svc_on_event_after = hook_svc_on_event_after_default_cb,
1056         .hook_svc_call = hook_svc_call_default_cb,
1057         .hook_svc_call_result = hook_svc_call_result_default_cb,
1058         .hook_svc_callsync = hook_svc_callsync_default_cb,
1059         .hook_svc_callsync_result = hook_svc_callsync_result_default_cb
1060 };
1061
1062 /******************************************************************************
1063  * section: hooks for tracing service interface (export)
1064  *****************************************************************************/
1065
1066 #define _HOOK_SVC_(what,...)   \
1067         struct afb_hook_svc *hook; \
1068         struct afb_hookid hookid; \
1069         const char *apiname = afb_export_apiname(export); \
1070         pthread_rwlock_rdlock(&rwlock); \
1071         init_hookid(&hookid); \
1072         hook = list_of_svc_hooks; \
1073         while (hook) { \
1074                 if (hook->itf->hook_svc_##what \
1075                  && (hook->flags & afb_hook_flag_svc_##what) != 0 \
1076                  && (!hook->api || !strcasecmp(hook->api, apiname))) { \
1077                         hook->itf->hook_svc_##what(hook->closure, &hookid, __VA_ARGS__); \
1078                 } \
1079                 hook = hook->next; \
1080         } \
1081         pthread_rwlock_unlock(&rwlock);
1082
1083 void afb_hook_svc_start_before(const struct afb_export *export)
1084 {
1085         _HOOK_SVC_(start_before, export);
1086 }
1087
1088 int afb_hook_svc_start_after(const struct afb_export *export, int status)
1089 {
1090         _HOOK_SVC_(start_after, export, status);
1091         return status;
1092 }
1093
1094 void afb_hook_svc_on_event_before(const struct afb_export *export, const char *event, int eventid, struct json_object *object)
1095 {
1096         _HOOK_SVC_(on_event_before, export, event, eventid, object);
1097 }
1098
1099 void afb_hook_svc_on_event_after(const struct afb_export *export, const char *event, int eventid, struct json_object *object)
1100 {
1101         _HOOK_SVC_(on_event_after, export, event, eventid, object);
1102 }
1103
1104 void afb_hook_svc_call(const struct afb_export *export, const char *api, const char *verb, struct json_object *args)
1105 {
1106         _HOOK_SVC_(call, export, api, verb, args);
1107 }
1108
1109 void afb_hook_svc_call_result(const struct afb_export *export, int status, struct json_object *result)
1110 {
1111         _HOOK_SVC_(call_result, export, status, result);
1112 }
1113
1114 void afb_hook_svc_callsync(const struct afb_export *export, const char *api, const char *verb, struct json_object *args)
1115 {
1116         _HOOK_SVC_(callsync, export, api, verb, args);
1117 }
1118
1119 int afb_hook_svc_callsync_result(const struct afb_export *export, int status, struct json_object *result)
1120 {
1121         _HOOK_SVC_(callsync_result, export, status, result);
1122         return status;
1123 }
1124
1125 /******************************************************************************
1126  * section: hooking services (export)
1127  *****************************************************************************/
1128
1129 int afb_hook_flags_svc(const char *api)
1130 {
1131         int flags;
1132         struct afb_hook_svc *hook;
1133
1134         pthread_rwlock_rdlock(&rwlock);
1135         flags = 0;
1136         hook = list_of_svc_hooks;
1137         while (hook) {
1138                 if (!api || !hook->api || !strcasecmp(hook->api, api))
1139                         flags |= hook->flags;
1140                 hook = hook->next;
1141         }
1142         pthread_rwlock_unlock(&rwlock);
1143         return flags;
1144 }
1145
1146 struct afb_hook_svc *afb_hook_create_svc(const char *api, int flags, struct afb_hook_svc_itf *itf, void *closure)
1147 {
1148         struct afb_hook_svc *hook;
1149
1150         /* alloc the result */
1151         hook = calloc(1, sizeof *hook);
1152         if (hook == NULL)
1153                 return NULL;
1154
1155         /* get a copy of the names */
1156         hook->api = api ? strdup(api) : NULL;
1157         if (api && !hook->api) {
1158                 free(hook);
1159                 return NULL;
1160         }
1161
1162         /* initialise the rest */
1163         hook->refcount = 1;
1164         hook->flags = flags;
1165         hook->itf = itf ? itf : &hook_svc_default_itf;
1166         hook->closure = closure;
1167
1168         /* record the hook */
1169         pthread_rwlock_wrlock(&rwlock);
1170         hook->next = list_of_svc_hooks;
1171         list_of_svc_hooks = hook;
1172         pthread_rwlock_unlock(&rwlock);
1173
1174         /* returns it */
1175         return hook;
1176 }
1177
1178 struct afb_hook_svc *afb_hook_addref_svc(struct afb_hook_svc *hook)
1179 {
1180         pthread_rwlock_wrlock(&rwlock);
1181         hook->refcount++;
1182         pthread_rwlock_unlock(&rwlock);
1183         return hook;
1184 }
1185
1186 void afb_hook_unref_svc(struct afb_hook_svc *hook)
1187 {
1188         struct afb_hook_svc **prv;
1189
1190         if (hook) {
1191                 pthread_rwlock_wrlock(&rwlock);
1192                 if (--hook->refcount)
1193                         hook = NULL;
1194                 else {
1195                         /* unlink */
1196                         prv = &list_of_svc_hooks;
1197                         while (*prv && *prv != hook)
1198                                 prv = &(*prv)->next;
1199                         if(*prv)
1200                                 *prv = hook->next;
1201                 }
1202                 pthread_rwlock_unlock(&rwlock);
1203                 if (hook) {
1204                         /* free */
1205                         free(hook->api);
1206                         free(hook);
1207                 }
1208         }
1209 }
1210
1211 /******************************************************************************
1212  * section: default callbacks for tracing service interface (evt)
1213  *****************************************************************************/
1214
1215 static void _hook_evt_(const char *evt, int id, const char *format, ...)
1216 {
1217         va_list ap;
1218         va_start(ap, format);
1219         _hook_("evt-%s:%d", format, ap, evt, id);
1220         va_end(ap);
1221 }
1222
1223 static void hook_evt_create_default_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
1224 {
1225         _hook_evt_(evt, id, "create");
1226 }
1227
1228 static void hook_evt_push_before_default_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj)
1229 {
1230         _hook_evt_(evt, id, "push.before(%s)", json_object_to_json_string(obj));
1231 }
1232
1233
1234 static void hook_evt_push_after_default_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj, int result)
1235 {
1236         _hook_evt_(evt, id, "push.after(%s) -> %d", json_object_to_json_string(obj), result);
1237 }
1238
1239 static void hook_evt_broadcast_before_default_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj)
1240 {
1241         _hook_evt_(evt, id, "broadcast.before(%s)", json_object_to_json_string(obj));
1242 }
1243
1244 static void hook_evt_broadcast_after_default_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj, int result)
1245 {
1246         _hook_evt_(evt, id, "broadcast.after(%s) -> %d", json_object_to_json_string(obj), result);
1247 }
1248
1249 static void hook_evt_name_default_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, const char *result)
1250 {
1251         _hook_evt_(evt, id, "name -> %s", result);
1252 }
1253
1254 static void hook_evt_addref_default_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
1255 {
1256         _hook_evt_(evt, id, "addref");
1257 }
1258
1259 static void hook_evt_unref_default_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id)
1260 {
1261         _hook_evt_(evt, id, "unref");
1262 }
1263
1264 static struct afb_hook_evt_itf hook_evt_default_itf = {
1265         .hook_evt_create = hook_evt_create_default_cb,
1266         .hook_evt_push_before = hook_evt_push_before_default_cb,
1267         .hook_evt_push_after = hook_evt_push_after_default_cb,
1268         .hook_evt_broadcast_before = hook_evt_broadcast_before_default_cb,
1269         .hook_evt_broadcast_after = hook_evt_broadcast_after_default_cb,
1270         .hook_evt_name = hook_evt_name_default_cb,
1271         .hook_evt_addref = hook_evt_addref_default_cb,
1272         .hook_evt_unref = hook_evt_unref_default_cb
1273 };
1274
1275 /******************************************************************************
1276  * section: hooks for tracing events interface (evt)
1277  *****************************************************************************/
1278
1279 #define _HOOK_EVT_(what,...)   \
1280         struct afb_hook_evt *hook; \
1281         struct afb_hookid hookid; \
1282         pthread_rwlock_rdlock(&rwlock); \
1283         init_hookid(&hookid); \
1284         hook = list_of_evt_hooks; \
1285         while (hook) { \
1286                 if (hook->itf->hook_evt_##what \
1287                  && (hook->flags & afb_hook_flag_evt_##what) != 0 \
1288                  && (!hook->pattern || !fnmatch(hook->pattern, evt, FNM_CASEFOLD))) { \
1289                         hook->itf->hook_evt_##what(hook->closure, &hookid, __VA_ARGS__); \
1290                 } \
1291                 hook = hook->next; \
1292         } \
1293         pthread_rwlock_unlock(&rwlock);
1294
1295 void afb_hook_evt_create(const char *evt, int id)
1296 {
1297         _HOOK_EVT_(create, evt, id);
1298 }
1299
1300 void afb_hook_evt_push_before(const char *evt, int id, struct json_object *obj)
1301 {
1302         _HOOK_EVT_(push_before, evt, id, obj);
1303 }
1304
1305 int afb_hook_evt_push_after(const char *evt, int id, struct json_object *obj, int result)
1306 {
1307         _HOOK_EVT_(push_after, evt, id, obj, result);
1308         return result;
1309 }
1310
1311 void afb_hook_evt_broadcast_before(const char *evt, int id, struct json_object *obj)
1312 {
1313         _HOOK_EVT_(broadcast_before, evt, id, obj);
1314 }
1315
1316 int afb_hook_evt_broadcast_after(const char *evt, int id, struct json_object *obj, int result)
1317 {
1318         _HOOK_EVT_(broadcast_after, evt, id, obj, result);
1319         return result;
1320 }
1321
1322 void afb_hook_evt_name(const char *evt, int id, const char *result)
1323 {
1324         _HOOK_EVT_(name, evt, id, result);
1325 }
1326
1327 void afb_hook_evt_addref(const char *evt, int id)
1328 {
1329         _HOOK_EVT_(addref, evt, id);
1330 }
1331
1332 void afb_hook_evt_unref(const char *evt, int id)
1333 {
1334         _HOOK_EVT_(unref, evt, id);
1335 }
1336
1337 /******************************************************************************
1338  * section: hooking services (evt)
1339  *****************************************************************************/
1340
1341 int afb_hook_flags_evt(const char *name)
1342 {
1343         int flags;
1344         struct afb_hook_evt *hook;
1345
1346         pthread_rwlock_rdlock(&rwlock);
1347         flags = 0;
1348         hook = list_of_evt_hooks;
1349         while (hook) {
1350                 if (!name || !hook->pattern || !fnmatch(hook->pattern, name, FNM_CASEFOLD))
1351                         flags |= hook->flags;
1352                 hook = hook->next;
1353         }
1354         pthread_rwlock_unlock(&rwlock);
1355         return flags;
1356 }
1357
1358 struct afb_hook_evt *afb_hook_create_evt(const char *pattern, int flags, struct afb_hook_evt_itf *itf, void *closure)
1359 {
1360         struct afb_hook_evt *hook;
1361
1362         /* alloc the result */
1363         hook = calloc(1, sizeof *hook);
1364         if (hook == NULL)
1365                 return NULL;
1366
1367         /* get a copy of the names */
1368         hook->pattern = pattern ? strdup(pattern) : NULL;
1369         if (pattern && !hook->pattern) {
1370                 free(hook);
1371                 return NULL;
1372         }
1373
1374         /* initialise the rest */
1375         hook->refcount = 1;
1376         hook->flags = flags;
1377         hook->itf = itf ? itf : &hook_evt_default_itf;
1378         hook->closure = closure;
1379
1380         /* record the hook */
1381         pthread_rwlock_wrlock(&rwlock);
1382         hook->next = list_of_evt_hooks;
1383         list_of_evt_hooks = hook;
1384         pthread_rwlock_unlock(&rwlock);
1385
1386         /* returns it */
1387         return hook;
1388 }
1389
1390 struct afb_hook_evt *afb_hook_addref_evt(struct afb_hook_evt *hook)
1391 {
1392         pthread_rwlock_wrlock(&rwlock);
1393         hook->refcount++;
1394         pthread_rwlock_unlock(&rwlock);
1395         return hook;
1396 }
1397
1398 void afb_hook_unref_evt(struct afb_hook_evt *hook)
1399 {
1400         struct afb_hook_evt **prv;
1401
1402         if (hook) {
1403                 pthread_rwlock_wrlock(&rwlock);
1404                 if (--hook->refcount)
1405                         hook = NULL;
1406                 else {
1407                         /* unlink */
1408                         prv = &list_of_evt_hooks;
1409                         while (*prv && *prv != hook)
1410                                 prv = &(*prv)->next;
1411                         if(*prv)
1412                                 *prv = hook->next;
1413                 }
1414                 pthread_rwlock_unlock(&rwlock);
1415                 if (hook) {
1416                         /* free */
1417                         free(hook->pattern);
1418                         free(hook);
1419                 }
1420         }
1421 }
1422
1423 /******************************************************************************
1424  * section: default callbacks for globals (global)
1425  *****************************************************************************/
1426
1427 static void _hook_global_(const char *format, ...)
1428 {
1429         va_list ap;
1430         va_start(ap, format);
1431         _hook_("global", format, ap);
1432         va_end(ap);
1433 }
1434
1435 static void hook_global_vverbose_default_cb(void *closure, const struct afb_hookid *hookid, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
1436 {
1437         int len;
1438         char *msg;
1439         va_list ap;
1440
1441         va_copy(ap, args);
1442         len = vasprintf(&msg, fmt, ap);
1443         va_end(ap);
1444
1445         if (len < 0)
1446                 _hook_global_("vverbose(%d, %s, %d, %s) -> %s ? ? ?", level, file, line, func, fmt);
1447         else {
1448                 _hook_global_("vverbose(%d, %s, %d, %s) -> %s", level, file, line, func, msg);
1449                 free(msg);
1450         }
1451 }
1452
1453 static struct afb_hook_global_itf hook_global_default_itf = {
1454         .hook_global_vverbose = hook_global_vverbose_default_cb
1455 };
1456
1457 /******************************************************************************
1458  * section: hooks for tracing globals (global)
1459  *****************************************************************************/
1460
1461 #define _HOOK_GLOBAL_(what,...)   \
1462         struct afb_hook_global *hook; \
1463         struct afb_hookid hookid; \
1464         pthread_rwlock_rdlock(&rwlock); \
1465         init_hookid(&hookid); \
1466         hook = list_of_global_hooks; \
1467         while (hook) { \
1468                 if (hook->itf->hook_global_##what \
1469                  && (hook->flags & afb_hook_flag_global_##what) != 0) { \
1470                         hook->itf->hook_global_##what(hook->closure, &hookid, __VA_ARGS__); \
1471                 } \
1472                 hook = hook->next; \
1473         } \
1474         pthread_rwlock_unlock(&rwlock);
1475
1476 static void afb_hook_global_vverbose(int level, const char *file, int line, const char *func, const char *fmt, va_list args)
1477 {
1478         _HOOK_GLOBAL_(vverbose, level, file ?: "?", line, func ?: "?", fmt ?: "", args);
1479 }
1480
1481 /******************************************************************************
1482  * section: hooking globals (global)
1483  *****************************************************************************/
1484
1485 static void update_global()
1486 {
1487         struct afb_hook_global *hook;
1488         int flags = 0;
1489
1490         pthread_rwlock_rdlock(&rwlock);
1491         hook = list_of_global_hooks;
1492         while (hook) {
1493                 flags = hook->flags;
1494                 hook = hook->next;
1495         }
1496         verbose_observer = (flags & afb_hook_flag_global_vverbose) ? afb_hook_global_vverbose : NULL;
1497         pthread_rwlock_unlock(&rwlock);
1498 }
1499
1500 struct afb_hook_global *afb_hook_create_global(int flags, struct afb_hook_global_itf *itf, void *closure)
1501 {
1502         struct afb_hook_global *hook;
1503
1504         /* alloc the result */
1505         hook = calloc(1, sizeof *hook);
1506         if (hook == NULL)
1507                 return NULL;
1508
1509         /* initialise the rest */
1510         hook->refcount = 1;
1511         hook->flags = flags;
1512         hook->itf = itf ? itf : &hook_global_default_itf;
1513         hook->closure = closure;
1514
1515         /* record the hook */
1516         pthread_rwlock_wrlock(&rwlock);
1517         hook->next = list_of_global_hooks;
1518         list_of_global_hooks = hook;
1519         pthread_rwlock_unlock(&rwlock);
1520
1521         /* update hooking */
1522         update_global();
1523
1524         /* returns it */
1525         return hook;
1526 }
1527
1528 struct afb_hook_global *afb_hook_addref_global(struct afb_hook_global *hook)
1529 {
1530         pthread_rwlock_wrlock(&rwlock);
1531         hook->refcount++;
1532         pthread_rwlock_unlock(&rwlock);
1533         return hook;
1534 }
1535
1536 void afb_hook_unref_global(struct afb_hook_global *hook)
1537 {
1538         struct afb_hook_global **prv;
1539
1540         if (hook) {
1541                 pthread_rwlock_wrlock(&rwlock);
1542                 if (--hook->refcount)
1543                         hook = NULL;
1544                 else {
1545                         /* unlink */
1546                         prv = &list_of_global_hooks;
1547                         while (*prv && *prv != hook)
1548                                 prv = &(*prv)->next;
1549                         if(*prv)
1550                                 *prv = hook->next;
1551                 }
1552                 pthread_rwlock_unlock(&rwlock);
1553                 if (hook) {
1554                         /* free */
1555                         free(hook);
1556
1557                         /* update hooking */
1558                         update_global();
1559                 }
1560         }
1561 }
1562