aboutsummaryrefslogtreecommitdiffstats
path: root/src/afb-export.c
blob: 38ef402138f32b31f3eb62c028fe02b7f985d6d0 (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
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
/*
 * Copyright (C) 2016-2019 "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 <fnmatch.h>
#include <ctype.h>

#include <json-c/json.h>
#if !defined(JSON_C_TO_STRING_NOSLASHESCAPE)
#define JSON_C_TO_STRING_NOSLASHESCAPE 0
#endif

#define AFB_BINDING_VERSION 0
#include <afb/afb-binding.h>

#include "afb-api.h"
#include "afb-apiset.h"
#if WITH_LEGACY_BINDING_V1
#include "afb-api-so-v1.h"
#endif
#if WITH_LEGACY_BINDING_V2
#include "afb-api-so-v2.h"
#endif
#include "afb-api-v3.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 "afb-calls.h"

#include "systemd.h"
#include "jobs.h"
#include "verbose.h"
#include "globset.h"
#include "sig-monitor.h"
#include "wrap-json.h"

/*************************************************************************
 * internal types
 ************************************************************************/

/*
 * Actually supported versions
 */
enum afb_api_version
{
	Api_Version_None = 0,
#if WITH_LEGACY_BINDING_V1
	Api_Version_1 = 1,
#endif
#if WITH_LEGACY_BINDING_V2
	Api_Version_2 = 2,
#endif
	Api_Version_3 = 3
};

/*
 * The states of exported APIs
 */
enum afb_api_state
{
	Api_State_Pre_Init,
	Api_State_Init,
	Api_State_Run
};

/*
 * structure of the exported API
 */
struct afb_export
{
	/* keep it first */
	struct afb_api_x3 api;

	/* reference count */
	int refcount;

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

	/* current state */
	unsigned state: 4;

	/* declared */
	unsigned declared: 1;

	/* unsealed */
	unsigned unsealed: 1;

#if WITH_AFB_HOOK
	/* hooking flags */
	int hookditf;
	int hooksvc;
#endif

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

	/* apiset the API is declared in */
	struct afb_apiset *declare_set;

	/* apiset for calls */
	struct afb_apiset *call_set;

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

	/* event handler list */
	struct globset *event_handlers;

	/* creator if any */
	struct afb_export *creator;

	/* path indication if any */
	const char *path;

	/* settings */
	struct json_object *settings;

	/* internal descriptors */
	union {
#if WITH_LEGACY_BINDING_V1
		struct afb_binding_v1 *v1;
#endif
		const struct afb_binding_v2 *v2;
		struct afb_api_v3 *v3;
	} desc;

	/* start function */
	union {
#if WITH_LEGACY_BINDING_V1
		int (*v1)(struct afb_service_x1);
#endif
		int (*v2)();
		int (*v3)(struct afb_api_x3 *api);
	} init;

	/* event handling */
	void (*on_any_event_v12)(const char *event, struct json_object *object);
	void (*on_any_event_v3)(struct afb_api_x3 *api, const char *event, struct json_object *object);

	/* exported data */
	union {
#if WITH_LEGACY_BINDING_V1
		struct afb_binding_interface_v1 v1;
#endif
		struct afb_binding_data_v2 *v2;
	} export;

	/* initial name */
	char name[];
};

/*****************************************************************************/

static inline struct afb_api_x3 *to_api_x3(struct afb_export *export)
{
	return (struct afb_api_x3*)export;
}

static inline struct afb_export *from_api_x3(struct afb_api_x3 *api)
{
	return (struct afb_export*)api;
}

struct afb_export *afb_export_from_api_x3(struct afb_api_x3 *api)
{
	return from_api_x3(api);
}

struct afb_api_x3 *afb_export_to_api_x3(struct afb_export *export)
{
	return to_api_x3(export);
}

/******************************************************************************
 ******************************************************************************
 ******************************************************************************
 ******************************************************************************
	SETTINGS
 ******************************************************************************
 ******************************************************************************
 ******************************************************************************
 ******************************************************************************/

static struct json_object *configuration;

void afb_export_set_config(struct json_object *config)
{
	struct json_object *save = configuration;
	configuration = json_object_get(config);
	json_object_put(save);
}

static struct json_object *make_settings(struct afb_export *export)
{
	struct json_object *result;
	struct json_object *obj;
	struct afb_export *iter;
	char *path;

	/* clone the globals */
	if (json_object_object_get_ex(configuration, "*", &obj))
		result = wrap_json_clone(obj);
	else
		result = json_object_new_object();

	/* add locals */
	if (json_object_object_get_ex(configuration, export->name, &obj))
		wrap_json_object_add(result, obj);

	/* add library path */
	for (iter = export ; iter && !iter->path ; iter = iter->creator);
	if (iter) {
		path = realpath(iter->path, NULL);
		json_object_object_add(result, "binding-path", json_object_new_string(path));
		free(path);
	}

	export->settings = result;
	return result;
}

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

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

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

static struct afb_event_x2 *event_x2_make_cb(struct afb_api_x3 *closure, const char *name)
{
	struct afb_export *export = from_api_x3(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->api.apiname, name);
		errno = EINVAL;
		return NULL;
	}

	/* create the event */
	return afb_evt_event_x2_create2(export->api.apiname, name);
}

static int event_broadcast_cb(struct afb_api_x3 *closure, const char *name, struct json_object *object)
{
	size_t plen, nlen;
	char *event;
	struct afb_export *export = from_api_x3(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->api.apiname, name, json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE));
		errno = EINVAL;
		return 0;
	}

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

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

static struct sd_event *get_event_loop(struct afb_api_x3 *closure)
{
	jobs_acquire_event_manager();
	return systemd_get_event_loop();
}

static struct sd_bus *get_user_bus(struct afb_api_x3 *closure)
{
	jobs_acquire_event_manager();
	return systemd_get_user_bus();
}

static struct sd_bus *get_system_bus(struct afb_api_x3 *closure)
{
	jobs_acquire_event_manager();
	return systemd_get_system_bus();
}

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

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

