aboutsummaryrefslogtreecommitdiffstats
path: root/src/afb-export.c
blob: bd5b61e0794fc4e3fb2db479e500da2685c22c86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
/*
 * Copyright (C) 2016, 2017 "IoT.bzh"
 * Author: José Bollo <jose.bollo@iot.bzh>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define _GNU_SOURCE

#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <json-c/json.h>

#include <afb/afb-binding-v1.h>
#include <afb/afb-binding-v2.h>

#include "afb-api.h"
#include "afb-apiset.h"
#include "afb-common.h"
#include "afb-cred.h"
#include "afb-evt.h"
#include "afb-export.h"
#include "afb-hook.h"
#include "afb-msg-json.h"
#include "afb-session.h"
#include "afb-xreq.h"
#include "jobs.h"
#include "verbose.h"


/*************************************************************************
 * internal types and structures
 ************************************************************************/

enum afb_api_version
{
	Api_Version_None = 0,
	Api_Version_1 = 1,
	Api_Version_2 = 2,
	Api_Version_3 = 3
};

enum afb_api_state
{
	Api_State_Pre_Init,
	Api_State_Init,
	Api_State_Run
};

struct afb_export
{
	/* name of the api */
	char *apiname;

	/* version of the api */
	unsigned version: 4;

	/* current state */
	unsigned state: 4;

	/* hooking flags */
	int hookditf;
	int hooksvc;

	/* session for service */
	struct afb_session *session;

	/* apiset for service */
	struct afb_apiset *apiset;

	/* event listener for service or NULL */
	struct afb_evt_listener *listener;

	/* start function */
	union {
		int (*v1)(struct afb_service);
		int (*v2)();
	} init;

	/* event handling */
	union {
		void (*v12)(const char *event, struct json_object *object);
	} on_event;

	/* exported data */
	union {
		struct afb_binding_interface_v1 v1;
		struct afb_binding_data_v2 *v2;
	} export;
};

/*************************************************************************************************************
 *************************************************************************************************************
 *************************************************************************************************************
 *************************************************************************************************************
                                           F R O M     D I T F
 *************************************************************************************************************
 *************************************************************************************************************
 *************************************************************************************************************
 *************************************************************************************************************/

/**********************************************
* normal flow
**********************************************/
static void vverbose_cb(void *closure, int level, const char *file, int line, const char *function, const char *fmt, va_list args)
{
	char *p;
	struct afb_export *export = closure;

	if (!fmt || vasprintf(&p, fmt, args) < 0)
		vverbose(level, file, line, function, fmt, args);
	else {
		verbose(level, file, line, function, "[API %s] %s", export->apiname, p);
		free(p);
	}
}

static void old_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args)
{
	vverbose_cb(closure, level, file, line, NULL, fmt, args);
}

static struct afb_eventid *eventid_make_cb(void *closure, const char *name)
{
	size_t plen, nlen;
	char *event;
	struct afb_export *export = closure;

	/* check daemon state */
	if (export->state == Api_State_Pre_Init) {
		ERROR("[API %s] Bad call to 'afb_daemon_event_make(%s)', must not be in PreInit", export->apiname, name);
		errno = EINVAL;
		return NULL;
	}

	/* makes the event name */
	plen = strlen(export->apiname);
	nlen = strlen(name);
	event = alloca(nlen + plen + 2);
	memcpy(event, export->apiname, plen);
	event[plen] = '/';
	memcpy(event + plen + 1, name, nlen + 1);

	/* create the event */
	return afb_evt_create_event(event);
}

static struct afb_event event_make_cb(void *closure, const char *name)
{
	struct afb_eventid *eventid = eventid_make_cb(closure, name);
	return (struct afb_event){ .itf = eventid ? eventid->itf : NULL, .closure = eventid };
}

