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