static int require_api_cb(struct afb_api_x3 *closure, const char *name, int initialized)
{
	struct afb_export *export = from_api_x3(closure);
	int rc, rc2;
	char *iter, *end, save;

	/* emit a warning about unexpected require in preinit */
	if (export->state == Api_State_Pre_Init && initialized) {
		ERROR("[API %s] requiring initialized apis in pre-init is forbiden", export->api.apiname);
		errno = EINVAL;
		return -1;
	}

	/* scan the names in a local copy */
	rc = 0;
	iter = strdupa(name);
	for(;;) {
		/* skip any space */
		save = *iter;
		while(isspace(save))
			save = *++iter;
		if (!save) /* at end? */
			return rc;

		/* search for the end */
		end = iter;
		while (save && !isspace(save))
			save = *++end;
		*end = 0;

		/* check the required api */
		if (export->state == Api_State_Pre_Init) {
			rc2 = afb_apiset_require(export->declare_set, export->api.apiname, iter);
			if (rc2 < 0) {
				if (rc == 0)
					WARNING("[API %s] requiring apis pre-init may lead to unexpected result", export->api.apiname);
				ERROR("[API %s] requiring api %s in pre-init failed", export->api.apiname, iter);
			}
		} else {
			rc2 = -!((initialized ? afb_apiset_lookup_started : afb_apiset_lookup)(export->call_set, iter, 1));
			if (rc2 < 0) {
				ERROR("[API %s] requiring api %s%s failed", export->api.apiname,
					 iter, initialized ? " initialized" : "");
			}
		}
		if (rc2 < 0)
			rc = rc2;

		*end = save;
		iter = end;
	}
}

static int add_alias_cb(struct afb_api_x3 *closure, const char *apiname, const char *aliasname)
{
	struct afb_export *export = from_api_x3(closure);
	if (!afb_api_is_valid_name(aliasname)) {
		ERROR("[API %s] Can't add alias to %s: bad API name", export->api.apiname, aliasname);
		errno = EINVAL;
		return -1;
	}
	NOTICE("[API %s] aliasing [API %s] to [API %s]", export->api.apiname, apiname?:"<null>", aliasname);
	afb_export_add_alias(export, apiname, aliasname);
	return 0;
}

static struct afb_api_x3 *api_new_api_cb(
		struct afb_api_x3 *closure,
		const char *api,
		const char *info,
		int noconcurrency,
		int (*preinit)(void*, struct afb_api_x3 *),
		void *preinit_closure)
{
	struct afb_export *export = from_api_x3(closure);
	struct afb_api_v3 *apiv3 = afb_api_v3_create(
					export->declare_set, export->call_set,
					api, info, noconcurrency,
					preinit, preinit_closure, 1,
					export, NULL);
	return apiv3 ? to_api_x3(afb_api_v3_export(apiv3)) : NULL;
}

#if WITH_LEGACY_BINDING_V1 || WITH_LEGACY_BINDING_V2