static int event_broadcast_cb(void *closure, const char *name, struct json_object *object)
{
	size_t plen, nlen;
	char *event;
	struct afb_export *export = closure;

	/* check daemon state */
	if (export->state == Api_State_Pre_Init) {
		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));
		errno = EINVAL;
		return 0;
	}

	/* makes the event name */
	plen = strlen(export->apiname);
	nlen = strlen(name);
	event = alloca(nlen + plen + 2);
	memcpy(event, export->apiname, plen);
	event[plen] = '/';
	memcpy(event + plen + 1, name, nlen + 1);

	/* broadcast the event */
	return afb_evt_broadcast(event, object);
}

static int rootdir_open_locale_cb(void *closure, const char *filename, int flags, const char *locale)
{
	return afb_common_rootdir_open_locale(filename, flags, locale);
}

static int queue_job_cb(void *closure, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout)
{
	return jobs_queue(group, timeout, callback, argument);
}

static struct afb_req unstore_req_cb(void *closure, struct afb_stored_req *sreq)
{
	return afb_xreq_unstore(sreq);
}

static int require_api_cb(void *closure, const char *name, int initialized)
{
	struct afb_export *export = closure;
	if (export->state != Api_State_Init) {
		ERROR("[API %s] Bad call to 'afb_daemon_require(%s, %d)', must be in Init", export->apiname, name, initialized);
		errno = EINVAL;
		return -1;
	}
	return -!(initialized ? afb_apiset_lookup_started : afb_apiset_lookup)(export->apiset, name, 1);
}

static int rename_api_cb(void *closure, const char *name)
{
	struct afb_export *export = closure;
	if (export->state != Api_State_Pre_Init) {
		ERROR("[API %s] Bad call to 'afb_daemon_rename(%s)', must be in PreInit", export->apiname, name);
		errno = EINVAL;
		return -1;
	}
	if (!afb_api_is_valid_name(name)) {
		ERROR("[API %s] Can't rename to %s: bad API name", export->apiname, name);
		errno = EINVAL;
		return -1;
	}
	NOTICE("[API %s] renamed to [API %s]", export->apiname, name);
	afb_export_rename(export, name);
	return 0;
}

/**********************************************
* hooked flow
**********************************************/
static void hooked_vverbose_cb(void *closure, int level, const char *file, int line, const char *function, const char *fmt, va_list args)
{
	struct afb_export *export = closure;
	va_list ap;
	va_copy(ap, args);
	vverbose_cb(closure, level, file, line, function, fmt, args);
	afb_hook_ditf_vverbose(export, level, file, line, function, fmt, ap);
	va_end(ap);
}

static void hooked_old_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args)
{
	hooked_vverbose_cb(closure, level, file, line, NULL, fmt, args);
}

static struct afb_eventid *hooked_eventid_make_cb(void *closure, const char *name)
{
	struct afb_export *export = closure;
	struct afb_eventid *r = eventid_make_cb(closure, name);
	afb_hook_ditf_event_make(export, name, r);
	return r;
}

static struct afb_event hooked_event_make_cb(void *closure, const char *name)
{
	struct afb_eventid *eventid = hooked_eventid_make_cb(closure, name);
	return (struct afb_event){ .itf = eventid ? eventid->itf : NULL, .closure = eventid };
}

static int hooked_event_broadcast_cb(void *closure, const char *name, struct json_object *object)
{
	int r;
	struct afb_export *export = closure;
	json_object_get(object);
	afb_hook_ditf_event_broadcast_before(export, name, json_object_get(object));
	r = event_broadcast_cb(closure, name, object);
	afb_hook_ditf_event_broadcast_after(export, name, object, r);
	json_object_put(object);
	return r;
}

static struct sd_event *hooked_get_event_loop(void *closure)
{
	struct afb_export *export = closure;
	struct sd_event *r = afb_common_get_event_loop();
	return afb_hook_ditf_get_event_loop(export, r);
}

static struct sd_bus *hooked_get_user_bus(void *closure)
{
	struct afb_export *export = closure;
	struct sd_bus *r = afb_common_get_user_bus();
	return afb_hook_ditf_get_user_bus(export, r);
}

