Adds hooks for service (svc)
[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
27 #include <json-c/json.h>
28
29 #include <afb/afb-req-itf.h>
30 #include <afb/afb-event-itf.h>
31
32 #include "afb-context.h"
33 #include "afb-hook.h"
34 #include "afb-session.h"
35 #include "afb-cred.h"
36 #include "afb-xreq.h"
37 #include "afb-ditf.h"
38 #include "afb-svc.h"
39 #include "verbose.h"
40
41 /**
42  * Definition of a hook for xreq
43  */
44 struct afb_hook_xreq {
45         struct afb_hook_xreq *next; /**< next hook */
46         unsigned refcount; /**< reference count */
47         char *api; /**< api hooked or NULL for any */
48         char *verb; /**< verb hooked or NULL for any */
49         struct afb_session *session; /**< session hooked or NULL if any */
50         unsigned flags; /**< hook flags */
51         struct afb_hook_xreq_itf *itf; /**< interface of hook */
52         void *closure; /**< closure for callbacks */
53 };
54
55 /**
56  * Definition of a hook for ditf
57  */
58 struct afb_hook_ditf {
59         struct afb_hook_ditf *next; /**< next hook */
60         unsigned refcount; /**< reference count */
61         char *api; /**< api hooked or NULL for any */
62         unsigned flags; /**< hook flags */
63         struct afb_hook_ditf_itf *itf; /**< interface of hook */
64         void *closure; /**< closure for callbacks */
65 };
66
67 /**
68  * Definition of a hook for svc
69  */
70 struct afb_hook_svc {
71         struct afb_hook_svc *next; /**< next hook */
72         unsigned refcount; /**< reference count */
73         char *api; /**< api hooked or NULL for any */
74         unsigned flags; /**< hook flags */
75         struct afb_hook_svc_itf *itf; /**< interface of hook */
76         void *closure; /**< closure for callbacks */
77 };
78
79 /* synchronisation across threads */
80 static pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
81
82 /* list of hooks for xreq */
83 static struct afb_hook_xreq *list_of_xreq_hooks = NULL;
84
85 /* list of hooks for ditf */
86 static struct afb_hook_ditf *list_of_ditf_hooks = NULL;
87
88 /* list of hooks for svc */
89 static struct afb_hook_svc *list_of_svc_hooks = NULL;
90
91 /******************************************************************************
92  * section: default callbacks for tracing requests
93  *****************************************************************************/
94
95 static void _hook_xreq_(const struct afb_xreq *xreq, const char *format, ...)
96 {
97         int len;
98         char *buffer;
99         va_list ap;
100
101         va_start(ap, format);
102         len = vasprintf(&buffer, format, ap);
103         va_end(ap);
104
105         if (len < 0)
106                 NOTICE("hook xreq-%06d:%s/%s allocation error", xreq->hookindex, xreq->api, xreq->verb);
107         else {
108                 NOTICE("hook xreq-%06d:%s/%s %s", xreq->hookindex, xreq->api, xreq->verb, buffer);
109                 free(buffer);
110         }
111 }
112
113 static void hook_xreq_begin_default_cb(void * closure, const struct afb_xreq *xreq)
114 {
115         if (!xreq->cred)
116                 _hook_xreq_(xreq, "BEGIN");
117         else
118                 _hook_xreq_(xreq, "BEGIN uid=%d=%s gid=%d pid=%d label=%s id=%s",
119                         (int)xreq->cred->uid,
120                         xreq->cred->user,
121                         (int)xreq->cred->gid,
122                         (int)xreq->cred->pid,
123                         xreq->cred->label?:"(null)",
124                         xreq->cred->id?:"(null)"
125                 );
126 }
127
128 static void hook_xreq_end_default_cb(void * closure, const struct afb_xreq *xreq)
129 {
130         _hook_xreq_(xreq, "END");
131 }
132
133 static void hook_xreq_json_default_cb(void * closure, const struct afb_xreq *xreq, struct json_object *obj)
134 {
135         _hook_xreq_(xreq, "json() -> %s", json_object_to_json_string(obj));
136 }
137
138 static void hook_xreq_get_default_cb(void * closure, const struct afb_xreq *xreq, const char *name, struct afb_arg arg)
139 {
140         _hook_xreq_(xreq, "get(%s) -> { name: %s, value: %s, path: %s }", name, arg.name, arg.value, arg.path);
141 }
142
143 static void hook_xreq_success_default_cb(void * closure, const struct afb_xreq *xreq, struct json_object *obj, const char *info)
144 {
145         _hook_xreq_(xreq, "success(%s, %s)", json_object_to_json_string(obj), info);
146 }
147
148 static void hook_xreq_fail_default_cb(void * closure, const struct afb_xreq *xreq, const char *status, const char *info)
149 {
150         _hook_xreq_(xreq, "fail(%s, %s)", status, info);
151 }
152
153 static void hook_xreq_context_get_default_cb(void * closure, const struct afb_xreq *xreq, void *value)
154 {
155         _hook_xreq_(xreq, "context_get() -> %p", value);
156 }
157
158 static void hook_xreq_context_set_default_cb(void * closure, const struct afb_xreq *xreq, void *value, void (*free_value)(void*))
159 {
160         _hook_xreq_(xreq, "context_set(%p, %p)", value, free_value);
161 }
162
163 static void hook_xreq_addref_default_cb(void * closure, const struct afb_xreq *xreq)
164 {
165         _hook_xreq_(xreq, "addref()");
166 }
167
168 static void hook_xreq_unref_default_cb(void * closure, const struct afb_xreq *xreq)
169 {
170         _hook_xreq_(xreq, "unref()");
171 }
172
173 static void hook_xreq_session_close_default_cb(void * closure, const struct afb_xreq *xreq)
174 {
175         _hook_xreq_(xreq, "session_close()");
176 }
177
178 static void hook_xreq_session_set_LOA_default_cb(void * closure, const struct afb_xreq *xreq, unsigned level, int result)
179 {
180         _hook_xreq_(xreq, "session_set_LOA(%u) -> %d", level, result);
181 }
182
183 static void hook_xreq_subscribe_default_cb(void * closure, const struct afb_xreq *xreq, struct afb_event event, int result)
184 {
185         _hook_xreq_(xreq, "subscribe(%s:%p) -> %d", afb_event_name(event), event.closure, result);
186 }
187
188 static void hook_xreq_unsubscribe_default_cb(void * closure, const struct afb_xreq *xreq, struct afb_event event, int result)
189 {
190         _hook_xreq_(xreq, "unsubscribe(%s:%p) -> %d", afb_event_name(event), event.closure, result);
191 }
192
193 static void hook_xreq_subcall_default_cb(void * closure, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
194 {
195         _hook_xreq_(xreq, "subcall(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
196 }
197
198 static void hook_xreq_subcall_result_default_cb(void * closure, const struct afb_xreq *xreq, int status, struct json_object *result)
199 {
200         _hook_xreq_(xreq, "    ...subcall... -> %d: %s", status, json_object_to_json_string(result));
201 }
202
203 static void hook_xreq_subcallsync_default_cb(void * closure, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
204 {
205         _hook_xreq_(xreq, "subcallsync(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
206 }
207
208 static void hook_xreq_subcallsync_result_default_cb(void * closure, const struct afb_xreq *xreq, int status, struct json_object *result)
209 {
210         _hook_xreq_(xreq, "    ...subcallsync... -> %d: %s", status, json_object_to_json_string(result));
211 }
212
213 static struct afb_hook_xreq_itf hook_xreq_default_itf = {
214         .hook_xreq_begin = hook_xreq_begin_default_cb,
215         .hook_xreq_end = hook_xreq_end_default_cb,
216         .hook_xreq_json = hook_xreq_json_default_cb,
217         .hook_xreq_get = hook_xreq_get_default_cb,
218         .hook_xreq_success = hook_xreq_success_default_cb,
219         .hook_xreq_fail = hook_xreq_fail_default_cb,
220         .hook_xreq_context_get = hook_xreq_context_get_default_cb,
221         .hook_xreq_context_set = hook_xreq_context_set_default_cb,
222         .hook_xreq_addref = hook_xreq_addref_default_cb,
223         .hook_xreq_unref = hook_xreq_unref_default_cb,
224         .hook_xreq_session_close = hook_xreq_session_close_default_cb,
225         .hook_xreq_session_set_LOA = hook_xreq_session_set_LOA_default_cb,
226         .hook_xreq_subscribe = hook_xreq_subscribe_default_cb,
227         .hook_xreq_unsubscribe = hook_xreq_unsubscribe_default_cb,
228         .hook_xreq_subcall = hook_xreq_subcall_default_cb,
229         .hook_xreq_subcall_result = hook_xreq_subcall_result_default_cb,
230         .hook_xreq_subcallsync = hook_xreq_subcallsync_default_cb,
231         .hook_xreq_subcallsync_result = hook_xreq_subcallsync_result_default_cb,
232 };
233
234 /******************************************************************************
235  * section: hooks for tracing requests
236  *****************************************************************************/
237
238 #define _HOOK_XREQ_(what,...)   \
239         struct afb_hook_xreq *hook; \
240         pthread_rwlock_rdlock(&rwlock); \
241         hook = list_of_xreq_hooks; \
242         while (hook) { \
243                 if (hook->itf->hook_xreq_##what \
244                  && (hook->flags & afb_hook_flag_req_##what) != 0 \
245                  && (!hook->session || hook->session == xreq->context.session) \
246                  && (!hook->api || !strcasecmp(hook->api, xreq->api)) \
247                  && (!hook->verb || !strcasecmp(hook->verb, xreq->verb))) { \
248                         hook->itf->hook_xreq_##what(hook->closure, __VA_ARGS__); \
249                 } \
250                 hook = hook->next; \
251         } \
252         pthread_rwlock_unlock(&rwlock);
253
254
255 void afb_hook_xreq_begin(const struct afb_xreq *xreq)
256 {
257         _HOOK_XREQ_(begin, xreq);
258 }
259
260 void afb_hook_xreq_end(const struct afb_xreq *xreq)
261 {
262         _HOOK_XREQ_(end, xreq);
263 }
264
265 struct json_object *afb_hook_xreq_json(const struct afb_xreq *xreq, struct json_object *obj)
266 {
267         _HOOK_XREQ_(json, xreq, obj);
268         return obj;
269 }
270
271 struct afb_arg afb_hook_xreq_get(const struct afb_xreq *xreq, const char *name, struct afb_arg arg)
272 {
273         _HOOK_XREQ_(get, xreq, name, arg);
274         return arg;
275 }
276
277 void afb_hook_xreq_success(const struct afb_xreq *xreq, struct json_object *obj, const char *info)
278 {
279         _HOOK_XREQ_(success, xreq, obj, info);
280 }
281
282 void afb_hook_xreq_fail(const struct afb_xreq *xreq, const char *status, const char *info)
283 {
284         _HOOK_XREQ_(fail, xreq, status, info);
285 }
286
287 void *afb_hook_xreq_context_get(const struct afb_xreq *xreq, void *value)
288 {
289         _HOOK_XREQ_(context_get, xreq, value);
290         return value;
291 }
292
293 void afb_hook_xreq_context_set(const struct afb_xreq *xreq, void *value, void (*free_value)(void*))
294 {
295         _HOOK_XREQ_(context_set, xreq, value, free_value);
296 }
297
298 void afb_hook_xreq_addref(const struct afb_xreq *xreq)
299 {
300         _HOOK_XREQ_(addref, xreq);
301 }
302
303 void afb_hook_xreq_unref(const struct afb_xreq *xreq)
304 {
305         _HOOK_XREQ_(unref, xreq);
306 }
307
308 void afb_hook_xreq_session_close(const struct afb_xreq *xreq)
309 {
310         _HOOK_XREQ_(session_close, xreq);
311 }
312
313 int afb_hook_xreq_session_set_LOA(const struct afb_xreq *xreq, unsigned level, int result)
314 {
315         _HOOK_XREQ_(session_set_LOA, xreq, level, result);
316         return result;
317 }
318
319 int afb_hook_xreq_subscribe(const struct afb_xreq *xreq, struct afb_event event, int result)
320 {
321         _HOOK_XREQ_(subscribe, xreq, event, result);
322         return result;
323 }
324
325 int afb_hook_xreq_unsubscribe(const struct afb_xreq *xreq, struct afb_event event, int result)
326 {
327         _HOOK_XREQ_(unsubscribe, xreq, event, result);
328         return result;
329 }
330
331 void afb_hook_xreq_subcall(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
332 {
333         _HOOK_XREQ_(subcall, xreq, api, verb, args);
334 }
335
336 void afb_hook_xreq_subcall_result(const struct afb_xreq *xreq, int status, struct json_object *result)
337 {
338         _HOOK_XREQ_(subcall_result, xreq, status, result);
339 }
340
341 void afb_hook_xreq_subcallsync(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args)
342 {
343         _HOOK_XREQ_(subcallsync, xreq, api, verb, args);
344 }
345
346 int afb_hook_xreq_subcallsync_result(const struct afb_xreq *xreq, int status, struct json_object *result)
347 {
348         _HOOK_XREQ_(subcallsync_result, xreq, status, result);
349         return status;
350 }
351
352 /******************************************************************************
353  * section: hooking xreqs
354  *****************************************************************************/
355
356 void afb_hook_init_xreq(struct afb_xreq *xreq)
357 {
358         static int reqindex;
359
360         int f, flags;
361         int add;
362         struct afb_hook_xreq *hook;
363
364         /* scan hook list to get the expected flags */
365         flags = 0;
366         pthread_rwlock_rdlock(&rwlock);
367         hook = list_of_xreq_hooks;
368         while (hook) {
369                 f = hook->flags & afb_hook_flags_req_all;
370                 add = f != 0
371                    && (!hook->session || hook->session == xreq->context.session)
372                    && (!hook->api || !strcasecmp(hook->api, xreq->api))
373                    && (!hook->verb || !strcasecmp(hook->verb, xreq->verb));
374                 if (add)
375                         flags |= f;
376                 hook = hook->next;
377         }
378         pthread_rwlock_unlock(&rwlock);
379
380         /* store the hooking data */
381         xreq->hookflags = flags;
382         if (flags) {
383                 pthread_rwlock_wrlock(&rwlock);
384                 if (++reqindex < 0)
385                         reqindex = 1;
386                 xreq->hookindex = reqindex;
387                 pthread_rwlock_unlock(&rwlock);
388         }
389 }
390
391 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)
392 {
393         struct afb_hook_xreq *hook;
394
395         /* alloc the result */
396         hook = calloc(1, sizeof *hook);
397         if (hook == NULL)
398                 return NULL;
399
400         /* get a copy of the names */
401         hook->api = api ? strdup(api) : NULL;
402         hook->verb = verb ? strdup(verb) : NULL;
403         if ((api && !hook->api) || (verb && !hook->verb)) {
404                 free(hook->api);
405                 free(hook->verb);
406                 free(hook);
407                 return NULL;
408         }
409
410         /* initialise the rest */
411         hook->session = session;
412         if (session)
413                 afb_session_addref(session);
414         hook->refcount = 1;
415         hook->flags = flags;
416         hook->itf = itf ? itf : &hook_xreq_default_itf;
417         hook->closure = closure;
418
419         /* record the hook */
420         pthread_rwlock_wrlock(&rwlock);
421         hook->next = list_of_xreq_hooks;
422         list_of_xreq_hooks = hook;
423         pthread_rwlock_unlock(&rwlock);
424
425         /* returns it */
426         return hook;
427 }
428
429 struct afb_hook_xreq *afb_hook_addref_xreq(struct afb_hook_xreq *hook)
430 {
431         pthread_rwlock_wrlock(&rwlock);
432         hook->refcount++;
433         pthread_rwlock_unlock(&rwlock);
434         return hook;
435 }
436
437 void afb_hook_unref_xreq(struct afb_hook_xreq *hook)
438 {
439         struct afb_hook_xreq **prv;
440
441         if (hook) {
442                 pthread_rwlock_wrlock(&rwlock);
443                 if (--hook->refcount)
444                         hook = NULL;
445                 else {
446                         /* unlink */
447                         prv = &list_of_xreq_hooks;
448                         while (*prv && *prv != hook)
449                                 prv = &(*prv)->next;
450                         if(*prv)
451                                 *prv = hook->next;
452                 }
453                 pthread_rwlock_unlock(&rwlock);
454                 if (hook) {
455                         /* free */
456                         free(hook->api);
457                         free(hook->verb);
458                         if (hook->session)
459                                 afb_session_unref(hook->session);
460                         free(hook);
461                 }
462         }
463 }
464
465 /******************************************************************************
466  * section: default callbacks for tracing daemon interface
467  *****************************************************************************/
468
469 static void _hook_ditf_(const struct afb_ditf *ditf, const char *format, ...)
470 {
471         int len;
472         char *buffer;
473         va_list ap;
474
475         va_start(ap, format);
476         len = vasprintf(&buffer, format, ap);
477         va_end(ap);
478
479         if (len < 0)
480                 NOTICE("hook ditf-%s allocation error for %s", ditf->prefix, format);
481         else {
482                 NOTICE("hook ditf-%s %s", ditf->prefix, buffer);
483                 free(buffer);
484         }
485 }
486
487 static void hook_ditf_event_broadcast_before_cb(void *closure, const struct afb_ditf *ditf, const char *name, struct json_object *object)
488 {
489         _hook_ditf_(ditf, "event_broadcast.before(%s, %s)....", name, json_object_to_json_string(object));
490 }
491
492 static void hook_ditf_event_broadcast_after_cb(void *closure, const struct afb_ditf *ditf, const char *name, struct json_object *object, int result)
493 {
494         _hook_ditf_(ditf, "event_broadcast.after(%s, %s) -> %d", name, json_object_to_json_string(object), result);
495 }
496
497 static void hook_ditf_get_event_loop_cb(void *closure, const struct afb_ditf *ditf, struct sd_event *result)
498 {
499         _hook_ditf_(ditf, "get_event_loop() -> %p", result);
500 }
501
502 static void hook_ditf_get_user_bus_cb(void *closure, const struct afb_ditf *ditf, struct sd_bus *result)
503 {
504         _hook_ditf_(ditf, "get_user_bus() -> %p", result);
505 }
506
507 static void hook_ditf_get_system_bus_cb(void *closure, const struct afb_ditf *ditf, struct sd_bus *result)
508 {
509         _hook_ditf_(ditf, "get_system_bus() -> %p", result);
510 }
511
512 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)
513 {
514         int len;
515         char *msg;
516         va_list ap;
517
518         va_copy(ap, args);
519         len = vasprintf(&msg, fmt, ap);
520         va_end(ap);
521
522         if (len < 0)
523                 _hook_ditf_(ditf, "vverbose(%d, %s, %d, %s) -> %s ? ? ?", level, file, line, function, fmt);
524         else {
525                 _hook_ditf_(ditf, "vverbose(%d, %s, %d, %s) -> %s", level, file, line, function, msg);
526                 free(msg);
527         }
528 }
529
530 static void hook_ditf_event_make_cb(void *closure, const struct afb_ditf *ditf, const char *name, struct afb_event result)
531 {
532         _hook_ditf_(ditf, "event_make(%s) -> %s:%p", name, afb_event_name(result), result.closure);
533 }
534
535 static void hook_ditf_rootdir_get_fd_cb(void *closure, const struct afb_ditf *ditf, int result)
536 {
537         char path[PATH_MAX];
538         if (result < 0)
539                 _hook_ditf_(ditf, "rootdir_get_fd() -> %d, %m", result);
540         else {
541                 sprintf(path, "/proc/self/fd/%d", result);
542                 readlink(path, path, sizeof path);
543                 _hook_ditf_(ditf, "rootdir_get_fd() -> %d = %s", result, path);
544         }
545 }
546
547 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)
548 {
549         char path[PATH_MAX];
550         if (!locale)
551                 locale = "(null)";
552         if (result < 0)
553                 _hook_ditf_(ditf, "rootdir_open_locale(%s, %d, %s) -> %d, %m", filename, flags, locale, result);
554         else {
555                 sprintf(path, "/proc/self/fd/%d", result);
556                 readlink(path, path, sizeof path);
557                 _hook_ditf_(ditf, "rootdir_open_locale(%s, %d, %s) -> %d = %s", filename, flags, locale, result, path);
558         }
559 }
560
561 static void hook_ditf_queue_job(void *closure, const struct afb_ditf *ditf, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout, int result)
562 {
563         _hook_ditf_(ditf, "queue_job(%p, %p, %p, %d) -> %d", callback, argument, group, timeout, result);
564 }
565
566 static struct afb_hook_ditf_itf hook_ditf_default_itf = {
567         .hook_ditf_event_broadcast_before = hook_ditf_event_broadcast_before_cb,
568         .hook_ditf_event_broadcast_after = hook_ditf_event_broadcast_after_cb,
569         .hook_ditf_get_event_loop = hook_ditf_get_event_loop_cb,
570         .hook_ditf_get_user_bus = hook_ditf_get_user_bus_cb,
571         .hook_ditf_get_system_bus = hook_ditf_get_system_bus_cb,
572         .hook_ditf_vverbose = hook_ditf_vverbose_cb,
573         .hook_ditf_event_make = hook_ditf_event_make_cb,
574         .hook_ditf_rootdir_get_fd = hook_ditf_rootdir_get_fd_cb,
575         .hook_ditf_rootdir_open_locale = hook_ditf_rootdir_open_locale_cb,
576         .hook_ditf_queue_job = hook_ditf_queue_job
577 };
578
579 /******************************************************************************
580  * section: hooks for tracing daemon interface (ditf)
581  *****************************************************************************/
582
583 #define _HOOK_DITF_(what,...)   \
584         struct afb_hook_ditf *hook; \
585         pthread_rwlock_rdlock(&rwlock); \
586         hook = list_of_ditf_hooks; \
587         while (hook) { \
588                 if (hook->itf->hook_ditf_##what \
589                  && (hook->flags & afb_hook_flag_ditf_##what) != 0 \
590                  && (!hook->api || !strcasecmp(hook->api, ditf->prefix))) { \
591                         hook->itf->hook_ditf_##what(hook->closure, __VA_ARGS__); \
592                 } \
593                 hook = hook->next; \
594         } \
595         pthread_rwlock_unlock(&rwlock);
596
597 void afb_hook_ditf_event_broadcast_before(const struct afb_ditf *ditf, const char *name, struct json_object *object)
598 {
599         _HOOK_DITF_(event_broadcast_before, ditf, name, object);
600 }
601
602 int afb_hook_ditf_event_broadcast_after(const struct afb_ditf *ditf, const char *name, struct json_object *object, int result)
603 {
604         _HOOK_DITF_(event_broadcast_after, ditf, name, object, result);
605         return result;
606 }
607
608 struct sd_event *afb_hook_ditf_get_event_loop(const struct afb_ditf *ditf, struct sd_event *result)
609 {
610         _HOOK_DITF_(get_event_loop, ditf, result);
611         return result;
612 }
613
614 struct sd_bus *afb_hook_ditf_get_user_bus(const struct afb_ditf *ditf, struct sd_bus *result)
615 {
616         _HOOK_DITF_(get_user_bus, ditf, result);
617         return result;
618 }
619
620 struct sd_bus *afb_hook_ditf_get_system_bus(const struct afb_ditf *ditf, struct sd_bus *result)
621 {
622         _HOOK_DITF_(get_system_bus, ditf, result);
623         return result;
624 }
625
626 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)
627 {
628         _HOOK_DITF_(vverbose, ditf, level, file, line, function, fmt, args);
629 }
630
631 struct afb_event afb_hook_ditf_event_make(const struct afb_ditf *ditf, const char *name, struct afb_event result)
632 {
633         _HOOK_DITF_(event_make, ditf, name, result);
634         return result;
635 }
636
637 int afb_hook_ditf_rootdir_get_fd(const struct afb_ditf *ditf, int result)
638 {
639         _HOOK_DITF_(rootdir_get_fd, ditf, result);
640         return result;
641 }
642
643 int afb_hook_ditf_rootdir_open_locale(const struct afb_ditf *ditf, const char *filename, int flags, const char *locale, int result)
644 {
645         _HOOK_DITF_(rootdir_open_locale, ditf, filename, flags, locale, result);
646         return result;
647 }
648
649 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)
650 {
651         _HOOK_DITF_(queue_job, ditf, callback, argument, group, timeout, result);
652         return result;
653 }
654
655 /******************************************************************************
656  * section: hooking ditf
657  *****************************************************************************/
658
659 int afb_hook_flags_ditf(const char *api)
660 {
661         int flags;
662         struct afb_hook_ditf *hook;
663
664         pthread_rwlock_rdlock(&rwlock);
665         flags = 0;
666         hook = list_of_ditf_hooks;
667         while (hook) {
668                 if (!api || !hook->api || !strcasecmp(hook->api, api))
669                         flags |= hook->flags;
670                 hook = hook->next;
671         }
672         pthread_rwlock_unlock(&rwlock);
673         return flags;
674 }
675
676 struct afb_hook_ditf *afb_hook_create_ditf(const char *api, int flags, struct afb_hook_ditf_itf *itf, void *closure)
677 {
678         struct afb_hook_ditf *hook;
679
680         /* alloc the result */
681         hook = calloc(1, sizeof *hook);
682         if (hook == NULL)
683                 return NULL;
684
685         /* get a copy of the names */
686         hook->api = api ? strdup(api) : NULL;
687         if (api && !hook->api) {
688                 free(hook);
689                 return NULL;
690         }
691
692         /* initialise the rest */
693         hook->refcount = 1;
694         hook->flags = flags;
695         hook->itf = itf ? itf : &hook_ditf_default_itf;
696         hook->closure = closure;
697
698         /* record the hook */
699         pthread_rwlock_wrlock(&rwlock);
700         hook->next = list_of_ditf_hooks;
701         list_of_ditf_hooks = hook;
702         pthread_rwlock_unlock(&rwlock);
703
704         /* returns it */
705         return hook;
706 }
707
708 struct afb_hook_ditf *afb_hook_addref_ditf(struct afb_hook_ditf *hook)
709 {
710         pthread_rwlock_wrlock(&rwlock);
711         hook->refcount++;
712         pthread_rwlock_unlock(&rwlock);
713         return hook;
714 }
715
716 void afb_hook_unref_ditf(struct afb_hook_ditf *hook)
717 {
718         struct afb_hook_ditf **prv;
719
720         if (hook) {
721                 pthread_rwlock_wrlock(&rwlock);
722                 if (--hook->refcount)
723                         hook = NULL;
724                 else {
725                         /* unlink */
726                         prv = &list_of_ditf_hooks;
727                         while (*prv && *prv != hook)
728                                 prv = &(*prv)->next;
729                         if(*prv)
730                                 *prv = hook->next;
731                 }
732                 pthread_rwlock_unlock(&rwlock);
733                 if (hook) {
734                         /* free */
735                         free(hook->api);
736                         free(hook);
737                 }
738         }
739 }
740
741 /******************************************************************************
742  * section: default callbacks for tracing service interface (svc)
743  *****************************************************************************/
744
745 static void _hook_svc_(const struct afb_svc *svc, const char *format, ...)
746 {
747         int len;
748         char *buffer;
749         va_list ap;
750
751         va_start(ap, format);
752         len = vasprintf(&buffer, format, ap);
753         va_end(ap);
754
755         if (len < 0)
756                 NOTICE("hook svc-%s allocation error for %s", svc->api, format);
757         else {
758                 NOTICE("hook svc-%s %s", svc->api, buffer);
759                 free(buffer);
760         }
761 }
762
763 static void hook_svc_start_before_default_cb(void *closure, const struct afb_svc *svc)
764 {
765         _hook_svc_(svc, "start.before");
766 }
767
768 static void hook_svc_start_after_default_cb(void *closure, const struct afb_svc *svc, int status)
769 {
770         _hook_svc_(svc, "start.after -> %d", status);
771 }
772
773 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)
774 {
775         _hook_svc_(svc, "on_event.before(%s, %d, %s)", event, eventid, json_object_to_json_string(object));
776 }
777
778 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)
779 {
780         _hook_svc_(svc, "on_event.after(%s, %d, %s)", event, eventid, json_object_to_json_string(object));
781 }
782
783 static void hook_svc_call_default_cb(void *closure, const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args)
784 {
785         _hook_svc_(svc, "call(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
786 }
787
788 static void hook_svc_call_result_default_cb(void *closure, const struct afb_svc *svc, int status, struct json_object *result)
789 {
790         _hook_svc_(svc, "    ...call... -> %d: %s", status, json_object_to_json_string(result));
791 }
792
793 static void hook_svc_callsync_default_cb(void *closure, const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args)
794 {
795         _hook_svc_(svc, "callsync(%s/%s, %s) ...", api, verb, json_object_to_json_string(args));
796 }
797
798 static void hook_svc_callsync_result_default_cb(void *closure, const struct afb_svc *svc, int status, struct json_object *result)
799 {
800         _hook_svc_(svc, "    ...callsync... -> %d: %s", status, json_object_to_json_string(result));
801 }
802
803 static struct afb_hook_svc_itf hook_svc_default_itf = {
804         .hook_svc_start_before = hook_svc_start_before_default_cb,
805         .hook_svc_start_after = hook_svc_start_after_default_cb,
806         .hook_svc_on_event_before = hook_svc_on_event_before_default_cb,
807         .hook_svc_on_event_after = hook_svc_on_event_after_default_cb,
808         .hook_svc_call = hook_svc_call_default_cb,
809         .hook_svc_call_result = hook_svc_call_result_default_cb,
810         .hook_svc_callsync = hook_svc_callsync_default_cb,
811         .hook_svc_callsync_result = hook_svc_callsync_result_default_cb
812 };
813
814 /******************************************************************************
815  * section: hooks for tracing service interface (svc)
816  *****************************************************************************/
817
818 #define _HOOK_SVC_(what,...)   \
819         struct afb_hook_svc *hook; \
820         pthread_rwlock_rdlock(&rwlock); \
821         hook = list_of_svc_hooks; \
822         while (hook) { \
823                 if (hook->itf->hook_svc_##what \
824                  && (hook->flags & afb_hook_flag_svc_##what) != 0 \
825                  && (!hook->api || !strcasecmp(hook->api, svc->api))) { \
826                         hook->itf->hook_svc_##what(hook->closure, __VA_ARGS__); \
827                 } \
828                 hook = hook->next; \
829         } \
830         pthread_rwlock_unlock(&rwlock);
831
832 void afb_hook_svc_start_before(const struct afb_svc *svc)
833 {
834         _HOOK_SVC_(start_before, svc);
835 }
836
837 int afb_hook_svc_start_after(const struct afb_svc *svc, int status)
838 {
839         _HOOK_SVC_(start_after, svc, status);
840         return status;
841 }
842
843 void afb_hook_svc_on_event_before(const struct afb_svc *svc, const char *event, int eventid, struct json_object *object)
844 {
845         _HOOK_SVC_(on_event_before, svc, event, eventid, object);
846 }
847
848 void afb_hook_svc_on_event_after(const struct afb_svc *svc, const char *event, int eventid, struct json_object *object)
849 {
850         _HOOK_SVC_(on_event_after, svc, event, eventid, object);
851 }
852
853 void afb_hook_svc_call(const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args)
854 {
855         _HOOK_SVC_(call, svc, api, verb, args);
856 }
857
858 void afb_hook_svc_call_result(const struct afb_svc *svc, int status, struct json_object *result)
859 {
860         _HOOK_SVC_(call_result, svc, status, result);
861 }
862
863 void afb_hook_svc_callsync(const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args)
864 {
865         _HOOK_SVC_(callsync, svc, api, verb, args);
866 }
867
868 int afb_hook_svc_callsync_result(const struct afb_svc *svc, int status, struct json_object *result)
869 {
870         _HOOK_SVC_(callsync_result, svc, status, result);
871         return status;
872 }
873
874 /******************************************************************************
875  * section: hooking services (svc)
876  *****************************************************************************/
877
878 int afb_hook_flags_svc(const char *api)
879 {
880         int flags;
881         struct afb_hook_svc *hook;
882
883         pthread_rwlock_rdlock(&rwlock);
884         flags = 0;
885         hook = list_of_svc_hooks;
886         while (hook) {
887                 if (!api || !hook->api || !strcasecmp(hook->api, api))
888                         flags |= hook->flags;
889                 hook = hook->next;
890         }
891         pthread_rwlock_unlock(&rwlock);
892         return flags;
893 }
894
895 struct afb_hook_svc *afb_hook_create_svc(const char *api, int flags, struct afb_hook_svc_itf *itf, void *closure)
896 {
897         struct afb_hook_svc *hook;
898
899         /* alloc the result */
900         hook = calloc(1, sizeof *hook);
901         if (hook == NULL)
902                 return NULL;
903
904         /* get a copy of the names */
905         hook->api = api ? strdup(api) : NULL;
906         if (api && !hook->api) {
907                 free(hook);
908                 return NULL;
909         }
910
911         /* initialise the rest */
912         hook->refcount = 1;
913         hook->flags = flags;
914         hook->itf = itf ? itf : &hook_svc_default_itf;
915         hook->closure = closure;
916
917         /* record the hook */
918         pthread_rwlock_wrlock(&rwlock);
919         hook->next = list_of_svc_hooks;
920         list_of_svc_hooks = hook;
921         pthread_rwlock_unlock(&rwlock);
922
923         /* returns it */
924         return hook;
925 }
926
927 struct afb_hook_svc *afb_hook_addref_svc(struct afb_hook_svc *hook)
928 {
929         pthread_rwlock_wrlock(&rwlock);
930         hook->refcount++;
931         pthread_rwlock_unlock(&rwlock);
932         return hook;
933 }
934
935 void afb_hook_unref_svc(struct afb_hook_svc *hook)
936 {
937         struct afb_hook_svc **prv;
938
939         if (hook) {
940                 pthread_rwlock_wrlock(&rwlock);
941                 if (--hook->refcount)
942                         hook = NULL;
943                 else {
944                         /* unlink */
945                         prv = &list_of_svc_hooks;
946                         while (*prv && *prv != hook)
947                                 prv = &(*prv)->next;
948                         if(*prv)
949                                 *prv = hook->next;
950                 }
951                 pthread_rwlock_unlock(&rwlock);
952                 if (hook) {
953                         /* free */
954                         free(hook->api);
955                         free(hook);
956                 }
957         }
958 }
959