static void legacy_vverbose_v1_cb(struct afb_api_x3 *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_event_x1 legacy_event_x1_make_cb(struct afb_api_x3 *closure, const char *name)
{
	struct afb_event_x2 *event = event_x2_make_cb(closure, name);
	return afb_evt_event_from_evtid(afb_evt_event_x2_to_evtid(event));
}

static struct afb_req_x1 legacy_unstore_req_cb(struct afb_api_x3 *closure, struct afb_stored_req *sreq)
{
	return afb_xreq_unstore(sreq);
}

static const struct afb_daemon_itf_x1 daemon_itf = {
	.vverbose_v1 = legacy_vverbose_v1_cb,
	.vverbose_v2 = vverbose_cb,
	.event_make = legacy_event_x1_make_cb,
	.event_broadcast = event_broadcast_cb,
	.get_event_loop = get_event_loop,
	.get_user_bus = get_user_bus,
	.get_system_bus = 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 = legacy_unstore_req_cb,
	.require_api = require_api_cb,
	.add_alias = add_alias_cb,
	.new_api = api_new_api_cb,
};
#endif

#if WITH_AFB_HOOK
static void hooked_vverbose_cb(struct afb_api_x3 *closure, int level, const char *file, int line, const char *function, const char *fmt, va_list args)
{
	struct afb_export *export = from_api_x3(closure);
	va_list ap;
	va_copy(ap, args);
	vverbose_cb(closure, level, file, line, function, fmt, args);
	afb_hook_api_vverbose(export, level, file, line, function, fmt, ap);
	va_end(ap);
}

static struct afb_event_x2 *hooked_event_x2_make_cb(struct afb_api_x3 *closure, const char *name)
{
	struct afb_export *export = from_api_x3(closure);
	struct afb_event_x2 *r = event_x2_make_cb(closure, name);
	afb_hook_api_event_make(export, name, r);
	return r;
}

static int hooked_event_broadcast_cb(struct afb_api_x3 *closure, const char *name, struct json_object *object)
{
	int r;
	struct afb_export *export = from_api_x3(closure);
	json_object_get(object);
	afb_hook_api_event_broadcast_before(export, name, json_object_get(object));
	r = event_broadcast_cb(closure, name, object);
	afb_hook_api_event_broadcast_after(export, name, object, r);
	json_object_put(object);
	return r;
}

static struct sd_event *hooked_get_event_loop(struct afb_api_x3 *closure)
{
	struct afb_export *export = from_api_x3(closure);
	struct sd_event *r;

	jobs_acquire_event_manager();
	r = get_event_loop(closure);
	return afb_hook_api_get_event_loop(export, r);
}

static struct sd_bus *hooked_get_user_bus(struct afb_api_x3 *closure)
{
	struct afb_export *export = from_api_x3(closure);
	struct sd_bus *r;

	jobs_acquire_event_manager();
	r = get_user_bus(closure);
	return afb_hook_api_get_user_bus(export, r);
}

static struct sd_bus *hooked_get_system_bus(struct afb_api_x3 *closure)
{
	struct afb_export *export = from_api_x3(closure);
	struct sd_bus *r;

	jobs_acquire_event_manager();
	r = get_system_bus(closure);
	return afb_hook_api_get_system_bus(export, r);
}

static int hooked_rootdir_get_fd(struct afb_api_x3 *closure)
{
	struct afb_export *export = from_api_x3(closure);
	int r = afb_common_rootdir_get_fd();
	return afb_hook_api_rootdir_get_fd(export, r);
}

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

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

static int hooked_require_api_cb(struct afb_api_x3 *closure, const char *name, int initialized)
{
	int result;
	struct afb_export *export = from_api_x3(closure);
	afb_hook_api_require_api(export, name, initialized);
	result = require_api_cb(closure, name, initialized);
	return afb_hook_api_require_api_result(export, name, initialized, result);
}

static int hooked_add_alias_cb(struct afb_api_x3 *closure, const char *apiname, const char *aliasname)
{
	struct afb_export *export = from_api_x3(closure);
	int result = add_alias_cb(closure, apiname, aliasname);
	return afb_hook_api_add_alias(export, apiname, aliasname, result);
}

static struct afb_api_x3 *hooked_api_new_api_cb(
		struct afb_api_x3 *closure,
		const char *api,
		const char *info,
		int noconcurrency,
		int (*preinit)(void*, struct afb_api_x3 *),
		void *preinit_closure)
{
	struct afb_api_x3 *result;
	struct afb_export *export = from_api_x3(closure);
	afb_hook_api_new_api_before(export, api, info, noconcurrency);
	result = api_new_api_cb(closure, api, info, noconcurrency, preinit, preinit_closure);
	afb_hook_api_new_api_after(export, -!result, api);
	return result;
}

#if WITH_LEGACY_BINDING_V1 || WITH_LEGACY_BINDING_V2

static void legacy_hooked_vverbose_v1_cb(struct afb_api_x3 *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_event_x1 legacy_hooked_event_x1_make_cb(struct afb_api_x3 *closure, const char *name)
{
	struct afb_event_x2 *event = hooked_event_x2_make_cb(closure, name);
	struct afb_event_x1 e;
	e.closure = event;
	e.itf = event ? event->itf : NULL;
	return e;
}

static struct afb_req_x1 legacy_hooked_unstore_req_cb(struct afb_api_x3 *closure, struct afb_stored_req *sreq)
{
	struct afb_export *export = from_api_x3(closure);
	afb_hook_api_legacy_unstore_req(export, sreq);
	return legacy_unstore_req_cb(closure, sreq);
}

static const struct afb_daemon_itf_x1 hooked_daemon_itf = {
	.vverbose_v1 = legacy_hooked_vverbose_v1_cb,
	.vverbose_v2 = hooked_vverbose_cb,
	.event_make = legacy_hooked_event_x1_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 = legacy_hooked_unstore_req_cb,
	.require_api = hooked_require_api_cb,
	.add_alias = hooked_add_alias_cb,
	.new_api = hooked_api_new_api_cb,
};
#endif

#endif

/******************************************************************************
 ******************************************************************************
 ******************************************************************************
 ******************************************************************************
                                           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
 ******************************************************************************
 ******************************************************************************
 ******************************************************************************
 ******************************************************************************/

static void call_x3(
		struct afb_api_x3 *apix3,
		const char *api,
		const char *verb,
		struct json_object *args,
		void (*callback)(void*, struct json_object*, const char *error, const char *info, struct afb_api_x3*),
		void *closure)
{
	struct afb_export *export = from_api_x3(apix3);
	return afb_calls_call(export, api, verb, args, callback, closure);
}

static int call_sync_x3(
		struct afb_api_x3 *apix3,
		const char *api,
		const char *verb,
		struct json_object *args,
		struct json_object **object,
		char **error,
		char **info)
{
	struct afb_export *export = from_api_x3(apix3);
	return afb_calls_call_sync(export, api, verb, args, object, error, info);
}

static void legacy_call_x3(
		struct afb_api_x3 *apix3,
		const char *api,
		const char *verb,
		struct json_object *args,
		void (*callback)(void*, int, struct json_object*, struct afb_api_x3*),
		void *closure)
{
	struct afb_export *export = from_api_x3(apix3);
	afb_calls_legacy_call_v3(export, api, verb, args, callback, closure);
}

static int legacy_call_sync(
		struct afb_api_x3 *apix3,
		const char *api,
		const char *verb,
		struct json_object *args,
		struct json_object **result)
{
	struct afb_export *export = from_api_x3(apix3);
	return afb_calls_legacy_call_sync(export, api, verb, args, result);
}

#if WITH_LEGACY_BINDING_V1 || WITH_LEGACY_BINDING_V2

static void legacy_call_v12(
		struct afb_api_x3 *apix3,
		const char *api,
		const char *verb,
		struct json_object *args,
		void (*callback)(void*, int, struct json_object*),
		void *closure)
{
	struct afb_export *export = from_api_x3(apix3);
	afb_calls_legacy_call_v12(export, api, verb, args, callback, closure);
}

/* the interface for services */
static const struct afb_service_itf_x1 service_itf = {
	.call = legacy_call_v12,
	.call_sync = legacy_call_sync
};
#endif

#if WITH_AFB_HOOK
static void hooked_call_x3(
		struct afb_api_x3 *apix3,
		const char *api,
		const char *verb,
		struct json_object *args,
		void (*callback)(void*, struct json_object*, const char*, const char*, struct afb_api_x3*),
		void *closure)
{
	struct afb_export *export = from_api_x3(apix3);
	afb_calls_hooked_call(export, api, verb, args, callback, closure);
}

static int hooked_call_sync_x3(
		struct afb_api_x3 *apix3,
		const char *api,
		const char *verb,
		struct json_object *args,
		struct json_object **object,
		char **error,
		char **info)
{
	struct afb_export *export = from_api_x3(apix3);
	return afb_calls_hooked_call_sync(export, api, verb, args, object, error, info);
}

static void legacy_hooked_call_x3(
		struct afb_api_x3 *apix3,
		const char *api,
		const char *verb,
		struct json_object *args,
		void (*callback)(void*, int, struct json_object*, struct afb_api_x3*),
		void *closure)
{
	struct afb_export *export = from_api_x3(apix3);
	afb_calls_legacy_hooked_call_v3(export, api, verb, args, callback, closure);
}

static int legacy_hooked_call_sync(
		struct afb_api_x3 *apix3,
		const char *api,
		const char *verb,
		struct json_object *args,
		struct json_object **result)
{
	struct afb_export *export = from_api_x3(apix3);
	return afb_calls_legacy_hooked_call_sync(export, api, verb, args, result);
}

#if WITH_LEGACY_BINDING_V1 || WITH_LEGACY_BINDING_V2

static void legacy_hooked_call_v12(
		struct afb_api_x3 *apix3,
		const char *api,
		const char *verb,
		struct json_object *args,
		void (*callback)(void*, int, struct json_object*),
		void *closure)
{
	struct afb_export *export = from_api_x3(apix3);
	afb_calls_legacy_hooked_call_v12(export, api, verb, args, callback, closure);
}

/* the interface for services */
static const struct afb_service_itf_x1 hooked_service_itf = {
	.call = legacy_hooked_call_v12,
	.call_sync = legacy_hooked_call_sync
};
#endif

#endif

/******************************************************************************
 ******************************************************************************
 ******************************************************************************
 ******************************************************************************
                                           F R O M     D Y N A P I
 ******************************************************************************
 ******************************************************************************
 ******************************************************************************
 ******************************************************************************/

static int api_set_verbs_v2_cb(
		struct afb_api_x3 *api,
		const struct afb_verb_v2 *verbs)
{
#if WITH_LEGACY_BINDING_V2
	struct afb_export *export = from_api_x3(api);

	if (export->unsealed) {
		afb_api_v3_set_verbs_v2(export->desc.v3, verbs);
		return 0;
	}

	errno = EPERM;
#else
	errno = ECANCELED;
#endif
	return -1;
}

static int api_set_verbs_v3_cb(
		struct afb_api_x3 *api,
		const struct afb_verb_v3 *verbs)
{
	struct afb_export *export = from_api_x3(api);

	if (!export->unsealed) {
		errno = EPERM;
		return -1;
	}

	afb_api_v3_set_verbs_v3(export->desc.v3, verbs);
	return 0;
}

static int api_add_verb_cb(
		struct afb_api_x3 *api,
		const char *verb,
		const char *info,
		void (*callback)(struct afb_req_x2 *req),
		void *vcbdata,
		const struct afb_auth *auth,
		uint32_t session,
		int glob)
{
	struct afb_export *export = from_api_x3(api);

	if (!export->unsealed) {
		errno = EPERM;
		return -1;
	}

	return afb_api_v3_add_verb(export->desc.v3, verb, info, callback, vcbdata, auth, (uint16_t)session, glob);
}

static int api_del_verb_cb(
		struct afb_api_x3 *api,
		const char *verb,
		void **vcbdata)
{
	struct afb_export *export = from_api_x3(api);

	if (!export->unsealed) {
		errno = EPERM;
		return -1;
	}

	return afb_api_v3_del_verb(export->desc.v3, verb, vcbdata);
}

static int api_set_on_event_cb(
		struct afb_api_x3 *api,
		void (*onevent)(struct afb_api_x3 *api, const char *event, struct json_object *object))
{
	struct afb_export *export = from_api_x3(api);
	return afb_export_handle_events_v3(export, onevent);
}

static int api_set_on_init_cb(
		struct afb_api_x3 *api,
		int (*oninit)(struct afb_api_x3 *api))
{
	struct afb_export *export = from_api_x3(api);

	return afb_export_handle_init_v3(export, oninit);
}

static void api_seal_cb(
		struct afb_api_x3 *api)
{
	struct afb_export *export = from_api_x3(api);

	export->unsealed = 0;
}

static int event_handler_add_cb(
		struct afb_api_x3 *api,
		const char *pattern,
		void (*callback)(void *, const char*, struct json_object*, struct afb_api_x3*),
		void *closure)
{
	struct afb_export *export = from_api_x3(api);

	return afb_export_event_handler_add(export, pattern, callback, closure);
}

static int event_handler_del_cb(
		struct afb_api_x3 *api,
		const char *pattern,
		void **closure)
{
	struct afb_export *export = from_api_x3(api);

	return afb_export_event_handler_del(export, pattern, closure);
}

static int class_provide_cb(struct afb_api_x3 *api, const char *name)
{
	struct afb_export *export = from_api_x3(api);

	int rc = 0, rc2;
	char *iter, *end, save;

	iter = strdupa(name);
	for(;;) {
		/* skip any space */
		save = *iter;
		while(isspace(save))
			save = *++iter;
		if (!save) /* at end? */
			return rc;

		/* search for the end */
		end = iter;
		while (save && !isspace(save))
			save = *++end;
		*end = 0;

		rc2 = afb_apiset_provide_class(export->declare_set, api->apiname, iter);
		if (rc2 < 0)
			rc = rc2;

		*end = save;
		iter = end;
	}
}

static int class_require_cb(struct afb_api_x3 *api, const char *name)
{
	struct afb_export *export = from_api_x3(api);

	int rc = 0, rc2;
	char *iter, *end, save;

	iter = strdupa(name);
	for(;;) {
		/* skip any space */
		save = *iter;
		while(isspace(save))
			save = *++iter;
		if (!save) /* at end? */
			return rc;

		/* search for the end */
		end = iter;
		while (save && !isspace(save))
			save = *++end;
		*end = 0;

		rc2 = afb_apiset_require_class(export->declare_set, api->apiname, iter);
		if (rc2 < 0)
			rc = rc2;

		*end = save;
		iter = end;
	}
}

static int delete_api_cb(struct afb_api_x3 *api)
{
	struct afb_export *export = from_api_x3(api);

	if (!export->unsealed) {
		errno = EPERM;
		return -1;
	}

	afb_export_undeclare(export);
	afb_export_unref(export);
	return 0;
}

static struct json_object *settings_cb(struct afb_api_x3 *api)
{
	struct afb_export *export = from_api_x3(api);
	struct json_object *result = export->settings;
	if (!result)
		result = make_settings(export);
	return result;
}

static const struct afb_api_x3_itf api_x3_itf = {

	.vverbose = (void*)vverbose_cb,

	.get_event_loop = get_event_loop,
	.get_user_bus = get_user_bus,
	.get_system_bus = get_system_bus,
	.rootdir_get_fd = afb_common_rootdir_get_fd,
	.rootdir_open_locale = rootdir_open_locale_cb,
	.queue_job = queue_job_cb,

	.require_api = require_api_cb,
	.add_alias = add_alias_cb,

	.event_broadcast = event_broadcast_cb,
	.event_make = event_x2_make_cb,

	.legacy_call = legacy_call_x3,
	.legacy_call_sync = legacy_call_sync,

	.api_new_api = api_new_api_cb,
	.api_set_verbs_v2 = api_set_verbs_v2_cb,
	.api_add_verb = api_add_verb_cb,
	.api_del_verb = api_del_verb_cb,
	.api_set_on_event = api_set_on_event_cb,
	.api_set_on_init = api_set_on_init_cb,
	.api_seal = api_seal_cb,
	.api_set_verbs_v3 = api_set_verbs_v3_cb,
	.event_handler_add = event_handler_add_cb,
	.event_handler_del = event_handler_del_cb,

	.call = call_x3,
	.call_sync = call_sync_x3,

	.class_provide = class_provide_cb,
	.class_require = class_require_cb,

	.delete_api = delete_api_cb,
	.settings = settings_cb,
};

#if WITH_AFB_HOOK
static int hooked_api_set_verbs_v2_cb(
		struct afb_api_x3 *api,
		const struct afb_verb_v2 *verbs)
{
	struct afb_export *export = from_api_x3(api);
	int result = api_set_verbs_v2_cb(api, verbs);
	return afb_hook_api_api_set_verbs_v2(export, result, verbs);
}

static int hooked_api_set_verbs_v3_cb(
		struct afb_api_x3 *api,
		const struct afb_verb_v3 *verbs)
{
	struct afb_export *export = from_api_x3(api);
	int result = api_set_verbs_v3_cb(api, verbs);
	return afb_hook_api_api_set_verbs_v3(export, result, verbs);
}

static int hooked_api_add_verb_cb(
		struct afb_api_x3 *api,
		const char *verb,
		const char *info,
		void (*callback)(struct afb_req_x2 *req),
		void *vcbdata,
		const struct afb_auth *auth,
		uint32_t session,
		int glob)
{
	struct afb_export *export = from_api_x3(api);
	int result = api_add_verb_cb(api, verb, info, callback, vcbdata, auth, session, glob);
	return afb_hook_api_api_add_verb(export, result, verb, info, glob);
}

static int hooked_api_del_verb_cb(
		struct afb_api_x3 *api,
		const char *verb,
		void **vcbdata)
{
	struct afb_export *export = from_api_x3(api);
	int result = api_del_verb_cb(api, verb, vcbdata);
	return afb_hook_api_api_del_verb(export, result, verb);
}

static int hooked_api_set_on_event_cb(
		struct afb_api_x3 *api,
		void (*onevent)(struct afb_api_x3 *api, const char *event, struct json_object *object))
{
	struct afb_export *export = from_api_x3(api);
	int result = api_set_on_event_cb(api, onevent);
	return afb_hook_api_api_set_on_event(export, result);
}

static int hooked_api_set_on_init_cb(
		struct afb_api_x3 *api,
		int (*oninit)(struct afb_api_x3 *api))
{
	struct afb_export *export = from_api_x3(api);
	int result = api_set_on_init_cb(api, oninit);
	return afb_hook_api_api_set_on_init(export, result);
}

static void hooked_api_seal_cb(
		struct afb_api_x3 *api)
{
	struct afb_export *export = from_api_x3(api);
	afb_hook_api_api_seal(export);
	api_seal_cb(api);
}

static int hooked_event_handler_add_cb(
		struct afb_api_x3 *api,
		const char *pattern,
		void (*callback)(void *, const char*, struct json_object*, struct afb_api_x3*),
		void *closure)
{
	struct afb_export *export = from_api_x3(api);
	int result = event_handler_add_cb(api, pattern, callback, closure);
	return afb_hook_api_event_handler_add(export, result, pattern);
}

static int hooked_event_handler_del_cb(
		struct afb_api_x3 *api,
		const char *pattern,
		void **closure)
{
	struct afb_export *export = from_api_x3(api);
	int result = event_handler_del_cb(api, pattern, closure);
	return afb_hook_api_event_handler_del(export, result, pattern);
}

static int hooked_class_provide_cb(struct afb_api_x3 *api, const char *name)
{
	struct afb_export *export = from_api_x3(api);
	int result = class_provide_cb(api, name);
	return afb_hook_api_class_provide(export, result, name);
}

static int hooked_class_require_cb(struct afb_api_x3 *api, const char *name)
{
	struct afb_export *export = from_api_x3(api);
	int result = class_require_cb(api, name);
	return afb_hook_api_class_require(export, result, name);
}

static int hooked_delete_api_cb(struct afb_api_x3 *api)
{
	struct afb_export *export = afb_export_addref(from_api_x3(api));
	int result = delete_api_cb(api);
	result = afb_hook_api_delete_api(export, result);
	afb_export_unref(export);
	return result;
}

static struct json_object *hooked_settings_cb(struct afb_api_x3 *api)
{
	struct afb_export *export = from_api_x3(api);
	struct json_object *result = settings_cb(api);
	result = afb_hook_api_settings(export, result);
	return result;
}

static const struct afb_api_x3_itf hooked_api_x3_itf = {

	.vverbose = hooked_vverbose_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,

	.require_api = hooked_require_api_cb,
	.add_alias = hooked_add_alias_cb,

	.event_broadcast = hooked_event_broadcast_cb,
	.event_make = hooked_event_x2_make_cb,

	.legacy_call = legacy_hooked_call_x3,
	.legacy_call_sync = legacy_hooked_call_sync,

	.api_new_api = hooked_api_new_api_cb,
	.api_set_verbs_v2 = hooked_api_set_verbs_v2_cb,
	.api_add_verb = hooked_api_add_verb_cb,
	.api_del_verb = hooked_api_del_verb_cb,
	.api_set_on_event = hooked_api_set_on_event_cb,
	.api_set_on_init = hooked_api_set_on_init_cb,
	.api_seal = hooked_api_seal_cb,
	.api_set_verbs_v3 = hooked_api_set_verbs_v3_cb,
	.event_handler_add = hooked_event_handler_add_cb,
	.event_handler_del = hooked_event_handler_del_cb,

	.call = hooked_call_x3,
	.call_sync = hooked_call_sync_x3,

	.class_provide = hooked_class_provide_cb,
	.class_require = hooked_class_require_cb,

	.delete_api = hooked_delete_api_cb,
	.settings = hooked_settings_cb,
};
#endif

/******************************************************************************
 ******************************************************************************
 ******************************************************************************
 ******************************************************************************
                      L I S T E N E R S
 ******************************************************************************
 ******************************************************************************
 ******************************************************************************
 ******************************************************************************/

/*
 * Propagates the event to the service
 */
static void listener_of_events(void *closure, const char *event, int eventid, struct json_object *object)
{
	const struct globset_handler *handler;
	void (*callback)(void *, const char*, struct json_object*, struct afb_api_x3*);
	struct afb_export *export = from_api_x3(closure);

#if WITH_AFB_HOOK
	/* hook the event before */
	if (export->hooksvc & afb_hook_flag_api_on_event)
		afb_hook_api_on_event_before(export, event, eventid, object);
#endif

	/* transmit to specific handlers */
	/* search the handler */
	handler = export->event_handlers ? globset_match(export->event_handlers, event) : NULL;
	if (handler) {
		callback = handler->callback;
#if WITH_AFB_HOOK
		if (!(export->hooksvc & afb_hook_flag_api_on_event_handler))
#endif
			callback(handler->closure, event, object, to_api_x3(export));
#if WITH_AFB_HOOK
		else {
			afb_hook_api_on_event_handler_before(export, event, eventid, object, handler->pattern);
			callback(handler->closure, event, object, to_api_x3(export));
			afb_hook_api_on_event_handler_after(export, event, eventid, object, handler->pattern);
		}
#endif
	} else {
		/* transmit to default handler */
		if (export->on_any_event_v3)
			export->on_any_event_v3(to_api_x3(export), event, object);
		else if (export->on_any_event_v12)
			export->on_any_event_v12(event, object);
	}

#if WITH_AFB_HOOK
	/* hook the event after */
	if (export->hooksvc & afb_hook_flag_api_on_event)
		afb_hook_api_on_event_after(export, event, eventid, object);
#endif
	json_object_put(object);
}

static void listener_of_pushed_events(void *closure, const char *event, int eventid, struct json_object *object)
{
	listener_of_events(closure, event, eventid, object);
}

static void listener_of_broadcasted_events(void *closure, const char *event, struct json_object *object, const uuid_binary_t uuid, uint8_t hop)
{
	listener_of_events(closure, event, 0, object);
}

/* the interface for events */
static const struct afb_evt_itf evt_itf = {
	.broadcast = listener_of_broadcasted_events,
	.push = listener_of_pushed_events
};

/* ensure an existing listener */
static int ensure_listener(struct afb_export *export)
{
	if (!export->listener) {
		export->listener = afb_evt_listener_create(&evt_itf, export);
		if (export->listener == NULL)
			return -1;
	}
	return 0;
}

int afb_export_event_handler_add(
			struct afb_export *export,
			const char *pattern,
			void (*callback)(void *, const char*, struct json_object*, struct afb_api_x3*),
			void *closure)
{
	int rc;

	/* ensure the listener */
	rc = ensure_listener(export);
	if (rc < 0)
		return rc;

	/* ensure the globset for event handling */
	if (!export->event_handlers) {
		export->event_handlers = globset_create();
		if (!export->event_handlers)
			goto oom_error;
	}

	/* add the handler */
	rc = globset_add(export->event_handlers, pattern, callback, closure);
	if (rc == 0)
		return 0;

	if (errno == EEXIST) {
		ERROR("[API %s] event handler %s already exists", export->api.apiname, pattern);
		return -1;
	}

oom_error:
	ERROR("[API %s] can't allocate event handler %s", export->api.apiname, pattern);
	return -1;
}

int afb_export_event_handler_del(
			struct afb_export *export,
			const char *pattern,
			void **closure)
{
	if (export->event_handlers
	&& !globset_del(export->event_handlers, pattern, closure))
		return 0;

	ERROR("[API %s] event handler %s not found", export->api.apiname, pattern);
	errno = ENOENT;
	return -1;
}

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

static void set_interfaces(struct afb_export *export)
{
#if WITH_AFB_HOOK
	export->hookditf = afb_hook_flags_api(export->api.apiname);
	export->hooksvc = afb_hook_flags_api(export->api.apiname);
	export->api.itf = export->hookditf|export->hooksvc ? &hooked_api_x3_itf : &api_x3_itf;

	switch (export->version) {
#if WITH_LEGACY_BINDING_V1
	case Api_Version_1:
		export->export.v1.daemon.itf = export->hookditf ? &hooked_daemon_itf : &daemon_itf;
		break;
#endif
#if WITH_LEGACY_BINDING_V2
	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;
#endif
	}
#else

	export->api.itf = &api_x3_itf;

	switch (export->version) {
#if WITH_LEGACY_BINDING_V1
	case Api_Version_1:
		export->export.v1.daemon.itf = &daemon_itf;
		break;
#endif
#if WITH_LEGACY_BINDING_V2
	case Api_Version_2:
		export->export.v2->daemon.itf = &daemon_itf;
		export->export.v2->service.itf = &service_itf;
		break;
#endif
	}

#endif
}

static struct afb_export *create(
				struct afb_apiset *declare_set,
				struct afb_apiset *call_set,
				const char *apiname,
				const char *path,
				enum afb_api_version version)
{
	struct afb_export *export;
	size_t lenapi;

	/* session shared with other exports */
	if (common_session == NULL) {
		common_session = afb_session_create (0);
		if (common_session == NULL)
			return NULL;
	}
	lenapi = strlen(apiname);
	export = calloc(1, sizeof *export + 1 + lenapi + (path == apiname || !path ? 0 : 1 + strlen(path)));
	if (!export)
		errno = ENOMEM;
	else {
		export->refcount = 1;
		strcpy(export->name, apiname);
		export->api.apiname = export->name;
		if (path == apiname)
			export->path = export->name;
		else if (path)
			export->path = strcpy(&export->name[lenapi + 1], path);
		export->version = version;
		export->state = Api_State_Pre_Init;
		export->session = afb_session_addref(common_session);
		export->declare_set = afb_apiset_addref(declare_set);
		export->call_set = afb_apiset_addref(call_set);
	}
	return export;
}

struct afb_export *afb_export_addref(struct afb_export *export)
{
	if (export)
		__atomic_add_fetch(&export->refcount, 1, __ATOMIC_RELAXED);
	return export;
}

void afb_export_unref(struct afb_export *export)
{
	if (export && !__atomic_sub_fetch(&export->refcount, 1, __ATOMIC_RELAXED))
		afb_export_destroy(export);
}

void afb_export_destroy(struct afb_export *export)
{
	if (export) {
		if (export->event_handlers)
			globset_destroy(export->event_handlers);
		if (export->listener != NULL)
			afb_evt_listener_unref(export->listener);
		afb_session_unref(export->session);
		afb_apiset_unref(export->declare_set);
		afb_apiset_unref(export->call_set);
		json_object_put(export->settings);
		afb_export_unref(export->creator);
		if (export->api.apiname != export->name)
			free((void*)export->api.apiname);
		free(export);
	}
}

struct afb_export *afb_export_create_none_for_path(
			struct afb_apiset *declare_set,
			struct afb_apiset *call_set,
			const char *path,
			int (*creator)(void*, struct afb_api_x3*),
			void *closure)
{
	struct afb_export *export = create(declare_set, call_set, path, path, Api_Version_None);
	if (export) {
		afb_export_logmask_set(export, logmask);
		set_interfaces(export);
		if (creator && creator(closure, to_api_x3(export)) < 0) {
			afb_export_unref(export);
			export = NULL;
		}
	}
	return export;
}

#if WITH_LEGACY_BINDING_V1
struct afb_export *afb_export_create_v1(struct afb_apiset *declare_set,
			struct afb_apiset *call_set,
			const char *apiname,
			int (*init)(struct afb_service_x1),
			void (*onevent)(const char*, struct json_object*),
			const char* path)
{
	struct afb_export *export = create(declare_set, call_set, apiname, path, Api_Version_1);
	if (export) {
		export->init.v1 = init;
		export->on_any_event_v12 = onevent;
		export->export.v1.mode = AFB_MODE_LOCAL;
		export->export.v1.daemon.closure = to_api_x3(export);
		afb_export_logmask_set(export, logmask);
		set_interfaces(export);
	}
	return export;
}
#endif

#if WITH_LEGACY_BINDING_V2
struct afb_export *afb_export_create_v2(struct afb_apiset *declare_set,
			struct afb_apiset *call_set,
			const char *apiname,
			const struct afb_binding_v2 *binding,
			struct afb_binding_data_v2 *data,
			int (*init)(),
			void (*onevent)(const char*, struct json_object*),
			const char* path)
{
	struct afb_export *export = create(declare_set, call_set, apiname, path, Api_Version_2);
	if (export) {
		export->init.v2 = init;
		export->on_any_event_v12 = onevent;
		export->desc.v2 = binding;
		export->export.v2 = data;
		data->daemon.closure = to_api_x3(export);
		data->service.closure = to_api_x3(export);
		afb_export_logmask_set(export, logmask);
		set_interfaces(export);
	}
	return export;
}
#endif

struct afb_export *afb_export_create_v3(struct afb_apiset *declare_set,
			struct afb_apiset *call_set,
			const char *apiname,
			struct afb_api_v3 *apiv3,
			struct afb_export* creator,
			const char* path)
{
	struct afb_export *export = create(declare_set, call_set, apiname, path, Api_Version_3);
	if (export) {
		export->unsealed = 1;
		export->desc.v3 = apiv3;
		export->creator = afb_export_addref(creator);
		afb_export_logmask_set(export, logmask);
		set_interfaces(export);
	}
	return export;
}

int afb_export_add_alias(struct afb_export *export, const char *apiname, const char *aliasname)
{
	return afb_apiset_add_alias(export->declare_set, apiname ?: export->api.apiname, aliasname);
}

int afb_export_rename(struct afb_export *export, const char *apiname)
{
	char *name;

	if (export->declared) {
		errno = EBUSY;
		return -1;
	}

	/* copy the name locally */
	name = strdup(apiname);
	if (!name) {
		errno = ENOMEM;
		return -1;
	}

	if (export->api.apiname != export->name)
		free((void*)export->api.apiname);
	export->api.apiname = name;

	set_interfaces(export);
	return 0;
}

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

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

#if WITH_LEGACY_BINDING_V1 || WITH_LEGACY_BINDING_V2
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) {
#if WITH_LEGACY_BINDING_V1
	case Api_Version_1:
#endif
#if WITH_LEGACY_BINDING_V2
	case Api_Version_2:
		break;
#endif
	default:
		ERROR("invalid version 12 for API %s", export->api.apiname);
		errno = EINVAL;
		return -1;
	}

	export->on_any_event_v12 = on_event;
	return ensure_listener(export);
}
#endif