static struct sd_bus *hooked_get_system_bus(void *closure)
{
	struct afb_export *export = closure;
	struct sd_bus *r = afb_common_get_system_bus();
	return afb_hook_ditf_get_system_bus(export, r);
}

static int hooked_rootdir_get_fd(void *closure)
{
	struct afb_export *export = closure;
	int r = afb_common_rootdir_get_fd();
	return afb_hook_ditf_rootdir_get_fd(export, r);
}

static int hooked_rootdir_open_locale_cb(void *closure, const char *filename, int flags, const char *locale)
{
	struct afb_export *export = closure;
	int r = rootdir_open_locale_cb(closure, filename, flags, locale);
	return afb_hook_ditf_rootdir_open_locale(export, filename, flags, locale, r);
}

static int hooked_queue_job_cb(void *closure, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout)
{
	struct afb_export *export = closure;
	int r = queue_job_cb(closure, callback, argument, group, timeout);
	return afb_hook_ditf_queue_job(export, callback, argument, group, timeout, r);
}

static struct afb_req hooked_unstore_req_cb(void *closure, struct afb_stored_req *sreq)
{
	struct afb_export *export = closure;
	afb_hook_ditf_unstore_req(export, sreq);
	return unstore_req_cb(closure, sreq);
}

static int hooked_require_api_cb(void *closure, const char *name, int initialized)
{
	int result;
	struct afb_export *export = closure;
	afb_hook_ditf_require_api(export, name, initialized);
	result = require_api_cb(closure, name, initialized);
	return afb_hook_ditf_require_api_result(export, name, initialized, result);
}

static int hooked_rename_api_cb(void *closure, const char *name)
{
	struct afb_export *export = closure;
	const char *oldname = export->apiname;
	int result = rename_api_cb(closure, name);
	return afb_hook_ditf_rename_api(export, oldname, name, result);
}

/**********************************************
* vectors
**********************************************/
static const struct afb_daemon_itf daemon_itf = {
	.vverbose_v1 = old_vverbose_cb,
	.vverbose_v2 = vverbose_cb,
	.event_make = event_make_cb,
	.event_broadcast = event_broadcast_cb,
	.get_event_loop = afb_common_get_event_loop,
	.get_user_bus = afb_common_get_user_bus,
	.get_system_bus = afb_common_get_system_bus,
	.rootdir_get_fd = afb_common_rootdir_get_fd,
	.rootdir_open_locale = rootdir_open_locale_cb,
	.queue_job = queue_job_cb,
	.unstore_req = unstore_req_cb,
	.require_api = require_api_cb,
	.rename_api = rename_api_cb
};

static const struct afb_daemon_itf hooked_daemon_itf = {
	.vverbose_v1 = hooked_old_vverbose_cb,
	.vverbose_v2 = hooked_vverbose_cb,
	.event_make = hooked_event_make_cb,
	.event_broadcast = hooked_event_broadcast_cb,
	.get_event_loop = hooked_get_event_loop,
	.get_user_bus = hooked_get_user_bus,
	.get_system_bus = hooked_get_system_bus,
	.rootdir_get_fd = hooked_rootdir_get_fd,
	.rootdir_open_locale = hooked_rootdir_open_locale_cb,
	.queue_job = hooked_queue_job_cb,
	.unstore_req = hooked_unstore_req_cb,
	.require_api = hooked_require_api_cb,
	.rename_api = hooked_rename_api_cb
};


/*************************************************************************************************************
 *************************************************************************************************************
 *************************************************************************************************************
 *************************************************************************************************************
                                           F R O M     S V C
 *************************************************************************************************************
 *************************************************************************************************************
 *************************************************************************************************************
 *************************************************************************************************************/

/* the common session for services sharing their session */
static struct afb_session *common_session;

/*************************************************************************************************************
 *************************************************************************************************************
 *************************************************************************************************************
 *************************************************************************************************************
                                           F R O M     S V C
 *************************************************************************************************************
 *************************************************************************************************************
 *************************************************************************************************************
 *************************************************************************************************************/

