Add support for L4Re Virtio Sockets
[src/app-framework-binder.git] / src / afb-export.c
1 /*
2  * Copyright (C) 2016-2019 "IoT.bzh"
3  * Author: José Bollo <jose.bollo@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #define _GNU_SOURCE
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <ctype.h>
24
25 #include <json-c/json.h>
26 #if !defined(JSON_C_TO_STRING_NOSLASHESCAPE)
27 #define JSON_C_TO_STRING_NOSLASHESCAPE 0
28 #endif
29
30 #define AFB_BINDING_VERSION 0
31 #include <afb/afb-binding.h>
32
33 #include "afb-api.h"
34 #include "afb-apiset.h"
35 #if WITH_LEGACY_BINDING_V1
36 #include "afb-api-so-v1.h"
37 #endif
38 #if WITH_LEGACY_BINDING_V2
39 #include "afb-api-so-v2.h"
40 #endif
41 #include "afb-api-v3.h"
42 #include "afb-common.h"
43 #include "afb-cred.h"
44 #include "afb-evt.h"
45 #include "afb-export.h"
46 #include "afb-hook.h"
47 #include "afb-msg-json.h"
48 #include "afb-session.h"
49 #include "afb-xreq.h"
50 #include "afb-calls.h"
51
52 #if WITH_SYSTEMD
53 #include "systemd.h"
54 #endif
55 #include "jobs.h"
56 #include "verbose.h"
57 #include "globset.h"
58 #include "sig-monitor.h"
59 #include "wrap-json.h"
60
61 /*************************************************************************
62  * internal types
63  ************************************************************************/
64
65 /*
66  * Actually supported versions
67  */
68 enum afb_api_version
69 {
70         Api_Version_None = 0,
71 #if WITH_LEGACY_BINDING_V1
72         Api_Version_1 = 1,
73 #endif
74 #if WITH_LEGACY_BINDING_V2
75         Api_Version_2 = 2,
76 #endif
77         Api_Version_3 = 3
78 };
79
80 /*
81  * The states of exported APIs
82  */
83 enum afb_api_state
84 {
85         Api_State_Pre_Init,
86         Api_State_Init,
87         Api_State_Run
88 };
89
90 /*
91  * structure of the exported API
92  */
93 struct afb_export
94 {
95         /* keep it first */
96         struct afb_api_x3 api;
97
98         /* reference count */
99         int refcount;
100
101         /* version of the api */
102         unsigned version: 4;
103
104         /* current state */
105         unsigned state: 4;
106
107         /* declared */
108         unsigned declared: 1;
109
110         /* unsealed */
111         unsigned unsealed: 1;
112
113 #if WITH_AFB_HOOK
114         /* hooking flags */
115         int hookditf;
116         int hooksvc;
117 #endif
118
119         /* session for service */
120         struct afb_session *session;
121
122         /* apiset the API is declared in */
123         struct afb_apiset *declare_set;
124
125         /* apiset for calls */
126         struct afb_apiset *call_set;
127
128         /* event listener for service or NULL */
129         struct afb_evt_listener *listener;
130
131         /* event handler list */
132         struct globset *event_handlers;
133
134         /* creator if any */
135         struct afb_export *creator;
136
137         /* path indication if any */
138         const char *path;
139
140         /* settings */
141         struct json_object *settings;
142
143         /* internal descriptors */
144         union {
145 #if WITH_LEGACY_BINDING_V1
146                 struct afb_binding_v1 *v1;
147 #endif
148                 const struct afb_binding_v2 *v2;
149                 struct afb_api_v3 *v3;
150         } desc;
151
152         /* start function */
153         union {
154 #if WITH_LEGACY_BINDING_V1
155                 int (*v1)(struct afb_service_x1);
156 #endif
157                 int (*v2)();
158                 int (*v3)(struct afb_api_x3 *api);
159         } init;
160
161         /* event handling */
162         void (*on_any_event_v12)(const char *event, struct json_object *object);
163         void (*on_any_event_v3)(struct afb_api_x3 *api, const char *event, struct json_object *object);
164
165         /* exported data */
166         union {
167 #if WITH_LEGACY_BINDING_V1
168                 struct afb_binding_interface_v1 v1;
169 #endif
170                 struct afb_binding_data_v2 *v2;
171         } export;
172
173         /* initial name */
174         char name[1];
175 };
176
177 /*****************************************************************************/
178
179 static inline struct afb_api_x3 *to_api_x3(struct afb_export *export)
180 {
181         return (struct afb_api_x3*)export;
182 }
183
184 static inline struct afb_export *from_api_x3(struct afb_api_x3 *api)
185 {
186         return (struct afb_export*)api;
187 }
188
189 struct afb_export *afb_export_from_api_x3(struct afb_api_x3 *api)
190 {
191         return from_api_x3(api);
192 }
193
194 struct afb_api_x3 *afb_export_to_api_x3(struct afb_export *export)
195 {
196         return to_api_x3(export);
197 }
198
199 /******************************************************************************
200  ******************************************************************************
201  ******************************************************************************
202  ******************************************************************************
203         SETTINGS
204  ******************************************************************************
205  ******************************************************************************
206  ******************************************************************************
207  ******************************************************************************/
208
209 static struct json_object *configuration;
210
211 void afb_export_set_config(struct json_object *config)
212 {
213         struct json_object *save = configuration;
214         configuration = json_object_get(config);
215         json_object_put(save);
216 }
217
218 static struct json_object *make_settings(struct afb_export *export)
219 {
220         struct json_object *result;
221         struct json_object *obj;
222         struct afb_export *iter;
223         char *path;
224
225         /* clone the globals */
226         if (json_object_object_get_ex(configuration, "*", &obj))
227                 result = wrap_json_clone(obj);
228         else
229                 result = json_object_new_object();
230
231         /* add locals */
232         if (json_object_object_get_ex(configuration, export->name, &obj))
233                 wrap_json_object_add(result, obj);
234
235         /* add library path */
236         for (iter = export ; iter && !iter->path ; iter = iter->creator);
237         if (iter) {
238                 path = realpath(iter->path, NULL);
239                 json_object_object_add(result, "binding-path", json_object_new_string(path));
240                 free(path);
241         }
242
243         export->settings = result;
244         return result;
245 }
246
247 /******************************************************************************
248  ******************************************************************************
249  ******************************************************************************
250  ******************************************************************************
251                                            F R O M     D I T F
252  ******************************************************************************
253  ******************************************************************************
254  ******************************************************************************
255  ******************************************************************************/
256
257 /**********************************************
258 * normal flow
259 **********************************************/
260 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)
261 {
262         char *p;
263         struct afb_export *export = from_api_x3(closure);
264
265         if (!fmt || vasprintf(&p, fmt, args) < 0)
266                 vverbose(level, file, line, function, fmt, args);
267         else {
268                 verbose(level, file, line, function, (verbose_is_colorized() == 0 ? "[API %s] %s" : COLOR_API "[API %s]" COLOR_DEFAULT " %s"), export->api.apiname, p);
269                 free(p);
270         }
271 }
272
273 static struct afb_event_x2 *event_x2_make_cb(struct afb_api_x3 *closure, const char *name)
274 {
275         struct afb_export *export = from_api_x3(closure);
276
277         /* check daemon state */
278         if (export->state == Api_State_Pre_Init) {
279                 ERROR("[API %s] Bad call to 'afb_daemon_event_make(%s)', must not be in PreInit", export->api.apiname, name);
280                 errno = EINVAL;
281                 return NULL;
282         }
283
284         /* create the event */
285         return afb_evt_event_x2_create2(export->api.apiname, name);
286 }
287
288 static int event_broadcast_cb(struct afb_api_x3 *closure, const char *name, struct json_object *object)
289 {
290         size_t plen, nlen;
291         char *event;
292         struct afb_export *export = from_api_x3(closure);
293
294         /* check daemon state */
295         if (export->state == Api_State_Pre_Init) {
296                 ERROR("[API %s] Bad call to 'afb_daemon_event_broadcast(%s, %s)', must not be in PreInit",
297                         export->api.apiname, name, json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE));
298                 errno = EINVAL;
299                 return 0;
300         }
301
302         /* makes the event name */
303         plen = strlen(export->api.apiname);
304         nlen = strlen(name);
305         event = alloca(nlen + plen + 2);
306         memcpy(event, export->api.apiname, plen);
307         event[plen] = '/';
308         memcpy(event + plen + 1, name, nlen + 1);
309
310         /* broadcast the event */
311         return afb_evt_broadcast(event, object);
312 }
313
314 static struct sd_event *get_event_loop(struct afb_api_x3 *closure)
315 {
316 #if WITH_SYSTEMD
317         jobs_acquire_event_manager();
318         return systemd_get_event_loop();
319 #else
320         errno = ENOTSUP;
321         return NULL;
322 #endif
323 }
324
325 static struct sd_bus *get_user_bus(struct afb_api_x3 *closure)
326 {
327 #if WITH_SYSTEMD
328         jobs_acquire_event_manager();
329         return systemd_get_user_bus();
330 #else
331         errno = ENOTSUP;
332         return NULL;
333 #endif
334 }
335
336 static struct sd_bus *get_system_bus(struct afb_api_x3 *closure)
337 {
338 #if WITH_SYSTEMD
339         jobs_acquire_event_manager();
340         return systemd_get_system_bus();
341 #else
342         errno = ENOTSUP;
343         return NULL;
344 #endif
345 }
346
347 static int rootdir_open_locale_cb(struct afb_api_x3 *closure, const char *filename, int flags, const char *locale)
348 {
349         return afb_common_rootdir_open_locale(filename, flags, locale);
350 }
351
352 static int queue_job_cb(struct afb_api_x3 *closure, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout)
353 {
354         return jobs_queue(group, timeout, callback, argument);
355 }
356
357 static int require_api_cb(struct afb_api_x3 *closure, const char *name, int initialized)
358 {
359         struct afb_export *export = from_api_x3(closure);
360         int rc, rc2;
361         char *iter, *end, save;
362
363         /* emit a warning about unexpected require in preinit */
364         if (export->state == Api_State_Pre_Init)
365                 WARNING("[API %s] requiring apis in pre-init may lead to unexpected result (requires%s: %s)",
366                         export->api.apiname, initialized ? " initialized" : "", name);
367
368         /* scan the names in a local copy */
369         rc = 0;
370         iter = strdupa(name);
371         for(;;) {
372                 /* skip any space */
373                 save = *iter;
374                 while(isspace(save))
375                         save = *++iter;
376                 if (!save) /* at end? */
377                         return rc;
378
379                 /* search for the end */
380                 end = iter;
381                 while (save && !isspace(save))
382                         save = *++end;
383                 *end = 0;
384
385                 /* check the required api */
386                 if (export->state == Api_State_Pre_Init)
387                         rc2 = afb_apiset_require(export->declare_set, export->api.apiname, name);
388                 else
389                         rc2 = -!((initialized ? afb_apiset_lookup_started : afb_apiset_lookup)(export->call_set, iter, 1));
390                 if (rc2 < 0)
391                         rc = rc2;
392
393                 *end = save;
394                 iter = end;
395         }
396 }
397
398 static int add_alias_cb(struct afb_api_x3 *closure, const char *apiname, const char *aliasname)
399 {
400         struct afb_export *export = from_api_x3(closure);
401         if (!afb_api_is_valid_name(aliasname)) {
402                 ERROR("[API %s] Can't add alias to %s: bad API name", export->api.apiname, aliasname);
403                 errno = EINVAL;
404                 return -1;
405         }
406         NOTICE("[API %s] aliasing [API %s] to [API %s]", export->api.apiname, apiname?:"<null>", aliasname);
407         afb_export_add_alias(export, apiname, aliasname);
408         return 0;
409 }
410
411 static struct afb_api_x3 *api_new_api_cb(
412                 struct afb_api_x3 *closure,
413                 const char *api,
414                 const char *info,
415                 int noconcurrency,
416                 int (*preinit)(void*, struct afb_api_x3 *),
417                 void *preinit_closure)
418 {
419         struct afb_export *export = from_api_x3(closure);
420         struct afb_api_v3 *apiv3 = afb_api_v3_create(
421                                         export->declare_set, export->call_set,
422                                         api, info, noconcurrency,
423                                         preinit, preinit_closure, 1,
424                                         export, NULL);
425         return apiv3 ? to_api_x3(afb_api_v3_export(apiv3)) : NULL;
426 }
427
428 #if WITH_LEGACY_BINDING_V1 || WITH_LEGACY_BINDING_V2
429
430 static void legacy_vverbose_v1_cb(struct afb_api_x3 *closure, int level, const char *file, int line, const char *fmt, va_list args)
431 {
432         vverbose_cb(closure, level, file, line, NULL, fmt, args);
433 }
434
435 static struct afb_event_x1 legacy_event_x1_make_cb(struct afb_api_x3 *closure, const char *name)
436 {
437         struct afb_event_x2 *event = event_x2_make_cb(closure, name);
438         return afb_evt_event_from_evtid(afb_evt_event_x2_to_evtid(event));
439 }
440
441 static struct afb_req_x1 legacy_unstore_req_cb(struct afb_api_x3 *closure, struct afb_stored_req *sreq)
442 {
443         return afb_xreq_unstore(sreq);
444 }
445
446 static const struct afb_daemon_itf_x1 daemon_itf = {
447         .vverbose_v1 = legacy_vverbose_v1_cb,
448         .vverbose_v2 = vverbose_cb,
449         .event_make = legacy_event_x1_make_cb,
450         .event_broadcast = event_broadcast_cb,
451         .get_event_loop = get_event_loop,
452         .get_user_bus = get_user_bus,
453         .get_system_bus = get_system_bus,
454         .rootdir_get_fd = afb_common_rootdir_get_fd,
455         .rootdir_open_locale = rootdir_open_locale_cb,
456         .queue_job = queue_job_cb,
457         .unstore_req = legacy_unstore_req_cb,
458         .require_api = require_api_cb,
459         .add_alias = add_alias_cb,
460         .new_api = api_new_api_cb,
461 };
462 #endif
463
464 #if WITH_AFB_HOOK
465 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)
466 {
467         struct afb_export *export = from_api_x3(closure);
468         va_list ap;
469         va_copy(ap, args);
470         vverbose_cb(closure, level, file, line, function, fmt, args);
471         afb_hook_api_vverbose(export, level, file, line, function, fmt, ap);
472         va_end(ap);
473 }
474
475 static struct afb_event_x2 *hooked_event_x2_make_cb(struct afb_api_x3 *closure, const char *name)
476 {
477         struct afb_export *export = from_api_x3(closure);
478         struct afb_event_x2 *r = event_x2_make_cb(closure, name);
479         afb_hook_api_event_make(export, name, r);
480         return r;
481 }
482
483 static int hooked_event_broadcast_cb(struct afb_api_x3 *closure, const char *name, struct json_object *object)
484 {
485         int r;
486         struct afb_export *export = from_api_x3(closure);
487         json_object_get(object);
488         afb_hook_api_event_broadcast_before(export, name, json_object_get(object));
489         r = event_broadcast_cb(closure, name, object);
490         afb_hook_api_event_broadcast_after(export, name, object, r);
491         json_object_put(object);
492         return r;
493 }
494
495 static struct sd_event *hooked_get_event_loop(struct afb_api_x3 *closure)
496 {
497         struct afb_export *export = from_api_x3(closure);
498         struct sd_event *r;
499
500         r = get_event_loop(closure);
501         return afb_hook_api_get_event_loop(export, r);
502 }
503
504 static struct sd_bus *hooked_get_user_bus(struct afb_api_x3 *closure)
505 {
506         struct afb_export *export = from_api_x3(closure);
507         struct sd_bus *r;
508
509         r = get_user_bus(closure);
510         return afb_hook_api_get_user_bus(export, r);
511 }
512
513 static struct sd_bus *hooked_get_system_bus(struct afb_api_x3 *closure)
514 {
515         struct afb_export *export = from_api_x3(closure);
516         struct sd_bus *r;
517
518         r = get_system_bus(closure);
519         return afb_hook_api_get_system_bus(export, r);
520 }
521
522 static int hooked_rootdir_get_fd(struct afb_api_x3 *closure)
523 {
524         struct afb_export *export = from_api_x3(closure);
525         int r = afb_common_rootdir_get_fd();
526         return afb_hook_api_rootdir_get_fd(export, r);
527 }
528
529 static int hooked_rootdir_open_locale_cb(struct afb_api_x3 *closure, const char *filename, int flags, const char *locale)
530 {
531         struct afb_export *export = from_api_x3(closure);
532         int r = rootdir_open_locale_cb(closure, filename, flags, locale);
533         return afb_hook_api_rootdir_open_locale(export, filename, flags, locale, r);
534 }
535
536 static int hooked_queue_job_cb(struct afb_api_x3 *closure, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout)
537 {
538         struct afb_export *export = from_api_x3(closure);
539         int r = queue_job_cb(closure, callback, argument, group, timeout);
540         return afb_hook_api_queue_job(export, callback, argument, group, timeout, r);
541 }
542
543 static int hooked_require_api_cb(struct afb_api_x3 *closure, const char *name, int initialized)
544 {
545         int result;
546         struct afb_export *export = from_api_x3(closure);
547         afb_hook_api_require_api(export, name, initialized);
548         result = require_api_cb(closure, name, initialized);
549         return afb_hook_api_require_api_result(export, name, initialized, result);
550 }
551
552 static int hooked_add_alias_cb(struct afb_api_x3 *closure, const char *apiname, const char *aliasname)
553 {
554         struct afb_export *export = from_api_x3(closure);
555         int result = add_alias_cb(closure, apiname, aliasname);
556         return afb_hook_api_add_alias(export, apiname, aliasname, result);
557 }
558
559 static struct afb_api_x3 *hooked_api_new_api_cb(
560                 struct afb_api_x3 *closure,
561                 const char *api,
562                 const char *info,
563                 int noconcurrency,
564                 int (*preinit)(void*, struct afb_api_x3 *),
565                 void *preinit_closure)
566 {
567         struct afb_api_x3 *result;
568         struct afb_export *export = from_api_x3(closure);
569         afb_hook_api_new_api_before(export, api, info, noconcurrency);
570         result = api_new_api_cb(closure, api, info, noconcurrency, preinit, preinit_closure);
571         afb_hook_api_new_api_after(export, -!result, api);
572         return result;
573 }
574
575 #if WITH_LEGACY_BINDING_V1 || WITH_LEGACY_BINDING_V2
576
577 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)
578 {
579         hooked_vverbose_cb(closure, level, file, line, NULL, fmt, args);
580 }
581
582 static struct afb_event_x1 legacy_hooked_event_x1_make_cb(struct afb_api_x3 *closure, const char *name)
583 {
584         struct afb_event_x2 *event = hooked_event_x2_make_cb(closure, name);
585         struct afb_event_x1 e;
586         e.closure = event;
587         e.itf = event ? event->itf : NULL;
588         return e;
589 }
590
591 static struct afb_req_x1 legacy_hooked_unstore_req_cb(struct afb_api_x3 *closure, struct afb_stored_req *sreq)
592 {
593         struct afb_export *export = from_api_x3(closure);
594         afb_hook_api_legacy_unstore_req(export, sreq);
595         return legacy_unstore_req_cb(closure, sreq);
596 }
597
598 static const struct afb_daemon_itf_x1 hooked_daemon_itf = {
599         .vverbose_v1 = legacy_hooked_vverbose_v1_cb,
600         .vverbose_v2 = hooked_vverbose_cb,
601         .event_make = legacy_hooked_event_x1_make_cb,
602         .event_broadcast = hooked_event_broadcast_cb,
603         .get_event_loop = hooked_get_event_loop,
604         .get_user_bus = hooked_get_user_bus,
605         .get_system_bus = hooked_get_system_bus,
606         .rootdir_get_fd = hooked_rootdir_get_fd,
607         .rootdir_open_locale = hooked_rootdir_open_locale_cb,
608         .queue_job = hooked_queue_job_cb,
609         .unstore_req = legacy_hooked_unstore_req_cb,
610         .require_api = hooked_require_api_cb,
611         .add_alias = hooked_add_alias_cb,
612         .new_api = hooked_api_new_api_cb,
613 };
614 #endif
615
616 #endif
617
618 /******************************************************************************
619  ******************************************************************************
620  ******************************************************************************
621  ******************************************************************************
622                                            F R O M     S V C
623  ******************************************************************************
624  ******************************************************************************
625  ******************************************************************************
626  ******************************************************************************/
627
628 /* the common session for services sharing their session */
629 static struct afb_session *common_session;
630
631 /******************************************************************************
632  ******************************************************************************
633  ******************************************************************************
634  ******************************************************************************
635                                            F R O M     S V C
636  ******************************************************************************
637  ******************************************************************************
638  ******************************************************************************
639  ******************************************************************************/
640
641 static void call_x3(
642                 struct afb_api_x3 *apix3,
643                 const char *api,
644                 const char *verb,
645                 struct json_object *args,
646                 void (*callback)(void*, struct json_object*, const char *error, const char *info, struct afb_api_x3*),
647                 void *closure)
648 {
649         struct afb_export *export = from_api_x3(apix3);
650         return afb_calls_call(export, api, verb, args, callback, closure);
651 }
652
653 static int call_sync_x3(
654                 struct afb_api_x3 *apix3,
655                 const char *api,
656                 const char *verb,
657                 struct json_object *args,
658                 struct json_object **object,
659                 char **error,
660                 char **info)
661 {
662         struct afb_export *export = from_api_x3(apix3);
663         return afb_calls_call_sync(export, api, verb, args, object, error, info);
664 }
665
666 static void legacy_call_x3(
667                 struct afb_api_x3 *apix3,
668                 const char *api,
669                 const char *verb,
670                 struct json_object *args,
671                 void (*callback)(void*, int, struct json_object*, struct afb_api_x3*),
672                 void *closure)
673 {
674         struct afb_export *export = from_api_x3(apix3);
675         afb_calls_legacy_call_v3(export, api, verb, args, callback, closure);
676 }
677
678 static int legacy_call_sync(
679                 struct afb_api_x3 *apix3,
680                 const char *api,
681                 const char *verb,
682                 struct json_object *args,
683                 struct json_object **result)
684 {
685         struct afb_export *export = from_api_x3(apix3);
686         return afb_calls_legacy_call_sync(export, api, verb, args, result);
687 }
688
689 #if WITH_LEGACY_BINDING_V1 || WITH_LEGACY_BINDING_V2
690
691 static void legacy_call_v12(
692                 struct afb_api_x3 *apix3,
693                 const char *api,
694                 const char *verb,
695                 struct json_object *args,
696                 void (*callback)(void*, int, struct json_object*),
697                 void *closure)
698 {
699         struct afb_export *export = from_api_x3(apix3);
700         afb_calls_legacy_call_v12(export, api, verb, args, callback, closure);
701 }
702
703 /* the interface for services */
704 static const struct afb_service_itf_x1 service_itf = {
705         .call = legacy_call_v12,
706         .call_sync = legacy_call_sync
707 };
708 #endif
709
710 #if WITH_AFB_HOOK
711 static void hooked_call_x3(
712                 struct afb_api_x3 *apix3,
713                 const char *api,
714                 const char *verb,
715                 struct json_object *args,
716                 void (*callback)(void*, struct json_object*, const char*, const char*, struct afb_api_x3*),
717                 void *closure)
718 {
719         struct afb_export *export = from_api_x3(apix3);
720         afb_calls_hooked_call(export, api, verb, args, callback, closure);
721 }
722
723 static int hooked_call_sync_x3(
724                 struct afb_api_x3 *apix3,
725                 const char *api,
726                 const char *verb,
727                 struct json_object *args,
728                 struct json_object **object,
729                 char **error,
730                 char **info)
731 {
732         struct afb_export *export = from_api_x3(apix3);
733         return afb_calls_hooked_call_sync(export, api, verb, args, object, error, info);
734 }
735
736 static void legacy_hooked_call_x3(
737                 struct afb_api_x3 *apix3,
738                 const char *api,
739                 const char *verb,
740                 struct json_object *args,
741                 void (*callback)(void*, int, struct json_object*, struct afb_api_x3*),
742                 void *closure)
743 {
744         struct afb_export *export = from_api_x3(apix3);
745         afb_calls_legacy_hooked_call_v3(export, api, verb, args, callback, closure);
746 }
747
748 static int legacy_hooked_call_sync(
749                 struct afb_api_x3 *apix3,
750                 const char *api,
751                 const char *verb,
752                 struct json_object *args,
753                 struct json_object **result)
754 {
755         struct afb_export *export = from_api_x3(apix3);
756         return afb_calls_legacy_hooked_call_sync(export, api, verb, args, result);
757 }
758
759 #if WITH_LEGACY_BINDING_V1 || WITH_LEGACY_BINDING_V2
760
761 static void legacy_hooked_call_v12(
762                 struct afb_api_x3 *apix3,
763                 const char *api,
764                 const char *verb,
765                 struct json_object *args,
766                 void (*callback)(void*, int, struct json_object*),
767                 void *closure)
768 {
769         struct afb_export *export = from_api_x3(apix3);
770         afb_calls_legacy_hooked_call_v12(export, api, verb, args, callback, closure);
771 }
772
773 /* the interface for services */
774 static const struct afb_service_itf_x1 hooked_service_itf = {
775         .call = legacy_hooked_call_v12,
776         .call_sync = legacy_hooked_call_sync
777 };
778 #endif
779
780 #endif
781
782 /******************************************************************************
783  ******************************************************************************
784  ******************************************************************************
785  ******************************************************************************
786                                            F R O M     D Y N A P I
787  ******************************************************************************
788  ******************************************************************************
789  ******************************************************************************
790  ******************************************************************************/
791
792 static int api_set_verbs_v2_cb(
793                 struct afb_api_x3 *api,
794                 const struct afb_verb_v2 *verbs)
795 {
796 #if WITH_LEGACY_BINDING_V2
797         struct afb_export *export = from_api_x3(api);
798
799         if (export->unsealed) {
800                 afb_api_v3_set_verbs_v2(export->desc.v3, verbs);
801                 return 0;
802         }
803
804         errno = EPERM;
805 #else
806         errno = ECANCELED;
807 #endif
808         return -1;
809 }
810
811 static int api_set_verbs_v3_cb(
812                 struct afb_api_x3 *api,
813                 const struct afb_verb_v3 *verbs)
814 {
815         struct afb_export *export = from_api_x3(api);
816
817         if (!export->unsealed) {
818                 errno = EPERM;
819                 return -1;
820         }
821
822         afb_api_v3_set_verbs_v3(export->desc.v3, verbs);
823         return 0;
824 }
825
826 static int api_add_verb_cb(
827                 struct afb_api_x3 *api,
828                 const char *verb,
829                 const char *info,
830                 void (*callback)(struct afb_req_x2 *req),
831                 void *vcbdata,
832                 const struct afb_auth *auth,
833                 uint32_t session,
834                 int glob)
835 {
836         struct afb_export *export = from_api_x3(api);
837
838         if (!export->unsealed) {
839                 errno = EPERM;
840                 return -1;
841         }
842
843         return afb_api_v3_add_verb(export->desc.v3, verb, info, callback, vcbdata, auth, (uint16_t)session, glob);
844 }
845
846 static int api_del_verb_cb(
847                 struct afb_api_x3 *api,
848                 const char *verb,
849                 void **vcbdata)
850 {
851         struct afb_export *export = from_api_x3(api);
852
853         if (!export->unsealed) {
854                 errno = EPERM;
855                 return -1;
856         }
857
858         return afb_api_v3_del_verb(export->desc.v3, verb, vcbdata);
859 }
860
861 static int api_set_on_event_cb(
862                 struct afb_api_x3 *api,
863                 void (*onevent)(struct afb_api_x3 *api, const char *event, struct json_object *object))
864 {
865         struct afb_export *export = from_api_x3(api);
866         return afb_export_handle_events_v3(export, onevent);
867 }
868
869 static int api_set_on_init_cb(
870                 struct afb_api_x3 *api,
871                 int (*oninit)(struct afb_api_x3 *api))
872 {
873         struct afb_export *export = from_api_x3(api);
874
875         return afb_export_handle_init_v3(export, oninit);
876 }
877
878 static void api_seal_cb(
879                 struct afb_api_x3 *api)
880 {
881         struct afb_export *export = from_api_x3(api);
882
883         export->unsealed = 0;
884 }
885
886 static int event_handler_add_cb(
887                 struct afb_api_x3 *api,
888                 const char *pattern,
889                 void (*callback)(void *, const char*, struct json_object*, struct afb_api_x3*),
890                 void *closure)
891 {
892         struct afb_export *export = from_api_x3(api);
893
894         return afb_export_event_handler_add(export, pattern, callback, closure);
895 }
896
897 static int event_handler_del_cb(
898                 struct afb_api_x3 *api,
899                 const char *pattern,
900                 void **closure)
901 {
902         struct afb_export *export = from_api_x3(api);
903
904         return afb_export_event_handler_del(export, pattern, closure);
905 }
906
907 static int class_provide_cb(struct afb_api_x3 *api, const char *name)
908 {
909         struct afb_export *export = from_api_x3(api);
910
911         int rc = 0, rc2;
912         char *iter, *end, save;
913
914         iter = strdupa(name);
915         for(;;) {
916                 /* skip any space */
917                 save = *iter;
918                 while(isspace(save))
919                         save = *++iter;
920                 if (!save) /* at end? */
921                         return rc;
922
923                 /* search for the end */
924                 end = iter;
925                 while (save && !isspace(save))
926                         save = *++end;
927                 *end = 0;
928
929                 rc2 = afb_apiset_provide_class(export->declare_set, api->apiname, iter);
930                 if (rc2 < 0)
931                         rc = rc2;
932
933                 *end = save;
934                 iter = end;
935         }
936 }
937
938 static int class_require_cb(struct afb_api_x3 *api, const char *name)
939 {
940         struct afb_export *export = from_api_x3(api);
941
942         int rc = 0, rc2;
943         char *iter, *end, save;
944
945         iter = strdupa(name);
946         for(;;) {
947                 /* skip any space */
948                 save = *iter;
949                 while(isspace(save))
950                         save = *++iter;
951                 if (!save) /* at end? */
952                         return rc;
953
954                 /* search for the end */
955                 end = iter;
956                 while (save && !isspace(save))
957                         save = *++end;
958                 *end = 0;
959
960                 rc2 = afb_apiset_require_class(export->declare_set, api->apiname, iter);
961                 if (rc2 < 0)
962                         rc = rc2;
963
964                 *end = save;
965                 iter = end;
966         }
967 }
968
969 static int delete_api_cb(struct afb_api_x3 *api)
970 {
971         struct afb_export *export = from_api_x3(api);
972
973         if (!export->unsealed) {
974                 errno = EPERM;
975                 return -1;
976         }
977
978         afb_export_undeclare(export);
979         afb_export_unref(export);
980         return 0;
981 }
982
983 static struct json_object *settings_cb(struct afb_api_x3 *api)
984 {
985         struct afb_export *export = from_api_x3(api);
986         struct json_object *result = export->settings;
987         if (!result)
988                 result = make_settings(export);
989         return result;
990 }
991
992 static const struct afb_api_x3_itf api_x3_itf = {
993
994         .vverbose = (void*)vverbose_cb,
995
996         .get_event_loop = get_event_loop,
997         .get_user_bus = get_user_bus,
998         .get_system_bus = get_system_bus,
999         .rootdir_get_fd = afb_common_rootdir_get_fd,
1000         .rootdir_open_locale = rootdir_open_locale_cb,
1001         .queue_job = queue_job_cb,
1002
1003         .require_api = require_api_cb,
1004         .add_alias = add_alias_cb,
1005
1006         .event_broadcast = event_broadcast_cb,
1007         .event_make = event_x2_make_cb,
1008
1009         .legacy_call = legacy_call_x3,
1010         .legacy_call_sync = legacy_call_sync,
1011
1012         .api_new_api = api_new_api_cb,
1013         .api_set_verbs_v2 = api_set_verbs_v2_cb,
1014         .api_add_verb = api_add_verb_cb,
1015         .api_del_verb = api_del_verb_cb,
1016         .api_set_on_event = api_set_on_event_cb,
1017         .api_set_on_init = api_set_on_init_cb,
1018         .api_seal = api_seal_cb,
1019         .api_set_verbs_v3 = api_set_verbs_v3_cb,
1020         .event_handler_add = event_handler_add_cb,
1021         .event_handler_del = event_handler_del_cb,
1022
1023         .call = call_x3,
1024         .call_sync = call_sync_x3,
1025
1026         .class_provide = class_provide_cb,
1027         .class_require = class_require_cb,
1028
1029         .delete_api = delete_api_cb,
1030         .settings = settings_cb,
1031 };
1032
1033 #if WITH_AFB_HOOK
1034 static int hooked_api_set_verbs_v2_cb(
1035                 struct afb_api_x3 *api,
1036                 const struct afb_verb_v2 *verbs)
1037 {
1038         struct afb_export *export = from_api_x3(api);
1039         int result = api_set_verbs_v2_cb(api, verbs);
1040         return afb_hook_api_api_set_verbs_v2(export, result, verbs);
1041 }
1042
1043 static int hooked_api_set_verbs_v3_cb(
1044                 struct afb_api_x3 *api,
1045                 const struct afb_verb_v3 *verbs)
1046 {
1047         struct afb_export *export = from_api_x3(api);
1048         int result = api_set_verbs_v3_cb(api, verbs);
1049         return afb_hook_api_api_set_verbs_v3(export, result, verbs);
1050 }
1051
1052 static int hooked_api_add_verb_cb(
1053                 struct afb_api_x3 *api,
1054                 const char *verb,
1055                 const char *info,
1056                 void (*callback)(struct afb_req_x2 *req),
1057                 void *vcbdata,
1058                 const struct afb_auth *auth,
1059                 uint32_t session,
1060                 int glob)
1061 {
1062         struct afb_export *export = from_api_x3(api);
1063         int result = api_add_verb_cb(api, verb, info, callback, vcbdata, auth, session, glob);
1064         return afb_hook_api_api_add_verb(export, result, verb, info, glob);
1065 }
1066
1067 static int hooked_api_del_verb_cb(
1068                 struct afb_api_x3 *api,
1069                 const char *verb,
1070                 void **vcbdata)
1071 {
1072         struct afb_export *export = from_api_x3(api);
1073         int result = api_del_verb_cb(api, verb, vcbdata);
1074         return afb_hook_api_api_del_verb(export, result, verb);
1075 }
1076
1077 static int hooked_api_set_on_event_cb(
1078                 struct afb_api_x3 *api,
1079                 void (*onevent)(struct afb_api_x3 *api, const char *event, struct json_object *object))
1080 {
1081         struct afb_export *export = from_api_x3(api);
1082         int result = api_set_on_event_cb(api, onevent);
1083         return afb_hook_api_api_set_on_event(export, result);
1084 }
1085
1086 static int hooked_api_set_on_init_cb(
1087                 struct afb_api_x3 *api,
1088                 int (*oninit)(struct afb_api_x3 *api))
1089 {
1090         struct afb_export *export = from_api_x3(api);
1091         int result = api_set_on_init_cb(api, oninit);
1092         return afb_hook_api_api_set_on_init(export, result);
1093 }
1094
1095 static void hooked_api_seal_cb(
1096                 struct afb_api_x3 *api)
1097 {
1098         struct afb_export *export = from_api_x3(api);
1099         afb_hook_api_api_seal(export);
1100         api_seal_cb(api);
1101 }
1102
1103 static int hooked_event_handler_add_cb(
1104                 struct afb_api_x3 *api,
1105                 const char *pattern,
1106                 void (*callback)(void *, const char*, struct json_object*, struct afb_api_x3*),
1107                 void *closure)
1108 {
1109         struct afb_export *export = from_api_x3(api);
1110         int result = event_handler_add_cb(api, pattern, callback, closure);
1111         return afb_hook_api_event_handler_add(export, result, pattern);
1112 }
1113
1114 static int hooked_event_handler_del_cb(
1115                 struct afb_api_x3 *api,
1116                 const char *pattern,
1117                 void **closure)
1118 {
1119         struct afb_export *export = from_api_x3(api);
1120         int result = event_handler_del_cb(api, pattern, closure);
1121         return afb_hook_api_event_handler_del(export, result, pattern);
1122 }
1123
1124 static int hooked_class_provide_cb(struct afb_api_x3 *api, const char *name)
1125 {
1126         struct afb_export *export = from_api_x3(api);
1127         int result = class_provide_cb(api, name);
1128         return afb_hook_api_class_provide(export, result, name);
1129 }
1130
1131 static int hooked_class_require_cb(struct afb_api_x3 *api, const char *name)
1132 {
1133         struct afb_export *export = from_api_x3(api);
1134         int result = class_require_cb(api, name);
1135         return afb_hook_api_class_require(export, result, name);
1136 }
1137
1138 static int hooked_delete_api_cb(struct afb_api_x3 *api)
1139 {
1140         struct afb_export *export = afb_export_addref(from_api_x3(api));
1141         int result = delete_api_cb(api);
1142         result = afb_hook_api_delete_api(export, result);
1143         afb_export_unref(export);
1144         return result;
1145 }
1146
1147 static struct json_object *hooked_settings_cb(struct afb_api_x3 *api)
1148 {
1149         struct afb_export *export = from_api_x3(api);
1150         struct json_object *result = settings_cb(api);
1151         result = afb_hook_api_settings(export, result);
1152         return result;
1153 }
1154
1155 static const struct afb_api_x3_itf hooked_api_x3_itf = {
1156
1157         .vverbose = hooked_vverbose_cb,
1158
1159         .get_event_loop = hooked_get_event_loop,
1160         .get_user_bus = hooked_get_user_bus,
1161         .get_system_bus = hooked_get_system_bus,
1162         .rootdir_get_fd = hooked_rootdir_get_fd,
1163         .rootdir_open_locale = hooked_rootdir_open_locale_cb,
1164         .queue_job = hooked_queue_job_cb,
1165
1166         .require_api = hooked_require_api_cb,
1167         .add_alias = hooked_add_alias_cb,
1168
1169         .event_broadcast = hooked_event_broadcast_cb,
1170         .event_make = hooked_event_x2_make_cb,
1171
1172         .legacy_call = legacy_hooked_call_x3,
1173         .legacy_call_sync = legacy_hooked_call_sync,
1174
1175         .api_new_api = hooked_api_new_api_cb,
1176         .api_set_verbs_v2 = hooked_api_set_verbs_v2_cb,
1177         .api_add_verb = hooked_api_add_verb_cb,
1178         .api_del_verb = hooked_api_del_verb_cb,
1179         .api_set_on_event = hooked_api_set_on_event_cb,
1180         .api_set_on_init = hooked_api_set_on_init_cb,
1181         .api_seal = hooked_api_seal_cb,
1182         .api_set_verbs_v3 = hooked_api_set_verbs_v3_cb,
1183         .event_handler_add = hooked_event_handler_add_cb,
1184         .event_handler_del = hooked_event_handler_del_cb,
1185
1186         .call = hooked_call_x3,
1187         .call_sync = hooked_call_sync_x3,
1188
1189         .class_provide = hooked_class_provide_cb,
1190         .class_require = hooked_class_require_cb,
1191
1192         .delete_api = hooked_delete_api_cb,
1193         .settings = hooked_settings_cb,
1194 };
1195 #endif
1196
1197 /******************************************************************************
1198  ******************************************************************************
1199  ******************************************************************************
1200  ******************************************************************************
1201                       L I S T E N E R S
1202  ******************************************************************************
1203  ******************************************************************************
1204  ******************************************************************************
1205  ******************************************************************************/
1206
1207 /*
1208  * Propagates the event to the service
1209  */
1210 static void listener_of_events(void *closure, const char *event, int eventid, struct json_object *object)
1211 {
1212         const struct globset_handler *handler;
1213         void (*callback)(void *, const char*, struct json_object*, struct afb_api_x3*);
1214         struct afb_export *export = from_api_x3(closure);
1215
1216 #if WITH_AFB_HOOK
1217         /* hook the event before */
1218         if (export->hooksvc & afb_hook_flag_api_on_event)
1219                 afb_hook_api_on_event_before(export, event, eventid, object);
1220 #endif
1221
1222         /* transmit to specific handlers */
1223         /* search the handler */
1224         handler = export->event_handlers ? globset_match(export->event_handlers, event) : NULL;
1225         if (handler) {
1226                 callback = handler->callback;
1227 #if WITH_AFB_HOOK
1228                 if (!(export->hooksvc & afb_hook_flag_api_on_event_handler))
1229 #endif
1230                         callback(handler->closure, event, object, to_api_x3(export));
1231 #if WITH_AFB_HOOK
1232                 else {
1233                         afb_hook_api_on_event_handler_before(export, event, eventid, object, handler->pattern);
1234                         callback(handler->closure, event, object, to_api_x3(export));
1235                         afb_hook_api_on_event_handler_after(export, event, eventid, object, handler->pattern);
1236                 }
1237 #endif
1238         } else {
1239                 /* transmit to default handler */
1240                 if (export->on_any_event_v3)
1241                         export->on_any_event_v3(to_api_x3(export), event, object);
1242                 else if (export->on_any_event_v12)
1243                         export->on_any_event_v12(event, object);
1244         }
1245
1246 #if WITH_AFB_HOOK
1247         /* hook the event after */
1248         if (export->hooksvc & afb_hook_flag_api_on_event)
1249                 afb_hook_api_on_event_after(export, event, eventid, object);
1250 #endif
1251         json_object_put(object);
1252 }
1253
1254 /* the interface for events */
1255 static const struct afb_evt_itf evt_itf = {
1256         .broadcast = listener_of_events,
1257         .push = listener_of_events
1258 };
1259
1260 /* ensure an existing listener */
1261 static int ensure_listener(struct afb_export *export)
1262 {
1263         if (!export->listener) {
1264                 export->listener = afb_evt_listener_create(&evt_itf, export);
1265                 if (export->listener == NULL)
1266                         return -1;
1267         }
1268         return 0;
1269 }
1270
1271 int afb_export_event_handler_add(
1272                         struct afb_export *export,
1273                         const char *pattern,
1274                         void (*callback)(void *, const char*, struct json_object*, struct afb_api_x3*),
1275                         void *closure)
1276 {
1277         int rc;
1278
1279         /* ensure the listener */
1280         rc = ensure_listener(export);
1281         if (rc < 0)
1282                 return rc;
1283
1284         /* ensure the globset for event handling */
1285         if (!export->event_handlers) {
1286                 export->event_handlers = globset_create();
1287                 if (!export->event_handlers)
1288                         goto oom_error;
1289         }
1290
1291         /* add the handler */
1292         rc = globset_add(export->event_handlers, pattern, callback, closure);
1293         if (rc == 0)
1294                 return 0;
1295
1296         if (errno == EEXIST) {
1297                 ERROR("[API %s] event handler %s already exists", export->api.apiname, pattern);
1298                 return -1;
1299         }
1300
1301 oom_error:
1302         ERROR("[API %s] can't allocate event handler %s", export->api.apiname, pattern);
1303         return -1;
1304 }
1305
1306 int afb_export_event_handler_del(
1307                         struct afb_export *export,
1308                         const char *pattern,
1309                         void **closure)
1310 {
1311         if (export->event_handlers
1312         && !globset_del(export->event_handlers, pattern, closure))
1313                 return 0;
1314
1315         ERROR("[API %s] event handler %s not found", export->api.apiname, pattern);
1316         errno = ENOENT;
1317         return -1;
1318 }
1319
1320 /******************************************************************************
1321  ******************************************************************************
1322  ******************************************************************************
1323  ******************************************************************************
1324                                            M E R G E D
1325  ******************************************************************************
1326  ******************************************************************************
1327  ******************************************************************************
1328  ******************************************************************************/
1329
1330 static void set_interfaces(struct afb_export *export)
1331 {
1332 #if WITH_AFB_HOOK
1333         export->hookditf = afb_hook_flags_api(export->api.apiname);
1334         export->hooksvc = afb_hook_flags_api(export->api.apiname);
1335         export->api.itf = export->hookditf|export->hooksvc ? &hooked_api_x3_itf : &api_x3_itf;
1336
1337         switch (export->version) {
1338 #if WITH_LEGACY_BINDING_V1
1339         case Api_Version_1:
1340                 export->export.v1.daemon.itf = export->hookditf ? &hooked_daemon_itf : &daemon_itf;
1341                 break;
1342 #endif
1343 #if WITH_LEGACY_BINDING_V2
1344         case Api_Version_2:
1345                 export->export.v2->daemon.itf = export->hookditf ? &hooked_daemon_itf : &daemon_itf;
1346                 export->export.v2->service.itf = export->hooksvc ? &hooked_service_itf : &service_itf;
1347                 break;
1348 #endif
1349         }
1350 #else
1351
1352         export->api.itf = &api_x3_itf;
1353
1354         switch (export->version) {
1355 #if WITH_LEGACY_BINDING_V1
1356         case Api_Version_1:
1357                 export->export.v1.daemon.itf = &daemon_itf;
1358                 break;
1359 #endif
1360 #if WITH_LEGACY_BINDING_V2
1361         case Api_Version_2:
1362                 export->export.v2->daemon.itf = &daemon_itf;
1363                 export->export.v2->service.itf = &service_itf;
1364                 break;
1365 #endif
1366         }
1367
1368 #endif
1369 }
1370
1371 static struct afb_export *create(
1372                                 struct afb_apiset *declare_set,
1373                                 struct afb_apiset *call_set,
1374                                 const char *apiname,
1375                                 const char *path,
1376                                 enum afb_api_version version)
1377 {
1378         struct afb_export *export;
1379         size_t lenapi;
1380
1381         /* session shared with other exports */
1382         if (common_session == NULL) {
1383                 common_session = afb_session_create (0);
1384                 if (common_session == NULL)
1385                         return NULL;
1386         }
1387         lenapi = strlen(apiname);
1388         export = calloc(1, sizeof *export + lenapi + (path == apiname || !path ? 0 : strlen(path)));
1389         if (!export)
1390                 errno = ENOMEM;
1391         else {
1392                 export->refcount = 1;
1393                 strcpy(export->name, apiname);
1394                 export->api.apiname = export->name;
1395                 if (path == apiname)
1396                         export->path = export->name;
1397                 else if (path)
1398                         export->path = strcpy(&export->name[lenapi + 1], path);
1399                 export->version = version;
1400                 export->state = Api_State_Pre_Init;
1401                 export->session = afb_session_addref(common_session);
1402                 export->declare_set = afb_apiset_addref(declare_set);
1403                 export->call_set = afb_apiset_addref(call_set);
1404         }
1405         return export;
1406 }
1407
1408 struct afb_export *afb_export_addref(struct afb_export *export)
1409 {
1410         if (export)
1411                 __atomic_add_fetch(&export->refcount, 1, __ATOMIC_RELAXED);
1412         return export;
1413 }
1414
1415 void afb_export_unref(struct afb_export *export)
1416 {
1417         if (export && !__atomic_sub_fetch(&export->refcount, 1, __ATOMIC_RELAXED))
1418                 afb_export_destroy(export);
1419 }
1420
1421 void afb_export_destroy(struct afb_export *export)
1422 {
1423         if (export) {
1424                 if (export->event_handlers)
1425                         globset_destroy(export->event_handlers);
1426                 if (export->listener != NULL)
1427                         afb_evt_listener_unref(export->listener);
1428                 afb_session_unref(export->session);
1429                 afb_apiset_unref(export->declare_set);
1430                 afb_apiset_unref(export->call_set);
1431                 json_object_put(export->settings);
1432                 afb_export_unref(export->creator);
1433                 if (export->api.apiname != export->name)
1434                         free((void*)export->api.apiname);
1435                 free(export);
1436         }
1437 }
1438
1439 struct afb_export *afb_export_create_none_for_path(
1440                         struct afb_apiset *declare_set,
1441                         struct afb_apiset *call_set,
1442                         const char *path,
1443                         int (*creator)(void*, struct afb_api_x3*),
1444                         void *closure)
1445 {
1446         struct afb_export *export = create(declare_set, call_set, path, path, Api_Version_None);
1447         if (export) {
1448                 afb_export_logmask_set(export, logmask);
1449                 set_interfaces(export);
1450                 if (creator && creator(closure, to_api_x3(export)) < 0) {
1451                         afb_export_unref(export);
1452                         export = NULL;
1453                 }
1454         }
1455         return export;
1456 }
1457
1458 #if WITH_LEGACY_BINDING_V1
1459 struct afb_export *afb_export_create_v1(struct afb_apiset *declare_set,
1460                         struct afb_apiset *call_set,
1461                         const char *apiname,
1462                         int (*init)(struct afb_service_x1),
1463                         void (*onevent)(const char*, struct json_object*),
1464                         const char* path)
1465 {
1466         struct afb_export *export = create(declare_set, call_set, apiname, path, Api_Version_1);
1467         if (export) {
1468                 export->init.v1 = init;
1469                 export->on_any_event_v12 = onevent;
1470                 export->export.v1.mode = AFB_MODE_LOCAL;
1471                 export->export.v1.daemon.closure = to_api_x3(export);
1472                 afb_export_logmask_set(export, logmask);
1473                 set_interfaces(export);
1474         }
1475         return export;
1476 }
1477 #endif
1478
1479 #if WITH_LEGACY_BINDING_V2
1480 struct afb_export *afb_export_create_v2(struct afb_apiset *declare_set,
1481                         struct afb_apiset *call_set,
1482                         const char *apiname,
1483                         const struct afb_binding_v2 *binding,
1484                         struct afb_binding_data_v2 *data,
1485                         int (*init)(),
1486                         void (*onevent)(const char*, struct json_object*),
1487                         const char* path)
1488 {
1489         struct afb_export *export = create(declare_set, call_set, apiname, path, Api_Version_2);
1490         if (export) {
1491                 export->init.v2 = init;
1492                 export->on_any_event_v12 = onevent;
1493                 export->desc.v2 = binding;
1494                 export->export.v2 = data;
1495                 data->daemon.closure = to_api_x3(export);
1496                 data->service.closure = to_api_x3(export);
1497                 afb_export_logmask_set(export, logmask);
1498                 set_interfaces(export);
1499         }
1500         return export;
1501 }
1502 #endif
1503
1504 struct afb_export *afb_export_create_v3(struct afb_apiset *declare_set,
1505                         struct afb_apiset *call_set,
1506                         const char *apiname,
1507                         struct afb_api_v3 *apiv3,
1508                         struct afb_export* creator,
1509                         const char* path)
1510 {
1511         struct afb_export *export = create(declare_set, call_set, apiname, path, Api_Version_3);
1512         if (export) {
1513                 export->unsealed = 1;
1514                 export->desc.v3 = apiv3;
1515                 export->creator = afb_export_addref(creator);
1516                 afb_export_logmask_set(export, logmask);
1517                 set_interfaces(export);
1518         }
1519         return export;
1520 }
1521
1522 int afb_export_add_alias(struct afb_export *export, const char *apiname, const char *aliasname)
1523 {
1524         return afb_apiset_add_alias(export->declare_set, apiname ?: export->api.apiname, aliasname);
1525 }
1526
1527 int afb_export_rename(struct afb_export *export, const char *apiname)
1528 {
1529         char *name;
1530
1531         if (export->declared) {
1532                 errno = EBUSY;
1533                 return -1;
1534         }
1535
1536         /* copy the name locally */
1537         name = strdup(apiname);
1538         if (!name) {
1539                 errno = ENOMEM;
1540                 return -1;
1541         }
1542
1543         if (export->api.apiname != export->name)
1544                 free((void*)export->api.apiname);
1545         export->api.apiname = name;
1546
1547         set_interfaces(export);
1548         return 0;
1549 }
1550
1551 const char *afb_export_apiname(const struct afb_export *export)
1552 {
1553         return export->api.apiname;
1554 }
1555
1556 int afb_export_unshare_session(struct afb_export *export)
1557 {
1558         if (export->session == common_session) {
1559                 export->session = afb_session_create (0);
1560                 if (export->session)
1561                         afb_session_unref(common_session);
1562                 else {
1563                         export->session = common_session;
1564                         return -1;
1565                 }
1566         }
1567         return 0;
1568 }
1569
1570 #if WITH_LEGACY_BINDING_V1 || WITH_LEGACY_BINDING_V2
1571 int afb_export_handle_events_v12(struct afb_export *export, void (*on_event)(const char *event, struct json_object *object))
1572 {
1573         /* check version */
1574         switch (export->version) {
1575 #if WITH_LEGACY_BINDING_V1
1576         case Api_Version_1:
1577 #endif
1578 #if WITH_LEGACY_BINDING_V2
1579         case Api_Version_2:
1580                 break;
1581 #endif
1582         default:
1583                 ERROR("invalid version 12 for API %s", export->api.apiname);
1584                 errno = EINVAL;
1585                 return -1;
1586         }
1587
1588         export->on_any_event_v12 = on_event;
1589         return ensure_listener(export);
1590 }
1591 #endif
1592
1593 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))
1594 {
1595         /* check version */
1596         switch (export->version) {
1597         case Api_Version_3: break;
1598         default:
1599                 ERROR("invalid version Dyn for API %s", export->api.apiname);
1600                 errno = EINVAL;
1601                 return -1;
1602         }
1603
1604         export->on_any_event_v3 = on_event;
1605         return ensure_listener(export);
1606 }
1607
1608 int afb_export_handle_init_v3(struct afb_export *export, int (*oninit)(struct afb_api_x3 *api))
1609 {
1610         if (export->state != Api_State_Pre_Init) {
1611                 ERROR("[API %s] Bad call to 'afb_api_x3_on_init', must be in PreInit", export->api.apiname);
1612                 errno = EINVAL;
1613                 return -1;
1614         }
1615
1616         export->init.v3  = oninit;
1617         return 0;
1618 }
1619
1620 #if WITH_LEGACY_BINDING_V1
1621 /*
1622  * Starts a new service (v1)
1623  */
1624 struct afb_binding_v1 *afb_export_register_v1(struct afb_export *export, struct afb_binding_v1 *(*regfun)(const struct afb_binding_interface_v1*))
1625 {
1626         return export->desc.v1 = regfun(&export->export.v1);
1627 }
1628 #endif
1629
1630 int afb_export_preinit_x3(
1631                 struct afb_export *export,
1632                 int (*preinit)(void*, struct afb_api_x3*),
1633                 void *closure)
1634 {
1635         return preinit(closure, to_api_x3(export));
1636 }
1637
1638 int afb_export_logmask_get(const struct afb_export *export)
1639 {
1640         return export->api.logmask;
1641 }
1642
1643 void afb_export_logmask_set(struct afb_export *export, int mask)
1644 {
1645         export->api.logmask = mask;
1646         switch (export->version) {
1647 #if WITH_LEGACY_BINDING_V1
1648         case Api_Version_1: export->export.v1.verbosity = verbosity_from_mask(mask); break;
1649 #endif
1650 #if WITH_LEGACY_BINDING_V2
1651         case Api_Version_2: export->export.v2->verbosity = verbosity_from_mask(mask); break;
1652 #endif
1653         }
1654 }
1655
1656 void *afb_export_userdata_get(const struct afb_export *export)
1657 {
1658         return export->api.userdata;
1659 }
1660
1661 void afb_export_userdata_set(struct afb_export *export, void *data)
1662 {
1663         export->api.userdata = data;
1664 }
1665
1666 /******************************************************************************
1667  ******************************************************************************
1668  ******************************************************************************
1669  ******************************************************************************
1670                                            N E W
1671  ******************************************************************************
1672  ******************************************************************************
1673  ******************************************************************************
1674  ******************************************************************************/
1675
1676 struct init
1677 {
1678         int return_code;
1679         struct afb_export *export;
1680 };
1681
1682 static void do_init(int sig, void *closure)
1683 {
1684         int rc = -1;
1685         struct init *init = closure;
1686         struct afb_export *export;
1687
1688         if (sig)
1689                 errno = EFAULT;
1690         else {
1691                 export = init->export;
1692                 switch (export->version) {
1693 #if WITH_LEGACY_BINDING_V1
1694                 case Api_Version_1:
1695                         rc = export->init.v1 ? export->init.v1(
1696                                 (struct afb_service_x1){
1697 #if WITH_AFB_HOOK
1698                                         .itf = &hooked_service_itf,
1699 #else
1700                                         .itf = &service_itf,
1701 #endif
1702                                         .closure = to_api_x3(export) }) : 0;
1703                         break;
1704 #endif
1705 #if WITH_LEGACY_BINDING_V2
1706                 case Api_Version_2:
1707                         rc = export->init.v2 ? export->init.v2() : 0;
1708                         break;
1709 #endif
1710                 case Api_Version_3:
1711                         rc = export->init.v3 ? export->init.v3(to_api_x3(export)) : 0;
1712                         break;
1713                 default:
1714                         errno = EINVAL;
1715                         break;
1716                 }
1717         }
1718         init->return_code = rc;
1719 };
1720
1721
1722 int afb_export_start(struct afb_export *export)
1723 {
1724         struct init init;
1725         int rc;
1726
1727         /* check state */
1728         switch (export->state) {
1729         case Api_State_Run:
1730                 return 0;
1731
1732         case Api_State_Init:
1733                 /* starting in progress: it is an error */
1734                 ERROR("Service of API %s required started while starting", export->api.apiname);
1735                 return -1;
1736
1737         default:
1738                 break;
1739         }
1740
1741         /* set event handling */
1742 #if WITH_LEGACY_BINDING_V1 || WITH_LEGACY_BINDING_V2
1743         switch (export->version) {
1744 #if WITH_LEGACY_BINDING_V1
1745         case Api_Version_1:
1746 #endif
1747 #if WITH_LEGACY_BINDING_V2
1748         case Api_Version_2:
1749 #endif
1750                 if (export->on_any_event_v12) {
1751                         rc = afb_export_handle_events_v12(export, export->on_any_event_v12);
1752                         break;
1753                 }
1754                 /*@fallthrough@*/
1755         default:
1756                 rc = 0;
1757                 break;
1758         }
1759         if (rc < 0) {
1760                 ERROR("Can't set event handler for %s", export->api.apiname);
1761                 return -1;
1762         }
1763 #endif
1764
1765 #if WITH_AFB_HOOK
1766         /* Starts the service */
1767         if (export->hooksvc & afb_hook_flag_api_start)
1768                 afb_hook_api_start_before(export);
1769 #endif
1770
1771         export->state = Api_State_Init;
1772         init.export = export;
1773         sig_monitor(0, do_init, &init);
1774         rc = init.return_code;
1775         export->state = Api_State_Run;
1776
1777 #if WITH_AFB_HOOK
1778         if (export->hooksvc & afb_hook_flag_api_start)
1779                 afb_hook_api_start_after(export, rc);
1780 #endif
1781
1782         if (rc < 0) {
1783                 /* initialisation error */
1784                 ERROR("Initialisation of service API %s failed (%d): %m", export->api.apiname, rc);
1785                 return rc;
1786         }
1787
1788         return 0;
1789 }
1790
1791 static void api_call_cb(void *closure, struct afb_xreq *xreq)
1792 {
1793         struct afb_export *export = closure;
1794
1795         xreq->request.api = to_api_x3(export);
1796
1797         switch (export->version) {
1798 #if WITH_LEGACY_BINDING_V1
1799         case Api_Version_1:
1800                 afb_api_so_v1_process_call(export->desc.v1, xreq);
1801                 break;
1802 #endif
1803 #if WITH_LEGACY_BINDING_V2
1804         case Api_Version_2:
1805                 afb_api_so_v2_process_call(export->desc.v2, xreq);
1806                 break;
1807 #endif
1808         case Api_Version_3:
1809                 afb_api_v3_process_call(export->desc.v3, xreq);
1810                 break;
1811         default:
1812                 afb_xreq_reply(xreq, NULL, "bad-api-type", NULL);
1813                 break;
1814         }
1815 }
1816
1817 static struct json_object *api_describe_cb(void *closure)
1818 {
1819         struct afb_export *export = closure;
1820         struct json_object *result;
1821
1822         switch (export->version) {
1823 #if WITH_LEGACY_BINDING_V1
1824         case Api_Version_1:
1825                 result = afb_api_so_v1_make_description_openAPIv3(export->desc.v1, export->api.apiname);
1826                 break;
1827 #endif
1828 #if WITH_LEGACY_BINDING_V2
1829         case Api_Version_2:
1830                 result = afb_api_so_v2_make_description_openAPIv3(export->desc.v2, export->api.apiname);
1831                 break;
1832 #endif
1833         case Api_Version_3:
1834                 result = afb_api_v3_make_description_openAPIv3(export->desc.v3, export->api.apiname);
1835                 break;
1836         default:
1837                 result = NULL;
1838                 break;
1839         }
1840         return result;
1841 }
1842
1843 static int api_service_start_cb(void *closure)
1844 {
1845         struct afb_export *export = closure;
1846
1847         return afb_export_start(export);
1848 }
1849
1850 #if WITH_AFB_HOOK
1851 static void api_update_hooks_cb(void *closure)
1852         __attribute__((alias("set_interfaces")));
1853
1854 void afb_export_update_hooks(struct afb_export *export)
1855         __attribute__((alias("set_interfaces")));
1856 #endif
1857
1858 static int api_get_logmask_cb(void *closure)
1859 {
1860         struct afb_export *export = closure;
1861
1862         return afb_export_logmask_get(export);
1863 }
1864
1865 static void api_set_logmask_cb(void *closure, int level)
1866 {
1867         struct afb_export *export = closure;
1868
1869         afb_export_logmask_set(export, level);
1870 }
1871
1872 static void api_unref_cb(void *closure)
1873 {
1874         struct afb_export *export = closure;
1875
1876         afb_export_unref(export);
1877 }
1878
1879 static struct afb_api_itf export_api_itf =
1880 {
1881         .call = api_call_cb,
1882         .service_start = api_service_start_cb,
1883 #if WITH_AFB_HOOK
1884         .update_hooks = api_update_hooks_cb,
1885 #endif
1886         .get_logmask = api_get_logmask_cb,
1887         .set_logmask = api_set_logmask_cb,
1888         .describe = api_describe_cb,
1889         .unref = api_unref_cb
1890 };
1891
1892 int afb_export_declare(struct afb_export *export,
1893                         int noconcurrency)
1894 {
1895         int rc;
1896         struct afb_api_item afb_api;
1897
1898         if (export->declared)
1899                 rc = 0;
1900         else {
1901                 /* init the record structure */
1902                 afb_api.closure = afb_export_addref(export);
1903                 afb_api.itf = &export_api_itf;
1904                 afb_api.group = noconcurrency ? export : NULL;
1905
1906                 /* records the binding */
1907                 rc = afb_apiset_add(export->declare_set, export->api.apiname, afb_api);
1908                 if (rc >= 0)
1909                         export->declared = 1;
1910                 else {
1911                         ERROR("can't declare export %s to set %s, ABORTING it!",
1912                                 export->api.apiname,
1913                                 afb_apiset_name(export->declare_set));
1914                         afb_export_unref(export);
1915                 }
1916         }
1917
1918         return rc;
1919 }
1920
1921 void afb_export_undeclare(struct afb_export *export)
1922 {
1923         if (export->declared) {
1924                 export->declared = 0;
1925                 afb_apiset_del(export->declare_set, export->api.apiname);
1926         }
1927 }
1928
1929 int afb_export_subscribe(struct afb_export *export, struct afb_event_x2 *event)
1930 {
1931         return afb_evt_event_x2_add_watch(export->listener, event);
1932 }
1933
1934 int afb_export_unsubscribe(struct afb_export *export, struct afb_event_x2 *event)
1935 {
1936         return afb_evt_event_x2_remove_watch(export->listener, event);
1937 }
1938
1939 void afb_export_process_xreq(struct afb_export *export, struct afb_xreq *xreq)
1940 {
1941         afb_xreq_process(xreq, export->call_set);
1942 }
1943
1944 void afb_export_context_init(struct afb_export *export, struct afb_context *context)
1945 {
1946         afb_context_init(context, export->session, NULL);
1947         context->validated = 1;
1948 }
1949