int afb_export_handle_events_v3(struct afb_export *export, void (*on_event)(struct afb_api_x3 *api, const char *event, struct json_object *object))
{
	/* check version */
	switch (export->version) {
	case Api_Version_3: break;
	default:
		ERROR("invalid version Dyn for API %s", export->api.apiname);
		errno = EINVAL;
		return -1;
	}

	export->on_any_event_v3 = on_event;
	return ensure_listener(export);
}

int afb_export_handle_init_v3(struct afb_export *export, int (*oninit)(struct afb_api_x3 *api))
{
	if (export->state != Api_State_Pre_Init) {
		ERROR("[API %s] Bad call to 'afb_api_x3_on_init', must be in PreInit", export->api.apiname);
		errno = EINVAL;
		return -1;
	}

	export->init.v3  = oninit;
	return 0;
}

#if WITH_LEGACY_BINDING_V1
/*
 * 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 export->desc.v1 = regfun(&export->export.v1);
}
#endif

int afb_export_preinit_x3(
		struct afb_export *export,
		int (*preinit)(void*, struct afb_api_x3*),
		void *closure)
{
	return preinit(closure, to_api_x3(export));
}

int afb_export_logmask_get(const struct afb_export *export)
{
	return export->api.logmask;
}

void afb_export_logmask_set(struct afb_export *export, int mask)
{
	export->api.logmask = mask;
	switch (export->version) {
#if WITH_LEGACY_BINDING_V1
	case Api_Version_1: export->export.v1.verbosity = verbosity_from_mask(mask); break;
#endif
#if WITH_LEGACY_BINDING_V2
	case Api_Version_2: export->export.v2->verbosity = verbosity_from_mask(mask); break;
#endif
	}
}

void *afb_export_userdata_get(const struct afb_export *export)
{
	return export->api.userdata;
}

void afb_export_userdata_set(struct afb_export *export, void *data)
{
	export->api.userdata = data;
}

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

struct init
{
	int return_code;
	struct afb_export *export;
};

static void do_init(int sig, void *closure)
{
	int rc = -1;
	struct init *init = closure;
	struct afb_export *export;

	if (sig)
		errno = EFAULT;
	else {
		export = init->export;
		switch (export->version) {
#if WITH_LEGACY_BINDING_V1
		case Api_Version_1:
			rc = export->init.v1 ? export->init.v1(
				(struct afb_service_x1){
#if WITH_AFB_HOOK
					.itf = &hooked_service_itf,
#else
					.itf = &service_itf,
#endif
					.closure = to_api_x3(export) }) : 0;
			break;
#endif
#if WITH_LEGACY_BINDING_V2
		case Api_Version_2:
			rc = export->init.v2 ? export->init.v2() : 0;
			break;
#endif
		case Api_Version_3:
			rc = export->init.v3 ? export->init.v3(to_api_x3(export)) : 0;
			break;
		default:
			errno = EINVAL;
			break;
		}
	}
	init->return_code = rc;
};


int afb_export_start(struct afb_export *export)
{
	struct init init;
	int rc;

	/* check state */
	switch (export->state) {
	case Api_State_Run:
		return 0;

	case Api_State_Init:
		/* starting in progress: it is an error */
		ERROR("Service of API %s required started while starting", export->api.apiname);
		return -1;

	default:
		break;
	}

	/* set event handling */