/*
 * Structure for requests initiated by the service
 */
struct call_req
{
	struct afb_xreq xreq;

	struct afb_export *export;

	/* the args */
	void (*callback)(void*, int, struct json_object*);
	void *closure;

	/* sync */
	struct jobloop *jobloop;
	struct json_object *result;
	int status;
};

/*
 * destroys the call_req
 */
static void callreq_destroy(struct afb_xreq *xreq)
{
	struct call_req *callreq = CONTAINER_OF_XREQ(struct call_req, xreq);

	afb_context_disconnect(&callreq->xreq.context);
	json_object_put(callreq->xreq.json);
	afb_cred_unref(callreq->xreq.cred);
	free(callreq);
}

static void callreq_reply(struct afb_xreq *xreq, int status, json_object *obj)
{
	struct call_req *callreq = CONTAINER_OF_XREQ(struct call_req, xreq);
	if (callreq->callback)
		callreq->callback(callreq->closure, status, obj);
	json_object_put(obj);
}

static void callreq_sync_leave(struct call_req *callreq)
{
	struct jobloop *jobloop = callreq->jobloop;

	if (jobloop) {
		callreq->jobloop = NULL;
		jobs_leave(jobloop);
	}
}

static void callreq_reply_sync(struct afb_xreq *xreq, int status, json_object *obj)
{
	struct call_req *callreq = CONTAINER_OF_XREQ(struct call_req, xreq);
	callreq->status = status;
	callreq->result = obj;
	callreq_sync_leave(callreq);
}

static void callreq_sync_enter(int signum, void *closure, struct jobloop *jobloop)
{
	struct call_req *callreq = closure;

	if (!signum) {
		callreq->jobloop = jobloop;
		afb_xreq_process(&callreq->xreq, callreq->export->apiset);
	} else {
		callreq->result = afb_msg_json_internal_error();
		callreq->status = -1;
		callreq_sync_leave(callreq);
	}
}

/* interface for requests of services */
const struct afb_xreq_query_itf afb_export_xreq_itf = {
	.unref = callreq_destroy,
	.reply = callreq_reply
};

/* interface for requests of services */
const struct afb_xreq_query_itf afb_export_xreq_sync_itf = {
	.unref = callreq_destroy,
	.reply = callreq_reply_sync
};

/*
 * create an call_req
 */
static struct call_req *callreq_create(struct afb_export *export, const char *api, const char *verb, struct json_object *args, const struct afb_xreq_query_itf *itf)
{
	struct call_req *callreq;
	size_t lenapi, lenverb;
	char *copy;

	/* allocates the request */
	lenapi = 1 + strlen(api);
	lenverb = 1 + strlen(verb);
	callreq = malloc(lenapi + lenverb + sizeof *callreq);
	if (callreq != NULL) {
		/* initialises the request */
		afb_xreq_init(&callreq->xreq, itf);
		afb_context_init(&callreq->xreq.context, export->session, NULL);
		callreq->xreq.context.validated = 1;
		copy = (char*)&callreq[1];
		memcpy(copy, api, lenapi);
		callreq->xreq.api = copy;
		copy = &copy[lenapi];
		memcpy(copy, verb, lenverb);
		callreq->xreq.verb = copy;
		callreq->xreq.listener = export->listener;
		callreq->xreq.json = args;
		callreq->export = export;
	}
	return callreq;
}

/*
 * Initiates a call for the service
 */
static void svc_call(void *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cbclosure)
{
	struct afb_export *export = closure;
	struct call_req *callreq;
	struct json_object *ierr;

	/* allocates the request */
	callreq = callreq_create(export, api, verb, args, &afb_export_xreq_itf);
	if (callreq == NULL) {
		ERROR("out of memory");
		json_object_put(args);
		ierr = afb_msg_json_internal_error();
		if (callback)
			callback(cbclosure, -1, ierr);
		json_object_put(ierr);
		return;
	}

	/* initialises the request */
	callreq->jobloop = NULL;
	callreq->callback = callback;
	callreq->closure = cbclosure;

	/* terminates and frees ressources if needed */
	afb_xreq_process(&callreq->xreq, export->apiset);
}

