2 * Copyright (C) 2016-2019 "IoT.bzh"
3 * Author: José Bollo <jose.bollo@iot.bzh>
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 #include <json-c/json.h>
26 #if !defined(JSON_C_TO_STRING_NOSLASHESCAPE)
27 #define JSON_C_TO_STRING_NOSLASHESCAPE 0
30 #define AFB_BINDING_VERSION 0
31 #include <afb/afb-binding.h>
34 #include "afb-apiset.h"
35 #if WITH_LEGACY_BINDING_V1
36 #include "afb-api-so-v1.h"
38 #if WITH_LEGACY_BINDING_V2
39 #include "afb-api-so-v2.h"
41 #include "afb-api-v3.h"
42 #include "afb-common.h"
45 #include "afb-export.h"
47 #include "afb-msg-json.h"
48 #include "afb-session.h"
50 #include "afb-calls.h"
58 #include "sig-monitor.h"
59 #include "wrap-json.h"
61 /*************************************************************************
63 ************************************************************************/
66 * Actually supported versions
71 #if WITH_LEGACY_BINDING_V1
74 #if WITH_LEGACY_BINDING_V2
81 * The states of exported APIs
91 * structure of the exported API
96 struct afb_api_x3 api;
101 /* version of the api */
108 unsigned declared: 1;
111 unsigned unsealed: 1;
119 /* session for service */
120 struct afb_session *session;
122 /* apiset the API is declared in */
123 struct afb_apiset *declare_set;
125 /* apiset for calls */
126 struct afb_apiset *call_set;
128 /* event listener for service or NULL */
129 struct afb_evt_listener *listener;
131 /* event handler list */
132 struct globset *event_handlers;
135 struct afb_export *creator;
137 /* path indication if any */
141 struct json_object *settings;
143 /* internal descriptors */
145 #if WITH_LEGACY_BINDING_V1
146 struct afb_binding_v1 *v1;
148 const struct afb_binding_v2 *v2;
149 struct afb_api_v3 *v3;
154 #if WITH_LEGACY_BINDING_V1
155 int (*v1)(struct afb_service_x1);
158 int (*v3)(struct afb_api_x3 *api);
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);
167 #if WITH_LEGACY_BINDING_V1
168 struct afb_binding_interface_v1 v1;
170 struct afb_binding_data_v2 *v2;
177 /*****************************************************************************/
179 static inline struct afb_api_x3 *to_api_x3(struct afb_export *export)
181 return (struct afb_api_x3*)export;
184 static inline struct afb_export *from_api_x3(struct afb_api_x3 *api)
186 return (struct afb_export*)api;
189 struct afb_export *afb_export_from_api_x3(struct afb_api_x3 *api)
191 return from_api_x3(api);
194 struct afb_api_x3 *afb_export_to_api_x3(struct afb_export *export)
196 return to_api_x3(export);
199 /******************************************************************************
200 ******************************************************************************
201 ******************************************************************************
202 ******************************************************************************
204 ******************************************************************************
205 ******************************************************************************
206 ******************************************************************************
207 ******************************************************************************/
209 static struct json_object *configuration;
211 void afb_export_set_config(struct json_object *config)
213 struct json_object *save = configuration;
214 configuration = json_object_get(config);
215 json_object_put(save);
218 static struct json_object *make_settings(struct afb_export *export)
220 struct json_object *result;
221 struct json_object *obj;
222 struct afb_export *iter;
225 /* clone the globals */
226 if (json_object_object_get_ex(configuration, "*", &obj))
227 result = wrap_json_clone(obj);
229 result = json_object_new_object();
232 if (json_object_object_get_ex(configuration, export->name, &obj))
233 wrap_json_object_add(result, obj);
235 /* add library path */
236 for (iter = export ; iter && !iter->path ; iter = iter->creator);
238 path = realpath(iter->path, NULL);
239 json_object_object_add(result, "binding-path", json_object_new_string(path));
243 export->settings = result;
247 /******************************************************************************
248 ******************************************************************************
249 ******************************************************************************
250 ******************************************************************************
252 ******************************************************************************
253 ******************************************************************************
254 ******************************************************************************
255 ******************************************************************************/
257 /**********************************************
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)
263 struct afb_export *export = from_api_x3(closure);
265 if (!fmt || vasprintf(&p, fmt, args) < 0)
266 vverbose(level, file, line, function, fmt, args);
268 verbose(level, file, line, function, (verbose_is_colorized() == 0 ? "[API %s] %s" : COLOR_API "[API %s]" COLOR_DEFAULT " %s"), export->api.apiname, p);
273 static struct afb_event_x2 *event_x2_make_cb(struct afb_api_x3 *closure, const char *name)
275 struct afb_export *export = from_api_x3(closure);
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);
284 /* create the event */
285 return afb_evt_event_x2_create2(export->api.apiname, name);
288 static int event_broadcast_cb(struct afb_api_x3 *closure, const char *name, struct json_object *object)
292 struct afb_export *export = from_api_x3(closure);
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));
302 /* makes the event name */
303 plen = strlen(export->api.apiname);
305 event = alloca(nlen + plen + 2);
306 memcpy(event, export->api.apiname, plen);
308 memcpy(event + plen + 1, name, nlen + 1);
310 /* broadcast the event */
311 return afb_evt_broadcast(event, object);
314 static struct sd_event *get_event_loop(struct afb_api_x3 *closure)
317 jobs_acquire_event_manager();
318 return systemd_get_event_loop();
325 static struct sd_bus *get_user_bus(struct afb_api_x3 *closure)
328 jobs_acquire_event_manager();
329 return systemd_get_user_bus();
336 static struct sd_bus *get_system_bus(struct afb_api_x3 *closure)
339 jobs_acquire_event_manager();
340 return systemd_get_system_bus();
347 static int rootdir_open_locale_cb(struct afb_api_x3 *closure, const char *filename, int flags, const char *locale)
349 return afb_common_rootdir_open_locale(filename, flags, locale);
352 static int queue_job_cb(struct afb_api_x3 *closure, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout)
354 return jobs_queue(group, timeout, callback, argument);
357 static int require_api_cb(struct afb_api_x3 *closure, const char *name, int initialized)
359 struct afb_export *export = from_api_x3(closure);
361 char *iter, *end, save;
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);
368 /* scan the names in a local copy */
370 iter = strdupa(name);
376 if (!save) /* at end? */
379 /* search for the end */
381 while (save && !isspace(save))
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);
389 rc2 = -!((initialized ? afb_apiset_lookup_started : afb_apiset_lookup)(export->call_set, iter, 1));
398 static int add_alias_cb(struct afb_api_x3 *closure, const char *apiname, const char *aliasname)
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);
406 NOTICE("[API %s] aliasing [API %s] to [API %s]", export->api.apiname, apiname?:"<null>", aliasname);
407 afb_export_add_alias(export, apiname, aliasname);
411 static struct afb_api_x3 *api_new_api_cb(
412 struct afb_api_x3 *closure,
416 int (*preinit)(void*, struct afb_api_x3 *),
417 void *preinit_closure)
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,
425 return apiv3 ? to_api_x3(afb_api_v3_export(apiv3)) : NULL;
428 #if WITH_LEGACY_BINDING_V1 || WITH_LEGACY_BINDING_V2
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)
432 vverbose_cb(closure, level, file, line, NULL, fmt, args);
435 static struct afb_event_x1 legacy_event_x1_make_cb(struct afb_api_x3 *closure, const char *name)
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));
441 static struct afb_req_x1 legacy_unstore_req_cb(struct afb_api_x3 *closure, struct afb_stored_req *sreq)
443 return afb_xreq_unstore(sreq);
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,
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)
467 struct afb_export *export = from_api_x3(closure);
470 vverbose_cb(closure, level, file, line, function, fmt, args);
471 afb_hook_api_vverbose(export, level, file, line, function, fmt, ap);
475 static struct afb_event_x2 *hooked_event_x2_make_cb(struct afb_api_x3 *closure, const char *name)
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);
483 static int hooked_event_broadcast_cb(struct afb_api_x3 *closure, const char *name, struct json_object *object)
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);
495 static struct sd_event *hooked_get_event_loop(struct afb_api_x3 *closure)
497 struct afb_export *export = from_api_x3(closure);
500 r = get_event_loop(closure);
501 return afb_hook_api_get_event_loop(export, r);
504 static struct sd_bus *hooked_get_user_bus(struct afb_api_x3 *closure)
506 struct afb_export *export = from_api_x3(closure);
509 r = get_user_bus(closure);
510 return afb_hook_api_get_user_bus(export, r);
513 static struct sd_bus *hooked_get_system_bus(struct afb_api_x3 *closure)
515 struct afb_export *export = from_api_x3(closure);
518 r = get_system_bus(closure);
519 return afb_hook_api_get_system_bus(export, r);
522 static int hooked_rootdir_get_fd(struct afb_api_x3 *closure)
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);
529 static int hooked_rootdir_open_locale_cb(struct afb_api_x3 *closure, const char *filename, int flags, const char *locale)
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);
536 static int hooked_queue_job_cb(struct afb_api_x3 *closure, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout)
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);
543 static int hooked_require_api_cb(struct afb_api_x3 *closure, const char *name, int initialized)
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);
552 static int hooked_add_alias_cb(struct afb_api_x3 *closure, const char *apiname, const char *aliasname)
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);
559 static struct afb_api_x3 *hooked_api_new_api_cb(
560 struct afb_api_x3 *closure,
564 int (*preinit)(void*, struct afb_api_x3 *),
565 void *preinit_closure)
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);
575 #if WITH_LEGACY_BINDING_V1 || WITH_LEGACY_BINDING_V2
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)
579 hooked_vverbose_cb(closure, level, file, line, NULL, fmt, args);
582 static struct afb_event_x1 legacy_hooked_event_x1_make_cb(struct afb_api_x3 *closure, const char *name)
584 struct afb_event_x2 *event = hooked_event_x2_make_cb(closure, name);
585 struct afb_event_x1 e;
587 e.itf = event ? event->itf : NULL;
591 static struct afb_req_x1 legacy_hooked_unstore_req_cb(struct afb_api_x3 *closure, struct afb_stored_req *sreq)
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);
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,
618 /******************************************************************************
619 ******************************************************************************
620 ******************************************************************************
621 ******************************************************************************
623 ******************************************************************************
624 ******************************************************************************
625 ******************************************************************************
626 ******************************************************************************/
628 /* the common session for services sharing their session */
629 static struct afb_session *common_session;
631 /******************************************************************************
632 ******************************************************************************
633 ******************************************************************************
634 ******************************************************************************
636 ******************************************************************************
637 ******************************************************************************
638 ******************************************************************************
639 ******************************************************************************/
642 struct afb_api_x3 *apix3,
645 struct json_object *args,
646 void (*callback)(void*, struct json_object*, const char *error, const char *info, struct afb_api_x3*),
649 struct afb_export *export = from_api_x3(apix3);
650 return afb_calls_call(export, api, verb, args, callback, closure);
653 static int call_sync_x3(
654 struct afb_api_x3 *apix3,
657 struct json_object *args,
658 struct json_object **object,
662 struct afb_export *export = from_api_x3(apix3);
663 return afb_calls_call_sync(export, api, verb, args, object, error, info);
666 static void legacy_call_x3(
667 struct afb_api_x3 *apix3,
670 struct json_object *args,
671 void (*callback)(void*, int, struct json_object*, struct afb_api_x3*),
674 struct afb_export *export = from_api_x3(apix3);
675 afb_calls_legacy_call_v3(export, api, verb, args, callback, closure);
678 static int legacy_call_sync(
679 struct afb_api_x3 *apix3,
682 struct json_object *args,
683 struct json_object **result)
685 struct afb_export *export = from_api_x3(apix3);
686 return afb_calls_legacy_call_sync(export, api, verb, args, result);
689 #if WITH_LEGACY_BINDING_V1 || WITH_LEGACY_BINDING_V2
691 static void legacy_call_v12(
692 struct afb_api_x3 *apix3,
695 struct json_object *args,
696 void (*callback)(void*, int, struct json_object*),
699 struct afb_export *export = from_api_x3(apix3);
700 afb_calls_legacy_call_v12(export, api, verb, args, callback, closure);
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
711 static void hooked_call_x3(
712 struct afb_api_x3 *apix3,
715 struct json_object *args,
716 void (*callback)(void*, struct json_object*, const char*, const char*, struct afb_api_x3*),
719 struct afb_export *export = from_api_x3(apix3);
720 afb_calls_hooked_call(export, api, verb, args, callback, closure);
723 static int hooked_call_sync_x3(
724 struct afb_api_x3 *apix3,
727 struct json_object *args,
728 struct json_object **object,
732 struct afb_export *export = from_api_x3(apix3);
733 return afb_calls_hooked_call_sync(export, api, verb, args, object, error, info);
736 static void legacy_hooked_call_x3(
737 struct afb_api_x3 *apix3,
740 struct json_object *args,
741 void (*callback)(void*, int, struct json_object*, struct afb_api_x3*),
744 struct afb_export *export = from_api_x3(apix3);
745 afb_calls_legacy_hooked_call_v3(export, api, verb, args, callback, closure);
748 static int legacy_hooked_call_sync(
749 struct afb_api_x3 *apix3,
752 struct json_object *args,
753 struct json_object **result)
755 struct afb_export *export = from_api_x3(apix3);
756 return afb_calls_legacy_hooked_call_sync(export, api, verb, args, result);
759 #if WITH_LEGACY_BINDING_V1 || WITH_LEGACY_BINDING_V2
761 static void legacy_hooked_call_v12(
762 struct afb_api_x3 *apix3,
765 struct json_object *args,
766 void (*callback)(void*, int, struct json_object*),
769 struct afb_export *export = from_api_x3(apix3);
770 afb_calls_legacy_hooked_call_v12(export, api, verb, args, callback, closure);
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
782 /******************************************************************************
783 ******************************************************************************
784 ******************************************************************************
785 ******************************************************************************
787 ******************************************************************************
788 ******************************************************************************
789 ******************************************************************************
790 ******************************************************************************/
792 static int api_set_verbs_v2_cb(
793 struct afb_api_x3 *api,
794 const struct afb_verb_v2 *verbs)
796 #if WITH_LEGACY_BINDING_V2
797 struct afb_export *export = from_api_x3(api);
799 if (export->unsealed) {
800 afb_api_v3_set_verbs_v2(export->desc.v3, verbs);
811 static int api_set_verbs_v3_cb(
812 struct afb_api_x3 *api,
813 const struct afb_verb_v3 *verbs)
815 struct afb_export *export = from_api_x3(api);
817 if (!export->unsealed) {
822 afb_api_v3_set_verbs_v3(export->desc.v3, verbs);
826 static int api_add_verb_cb(
827 struct afb_api_x3 *api,
830 void (*callback)(struct afb_req_x2 *req),
832 const struct afb_auth *auth,
836 struct afb_export *export = from_api_x3(api);
838 if (!export->unsealed) {
843 return afb_api_v3_add_verb(export->desc.v3, verb, info, callback, vcbdata, auth, (uint16_t)session, glob);
846 static int api_del_verb_cb(
847 struct afb_api_x3 *api,
851 struct afb_export *export = from_api_x3(api);
853 if (!export->unsealed) {
858 return afb_api_v3_del_verb(export->desc.v3, verb, vcbdata);
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))
865 struct afb_export *export = from_api_x3(api);
866 return afb_export_handle_events_v3(export, onevent);
869 static int api_set_on_init_cb(
870 struct afb_api_x3 *api,
871 int (*oninit)(struct afb_api_x3 *api))
873 struct afb_export *export = from_api_x3(api);
875 return afb_export_handle_init_v3(export, oninit);
878 static void api_seal_cb(
879 struct afb_api_x3 *api)
881 struct afb_export *export = from_api_x3(api);
883 export->unsealed = 0;
886 static int event_handler_add_cb(
887 struct afb_api_x3 *api,
889 void (*callback)(void *, const char*, struct json_object*, struct afb_api_x3*),
892 struct afb_export *export = from_api_x3(api);
894 return afb_export_event_handler_add(export, pattern, callback, closure);
897 static int event_handler_del_cb(
898 struct afb_api_x3 *api,
902 struct afb_export *export = from_api_x3(api);
904 return afb_export_event_handler_del(export, pattern, closure);
907 static int class_provide_cb(struct afb_api_x3 *api, const char *name)
909 struct afb_export *export = from_api_x3(api);
912 char *iter, *end, save;
914 iter = strdupa(name);
920 if (!save) /* at end? */
923 /* search for the end */
925 while (save && !isspace(save))
929 rc2 = afb_apiset_provide_class(export->declare_set, api->apiname, iter);
938 static int class_require_cb(struct afb_api_x3 *api, const char *name)
940 struct afb_export *export = from_api_x3(api);
943 char *iter, *end, save;
945 iter = strdupa(name);
951 if (!save) /* at end? */
954 /* search for the end */
956 while (save && !isspace(save))
960 rc2 = afb_apiset_require_class(export->declare_set, api->apiname, iter);
969 static int delete_api_cb(struct afb_api_x3 *api)
971 struct afb_export *export = from_api_x3(api);
973 if (!export->unsealed) {
978 afb_export_undeclare(export);
979 afb_export_unref(export);
983 static struct json_object *settings_cb(struct afb_api_x3 *api)
985 struct afb_export *export = from_api_x3(api);
986 struct json_object *result = export->settings;
988 result = make_settings(export);
992 static const struct afb_api_x3_itf api_x3_itf = {
994 .vverbose = (void*)vverbose_cb,
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,
1003 .require_api = require_api_cb,
1004 .add_alias = add_alias_cb,
1006 .event_broadcast = event_broadcast_cb,
1007 .event_make = event_x2_make_cb,
1009 .legacy_call = legacy_call_x3,
1010 .legacy_call_sync = legacy_call_sync,
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,
1024 .call_sync = call_sync_x3,
1026 .class_provide = class_provide_cb,
1027 .class_require = class_require_cb,
1029 .delete_api = delete_api_cb,
1030 .settings = settings_cb,
1034 static int hooked_api_set_verbs_v2_cb(
1035 struct afb_api_x3 *api,
1036 const struct afb_verb_v2 *verbs)
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);
1043 static int hooked_api_set_verbs_v3_cb(
1044 struct afb_api_x3 *api,
1045 const struct afb_verb_v3 *verbs)
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);
1052 static int hooked_api_add_verb_cb(
1053 struct afb_api_x3 *api,
1056 void (*callback)(struct afb_req_x2 *req),
1058 const struct afb_auth *auth,
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);
1067 static int hooked_api_del_verb_cb(
1068 struct afb_api_x3 *api,
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);
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))
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);
1086 static int hooked_api_set_on_init_cb(
1087 struct afb_api_x3 *api,
1088 int (*oninit)(struct afb_api_x3 *api))
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);
1095 static void hooked_api_seal_cb(
1096 struct afb_api_x3 *api)
1098 struct afb_export *export = from_api_x3(api);
1099 afb_hook_api_api_seal(export);
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*),
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);
1114 static int hooked_event_handler_del_cb(
1115 struct afb_api_x3 *api,
1116 const char *pattern,
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);
1124 static int hooked_class_provide_cb(struct afb_api_x3 *api, const char *name)
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);
1131 static int hooked_class_require_cb(struct afb_api_x3 *api, const char *name)
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);
1138 static int hooked_delete_api_cb(struct afb_api_x3 *api)
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);
1147 static struct json_object *hooked_settings_cb(struct afb_api_x3 *api)
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);
1155 static const struct afb_api_x3_itf hooked_api_x3_itf = {
1157 .vverbose = hooked_vverbose_cb,
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,
1166 .require_api = hooked_require_api_cb,
1167 .add_alias = hooked_add_alias_cb,
1169 .event_broadcast = hooked_event_broadcast_cb,
1170 .event_make = hooked_event_x2_make_cb,
1172 .legacy_call = legacy_hooked_call_x3,
1173 .legacy_call_sync = legacy_hooked_call_sync,
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,
1186 .call = hooked_call_x3,
1187 .call_sync = hooked_call_sync_x3,
1189 .class_provide = hooked_class_provide_cb,
1190 .class_require = hooked_class_require_cb,
1192 .delete_api = hooked_delete_api_cb,
1193 .settings = hooked_settings_cb,
1197 /******************************************************************************
1198 ******************************************************************************
1199 ******************************************************************************
1200 ******************************************************************************
1202 ******************************************************************************
1203 ******************************************************************************
1204 ******************************************************************************
1205 ******************************************************************************/
1208 * Propagates the event to the service
1210 static void listener_of_events(void *closure, const char *event, int eventid, struct json_object *object)
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);
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);
1222 /* transmit to specific handlers */
1223 /* search the handler */
1224 handler = export->event_handlers ? globset_match(export->event_handlers, event) : NULL;
1226 callback = handler->callback;
1228 if (!(export->hooksvc & afb_hook_flag_api_on_event_handler))
1230 callback(handler->closure, event, object, to_api_x3(export));
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);
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);
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);
1251 json_object_put(object);
1254 /* the interface for events */
1255 static const struct afb_evt_itf evt_itf = {
1256 .broadcast = listener_of_events,
1257 .push = listener_of_events
1260 /* ensure an existing listener */
1261 static int ensure_listener(struct afb_export *export)
1263 if (!export->listener) {
1264 export->listener = afb_evt_listener_create(&evt_itf, export);
1265 if (export->listener == NULL)
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*),
1279 /* ensure the listener */
1280 rc = ensure_listener(export);
1284 /* ensure the globset for event handling */
1285 if (!export->event_handlers) {
1286 export->event_handlers = globset_create();
1287 if (!export->event_handlers)
1291 /* add the handler */
1292 rc = globset_add(export->event_handlers, pattern, callback, closure);
1296 if (errno == EEXIST) {
1297 ERROR("[API %s] event handler %s already exists", export->api.apiname, pattern);
1302 ERROR("[API %s] can't allocate event handler %s", export->api.apiname, pattern);
1306 int afb_export_event_handler_del(
1307 struct afb_export *export,
1308 const char *pattern,
1311 if (export->event_handlers
1312 && !globset_del(export->event_handlers, pattern, closure))
1315 ERROR("[API %s] event handler %s not found", export->api.apiname, pattern);
1320 /******************************************************************************
1321 ******************************************************************************
1322 ******************************************************************************
1323 ******************************************************************************
1325 ******************************************************************************
1326 ******************************************************************************
1327 ******************************************************************************
1328 ******************************************************************************/
1330 static void set_interfaces(struct afb_export *export)
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;
1337 switch (export->version) {
1338 #if WITH_LEGACY_BINDING_V1
1340 export->export.v1.daemon.itf = export->hookditf ? &hooked_daemon_itf : &daemon_itf;
1343 #if WITH_LEGACY_BINDING_V2
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;
1352 export->api.itf = &api_x3_itf;
1354 switch (export->version) {
1355 #if WITH_LEGACY_BINDING_V1
1357 export->export.v1.daemon.itf = &daemon_itf;
1360 #if WITH_LEGACY_BINDING_V2
1362 export->export.v2->daemon.itf = &daemon_itf;
1363 export->export.v2->service.itf = &service_itf;
1371 static struct afb_export *create(
1372 struct afb_apiset *declare_set,
1373 struct afb_apiset *call_set,
1374 const char *apiname,
1376 enum afb_api_version version)
1378 struct afb_export *export;
1381 /* session shared with other exports */
1382 if (common_session == NULL) {
1383 common_session = afb_session_create (0);
1384 if (common_session == NULL)
1387 lenapi = strlen(apiname);
1388 export = calloc(1, sizeof *export + lenapi + (path == apiname || !path ? 0 : strlen(path)));
1392 export->refcount = 1;
1393 strcpy(export->name, apiname);
1394 export->api.apiname = export->name;
1395 if (path == apiname)
1396 export->path = export->name;
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);
1408 struct afb_export *afb_export_addref(struct afb_export *export)
1411 __atomic_add_fetch(&export->refcount, 1, __ATOMIC_RELAXED);
1415 void afb_export_unref(struct afb_export *export)
1417 if (export && !__atomic_sub_fetch(&export->refcount, 1, __ATOMIC_RELAXED))
1418 afb_export_destroy(export);
1421 void afb_export_destroy(struct afb_export *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);
1439 struct afb_export *afb_export_create_none_for_path(
1440 struct afb_apiset *declare_set,
1441 struct afb_apiset *call_set,
1443 int (*creator)(void*, struct afb_api_x3*),
1446 struct afb_export *export = create(declare_set, call_set, path, path, Api_Version_None);
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);
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*),
1466 struct afb_export *export = create(declare_set, call_set, apiname, path, Api_Version_1);
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);
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,
1486 void (*onevent)(const char*, struct json_object*),
1489 struct afb_export *export = create(declare_set, call_set, apiname, path, Api_Version_2);
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);
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,
1511 struct afb_export *export = create(declare_set, call_set, apiname, path, Api_Version_3);
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);
1522 int afb_export_add_alias(struct afb_export *export, const char *apiname, const char *aliasname)
1524 return afb_apiset_add_alias(export->declare_set, apiname ?: export->api.apiname, aliasname);
1527 int afb_export_rename(struct afb_export *export, const char *apiname)
1531 if (export->declared) {
1536 /* copy the name locally */
1537 name = strdup(apiname);
1543 if (export->api.apiname != export->name)
1544 free((void*)export->api.apiname);
1545 export->api.apiname = name;
1547 set_interfaces(export);
1551 const char *afb_export_apiname(const struct afb_export *export)
1553 return export->api.apiname;
1556 int afb_export_unshare_session(struct afb_export *export)
1558 if (export->session == common_session) {
1559 export->session = afb_session_create (0);
1560 if (export->session)
1561 afb_session_unref(common_session);
1563 export->session = common_session;
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))
1574 switch (export->version) {
1575 #if WITH_LEGACY_BINDING_V1
1578 #if WITH_LEGACY_BINDING_V2
1583 ERROR("invalid version 12 for API %s", export->api.apiname);
1588 export->on_any_event_v12 = on_event;
1589 return ensure_listener(export);
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))
1596 switch (export->version) {
1597 case Api_Version_3: break;
1599 ERROR("invalid version Dyn for API %s", export->api.apiname);
1604 export->on_any_event_v3 = on_event;
1605 return ensure_listener(export);
1608 int afb_export_handle_init_v3(struct afb_export *export, int (*oninit)(struct afb_api_x3 *api))
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);
1616 export->init.v3 = oninit;
1620 #if WITH_LEGACY_BINDING_V1
1622 * Starts a new service (v1)
1624 struct afb_binding_v1 *afb_export_register_v1(struct afb_export *export, struct afb_binding_v1 *(*regfun)(const struct afb_binding_interface_v1*))
1626 return export->desc.v1 = regfun(&export->export.v1);
1630 int afb_export_preinit_x3(
1631 struct afb_export *export,
1632 int (*preinit)(void*, struct afb_api_x3*),
1635 return preinit(closure, to_api_x3(export));
1638 int afb_export_logmask_get(const struct afb_export *export)
1640 return export->api.logmask;
1643 void afb_export_logmask_set(struct afb_export *export, int mask)
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;
1650 #if WITH_LEGACY_BINDING_V2
1651 case Api_Version_2: export->export.v2->verbosity = verbosity_from_mask(mask); break;
1656 void *afb_export_userdata_get(const struct afb_export *export)
1658 return export->api.userdata;
1661 void afb_export_userdata_set(struct afb_export *export, void *data)
1663 export->api.userdata = data;
1666 /******************************************************************************
1667 ******************************************************************************
1668 ******************************************************************************
1669 ******************************************************************************
1671 ******************************************************************************
1672 ******************************************************************************
1673 ******************************************************************************
1674 ******************************************************************************/
1679 struct afb_export *export;
1682 static void do_init(int sig, void *closure)
1685 struct init *init = closure;
1686 struct afb_export *export;
1691 export = init->export;
1692 switch (export->version) {
1693 #if WITH_LEGACY_BINDING_V1
1695 rc = export->init.v1 ? export->init.v1(
1696 (struct afb_service_x1){
1698 .itf = &hooked_service_itf,
1700 .itf = &service_itf,
1702 .closure = to_api_x3(export) }) : 0;
1705 #if WITH_LEGACY_BINDING_V2
1707 rc = export->init.v2 ? export->init.v2() : 0;
1711 rc = export->init.v3 ? export->init.v3(to_api_x3(export)) : 0;
1718 init->return_code = rc;
1722 int afb_export_start(struct afb_export *export)
1728 switch (export->state) {
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);
1741 /* set event handling */
1742 #if WITH_LEGACY_BINDING_V1 || WITH_LEGACY_BINDING_V2
1743 switch (export->version) {
1744 #if WITH_LEGACY_BINDING_V1
1747 #if WITH_LEGACY_BINDING_V2
1750 if (export->on_any_event_v12) {
1751 rc = afb_export_handle_events_v12(export, export->on_any_event_v12);
1760 ERROR("Can't set event handler for %s", export->api.apiname);
1766 /* Starts the service */
1767 if (export->hooksvc & afb_hook_flag_api_start)
1768 afb_hook_api_start_before(export);
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;
1778 if (export->hooksvc & afb_hook_flag_api_start)
1779 afb_hook_api_start_after(export, rc);
1783 /* initialisation error */
1784 ERROR("Initialisation of service API %s failed (%d): %m", export->api.apiname, rc);
1791 static void api_call_cb(void *closure, struct afb_xreq *xreq)
1793 struct afb_export *export = closure;
1795 xreq->request.api = to_api_x3(export);
1797 switch (export->version) {
1798 #if WITH_LEGACY_BINDING_V1
1800 afb_api_so_v1_process_call(export->desc.v1, xreq);
1803 #if WITH_LEGACY_BINDING_V2
1805 afb_api_so_v2_process_call(export->desc.v2, xreq);
1809 afb_api_v3_process_call(export->desc.v3, xreq);
1812 afb_xreq_reply(xreq, NULL, "bad-api-type", NULL);
1817 static struct json_object *api_describe_cb(void *closure)
1819 struct afb_export *export = closure;
1820 struct json_object *result;
1822 switch (export->version) {
1823 #if WITH_LEGACY_BINDING_V1
1825 result = afb_api_so_v1_make_description_openAPIv3(export->desc.v1, export->api.apiname);
1828 #if WITH_LEGACY_BINDING_V2
1830 result = afb_api_so_v2_make_description_openAPIv3(export->desc.v2, export->api.apiname);
1834 result = afb_api_v3_make_description_openAPIv3(export->desc.v3, export->api.apiname);
1843 static int api_service_start_cb(void *closure)
1845 struct afb_export *export = closure;
1847 return afb_export_start(export);
1851 static void api_update_hooks_cb(void *closure)
1852 __attribute__((alias("set_interfaces")));
1854 void afb_export_update_hooks(struct afb_export *export)
1855 __attribute__((alias("set_interfaces")));
1858 static int api_get_logmask_cb(void *closure)
1860 struct afb_export *export = closure;
1862 return afb_export_logmask_get(export);
1865 static void api_set_logmask_cb(void *closure, int level)
1867 struct afb_export *export = closure;
1869 afb_export_logmask_set(export, level);
1872 static void api_unref_cb(void *closure)
1874 struct afb_export *export = closure;
1876 afb_export_unref(export);
1879 static struct afb_api_itf export_api_itf =
1881 .call = api_call_cb,
1882 .service_start = api_service_start_cb,
1884 .update_hooks = api_update_hooks_cb,
1886 .get_logmask = api_get_logmask_cb,
1887 .set_logmask = api_set_logmask_cb,
1888 .describe = api_describe_cb,
1889 .unref = api_unref_cb
1892 int afb_export_declare(struct afb_export *export,
1896 struct afb_api_item afb_api;
1898 if (export->declared)
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;
1906 /* records the binding */
1907 rc = afb_apiset_add(export->declare_set, export->api.apiname, afb_api);
1909 export->declared = 1;
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);
1921 void afb_export_undeclare(struct afb_export *export)
1923 if (export->declared) {
1924 export->declared = 0;
1925 afb_apiset_del(export->declare_set, export->api.apiname);
1929 int afb_export_subscribe(struct afb_export *export, struct afb_event_x2 *event)
1931 return afb_evt_event_x2_add_watch(export->listener, event);
1934 int afb_export_unsubscribe(struct afb_export *export, struct afb_event_x2 *event)
1936 return afb_evt_event_x2_remove_watch(export->listener, event);
1939 void afb_export_process_xreq(struct afb_export *export, struct afb_xreq *xreq)
1941 afb_xreq_process(xreq, export->call_set);
1944 void afb_export_context_init(struct afb_export *export, struct afb_context *context)
1946 afb_context_init(context, export->session, NULL);
1947 context->validated = 1;