#if WITH_LEGACY_BINDING_V1 || WITH_LEGACY_BINDING_V2
	switch (export->version) {
#if WITH_LEGACY_BINDING_V1
	case Api_Version_1:
#endif
#if WITH_LEGACY_BINDING_V2
	case Api_Version_2:
#endif
		if (export->on_any_event_v12) {
			rc = afb_export_handle_events_v12(export, export->on_any_event_v12);
			break;
		}
		/*@fallthrough@*/
	default:
		rc = 0;
		break;
	}
	if (rc < 0) {
		ERROR("Can't set event handler for %s", export->api.apiname);
		return -1;
	}
#endif

#if WITH_AFB_HOOK
	/* Starts the service */
	if (export->hooksvc & afb_hook_flag_api_start)
		afb_hook_api_start_before(export);
#endif

	export->state = Api_State_Init;
	init.export = export;
	sig_monitor(0, do_init, &init);
	rc = init.return_code;
	export->state = Api_State_Run;

#if WITH_AFB_HOOK
	if (export->hooksvc & afb_hook_flag_api_start)
		afb_hook_api_start_after(export, rc);
#endif

	if (rc < 0) {
		/* initialisation error */
		ERROR("Initialisation of service API %s failed (%d): %m", export->api.apiname, rc);
		return rc;
	}

	return 0;
}