static int svc_call_sync(void *closure, const char *api, const char *verb, struct json_object *args,
				struct json_object **result)
{
	struct afb_export *export = closure;
	struct call_req *callreq;
	struct json_object *resu;
	int rc;

	/* allocates the request */
	callreq = callreq_create(export, api, verb, args, &afb_export_xreq_sync_itf);
	if (callreq == NULL) {
		ERROR("out of memory");
		errno = ENOMEM;
		json_object_put(args);
		resu = afb_msg_json_internal_error();
		rc = -1;
	} else {
		/* initialises the request */
		callreq->jobloop = NULL;
		callreq->callback = NULL;
		callreq->result = NULL;
		callreq->status = 0;
		afb_xreq_unhooked_addref(&callreq->xreq); /* avoid early callreq destruction */
		rc = jobs_enter(NULL, 0, callreq_sync_enter, callreq);
		if (rc >= 0)
			rc = callreq->status;
		resu = (rc >= 0 || callreq->result) ? callreq->result : afb_msg_json_internal_error();
		afb_xreq_unhooked_unref(&callreq->xreq);
	}
	if (result)
		*result = resu;
	else
		json_object_put(resu);
	return rc;
}

struct hooked_call
{
	struct afb_export *export;
	void (*callback)(void*, int, struct json_object*);
	void *cbclosure;
};

static void svc_hooked_call_result(void *closure, int status, struct json_object *result)
{
	struct hooked_call *hc = closure;
	afb_hook_svc_call_result(hc->export, status, result);
	hc->callback(hc->cbclosure, status, result);
	free(hc);
}

static void svc_hooked_call(void *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cbclosure)
{
	struct afb_export *export = closure;
	struct hooked_call *hc;

	if (export->hooksvc & afb_hook_flag_svc_call)
		afb_hook_svc_call(export, api, verb, args);

	if (export->hooksvc & afb_hook_flag_svc_call_result) {
		hc = malloc(sizeof *hc);
		if (!hc)
			WARNING("allocation failed");
		else {
			hc->export = export;
			hc->callback = callback;
			hc->cbclosure = cbclosure;
			callback = svc_hooked_call_result;
			cbclosure = hc;
		}
	}
	svc_call(closure, api, verb, args, callback, cbclosure);
}

static int svc_hooked_call_sync(void *closure, const char *api, const char *verb, struct json_object *args,
				struct json_object **result)
{
	struct afb_export *export = closure;
	struct json_object *resu;
	int rc;

	if (export->hooksvc & afb_hook_flag_svc_callsync)
		afb_hook_svc_callsync(export, api, verb, args);

	rc = svc_call_sync(closure, api, verb, args, &resu);

	if (export->hooksvc & afb_hook_flag_svc_callsync_result)
		afb_hook_svc_callsync_result(export, rc, resu);

	if (result)
		*result = resu;
	else
		json_object_put(resu);

	return rc;
}

/* the interface for services */
static const struct afb_service_itf service_itf = {
	.call = svc_call,
	.call_sync = svc_call_sync
};

/* the interface for services */
static const struct afb_service_itf hooked_service_itf = {
	.call = svc_hooked_call,
	.call_sync = svc_hooked_call_sync
};

/*************************************************************************************************************
 *************************************************************************************************************
 *************************************************************************************************************
 *************************************************************************************************************
                                           F R O M     S V C
 *************************************************************************************************************
 *************************************************************************************************************
 *************************************************************************************************************
 *************************************************************************************************************/

/*
 * Propagates the event to the service
 */
