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