static void api_call_cb(void *closure, struct afb_xreq *xreq)
{
	struct afb_export *export = closure;

	xreq->request.api = to_api_x3(export);

	switch (export->version) {
#if WITH_LEGACY_BINDING_V1
	case Api_Version_1:
		afb_api_so_v1_process_call(export->desc.v1, xreq);
		break;
#endif
#if WITH_LEGACY_BINDING_V2
	case Api_Version_2:
		afb_api_so_v2_process_call(export->desc.v2, xreq);
		break;
#endif
	case Api_Version_3:
		afb_api_v3_process_call(export->desc.v3, xreq);
		break;
	default:
		afb_xreq_reply(xreq, NULL, "bad-api-type", NULL);
		break;
	}
}

static struct json_object *api_describe_cb(void *closure)
{
	struct afb_export *export = closure;
	struct json_object *result;

	switch (export->version) {
#if WITH_LEGACY_BINDING_V1
	case Api_Version_1:
		result = afb_api_so_v1_make_description_openAPIv3(export->desc.v1, export->api.apiname);
		break;
#endif
#if WITH_LEGACY_BINDING_V2
	case Api_Version_2:
		result = afb_api_so_v2_make_description_openAPIv3(export->desc.v2, export->api.apiname);
		break;
#endif
	case Api_Version_3:
		result = afb_api_v3_make_description_openAPIv3(export->desc.v3, export->api.apiname);
		break;
	default:
		result = NULL;
		break;
	}
	return result;
}