static void export_on_event_v12(void *closure, const char *event, int eventid, struct json_object *object)
{
	struct afb_export *export = closure;

	if (export->hooksvc & afb_hook_flag_svc_on_event_before)
		afb_hook_svc_on_event_before(export, event, eventid, object);
	export->on_event.v12(event, object);
	if (export->hooksvc & afb_hook_flag_svc_on_event_after)
		afb_hook_svc_on_event_after(export, event, eventid, object);
	json_object_put(object);
}

/* the interface for events */
static const struct afb_evt_itf evt_v12_itf = {
	.broadcast = export_on_event_v12,
	.push = export_on_event_v12
};

/*************************************************************************************************************
 *************************************************************************************************************
 *************************************************************************************************************
 *************************************************************************************************************
                                           M E R G E D
 *************************************************************************************************************
 *************************************************************************************************************
 *************************************************************************************************************
 *************************************************************************************************************/

static struct afb_export *create(struct afb_apiset *apiset, const char *apiname, enum afb_api_version version)
{
	struct afb_export *export;

	/* session shared with other exports */
	if (common_session == NULL) {
		common_session = afb_session_create (NULL, 0);
		if (common_session == NULL)
			return NULL;
	}
	export = calloc(1, sizeof *export);
	if (!export)
		errno = ENOMEM;
	else {
		memset(export, 0, sizeof *export);
		export->apiname = strdup(apiname);
		export->version = version;
		export->state = Api_State_Pre_Init;
		export->session = afb_session_addref(common_session);
		export->apiset = afb_apiset_addref(apiset);
	}
	return export;
}

void afb_export_destroy(struct afb_export *export)
{
	if (export) {
		if (export->listener != NULL)
			afb_evt_listener_unref(export->listener);
		afb_session_unref(export->session);
		afb_apiset_unref(export->apiset);
		free(export->apiname);
		free(export);
	}
}

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*))
{
	struct afb_export *export = create(apiset, apiname, Api_Version_1);
	if (export) {
		export->init.v1 = init;
		export->on_event.v12 = onevent;
		export->export.v1.verbosity = verbosity;
		export->export.v1.mode = AFB_MODE_LOCAL;
		export->export.v1.daemon.closure = export;
		afb_export_update_hook(export);
	}
	return export;
}

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*))
{
	struct afb_export *export = create(apiset, apiname, Api_Version_2);
	if (export) {
		export->init.v2 = init;
		export->on_event.v12 = onevent;
		export->export.v2 = data;
		data->verbosity = verbosity;
		data->daemon.closure = export;
		data->service.closure = export;
		afb_export_update_hook(export);
	}
	return export;
}

void afb_export_rename(struct afb_export *export, const char *apiname)
{
	free(export->apiname);
	export->apiname = strdup(apiname);
	afb_export_update_hook(export);
}

const char *afb_export_apiname(const struct afb_export *export)
{
	return export->apiname;
}

void afb_export_update_hook(struct afb_export *export)
{
	export->hookditf = afb_hook_flags_ditf(export->apiname);
	export->hooksvc = afb_hook_flags_svc(export->apiname);
	switch (export->version) {
	case Api_Version_1:
		export->export.v1.daemon.itf = export->hookditf ? &hooked_daemon_itf : &daemon_itf;
		break;
	default:
	case Api_Version_2:
		export->export.v2->daemon.itf = export->hookditf ? &hooked_daemon_itf : &daemon_itf;
		export->export.v2->service.itf = export->hooksvc ? &hooked_service_itf : &service_itf;
		break;
	}
}

struct afb_binding_interface_v1 *afb_export_get_interface_v1(struct afb_export *export)
{
	return export->version == Api_Version_1 ? &export->export.v1 : NULL;
}

int afb_export_unshare_session(struct afb_export *export)
{
	if (export->session == common_session) {
		export->session = afb_session_create (NULL, 0);
		if (export->session)
			afb_session_unref(common_session);
		else {
			export->session = common_session;
			return -1;
		}
	}
	return 0;
}

