5688cfee44f6cbc9e848d765c76b883bfab7e0aa
[src/app-framework-binder.git] / src / afb-export.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 <stdio.h>
21 #include <string.h>
22 #include <errno.h>
23
24 #include <json-c/json.h>
25
26 #define AFB_BINDING_VERSION 0
27 #include <afb/afb-binding.h>
28
29 #include "afb-api.h"
30 #include "afb-apiset.h"
31 #include "afb-api-dyn.h"
32 #include "afb-common.h"
33 #include "afb-cred.h"
34 #include "afb-evt.h"
35 #include "afb-export.h"
36 #include "afb-hook.h"
37 #include "afb-msg-json.h"
38 #include "afb-session.h"
39 #include "afb-xreq.h"
40 #include "jobs.h"
41 #include "verbose.h"
42
43 /*************************************************************************
44  * internal types and structures
45  ************************************************************************/
46
47 enum afb_api_version
48 {
49         Api_Version_Dyn = 0,
50         Api_Version_1 = 1,
51         Api_Version_2 = 2,
52 };
53
54 enum afb_api_state
55 {
56         Api_State_Pre_Init,
57         Api_State_Init,
58         Api_State_Run
59 };
60
61 struct afb_export
62 {
63         /* keep it first */
64         struct afb_dynapi dynapi;
65
66         /* name of the api */
67         char *apiname;
68
69         /* version of the api */
70         unsigned version: 4;
71
72         /* current state */
73         unsigned state: 4;
74
75         /* hooking flags */
76         int hookditf;
77         int hooksvc;
78
79         /* dynamic api */
80         struct afb_api_dyn *apidyn;
81
82         /* session for service */
83         struct afb_session *session;
84
85         /* apiset for service */
86         struct afb_apiset *apiset;
87
88         /* event listener for service or NULL */
89         struct afb_evt_listener *listener;
90
91         /* start function */
92         union {
93                 int (*v1)(struct afb_service);
94                 int (*v2)();
95                 int (*vdyn)(struct afb_dynapi *dynapi);
96         } init;
97
98         /* event handling */
99         union {
100                 void (*v12)(const char *event, struct json_object *object);
101                 void (*vdyn)(struct afb_dynapi *dynapi, const char *event, struct json_object *object);
102         } on_event;
103
104         /* exported data */
105         union {
106                 struct afb_binding_interface_v1 v1;
107                 struct afb_binding_data_v2 *v2;
108         } export;
109 };
110
111 /************************************************************************************************************/
112
113 static inline struct afb_dynapi *to_dynapi(struct afb_export *export)
114 {
115         return (struct afb_dynapi*)export;
116 }
117
118 static inline struct afb_export *from_dynapi(struct afb_dynapi *dynapi)
119 {
120         return (struct afb_export*)dynapi;
121 }
122
123 /*************************************************************************************************************
124  *************************************************************************************************************
125  *************************************************************************************************************
126  *************************************************************************************************************
127                                            F R O M     D I T F
128  *************************************************************************************************************
129  *************************************************************************************************************
130  *************************************************************************************************************
131  *************************************************************************************************************/
132
133 /**********************************************
134 * normal flow
135 **********************************************/
136 static void vverbose_cb(void *closure, int level, const char *file, int line, const char *function, const char *fmt, va_list args)
137 {
138         char *p;
139         struct afb_export *export = closure;
140
141         if (!fmt || vasprintf(&p, fmt, args) < 0)
142                 vverbose(level, file, line, function, fmt, args);
143         else {
144                 verbose(level, file, line, function, "[API %s] %s", export->apiname, p);
145                 free(p);
146         }
147 }
148
149 static void old_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args)
150 {
151         vverbose_cb(closure, level, file, line, NULL, fmt, args);
152 }
153
154 static struct afb_eventid *eventid_make_cb(void *closure, const char *name)
155 {
156         size_t plen, nlen;
157         char *event;
158         struct afb_export *export = closure;
159
160         /* check daemon state */
161         if (export->state == Api_State_Pre_Init) {
162                 ERROR("[API %s] Bad call to 'afb_daemon_event_make(%s)', must not be in PreInit", export->apiname, name);
163                 errno = EINVAL;
164                 return NULL;
165         }
166
167         /* makes the event name */
168         plen = strlen(export->apiname);
169         nlen = strlen(name);
170         event = alloca(nlen + plen + 2);
171         memcpy(event, export->apiname, plen);
172         event[plen] = '/';
173         memcpy(event + plen + 1, name, nlen + 1);
174
175         /* create the event */
176         return afb_evt_create_event(event);
177 }
178
179 static struct afb_event event_make_cb(void *closure, const char *name)
180 {
181         struct afb_eventid *eventid = eventid_make_cb(closure, name);
182         return (struct afb_event){ .itf = eventid ? eventid->itf : NULL, .closure = eventid };
183 }
184
185 static int event_broadcast_cb(void *closure, const char *name, struct json_object *object)
186 {
187         size_t plen, nlen;
188         char *event;
189         struct afb_export *export = closure;
190
191         /* check daemon state */
192         if (export->state == Api_State_Pre_Init) {
193                 ERROR("[API %s] Bad call to 'afb_daemon_event_broadcast(%s, %s)', must not be in PreInit", export->apiname, name, json_object_to_json_string(object));
194                 errno = EINVAL;
195                 return 0;
196         }
197
198         /* makes the event name */
199         plen = strlen(export->apiname);
200         nlen = strlen(name);
201         event = alloca(nlen + plen + 2);
202         memcpy(event, export->apiname, plen);
203         event[plen] = '/';
204         memcpy(event + plen + 1, name, nlen + 1);
205
206         /* broadcast the event */
207         return afb_evt_broadcast(event, object);
208 }
209
210 static int rootdir_open_locale_cb(void *closure, const char *filename, int flags, const char *locale)
211 {
212         return afb_common_rootdir_open_locale(filename, flags, locale);
213 }
214
215 static int queue_job_cb(void *closure, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout)
216 {
217         return jobs_queue(group, timeout, callback, argument);
218 }
219
220 static struct afb_req unstore_req_cb(void *closure, struct afb_stored_req *sreq)
221 {
222         return afb_xreq_unstore(sreq);
223 }
224
225 static int require_api_cb(void *closure, const char *name, int initialized)
226 {
227         struct afb_export *export = closure;
228         if (export->state != Api_State_Init) {
229                 ERROR("[API %s] Bad call to 'afb_daemon_require(%s, %d)', must be in Init", export->apiname, name, initialized);
230                 errno = EINVAL;
231                 return -1;
232         }
233         return -!(initialized ? afb_apiset_lookup_started : afb_apiset_lookup)(export->apiset, name, 1);
234 }
235
236 static int rename_api_cb(void *closure, const char *name)
237 {
238         struct afb_export *export = closure;
239         if (export->state != Api_State_Pre_Init) {
240                 ERROR("[API %s] Bad call to 'afb_daemon_rename(%s)', must be in PreInit", export->apiname, name);
241                 errno = EINVAL;
242                 return -1;
243         }
244         if (!afb_api_is_valid_name(name)) {
245                 ERROR("[API %s] Can't rename to %s: bad API name", export->apiname, name);
246                 errno = EINVAL;
247                 return -1;
248         }
249         NOTICE("[API %s] renamed to [API %s]", export->apiname, name);
250         afb_export_rename(export, name);
251         return 0;
252 }
253
254 static int api_new_api_cb(
255                 void *closure,
256                 const char *api,
257                 const char *info,
258                 int (*preinit)(void*, struct afb_dynapi *),
259                 void *preinit_closure)
260 {
261         struct afb_export *export = closure;
262         return afb_api_dyn_add(export->apiset, api, info, preinit, preinit_closure);
263 }
264
265 /**********************************************
266 * hooked flow
267 **********************************************/
268 static void hooked_vverbose_cb(void *closure, int level, const char *file, int line, const char *function, const char *fmt, va_list args)
269 {
270         struct afb_export *export = closure;
271         va_list ap;
272         va_copy(ap, args);
273         vverbose_cb(closure, level, file, line, function, fmt, args);
274         afb_hook_ditf_vverbose(export, level, file, line, function, fmt, ap);
275         va_end(ap);
276 }
277
278 static void hooked_old_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args)
279 {
280         hooked_vverbose_cb(closure, level, file, line, NULL, fmt, args);
281 }
282
283 static struct afb_eventid *hooked_eventid_make_cb(void *closure, const char *name)
284 {
285         struct afb_export *export = closure;
286         struct afb_eventid *r = eventid_make_cb(closure, name);
287         afb_hook_ditf_event_make(export, name, r);
288         return r;
289 }
290
291 static struct afb_event hooked_event_make_cb(void *closure, const char *name)
292 {
293         struct afb_eventid *eventid = hooked_eventid_make_cb(closure, name);
294         return (struct afb_event){ .itf = eventid ? eventid->itf : NULL, .closure = eventid };
295 }
296
297 static int hooked_event_broadcast_cb(void *closure, const char *name, struct json_object *object)
298 {
299         int r;
300         struct afb_export *export = closure;
301         json_object_get(object);
302         afb_hook_ditf_event_broadcast_before(export, name, json_object_get(object));
303         r = event_broadcast_cb(closure, name, object);
304         afb_hook_ditf_event_broadcast_after(export, name, object, r);
305         json_object_put(object);
306         return r;
307 }
308
309 static struct sd_event *hooked_get_event_loop(void *closure)
310 {
311         struct afb_export *export = closure;
312         struct sd_event *r = afb_common_get_event_loop();
313         return afb_hook_ditf_get_event_loop(export, r);
314 }
315
316 static struct sd_bus *hooked_get_user_bus(void *closure)
317 {
318         struct afb_export *export = closure;
319         struct sd_bus *r = afb_common_get_user_bus();
320         return afb_hook_ditf_get_user_bus(export, r);
321 }
322
323 static struct sd_bus *hooked_get_system_bus(void *closure)
324 {
325         struct afb_export *export = closure;
326         struct sd_bus *r = afb_common_get_system_bus();
327         return afb_hook_ditf_get_system_bus(export, r);
328 }
329
330 static int hooked_rootdir_get_fd(void *closure)
331 {
332         struct afb_export *export = closure;
333         int r = afb_common_rootdir_get_fd();
334         return afb_hook_ditf_rootdir_get_fd(export, r);
335 }
336
337 static int hooked_rootdir_open_locale_cb(void *closure, const char *filename, int flags, const char *locale)
338 {
339         struct afb_export *export = closure;
340         int r = rootdir_open_locale_cb(closure, filename, flags, locale);
341         return afb_hook_ditf_rootdir_open_locale(export, filename, flags, locale, r);
342 }
343
344 static int hooked_queue_job_cb(void *closure, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout)
345 {
346         struct afb_export *export = closure;
347         int r = queue_job_cb(closure, callback, argument, group, timeout);
348         return afb_hook_ditf_queue_job(export, callback, argument, group, timeout, r);
349 }
350
351 static struct afb_req hooked_unstore_req_cb(void *closure, struct afb_stored_req *sreq)
352 {
353         struct afb_export *export = closure;
354         afb_hook_ditf_unstore_req(export, sreq);
355         return unstore_req_cb(closure, sreq);
356 }
357
358 static int hooked_require_api_cb(void *closure, const char *name, int initialized)
359 {
360         int result;
361         struct afb_export *export = closure;
362         afb_hook_ditf_require_api(export, name, initialized);
363         result = require_api_cb(closure, name, initialized);
364         return afb_hook_ditf_require_api_result(export, name, initialized, result);
365 }
366
367 static int hooked_rename_api_cb(void *closure, const char *name)
368 {
369         struct afb_export *export = closure;
370         const char *oldname = export->apiname;
371         int result = rename_api_cb(closure, name);
372         return afb_hook_ditf_rename_api(export, oldname, name, result);
373 }
374
375 static int hooked_api_new_api_cb(
376                 void *closure,
377                 const char *api,
378                 const char *info,
379                 int (*preinit)(void*, struct afb_dynapi *),
380                 void *preinit_closure)
381 {
382         /* TODO */
383         return api_new_api_cb(closure, api, info, preinit, preinit_closure);
384 }
385 /**********************************************
386 * vectors
387 **********************************************/
388 static const struct afb_daemon_itf daemon_itf = {
389         .vverbose_v1 = old_vverbose_cb,
390         .vverbose_v2 = vverbose_cb,
391         .event_make = event_make_cb,
392         .event_broadcast = event_broadcast_cb,
393         .get_event_loop = afb_common_get_event_loop,
394         .get_user_bus = afb_common_get_user_bus,
395         .get_system_bus = afb_common_get_system_bus,
396         .rootdir_get_fd = afb_common_rootdir_get_fd,
397         .rootdir_open_locale = rootdir_open_locale_cb,
398         .queue_job = queue_job_cb,
399         .unstore_req = unstore_req_cb,
400         .require_api = require_api_cb,
401         .rename_api = rename_api_cb,
402         .new_api = api_new_api_cb,
403 };
404
405 static const struct afb_daemon_itf hooked_daemon_itf = {
406         .vverbose_v1 = hooked_old_vverbose_cb,
407         .vverbose_v2 = hooked_vverbose_cb,
408         .event_make = hooked_event_make_cb,
409         .event_broadcast = hooked_event_broadcast_cb,
410         .get_event_loop = hooked_get_event_loop,
411         .get_user_bus = hooked_get_user_bus,
412         .get_system_bus = hooked_get_system_bus,
413         .rootdir_get_fd = hooked_rootdir_get_fd,
414         .rootdir_open_locale = hooked_rootdir_open_locale_cb,
415         .queue_job = hooked_queue_job_cb,
416         .unstore_req = hooked_unstore_req_cb,
417         .require_api = hooked_require_api_cb,
418         .rename_api = hooked_rename_api_cb,
419         .new_api = hooked_api_new_api_cb,
420 };
421
422 /*************************************************************************************************************
423  *************************************************************************************************************
424  *************************************************************************************************************
425  *************************************************************************************************************
426                                            F R O M     S V C
427  *************************************************************************************************************
428  *************************************************************************************************************
429  *************************************************************************************************************
430  *************************************************************************************************************/
431
432 /* the common session for services sharing their session */
433 static struct afb_session *common_session;
434
435 /*************************************************************************************************************
436  *************************************************************************************************************
437  *************************************************************************************************************
438  *************************************************************************************************************
439                                            F R O M     S V C
440  *************************************************************************************************************
441  *************************************************************************************************************
442  *************************************************************************************************************
443  *************************************************************************************************************/
444
445 /*
446  * Structure for requests initiated by the service
447  */
448 struct call_req
449 {
450         struct afb_xreq xreq;
451
452         struct afb_export *export;
453
454         /* the args */
455         union {
456                 void (*callback)(void*, int, struct json_object*);
457                 void (*callback_dynapi)(void*, int, struct json_object*, struct afb_dynapi*);
458         };
459         void *closure;
460
461         /* sync */
462         struct jobloop *jobloop;
463         struct json_object *result;
464         int status;
465 };
466
467 /*
468  * destroys the call_req
469  */
470 static void callreq_destroy(struct afb_xreq *xreq)
471 {
472         struct call_req *callreq = CONTAINER_OF_XREQ(struct call_req, xreq);
473
474         afb_context_disconnect(&callreq->xreq.context);
475         json_object_put(callreq->xreq.json);
476         afb_cred_unref(callreq->xreq.cred);
477         free(callreq);
478 }
479
480 static void callreq_reply_async(struct afb_xreq *xreq, int status, json_object *obj)
481 {
482         struct call_req *callreq = CONTAINER_OF_XREQ(struct call_req, xreq);
483         if (callreq->callback)
484                 callreq->callback(callreq->closure, status, obj);
485         json_object_put(obj);
486 }
487
488 static void callreq_reply_async_dynapi(struct afb_xreq *xreq, int status, json_object *obj)
489 {
490         struct call_req *callreq = CONTAINER_OF_XREQ(struct call_req, xreq);
491         if (callreq->callback_dynapi)
492                 callreq->callback_dynapi(callreq->closure, status, obj, to_dynapi(callreq->export));
493         json_object_put(obj);
494 }
495
496 static void callreq_sync_leave(struct call_req *callreq)
497 {
498         struct jobloop *jobloop = callreq->jobloop;
499
500         if (jobloop) {
501                 callreq->jobloop = NULL;
502                 jobs_leave(jobloop);
503         }
504 }
505
506 static void callreq_reply_sync(struct afb_xreq *xreq, int status, json_object *obj)
507 {
508         struct call_req *callreq = CONTAINER_OF_XREQ(struct call_req, xreq);
509         callreq->status = status;
510         callreq->result = obj;
511         callreq_sync_leave(callreq);
512 }
513
514 static void callreq_sync_enter(int signum, void *closure, struct jobloop *jobloop)
515 {
516         struct call_req *callreq = closure;
517
518         if (!signum) {
519                 callreq->jobloop = jobloop;
520                 afb_xreq_process(&callreq->xreq, callreq->export->apiset);
521         } else {
522                 callreq->result = afb_msg_json_internal_error();
523                 callreq->status = -1;
524                 callreq_sync_leave(callreq);
525         }
526 }
527
528 /* interface for requests of services */
529 const struct afb_xreq_query_itf afb_export_xreq_async_itf = {
530         .unref = callreq_destroy,
531         .reply = callreq_reply_async
532 };
533
534 /* interface for requests of services */
535 const struct afb_xreq_query_itf afb_export_xreq_async_dynapi_itf = {
536         .unref = callreq_destroy,
537         .reply = callreq_reply_async_dynapi
538 };
539
540 /* interface for requests of services */
541 const struct afb_xreq_query_itf afb_export_xreq_sync_itf = {
542         .unref = callreq_destroy,
543         .reply = callreq_reply_sync
544 };
545
546 /*
547  * create an call_req
548  */
549 static struct call_req *callreq_create(
550                 struct afb_export *export,
551                 const char *api,
552                 const char *verb,
553                 struct json_object *args,
554                 const struct afb_xreq_query_itf *itf)
555 {
556         struct call_req *callreq;
557         size_t lenapi, lenverb;
558         char *copy;
559
560         /* allocates the request */
561         lenapi = 1 + strlen(api);
562         lenverb = 1 + strlen(verb);
563         callreq = malloc(lenapi + lenverb + sizeof *callreq);
564         if (callreq != NULL) {
565                 /* initialises the request */
566                 afb_xreq_init(&callreq->xreq, itf);
567                 afb_context_init(&callreq->xreq.context, export->session, NULL);
568                 callreq->xreq.context.validated = 1;
569                 copy = (char*)&callreq[1];
570                 memcpy(copy, api, lenapi);
571                 callreq->xreq.api = copy;
572                 copy = &copy[lenapi];
573                 memcpy(copy, verb, lenverb);
574                 callreq->xreq.verb = copy;
575                 callreq->xreq.listener = export->listener;
576                 callreq->xreq.json = args;
577                 callreq->export = export;
578         }
579         return callreq;
580 }
581
582 /*
583  * Initiates a call for the service
584  */
585 static void svc_call(
586                 void *closure,
587                 const char *api,
588                 const char *verb,
589                 struct json_object *args,
590                 void (*callback)(void*, int, struct json_object*),
591                 void *cbclosure)
592 {
593         struct afb_export *export = closure;
594         struct call_req *callreq;
595         struct json_object *ierr;
596
597         /* allocates the request */
598         callreq = callreq_create(export, api, verb, args, &afb_export_xreq_async_itf);
599         if (callreq == NULL) {
600                 ERROR("out of memory");
601                 json_object_put(args);
602                 ierr = afb_msg_json_internal_error();
603                 if (callback)
604                         callback(cbclosure, -1, ierr);
605                 json_object_put(ierr);
606                 return;
607         }
608
609         /* initialises the request */
610         callreq->jobloop = NULL;
611         callreq->callback = callback;
612         callreq->closure = cbclosure;
613
614         /* terminates and frees ressources if needed */
615         afb_xreq_process(&callreq->xreq, export->apiset);
616 }
617
618 static void svc_call_dynapi(
619                 struct afb_dynapi *dynapi,
620                 const char *api,
621                 const char *verb,
622                 struct json_object *args,
623                 void (*callback)(void*, int, struct json_object*, struct afb_dynapi*),
624                 void *cbclosure)
625 {
626         struct afb_export *export = from_dynapi(dynapi);
627         struct call_req *callreq;
628         struct json_object *ierr;
629
630         /* allocates the request */
631         callreq = callreq_create(export, api, verb, args, &afb_export_xreq_async_dynapi_itf);
632         if (callreq == NULL) {
633                 ERROR("out of memory");
634                 json_object_put(args);
635                 ierr = afb_msg_json_internal_error();
636                 if (callback)
637                         callback(cbclosure, -1, ierr, to_dynapi(export));
638                 json_object_put(ierr);
639                 return;
640         }
641
642         /* initialises the request */
643         callreq->jobloop = NULL;
644         callreq->callback_dynapi = callback;
645         callreq->closure = cbclosure;
646
647         /* terminates and frees ressources if needed */
648         afb_xreq_process(&callreq->xreq, export->apiset);
649 }
650
651 static int svc_call_sync(
652                 void *closure,
653                 const char *api,
654                 const char *verb,
655                 struct json_object *args,
656                 struct json_object **result)
657 {
658         struct afb_export *export = closure;
659         struct call_req *callreq;
660         struct json_object *resu;
661         int rc;
662
663         /* allocates the request */
664         callreq = callreq_create(export, api, verb, args, &afb_export_xreq_sync_itf);
665         if (callreq == NULL) {
666                 ERROR("out of memory");
667                 errno = ENOMEM;
668                 json_object_put(args);
669                 resu = afb_msg_json_internal_error();
670                 rc = -1;
671         } else {
672                 /* initialises the request */
673                 callreq->jobloop = NULL;
674                 callreq->callback = NULL;
675                 callreq->result = NULL;
676                 callreq->status = 0;
677                 afb_xreq_unhooked_addref(&callreq->xreq); /* avoid early callreq destruction */
678                 rc = jobs_enter(NULL, 0, callreq_sync_enter, callreq);
679                 if (rc >= 0)
680                         rc = callreq->status;
681                 resu = (rc >= 0 || callreq->result) ? callreq->result : afb_msg_json_internal_error();
682                 afb_xreq_unhooked_unref(&callreq->xreq);
683         }
684         if (result)
685                 *result = resu;
686         else
687                 json_object_put(resu);
688         return rc;
689 }
690
691 struct hooked_call
692 {
693         struct afb_export *export;
694         union {
695                 void (*callback)(void*, int, struct json_object*);
696                 void (*callback_dynapi)(void*, int, struct json_object*, struct afb_dynapi*);
697         };
698         void *cbclosure;
699 };
700
701 static void svc_hooked_call_result(void *closure, int status, struct json_object *result)
702 {
703         struct hooked_call *hc = closure;
704         afb_hook_svc_call_result(hc->export, status, result);
705         hc->callback(hc->cbclosure, status, result);
706         free(hc);
707 }
708
709 static void svc_hooked_call_dynapi_result(void *closure, int status, struct json_object *result, struct afb_dynapi *dynapi)
710 {
711         struct hooked_call *hc = closure;
712         afb_hook_svc_call_result(hc->export, status, result);
713         hc->callback_dynapi(hc->cbclosure, status, result, dynapi);
714         free(hc);
715 }
716
717 static void svc_hooked_call(
718                 void *closure,
719                 const char *api,
720                 const char *verb,
721                 struct json_object *args,
722                 void (*callback)(void*, int, struct json_object*),
723                 void *cbclosure)
724 {
725         struct afb_export *export = closure;
726         struct hooked_call *hc;
727
728         if (export->hooksvc & afb_hook_flag_svc_call)
729                 afb_hook_svc_call(export, api, verb, args);
730
731         if (export->hooksvc & afb_hook_flag_svc_call_result) {
732                 hc = malloc(sizeof *hc);
733                 if (!hc)
734                         WARNING("allocation failed");
735                 else {
736                         hc->export = export;
737                         hc->callback = callback;
738                         hc->cbclosure = cbclosure;
739                         callback = svc_hooked_call_result;
740                         cbclosure = hc;
741                 }
742         }
743         svc_call(closure, api, verb, args, callback, cbclosure);
744 }
745
746 static void svc_hooked_call_dynapi(
747                 struct afb_dynapi *dynapi,
748                 const char *api,
749                 const char *verb,
750                 struct json_object *args,
751                 void (*callback)(void*, int, struct json_object*, struct afb_dynapi*),
752                 void *cbclosure)
753 {
754         struct afb_export *export = from_dynapi(dynapi);
755         struct hooked_call *hc;
756
757         if (export->hooksvc & afb_hook_flag_svc_call)
758                 afb_hook_svc_call(export, api, verb, args);
759
760         if (export->hooksvc & afb_hook_flag_svc_call_result) {
761                 hc = malloc(sizeof *hc);
762                 if (!hc)
763                         WARNING("allocation failed");
764                 else {
765                         hc->export = export;
766                         hc->callback_dynapi = callback;
767                         hc->cbclosure = cbclosure;
768                         callback = svc_hooked_call_dynapi_result;
769                         cbclosure = hc;
770                 }
771         }
772         svc_call_dynapi(dynapi, api, verb, args, callback, cbclosure);
773 }
774
775 static int svc_hooked_call_sync(
776                 void *closure,
777                 const char *api,
778                 const char *verb,
779                 struct json_object *args,
780                 struct json_object **result)
781 {
782         struct afb_export *export = closure;
783         struct json_object *resu;
784         int rc;
785
786         if (export->hooksvc & afb_hook_flag_svc_callsync)
787                 afb_hook_svc_callsync(export, api, verb, args);
788
789         rc = svc_call_sync(closure, api, verb, args, &resu);
790
791         if (export->hooksvc & afb_hook_flag_svc_callsync_result)
792                 afb_hook_svc_callsync_result(export, rc, resu);
793
794         if (result)
795                 *result = resu;
796         else
797                 json_object_put(resu);
798
799         return rc;
800 }
801
802 /* the interface for services */
803 static const struct afb_service_itf service_itf = {
804         .call = svc_call,
805         .call_sync = svc_call_sync
806 };
807
808 /* the interface for services */
809 static const struct afb_service_itf hooked_service_itf = {
810         .call = svc_hooked_call,
811         .call_sync = svc_hooked_call_sync
812 };
813
814 /*************************************************************************************************************
815  *************************************************************************************************************
816  *************************************************************************************************************
817  *************************************************************************************************************
818                                            F R O M     D Y N A P I
819  *************************************************************************************************************
820  *************************************************************************************************************
821  *************************************************************************************************************
822  *************************************************************************************************************/
823
824 static int api_set_verbs_v2_cb(
825                 struct afb_dynapi *dynapi,
826                 const struct afb_verb_v2 *verbs)
827 {
828         struct afb_export *export = from_dynapi(dynapi);
829
830         if (export->apidyn) {
831                 afb_api_dyn_set_verbs_v2(export->apidyn, verbs);
832                 return 0;
833         }
834
835         errno = EPERM;
836         return -1;
837 }
838
839 static int api_add_verb_cb(
840                 struct afb_dynapi *dynapi,
841                 const char *verb,
842                 const char *info,
843                 void (*callback)(struct afb_request *request),
844                 const struct afb_auth *auth,
845                 uint32_t session)
846 {
847         struct afb_export *export = from_dynapi(dynapi);
848
849         if (export->apidyn)
850                 return afb_api_dyn_add_verb(export->apidyn, verb, info, callback, auth, session);
851
852         errno = EPERM;
853         return -1;
854 }
855
856 static int api_sub_verb_cb(
857                 struct afb_dynapi *dynapi,
858                 const char *verb)
859 {
860         struct afb_export *export = from_dynapi(dynapi);
861
862         if (export->apidyn)
863                 return afb_api_dyn_sub_verb(export->apidyn, verb);
864
865         errno = EPERM;
866         return -1;
867 }
868
869 static int api_set_on_event_cb(
870                 struct afb_dynapi *dynapi,
871                 void (*onevent)(struct afb_dynapi *dynapi, const char *event, struct json_object *object))
872 {
873         struct afb_export *export = from_dynapi(dynapi);
874         return afb_export_handle_events_vdyn(export, onevent);
875 }
876
877 static int api_set_on_init_cb(
878                 struct afb_dynapi *dynapi,
879                 int (*oninit)(struct afb_dynapi *dynapi))
880 {
881         struct afb_export *export = from_dynapi(dynapi);
882
883         return afb_export_handle_init_vdyn(export, oninit);
884 }
885
886 static void api_seal_cb(
887                 struct afb_dynapi *dynapi)
888 {
889         struct afb_export *export = from_dynapi(dynapi);
890
891         export->apidyn = NULL;
892 }
893
894 static int hooked_api_set_verbs_v2_cb(
895                 struct afb_dynapi *dynapi,
896                 const struct afb_verb_v2 *verbs)
897 {
898         /* TODO */
899         return api_set_verbs_v2_cb(dynapi, verbs);
900 }
901
902 static int hooked_api_add_verb_cb(
903                 struct afb_dynapi *dynapi,
904                 const char *verb,
905                 const char *info,
906                 void (*callback)(struct afb_request *request),
907                 const struct afb_auth *auth,
908                 uint32_t session)
909 {
910         /* TODO */
911         return api_add_verb_cb(dynapi, verb, info, callback, auth, session);
912 }
913
914 static int hooked_api_sub_verb_cb(
915                 struct afb_dynapi *dynapi,
916                 const char *verb)
917 {
918         /* TODO */
919         return api_sub_verb_cb(dynapi, verb);
920 }
921
922 static int hooked_api_set_on_event_cb(
923                 struct afb_dynapi *dynapi,
924                 void (*onevent)(struct afb_dynapi *dynapi, const char *event, struct json_object *object))
925 {
926         /* TODO */
927         return api_set_on_event_cb(dynapi, onevent);
928 }
929
930 static int hooked_api_set_on_init_cb(
931                 struct afb_dynapi *dynapi,
932                 int (*oninit)(struct afb_dynapi *dynapi))
933 {
934         /* TODO */
935         return api_set_on_init_cb(dynapi, oninit);
936 }
937
938 static void hooked_api_seal_cb(
939                 struct afb_dynapi *dynapi)
940 {
941         /* TODO */
942         api_seal_cb(dynapi);
943 }
944
945 static const struct afb_dynapi_itf dynapi_itf = {
946
947         .vverbose = (void*)vverbose_cb,
948
949         .get_event_loop = afb_common_get_event_loop,
950         .get_user_bus = afb_common_get_user_bus,
951         .get_system_bus = afb_common_get_system_bus,
952         .rootdir_get_fd = afb_common_rootdir_get_fd,
953         .rootdir_open_locale = rootdir_open_locale_cb,
954         .queue_job = queue_job_cb,
955
956         .require_api = require_api_cb,
957         .rename_api = rename_api_cb,
958
959         .event_broadcast = event_broadcast_cb,
960         .eventid_make = eventid_make_cb,
961
962         .call = svc_call_dynapi,
963         .call_sync = svc_call_sync,
964
965         .api_new_api = api_new_api_cb,
966         .api_set_verbs_v2 = api_set_verbs_v2_cb,
967         .api_add_verb = api_add_verb_cb,
968         .api_sub_verb = api_sub_verb_cb,
969         .api_set_on_event = api_set_on_event_cb,
970         .api_set_on_init = api_set_on_init_cb,
971         .api_seal = api_seal_cb,
972 };
973
974 static const struct afb_dynapi_itf hooked_dynapi_itf = {
975
976         .vverbose = hooked_vverbose_cb,
977
978         .get_event_loop = hooked_get_event_loop,
979         .get_user_bus = hooked_get_user_bus,
980         .get_system_bus = hooked_get_system_bus,
981         .rootdir_get_fd = hooked_rootdir_get_fd,
982         .rootdir_open_locale = hooked_rootdir_open_locale_cb,
983         .queue_job = hooked_queue_job_cb,
984
985         .require_api = hooked_require_api_cb,
986         .rename_api = hooked_rename_api_cb,
987
988         .event_broadcast = hooked_event_broadcast_cb,
989         .eventid_make = hooked_eventid_make_cb,
990
991         .call = svc_hooked_call_dynapi,
992         .call_sync = svc_hooked_call_sync,
993
994         .api_new_api = hooked_api_new_api_cb,
995         .api_set_verbs_v2 = hooked_api_set_verbs_v2_cb,
996         .api_add_verb = hooked_api_add_verb_cb,
997         .api_sub_verb = hooked_api_sub_verb_cb,
998         .api_set_on_event = hooked_api_set_on_event_cb,
999         .api_set_on_init = hooked_api_set_on_init_cb,
1000         .api_seal = hooked_api_seal_cb,
1001 };
1002
1003 /*************************************************************************************************************
1004  *************************************************************************************************************
1005  *************************************************************************************************************
1006  *************************************************************************************************************
1007                                            F R O M     S V C
1008  *************************************************************************************************************
1009  *************************************************************************************************************
1010  *************************************************************************************************************
1011  *************************************************************************************************************/
1012
1013 /*
1014  * Propagates the event to the service
1015  */
1016 static void export_on_event_v12(void *closure, const char *event, int eventid, struct json_object *object)
1017 {
1018         struct afb_export *export = closure;
1019
1020         if (export->hooksvc & afb_hook_flag_svc_on_event_before)
1021                 afb_hook_svc_on_event_before(export, event, eventid, object);
1022         export->on_event.v12(event, object);
1023         if (export->hooksvc & afb_hook_flag_svc_on_event_after)
1024                 afb_hook_svc_on_event_after(export, event, eventid, object);
1025         json_object_put(object);
1026 }
1027
1028 /* the interface for events */
1029 static const struct afb_evt_itf evt_v12_itf = {
1030         .broadcast = export_on_event_v12,
1031         .push = export_on_event_v12
1032 };
1033
1034 /*
1035  * Propagates the event to the service
1036  */
1037 static void export_on_event_vdyn(void *closure, const char *event, int eventid, struct json_object *object)
1038 {
1039         struct afb_export *export = closure;
1040
1041         if (export->hooksvc & afb_hook_flag_svc_on_event_before)
1042                 afb_hook_svc_on_event_before(export, event, eventid, object);
1043         export->on_event.vdyn(to_dynapi(export), event, object);
1044         if (export->hooksvc & afb_hook_flag_svc_on_event_after)
1045                 afb_hook_svc_on_event_after(export, event, eventid, object);
1046         json_object_put(object);
1047 }
1048
1049 /* the interface for events */
1050 static const struct afb_evt_itf evt_vdyn_itf = {
1051         .broadcast = export_on_event_vdyn,
1052         .push = export_on_event_vdyn
1053 };
1054
1055 /*************************************************************************************************************
1056  *************************************************************************************************************
1057  *************************************************************************************************************
1058  *************************************************************************************************************
1059                                            M E R G E D
1060  *************************************************************************************************************
1061  *************************************************************************************************************
1062  *************************************************************************************************************
1063  *************************************************************************************************************/
1064
1065 static struct afb_export *create(struct afb_apiset *apiset, const char *apiname, enum afb_api_version version)
1066 {
1067         struct afb_export *export;
1068
1069         /* session shared with other exports */
1070         if (common_session == NULL) {
1071                 common_session = afb_session_create (NULL, 0);
1072                 if (common_session == NULL)
1073                         return NULL;
1074         }
1075         export = calloc(1, sizeof *export);
1076         if (!export)
1077                 errno = ENOMEM;
1078         else {
1079                 memset(export, 0, sizeof *export);
1080                 export->apiname = strdup(apiname);
1081                 export->version = version;
1082                 export->state = Api_State_Pre_Init;
1083                 export->session = afb_session_addref(common_session);
1084                 export->apiset = afb_apiset_addref(apiset);
1085         }
1086         return export;
1087 }
1088
1089 void afb_export_destroy(struct afb_export *export)
1090 {
1091         if (export) {
1092                 if (export->listener != NULL)
1093                         afb_evt_listener_unref(export->listener);
1094                 afb_session_unref(export->session);
1095                 afb_apiset_unref(export->apiset);
1096                 free(export->apiname);
1097                 free(export);
1098         }
1099 }
1100
1101 struct afb_export *afb_export_create_v1(struct afb_apiset *apiset, const char *apiname, int (*init)(struct afb_service), void (*onevent)(const char*, struct json_object*))
1102 {
1103         struct afb_export *export = create(apiset, apiname, Api_Version_1);
1104         if (export) {
1105                 export->init.v1 = init;
1106                 export->on_event.v12 = onevent;
1107                 export->export.v1.mode = AFB_MODE_LOCAL;
1108                 export->export.v1.daemon.closure = export;
1109                 afb_export_verbosity_set(export, verbosity);
1110                 afb_export_update_hook(export);
1111         }
1112         return export;
1113 }
1114
1115 struct afb_export *afb_export_create_v2(struct afb_apiset *apiset, const char *apiname, struct afb_binding_data_v2 *data, int (*init)(), void (*onevent)(const char*, struct json_object*))
1116 {
1117         struct afb_export *export = create(apiset, apiname, Api_Version_2);
1118         if (export) {
1119                 export->init.v2 = init;
1120                 export->on_event.v12 = onevent;
1121                 export->export.v2 = data;
1122                 data->daemon.closure = export;
1123                 data->service.closure = export;
1124                 afb_export_verbosity_set(export, verbosity);
1125                 afb_export_update_hook(export);
1126         }
1127         return export;
1128 }
1129
1130 struct afb_export *afb_export_create_vdyn(struct afb_apiset *apiset, const char *apiname, struct afb_api_dyn *apidyn)
1131 {
1132         struct afb_export *export = create(apiset, apiname, Api_Version_Dyn);
1133         if (export) {
1134                 export->apidyn = apidyn;
1135                 afb_export_verbosity_set(export, verbosity);
1136                 afb_export_update_hook(export);
1137         }
1138         return export;
1139 }
1140
1141 void afb_export_rename(struct afb_export *export, const char *apiname)
1142 {
1143         free(export->apiname);
1144         export->apiname = strdup(apiname);
1145         afb_export_update_hook(export);
1146 }
1147
1148 const char *afb_export_apiname(const struct afb_export *export)
1149 {
1150         return export->apiname;
1151 }
1152
1153 void afb_export_update_hook(struct afb_export *export)
1154 {
1155         export->hookditf = afb_hook_flags_ditf(export->apiname);
1156         export->hooksvc = afb_hook_flags_svc(export->apiname);
1157         export->dynapi.itf = export->hookditf|export->hooksvc ? &hooked_dynapi_itf : &dynapi_itf;
1158
1159         switch (export->version) {
1160         case Api_Version_1:
1161                 export->export.v1.daemon.itf = export->hookditf ? &hooked_daemon_itf : &daemon_itf;
1162                 break;
1163         case Api_Version_2:
1164                 export->export.v2->daemon.itf = export->hookditf ? &hooked_daemon_itf : &daemon_itf;
1165                 export->export.v2->service.itf = export->hooksvc ? &hooked_service_itf : &service_itf;
1166                 break;
1167         }
1168 }
1169
1170 struct afb_binding_interface_v1 *afb_export_get_interface_v1(struct afb_export *export)
1171 {
1172         return export->version == Api_Version_1 ? &export->export.v1 : NULL;
1173 }
1174
1175 int afb_export_unshare_session(struct afb_export *export)
1176 {
1177         if (export->session == common_session) {
1178                 export->session = afb_session_create (NULL, 0);
1179                 if (export->session)
1180                         afb_session_unref(common_session);
1181                 else {
1182                         export->session = common_session;
1183                         return -1;
1184                 }
1185         }
1186         return 0;
1187 }
1188
1189 void afb_export_set_apiset(struct afb_export *export, struct afb_apiset *apiset)
1190 {
1191         struct afb_apiset *prvset = export->apiset;
1192         export->apiset = afb_apiset_addref(apiset);
1193         afb_apiset_unref(prvset);
1194 }
1195
1196 struct afb_apiset *afb_export_get_apiset(struct afb_export *export)
1197 {
1198         return export->apiset;
1199 }
1200
1201 int afb_export_handle_events_v12(struct afb_export *export, void (*on_event)(const char *event, struct json_object *object))
1202 {
1203         /* check version */
1204         switch (export->version) {
1205         case Api_Version_1: case Api_Version_2: break;
1206         default:
1207                 ERROR("invalid version 12 for API %s", export->apiname);
1208                 errno = EINVAL;
1209                 return -1;
1210         }
1211
1212         /* set the event handler */
1213         if (!on_event) {
1214                 if (export->listener) {
1215                         afb_evt_listener_unref(export->listener);
1216                         export->listener = NULL;
1217                 }
1218                 export->on_event.v12 = on_event;
1219         } else {
1220                 export->on_event.v12 = on_event;
1221                 if (!export->listener) {
1222                         export->listener = afb_evt_listener_create(&evt_v12_itf, export);
1223                         if (export->listener == NULL)
1224                                 return -1;
1225                 }
1226         }
1227         return 0;
1228 }
1229
1230 int afb_export_handle_events_vdyn(struct afb_export *export, void (*on_event)(struct afb_dynapi *dynapi, const char *event, struct json_object *object))
1231 {
1232         /* check version */
1233         switch (export->version) {
1234         case Api_Version_Dyn: break;
1235         default:
1236                 ERROR("invalid version Dyn for API %s", export->apiname);
1237                 errno = EINVAL;
1238                 return -1;
1239         }
1240
1241         /* set the event handler */
1242         if (!on_event) {
1243                 if (export->listener) {
1244                         afb_evt_listener_unref(export->listener);
1245                         export->listener = NULL;
1246                 }
1247                 export->on_event.vdyn = on_event;
1248         } else {
1249                 export->on_event.vdyn = on_event;
1250                 if (!export->listener) {
1251                         export->listener = afb_evt_listener_create(&evt_vdyn_itf, export);
1252                         if (export->listener == NULL)
1253                                 return -1;
1254                 }
1255         }
1256         return 0;
1257 }
1258
1259 int afb_export_handle_init_vdyn(struct afb_export *export, int (*oninit)(struct afb_dynapi *dynapi))
1260 {
1261         if (export->state != Api_State_Pre_Init) {
1262                 ERROR("[API %s] Bad call to 'afb_dynapi_on_init', must be in PreInit", export->apiname);
1263                 errno = EINVAL;
1264                 return -1;
1265         }
1266
1267         export->init.vdyn  = oninit;
1268         return 0;
1269 }
1270
1271 /*
1272  * Starts a new service (v1)
1273  */
1274 struct afb_binding_v1 *afb_export_register_v1(struct afb_export *export, struct afb_binding_v1 *(*regfun)(const struct afb_binding_interface_v1*))
1275 {
1276         return regfun(&export->export.v1);
1277 }
1278
1279 int afb_export_preinit_vdyn(struct afb_export *export, int (*preinit)(void*, struct afb_dynapi*), void *closure)
1280 {
1281         return preinit(closure, to_dynapi(export));
1282 }
1283
1284 int afb_export_verbosity_get(const struct afb_export *export)
1285 {
1286         return export->dynapi.verbosity;
1287 }
1288
1289 void afb_export_verbosity_set(struct afb_export *export, int level)
1290 {
1291         export->dynapi.verbosity = level;
1292         switch (export->version) {
1293         case Api_Version_1: export->export.v1.verbosity = level; break;
1294         case Api_Version_2: export->export.v2->verbosity = level; break;
1295         }
1296 }
1297
1298 /*************************************************************************************************************
1299  *************************************************************************************************************
1300  *************************************************************************************************************
1301  *************************************************************************************************************
1302                                            N E W
1303  *************************************************************************************************************
1304  *************************************************************************************************************
1305  *************************************************************************************************************
1306  *************************************************************************************************************/
1307
1308 int afb_export_start(struct afb_export *export, int share_session, int onneed, struct afb_apiset *apiset)
1309 {
1310         int rc;
1311
1312         /* check state */
1313         if (export->state != Api_State_Pre_Init) {
1314                 /* not an error when onneed */
1315                 if (onneed != 0)
1316                         goto done;
1317
1318                 /* already started: it is an error */
1319                 ERROR("Service of API %s already started", export->apiname);
1320                 return -1;
1321         }
1322
1323         /* unshare the session if asked */
1324         if (!share_session) {
1325                 rc = afb_export_unshare_session(export);
1326                 if (rc < 0) {
1327                         ERROR("Can't unshare the session for %s", export->apiname);
1328                         return -1;
1329                 }
1330         }
1331
1332         /* set event handling */
1333         switch (export->version) {
1334         case Api_Version_1:
1335         case Api_Version_2:
1336                 rc = afb_export_handle_events_v12(export, export->on_event.v12);
1337                 break;
1338         default:
1339                 rc = 0;
1340                 break;
1341         }
1342         if (rc < 0) {
1343                 ERROR("Can't set event handler for %s", export->apiname);
1344                 return -1;
1345         }
1346
1347         /* Starts the service */
1348         if (export->hooksvc & afb_hook_flag_svc_start_before)
1349                 afb_hook_svc_start_before(export);
1350         export->state = Api_State_Init;
1351         switch (export->version) {
1352         case Api_Version_1:
1353                 rc = export->init.v1 ? export->init.v1((struct afb_service){ .itf = &hooked_service_itf, .closure = export }) : 0;
1354                 break;
1355         case Api_Version_2:
1356                 rc = export->init.v2 ? export->init.v2() : 0;
1357                 break;
1358         case Api_Version_Dyn:
1359                 rc = export->init.vdyn ? export->init.vdyn(to_dynapi(export)) : 0;
1360                 break;
1361         default:
1362                 break;
1363         }
1364         export->state = Api_State_Run;
1365         if (export->hooksvc & afb_hook_flag_svc_start_after)
1366                 afb_hook_svc_start_after(export, rc);
1367         if (rc < 0) {
1368                 /* initialisation error */
1369                 ERROR("Initialisation of service API %s failed (%d): %m", export->apiname, rc);
1370                 return rc;
1371         }
1372
1373 done:
1374         return 0;
1375 }
1376