static int api_service_start_cb(void *closure)
{
	struct afb_export *export = closure;

	return afb_export_start(export);
}

#if WITH_AFB_HOOK
static void api_update_hooks_cb(void *closure)
	__attribute__((alias("set_interfaces")));

void afb_export_update_hooks(struct afb_export *export)
	__attribute__((alias("set_interfaces")));
#endif

static int api_get_logmask_cb(void *closure)
{
	struct afb_export *export = closure;

	return afb_export_logmask_get(export);
}

static void api_set_logmask_cb(void *closure, int level)
{
	struct afb_export *export = closure;

	afb_export_logmask_set(export, level);
}

static void api_unref_cb(void *closure)
{
	struct afb_export *export = closure;

	afb_export_unref(export);
}

static struct afb_api_itf export_api_itf =
{
	.call = api_call_cb,
	.service_start = api_service_start_cb,
#if WITH_AFB_HOOK
	.update_hooks = api_update_hooks_cb,
#endif
	.get_logmask = api_get_logmask_cb,
	.set_logmask = api_set_logmask_cb,
	.describe = api_describe_cb,
	.unref = api_unref_cb
};

int afb_export_declare(struct afb_export *export,
			int noconcurrency)
{
	int rc;
	struct afb_api_item afb_api;

	if (export->declared)
		rc = 0;
	else {
		/* init the record structure */
		afb_api.closure = afb_export_addref(export);
		afb_api.itf = &export_api_itf;
		afb_api.group = noconcurrency ? export : NULL;

		/* records the binding */
		rc = afb_apiset_add(export->declare_set, export->api.apiname, afb_api);
		if (rc >= 0)
			export->declared = 1;
		else {
			ERROR("can't declare export %s to set %s, ABORTING it!",
				export->api.apiname,
				afb_apiset_name(export->declare_set));
			afb_export_unref(export);
		}
	}

	return rc;
}

void afb_export_undeclare(struct afb_export *export)
{
	if (export->declared) {
		export->declared = 0;
		afb_apiset_del(export->declare_set, export->api.apiname);
	}
}

int afb_export_subscribe(struct afb_export *export, struct afb_event_x2 *event)
{
	return afb_evt_event_x2_add_watch(export->listener, event);
}

int afb_export_unsubscribe(struct afb_export *export, struct afb_event_x2 *event)
{
	return afb_evt_event_x2_remove_watch(export->listener, event);
}

void afb_export_process_xreq(struct afb_export *export, struct afb_xreq *xreq)
{
	afb_xreq_process(xreq, export->call_set);
}

void afb_export_context_init(struct afb_export *export, struct afb_context *context)
{
	afb_context_init(context, export->session, NULL);
	context->validated = 1;
}