void afb_export_set_apiset(struct afb_export *export, struct afb_apiset *apiset)
{
	struct afb_apiset *prvset = export->apiset;
	export->apiset = afb_apiset_addref(apiset);
	afb_apiset_unref(prvset);
}

struct afb_apiset *afb_export_get_apiset(struct afb_export *export)
{
	return export->apiset;
}

/*
 * Creates a new service
 */
int afb_export_handle_events_v12(struct afb_export *export, void (*on_event)(const char *event, struct json_object *object))
{
	/* check version */
	switch (export->version) {
	case Api_Version_1: case Api_Version_2: break;
	default:
		ERROR("invalid version 12 for API %s", export->apiname);
		errno = EINVAL;
		return -1;
	}

	/* set the event handler */
	if (!on_event) {
		if (export->listener) {
			afb_evt_listener_unref(export->listener);
			export->listener = NULL;
		}
		export->on_event.v12 = on_event;
	} else {
		export->on_event.v12 = on_event;
		if (!export->listener) {
			export->listener = afb_evt_listener_create(&evt_v12_itf, export);
			if (export->listener == NULL)
				return -1;
		}
	}
	return 0;
}

/*
 * Starts a new service (v1)
 */
struct afb_binding_v1 *afb_export_register_v1(struct afb_export *export, struct afb_binding_v1 *(*regfun)(const struct afb_binding_interface_v1*))
{
	return regfun(&export->export.v1);
}

int afb_export_verbosity_get(const struct afb_export *export)
{
	switch (export->version) {
	case Api_Version_1: return export->export.v1.verbosity;
	case Api_Version_2: return export->export.v2->verbosity;
	}
	return verbosity;
}

void afb_export_verbosity_set(struct afb_export *export, int level)
{
	switch (export->version) {
	case Api_Version_1: export->export.v1.verbosity = level; break;
	case Api_Version_2: export->export.v2->verbosity = level; break;
	}
}

/*************************************************************************************************************
 *************************************************************************************************************
 *************************************************************************************************************
 *************************************************************************************************************
                                           N E W
 *************************************************************************************************************
 *************************************************************************************************************
 *************************************************************************************************************
 *************************************************************************************************************/

int afb_export_start(struct afb_export *export, int share_session, int onneed, struct afb_apiset *apiset)
{
	int rc;

	/* check state */
	if (export->state != Api_State_Pre_Init) {
		/* not an error when onneed */
		if (onneed != 0)
			goto done;

		/* already started: it is an error */
		ERROR("Service of API %s already started", export->apiname);
		return -1;
	}

	/* unshare the session if asked */
	if (!share_session) {
		rc = afb_export_unshare_session(export);
		if (rc < 0) {
			ERROR("Can't unshare the session for %s", export->apiname);
			return -1;
		}
	}

	/* set event handling */
	switch (export->version) {
	case Api_Version_1:
	case Api_Version_2:
		rc = afb_export_handle_events_v12(export, export->on_event.v12);
		break;
	default:
		rc = 0;
		break;
	}
	if (rc < 0) {
		ERROR("Can't set event handler for %s", export->apiname);
		return -1;
	}

	/* Starts the service */
	if (export->hooksvc & afb_hook_flag_svc_start_before)
		afb_hook_svc_start_before(export);
	export->state = Api_State_Init;
	switch (export->version) {
	case Api_Version_1:
		rc = export->init.v1 ? export->init.v1((struct afb_service){ .itf = &hooked_service_itf, .closure = export }) : 0;
		break;
	case Api_Version_2:
		rc = export->init.v2 ? export->init.v2() : 0;
		break;
	default:
		break;
	}
	export->state = Api_State_Run;
	if (export->hooksvc & afb_hook_flag_svc_start_after)
		afb_hook_svc_start_after(export, rc);
	if (rc < 0) {
		/* initialisation error */
		ERROR("Initialisation of service API %s failed (%d): %m", export->apiname, rc);
		return rc;
	}

done:
	return 0;
}