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