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