X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fafb-export.c;h=b46e6a085251b3df3ab150c622c584d725a23316;hb=60cd11786766ebc148b7ec088962dd6e112f8762;hp=b5a4b9e39796c3cbd219bcd722c59235f3972c57;hpb=d7e4faba5d1744160d538edd74bc4bdd8a173b1a;p=src%2Fapp-framework-binder.git diff --git a/src/afb-export.c b/src/afb-export.c index b5a4b9e3..b46e6a08 100644 --- a/src/afb-export.c +++ b/src/afb-export.c @@ -33,10 +33,12 @@ #include "afb-api.h" #include "afb-apiset.h" -#if defined(WITH_LEGACY_BINDING_V1) +#if WITH_LEGACY_BINDING_V1 #include "afb-api-so-v1.h" #endif +#if WITH_LEGACY_BINDING_V2 #include "afb-api-so-v2.h" +#endif #include "afb-api-v3.h" #include "afb-common.h" #include "afb-cred.h" @@ -65,10 +67,12 @@ enum afb_api_version { Api_Version_None = 0, -#if defined(WITH_LEGACY_BINDING_V1) +#if WITH_LEGACY_BINDING_V1 Api_Version_1 = 1, #endif +#if WITH_LEGACY_BINDING_V2 Api_Version_2 = 2, +#endif Api_Version_3 = 3 }; @@ -105,9 +109,11 @@ struct afb_export /* unsealed */ unsigned unsealed: 1; +#if WITH_AFB_HOOK /* hooking flags */ int hookditf; int hooksvc; +#endif /* session for service */ struct afb_session *session; @@ -135,7 +141,7 @@ struct afb_export /* internal descriptors */ union { -#if defined(WITH_LEGACY_BINDING_V1) +#if WITH_LEGACY_BINDING_V1 struct afb_binding_v1 *v1; #endif const struct afb_binding_v2 *v2; @@ -144,7 +150,7 @@ struct afb_export /* start function */ union { -#if defined(WITH_LEGACY_BINDING_V1) +#if WITH_LEGACY_BINDING_V1 int (*v1)(struct afb_service_x1); #endif int (*v2)(); @@ -157,14 +163,14 @@ struct afb_export /* exported data */ union { -#if defined(WITH_LEGACY_BINDING_V1) +#if WITH_LEGACY_BINDING_V1 struct afb_binding_interface_v1 v1; #endif struct afb_binding_data_v2 *v2; } export; /* initial name */ - char name[1]; + char name[]; }; /*****************************************************************************/ @@ -263,11 +269,6 @@ static void vverbose_cb(struct afb_api_x3 *closure, int level, const char *file, } } -static void legacy_vverbose_v1_cb(struct afb_api_x3 *closure, int level, const char *file, int line, const char *fmt, va_list args) -{ - vverbose_cb(closure, level, file, line, NULL, fmt, args); -} - static struct afb_event_x2 *event_x2_make_cb(struct afb_api_x3 *closure, const char *name) { struct afb_export *export = from_api_x3(closure); @@ -283,12 +284,6 @@ static struct afb_event_x2 *event_x2_make_cb(struct afb_api_x3 *closure, const c return afb_evt_event_x2_create2(export->api.apiname, name); } -static struct afb_event_x1 legacy_event_x1_make_cb(struct afb_api_x3 *closure, const char *name) -{ - struct afb_event_x2 *event = event_x2_make_cb(closure, name); - return afb_evt_event_from_evtid(afb_evt_event_x2_to_evtid(event)); -} - static int event_broadcast_cb(struct afb_api_x3 *closure, const char *name, struct json_object *object) { size_t plen, nlen; @@ -315,6 +310,24 @@ static int event_broadcast_cb(struct afb_api_x3 *closure, const char *name, stru return afb_evt_broadcast(event, object); } +static struct sd_event *get_event_loop(struct afb_api_x3 *closure) +{ + jobs_acquire_event_manager(); + return systemd_get_event_loop(); +} + +static struct sd_bus *get_user_bus(struct afb_api_x3 *closure) +{ + jobs_acquire_event_manager(); + return systemd_get_user_bus(); +} + +static struct sd_bus *get_system_bus(struct afb_api_x3 *closure) +{ + jobs_acquire_event_manager(); + return systemd_get_system_bus(); +} + static int rootdir_open_locale_cb(struct afb_api_x3 *closure, const char *filename, int flags, const char *locale) { return afb_common_rootdir_open_locale(filename, flags, locale); @@ -325,11 +338,6 @@ static int queue_job_cb(struct afb_api_x3 *closure, void (*callback)(int signum, return jobs_queue(group, timeout, callback, argument); } -static struct afb_req_x1 legacy_unstore_req_cb(struct afb_api_x3 *closure, struct afb_stored_req *sreq) -{ - return afb_xreq_unstore(sreq); -} - static int require_api_cb(struct afb_api_x3 *closure, const char *name, int initialized) { struct afb_export *export = from_api_x3(closure); @@ -401,9 +409,43 @@ static struct afb_api_x3 *api_new_api_cb( return apiv3 ? to_api_x3(afb_api_v3_export(apiv3)) : NULL; } -/********************************************** -* hooked flow -**********************************************/ +#if WITH_LEGACY_BINDING_V1 || WITH_LEGACY_BINDING_V2 + +static void legacy_vverbose_v1_cb(struct afb_api_x3 *closure, int level, const char *file, int line, const char *fmt, va_list args) +{ + vverbose_cb(closure, level, file, line, NULL, fmt, args); +} + +static struct afb_event_x1 legacy_event_x1_make_cb(struct afb_api_x3 *closure, const char *name) +{ + struct afb_event_x2 *event = event_x2_make_cb(closure, name); + return afb_evt_event_from_evtid(afb_evt_event_x2_to_evtid(event)); +} + +static struct afb_req_x1 legacy_unstore_req_cb(struct afb_api_x3 *closure, struct afb_stored_req *sreq) +{ + return afb_xreq_unstore(sreq); +} + +static const struct afb_daemon_itf_x1 daemon_itf = { + .vverbose_v1 = legacy_vverbose_v1_cb, + .vverbose_v2 = vverbose_cb, + .event_make = legacy_event_x1_make_cb, + .event_broadcast = event_broadcast_cb, + .get_event_loop = get_event_loop, + .get_user_bus = get_user_bus, + .get_system_bus = get_system_bus, + .rootdir_get_fd = afb_common_rootdir_get_fd, + .rootdir_open_locale = rootdir_open_locale_cb, + .queue_job = queue_job_cb, + .unstore_req = legacy_unstore_req_cb, + .require_api = require_api_cb, + .add_alias = add_alias_cb, + .new_api = api_new_api_cb, +}; +#endif + +#if WITH_AFB_HOOK static void hooked_vverbose_cb(struct afb_api_x3 *closure, int level, const char *file, int line, const char *function, const char *fmt, va_list args) { struct afb_export *export = from_api_x3(closure); @@ -414,11 +456,6 @@ static void hooked_vverbose_cb(struct afb_api_x3 *closure, int level, const char va_end(ap); } -static void legacy_hooked_vverbose_v1_cb(struct afb_api_x3 *closure, int level, const char *file, int line, const char *fmt, va_list args) -{ - hooked_vverbose_cb(closure, level, file, line, NULL, fmt, args); -} - static struct afb_event_x2 *hooked_event_x2_make_cb(struct afb_api_x3 *closure, const char *name) { struct afb_export *export = from_api_x3(closure); @@ -427,15 +464,6 @@ static struct afb_event_x2 *hooked_event_x2_make_cb(struct afb_api_x3 *closure, return r; } -static struct afb_event_x1 legacy_hooked_event_x1_make_cb(struct afb_api_x3 *closure, const char *name) -{ - struct afb_event_x2 *event = hooked_event_x2_make_cb(closure, name); - struct afb_event_x1 e; - e.closure = event; - e.itf = event ? event->itf : NULL; - return e; -} - static int hooked_event_broadcast_cb(struct afb_api_x3 *closure, const char *name, struct json_object *object) { int r; @@ -451,21 +479,30 @@ static int hooked_event_broadcast_cb(struct afb_api_x3 *closure, const char *nam static struct sd_event *hooked_get_event_loop(struct afb_api_x3 *closure) { struct afb_export *export = from_api_x3(closure); - struct sd_event *r = systemd_get_event_loop(); + struct sd_event *r; + + jobs_acquire_event_manager(); + r = get_event_loop(closure); return afb_hook_api_get_event_loop(export, r); } static struct sd_bus *hooked_get_user_bus(struct afb_api_x3 *closure) { struct afb_export *export = from_api_x3(closure); - struct sd_bus *r = systemd_get_user_bus(); + struct sd_bus *r; + + jobs_acquire_event_manager(); + r = get_user_bus(closure); return afb_hook_api_get_user_bus(export, r); } static struct sd_bus *hooked_get_system_bus(struct afb_api_x3 *closure) { struct afb_export *export = from_api_x3(closure); - struct sd_bus *r = systemd_get_system_bus(); + struct sd_bus *r; + + jobs_acquire_event_manager(); + r = get_system_bus(closure); return afb_hook_api_get_system_bus(export, r); } @@ -490,13 +527,6 @@ static int hooked_queue_job_cb(struct afb_api_x3 *closure, void (*callback)(int return afb_hook_api_queue_job(export, callback, argument, group, timeout, r); } -static struct afb_req_x1 legacy_hooked_unstore_req_cb(struct afb_api_x3 *closure, struct afb_stored_req *sreq) -{ - struct afb_export *export = from_api_x3(closure); - afb_hook_api_legacy_unstore_req(export, sreq); - return legacy_unstore_req_cb(closure, sreq); -} - static int hooked_require_api_cb(struct afb_api_x3 *closure, const char *name, int initialized) { int result; @@ -529,25 +559,28 @@ static struct afb_api_x3 *hooked_api_new_api_cb( return result; } -/********************************************** -* vectors -**********************************************/ -static const struct afb_daemon_itf_x1 daemon_itf = { - .vverbose_v1 = legacy_vverbose_v1_cb, - .vverbose_v2 = vverbose_cb, - .event_make = legacy_event_x1_make_cb, - .event_broadcast = event_broadcast_cb, - .get_event_loop = systemd_get_event_loop, - .get_user_bus = systemd_get_user_bus, - .get_system_bus = systemd_get_system_bus, - .rootdir_get_fd = afb_common_rootdir_get_fd, - .rootdir_open_locale = rootdir_open_locale_cb, - .queue_job = queue_job_cb, - .unstore_req = legacy_unstore_req_cb, - .require_api = require_api_cb, - .add_alias = add_alias_cb, - .new_api = api_new_api_cb, -}; +#if WITH_LEGACY_BINDING_V1 || WITH_LEGACY_BINDING_V2 + +static void legacy_hooked_vverbose_v1_cb(struct afb_api_x3 *closure, int level, const char *file, int line, const char *fmt, va_list args) +{ + hooked_vverbose_cb(closure, level, file, line, NULL, fmt, args); +} + +static struct afb_event_x1 legacy_hooked_event_x1_make_cb(struct afb_api_x3 *closure, const char *name) +{ + struct afb_event_x2 *event = hooked_event_x2_make_cb(closure, name); + struct afb_event_x1 e; + e.closure = event; + e.itf = event ? event->itf : NULL; + return e; +} + +static struct afb_req_x1 legacy_hooked_unstore_req_cb(struct afb_api_x3 *closure, struct afb_stored_req *sreq) +{ + struct afb_export *export = from_api_x3(closure); + afb_hook_api_legacy_unstore_req(export, sreq); + return legacy_unstore_req_cb(closure, sreq); +} static const struct afb_daemon_itf_x1 hooked_daemon_itf = { .vverbose_v1 = legacy_hooked_vverbose_v1_cb, @@ -565,6 +598,9 @@ static const struct afb_daemon_itf_x1 hooked_daemon_itf = { .add_alias = hooked_add_alias_cb, .new_api = hooked_api_new_api_cb, }; +#endif + +#endif /****************************************************************************** ****************************************************************************** @@ -614,41 +650,51 @@ static int call_sync_x3( return afb_calls_call_sync(export, api, verb, args, object, error, info); } -static void legacy_call_v12( +static void legacy_call_x3( struct afb_api_x3 *apix3, const char *api, const char *verb, struct json_object *args, - void (*callback)(void*, int, struct json_object*), + void (*callback)(void*, int, struct json_object*, struct afb_api_x3*), void *closure) { struct afb_export *export = from_api_x3(apix3); - afb_calls_legacy_call_v12(export, api, verb, args, callback, closure); + afb_calls_legacy_call_v3(export, api, verb, args, callback, closure); } -static void legacy_call_x3( +static int legacy_call_sync( struct afb_api_x3 *apix3, const char *api, const char *verb, struct json_object *args, - void (*callback)(void*, int, struct json_object*, struct afb_api_x3*), - void *closure) + struct json_object **result) { struct afb_export *export = from_api_x3(apix3); - afb_calls_legacy_call_v3(export, api, verb, args, callback, closure); + return afb_calls_legacy_call_sync(export, api, verb, args, result); } -static int legacy_call_sync( +#if WITH_LEGACY_BINDING_V1 || WITH_LEGACY_BINDING_V2 + +static void legacy_call_v12( struct afb_api_x3 *apix3, const char *api, const char *verb, struct json_object *args, - struct json_object **result) + void (*callback)(void*, int, struct json_object*), + void *closure) { struct afb_export *export = from_api_x3(apix3); - return afb_calls_legacy_call_sync(export, api, verb, args, result); + afb_calls_legacy_call_v12(export, api, verb, args, callback, closure); } +/* the interface for services */ +static const struct afb_service_itf_x1 service_itf = { + .call = legacy_call_v12, + .call_sync = legacy_call_sync +}; +#endif + +#if WITH_AFB_HOOK static void hooked_call_x3( struct afb_api_x3 *apix3, const char *api, @@ -674,52 +720,51 @@ static int hooked_call_sync_x3( return afb_calls_hooked_call_sync(export, api, verb, args, object, error, info); } -static void legacy_hooked_call_v12( +static void legacy_hooked_call_x3( struct afb_api_x3 *apix3, const char *api, const char *verb, struct json_object *args, - void (*callback)(void*, int, struct json_object*), + void (*callback)(void*, int, struct json_object*, struct afb_api_x3*), void *closure) { struct afb_export *export = from_api_x3(apix3); - afb_calls_legacy_hooked_call_v12(export, api, verb, args, callback, closure); + afb_calls_legacy_hooked_call_v3(export, api, verb, args, callback, closure); } -static void legacy_hooked_call_x3( +static int legacy_hooked_call_sync( struct afb_api_x3 *apix3, const char *api, const char *verb, struct json_object *args, - void (*callback)(void*, int, struct json_object*, struct afb_api_x3*), - void *closure) + struct json_object **result) { struct afb_export *export = from_api_x3(apix3); - afb_calls_legacy_hooked_call_v3(export, api, verb, args, callback, closure); + return afb_calls_legacy_hooked_call_sync(export, api, verb, args, result); } -static int legacy_hooked_call_sync( +#if WITH_LEGACY_BINDING_V1 || WITH_LEGACY_BINDING_V2 + +static void legacy_hooked_call_v12( struct afb_api_x3 *apix3, const char *api, const char *verb, struct json_object *args, - struct json_object **result) + void (*callback)(void*, int, struct json_object*), + void *closure) { struct afb_export *export = from_api_x3(apix3); - return afb_calls_legacy_hooked_call_sync(export, api, verb, args, result); + afb_calls_legacy_hooked_call_v12(export, api, verb, args, callback, closure); } -/* the interface for services */ -static const struct afb_service_itf_x1 service_itf = { - .call = legacy_call_v12, - .call_sync = legacy_call_sync -}; - /* the interface for services */ static const struct afb_service_itf_x1 hooked_service_itf = { .call = legacy_hooked_call_v12, .call_sync = legacy_hooked_call_sync }; +#endif + +#endif /****************************************************************************** ****************************************************************************** @@ -735,6 +780,7 @@ static int api_set_verbs_v2_cb( struct afb_api_x3 *api, const struct afb_verb_v2 *verbs) { +#if WITH_LEGACY_BINDING_V2 struct afb_export *export = from_api_x3(api); if (export->unsealed) { @@ -743,6 +789,9 @@ static int api_set_verbs_v2_cb( } errno = EPERM; +#else + errno = ECANCELED; +#endif return -1; } @@ -927,6 +976,48 @@ static struct json_object *settings_cb(struct afb_api_x3 *api) return result; } +static const struct afb_api_x3_itf api_x3_itf = { + + .vverbose = (void*)vverbose_cb, + + .get_event_loop = get_event_loop, + .get_user_bus = get_user_bus, + .get_system_bus = get_system_bus, + .rootdir_get_fd = afb_common_rootdir_get_fd, + .rootdir_open_locale = rootdir_open_locale_cb, + .queue_job = queue_job_cb, + + .require_api = require_api_cb, + .add_alias = add_alias_cb, + + .event_broadcast = event_broadcast_cb, + .event_make = event_x2_make_cb, + + .legacy_call = legacy_call_x3, + .legacy_call_sync = legacy_call_sync, + + .api_new_api = api_new_api_cb, + .api_set_verbs_v2 = api_set_verbs_v2_cb, + .api_add_verb = api_add_verb_cb, + .api_del_verb = api_del_verb_cb, + .api_set_on_event = api_set_on_event_cb, + .api_set_on_init = api_set_on_init_cb, + .api_seal = api_seal_cb, + .api_set_verbs_v3 = api_set_verbs_v3_cb, + .event_handler_add = event_handler_add_cb, + .event_handler_del = event_handler_del_cb, + + .call = call_x3, + .call_sync = call_sync_x3, + + .class_provide = class_provide_cb, + .class_require = class_require_cb, + + .delete_api = delete_api_cb, + .settings = settings_cb, +}; + +#if WITH_AFB_HOOK static int hooked_api_set_verbs_v2_cb( struct afb_api_x3 *api, const struct afb_verb_v2 *verbs) @@ -1048,47 +1139,6 @@ static struct json_object *hooked_settings_cb(struct afb_api_x3 *api) return result; } -static const struct afb_api_x3_itf api_x3_itf = { - - .vverbose = (void*)vverbose_cb, - - .get_event_loop = systemd_get_event_loop, - .get_user_bus = systemd_get_user_bus, - .get_system_bus = systemd_get_system_bus, - .rootdir_get_fd = afb_common_rootdir_get_fd, - .rootdir_open_locale = rootdir_open_locale_cb, - .queue_job = queue_job_cb, - - .require_api = require_api_cb, - .add_alias = add_alias_cb, - - .event_broadcast = event_broadcast_cb, - .event_make = event_x2_make_cb, - - .legacy_call = legacy_call_x3, - .legacy_call_sync = legacy_call_sync, - - .api_new_api = api_new_api_cb, - .api_set_verbs_v2 = api_set_verbs_v2_cb, - .api_add_verb = api_add_verb_cb, - .api_del_verb = api_del_verb_cb, - .api_set_on_event = api_set_on_event_cb, - .api_set_on_init = api_set_on_init_cb, - .api_seal = api_seal_cb, - .api_set_verbs_v3 = api_set_verbs_v3_cb, - .event_handler_add = event_handler_add_cb, - .event_handler_del = event_handler_del_cb, - - .call = call_x3, - .call_sync = call_sync_x3, - - .class_provide = class_provide_cb, - .class_require = class_require_cb, - - .delete_api = delete_api_cb, - .settings = settings_cb, -}; - static const struct afb_api_x3_itf hooked_api_x3_itf = { .vverbose = hooked_vverbose_cb, @@ -1129,6 +1179,7 @@ static const struct afb_api_x3_itf hooked_api_x3_itf = { .delete_api = hooked_delete_api_cb, .settings = hooked_settings_cb, }; +#endif /****************************************************************************** ****************************************************************************** @@ -1149,22 +1200,28 @@ static void listener_of_events(void *closure, const char *event, int eventid, st void (*callback)(void *, const char*, struct json_object*, struct afb_api_x3*); struct afb_export *export = from_api_x3(closure); +#if WITH_AFB_HOOK /* hook the event before */ if (export->hooksvc & afb_hook_flag_api_on_event) afb_hook_api_on_event_before(export, event, eventid, object); +#endif /* transmit to specific handlers */ /* search the handler */ handler = export->event_handlers ? globset_match(export->event_handlers, event) : NULL; if (handler) { callback = handler->callback; +#if WITH_AFB_HOOK if (!(export->hooksvc & afb_hook_flag_api_on_event_handler)) +#endif callback(handler->closure, event, object, to_api_x3(export)); +#if WITH_AFB_HOOK else { afb_hook_api_on_event_handler_before(export, event, eventid, object, handler->pattern); callback(handler->closure, event, object, to_api_x3(export)); afb_hook_api_on_event_handler_after(export, event, eventid, object, handler->pattern); } +#endif } else { /* transmit to default handler */ if (export->on_any_event_v3) @@ -1173,9 +1230,11 @@ static void listener_of_events(void *closure, const char *event, int eventid, st export->on_any_event_v12(event, object); } +#if WITH_AFB_HOOK /* hook the event after */ if (export->hooksvc & afb_hook_flag_api_on_event) afb_hook_api_on_event_after(export, event, eventid, object); +#endif json_object_put(object); } @@ -1255,6 +1314,47 @@ int afb_export_event_handler_del( ****************************************************************************** ******************************************************************************/ +static void set_interfaces(struct afb_export *export) +{ +#if WITH_AFB_HOOK + export->hookditf = afb_hook_flags_api(export->api.apiname); + export->hooksvc = afb_hook_flags_api(export->api.apiname); + export->api.itf = export->hookditf|export->hooksvc ? &hooked_api_x3_itf : &api_x3_itf; + + switch (export->version) { +#if WITH_LEGACY_BINDING_V1 + case Api_Version_1: + export->export.v1.daemon.itf = export->hookditf ? &hooked_daemon_itf : &daemon_itf; + break; +#endif +#if WITH_LEGACY_BINDING_V2 + case Api_Version_2: + export->export.v2->daemon.itf = export->hookditf ? &hooked_daemon_itf : &daemon_itf; + export->export.v2->service.itf = export->hooksvc ? &hooked_service_itf : &service_itf; + break; +#endif + } +#else + + export->api.itf = &api_x3_itf; + + switch (export->version) { +#if WITH_LEGACY_BINDING_V1 + case Api_Version_1: + export->export.v1.daemon.itf = &daemon_itf; + break; +#endif +#if WITH_LEGACY_BINDING_V2 + case Api_Version_2: + export->export.v2->daemon.itf = &daemon_itf; + export->export.v2->service.itf = &service_itf; + break; +#endif + } + +#endif +} + static struct afb_export *create( struct afb_apiset *declare_set, struct afb_apiset *call_set, @@ -1272,7 +1372,7 @@ static struct afb_export *create( return NULL; } lenapi = strlen(apiname); - export = calloc(1, sizeof *export + lenapi + (path == apiname || !path ? 0 : strlen(path))); + export = calloc(1, sizeof *export + 1 + lenapi + (path == apiname || !path ? 0 : strlen(path))); if (!export) errno = ENOMEM; else { @@ -1333,7 +1433,7 @@ struct afb_export *afb_export_create_none_for_path( struct afb_export *export = create(declare_set, call_set, path, path, Api_Version_None); if (export) { afb_export_logmask_set(export, logmask); - afb_export_update_hooks(export); + set_interfaces(export); if (creator && creator(closure, to_api_x3(export)) < 0) { afb_export_unref(export); export = NULL; @@ -1342,7 +1442,7 @@ struct afb_export *afb_export_create_none_for_path( return export; } -#if defined(WITH_LEGACY_BINDING_V1) +#if WITH_LEGACY_BINDING_V1 struct afb_export *afb_export_create_v1(struct afb_apiset *declare_set, struct afb_apiset *call_set, const char *apiname, @@ -1357,12 +1457,13 @@ struct afb_export *afb_export_create_v1(struct afb_apiset *declare_set, export->export.v1.mode = AFB_MODE_LOCAL; export->export.v1.daemon.closure = to_api_x3(export); afb_export_logmask_set(export, logmask); - afb_export_update_hooks(export); + set_interfaces(export); } return export; } #endif +#if WITH_LEGACY_BINDING_V2 struct afb_export *afb_export_create_v2(struct afb_apiset *declare_set, struct afb_apiset *call_set, const char *apiname, @@ -1381,10 +1482,11 @@ struct afb_export *afb_export_create_v2(struct afb_apiset *declare_set, data->daemon.closure = to_api_x3(export); data->service.closure = to_api_x3(export); afb_export_logmask_set(export, logmask); - afb_export_update_hooks(export); + set_interfaces(export); } return export; } +#endif struct afb_export *afb_export_create_v3(struct afb_apiset *declare_set, struct afb_apiset *call_set, @@ -1399,7 +1501,7 @@ struct afb_export *afb_export_create_v3(struct afb_apiset *declare_set, export->desc.v3 = apiv3; export->creator = afb_export_addref(creator); afb_export_logmask_set(export, logmask); - afb_export_update_hooks(export); + set_interfaces(export); } return export; } @@ -1429,7 +1531,7 @@ int afb_export_rename(struct afb_export *export, const char *apiname) free((void*)export->api.apiname); export->api.apiname = name; - afb_export_update_hooks(export); + set_interfaces(export); return 0; } @@ -1438,25 +1540,6 @@ const char *afb_export_apiname(const struct afb_export *export) return export->api.apiname; } -void afb_export_update_hooks(struct afb_export *export) -{ - export->hookditf = afb_hook_flags_api(export->api.apiname); - export->hooksvc = afb_hook_flags_api(export->api.apiname); - export->api.itf = export->hookditf|export->hooksvc ? &hooked_api_x3_itf : &api_x3_itf; - - switch (export->version) { -#if defined(WITH_LEGACY_BINDING_V1) - case Api_Version_1: - export->export.v1.daemon.itf = export->hookditf ? &hooked_daemon_itf : &daemon_itf; - break; -#endif - case Api_Version_2: - export->export.v2->daemon.itf = export->hookditf ? &hooked_daemon_itf : &daemon_itf; - export->export.v2->service.itf = export->hooksvc ? &hooked_service_itf : &service_itf; - break; - } -} - int afb_export_unshare_session(struct afb_export *export) { if (export->session == common_session) { @@ -1471,15 +1554,18 @@ int afb_export_unshare_session(struct afb_export *export) return 0; } +#if WITH_LEGACY_BINDING_V1 || WITH_LEGACY_BINDING_V2 int afb_export_handle_events_v12(struct afb_export *export, void (*on_event)(const char *event, struct json_object *object)) { /* check version */ switch (export->version) { -#if defined(WITH_LEGACY_BINDING_V1) +#if WITH_LEGACY_BINDING_V1 case Api_Version_1: #endif +#if WITH_LEGACY_BINDING_V2 case Api_Version_2: break; +#endif default: ERROR("invalid version 12 for API %s", export->api.apiname); errno = EINVAL; @@ -1489,6 +1575,7 @@ int afb_export_handle_events_v12(struct afb_export *export, void (*on_event)(con export->on_any_event_v12 = on_event; return ensure_listener(export); } +#endif int afb_export_handle_events_v3(struct afb_export *export, void (*on_event)(struct afb_api_x3 *api, const char *event, struct json_object *object)) { @@ -1517,7 +1604,7 @@ int afb_export_handle_init_v3(struct afb_export *export, int (*oninit)(struct af return 0; } -#if defined(WITH_LEGACY_BINDING_V1) +#if WITH_LEGACY_BINDING_V1 /* * Starts a new service (v1) */ @@ -1544,10 +1631,12 @@ void afb_export_logmask_set(struct afb_export *export, int mask) { export->api.logmask = mask; switch (export->version) { -#if defined(WITH_LEGACY_BINDING_V1) +#if WITH_LEGACY_BINDING_V1 case Api_Version_1: export->export.v1.verbosity = verbosity_from_mask(mask); break; #endif +#if WITH_LEGACY_BINDING_V2 case Api_Version_2: export->export.v2->verbosity = verbosity_from_mask(mask); break; +#endif } } @@ -1588,17 +1677,23 @@ static void do_init(int sig, void *closure) else { export = init->export; switch (export->version) { -#if defined(WITH_LEGACY_BINDING_V1) +#if WITH_LEGACY_BINDING_V1 case Api_Version_1: rc = export->init.v1 ? export->init.v1( (struct afb_service_x1){ +#if WITH_AFB_HOOK .itf = &hooked_service_itf, +#else + .itf = &service_itf, +#endif .closure = to_api_x3(export) }) : 0; break; #endif +#if WITH_LEGACY_BINDING_V2 case Api_Version_2: rc = export->init.v2 ? export->init.v2() : 0; break; +#endif case Api_Version_3: rc = export->init.v3 ? export->init.v3(to_api_x3(export)) : 0; break; @@ -1631,11 +1726,14 @@ int afb_export_start(struct afb_export *export) } /* set event handling */ +#if WITH_LEGACY_BINDING_V1 || WITH_LEGACY_BINDING_V2 switch (export->version) { -#if defined(WITH_LEGACY_BINDING_V1) +#if WITH_LEGACY_BINDING_V1 case Api_Version_1: #endif +#if WITH_LEGACY_BINDING_V2 case Api_Version_2: +#endif if (export->on_any_event_v12) { rc = afb_export_handle_events_v12(export, export->on_any_event_v12); break; @@ -1649,10 +1747,13 @@ int afb_export_start(struct afb_export *export) ERROR("Can't set event handler for %s", export->api.apiname); return -1; } +#endif +#if WITH_AFB_HOOK /* Starts the service */ if (export->hooksvc & afb_hook_flag_api_start) afb_hook_api_start_before(export); +#endif export->state = Api_State_Init; init.export = export; @@ -1660,8 +1761,10 @@ int afb_export_start(struct afb_export *export) rc = init.return_code; export->state = Api_State_Run; +#if WITH_AFB_HOOK if (export->hooksvc & afb_hook_flag_api_start) afb_hook_api_start_after(export, rc); +#endif if (rc < 0) { /* initialisation error */ @@ -1679,14 +1782,16 @@ static void api_call_cb(void *closure, struct afb_xreq *xreq) xreq->request.api = to_api_x3(export); switch (export->version) { -#if defined(WITH_LEGACY_BINDING_V1) +#if WITH_LEGACY_BINDING_V1 case Api_Version_1: afb_api_so_v1_process_call(export->desc.v1, xreq); break; #endif +#if WITH_LEGACY_BINDING_V2 case Api_Version_2: afb_api_so_v2_process_call(export->desc.v2, xreq); break; +#endif case Api_Version_3: afb_api_v3_process_call(export->desc.v3, xreq); break; @@ -1702,14 +1807,16 @@ static struct json_object *api_describe_cb(void *closure) struct json_object *result; switch (export->version) { -#if defined(WITH_LEGACY_BINDING_V1) +#if WITH_LEGACY_BINDING_V1 case Api_Version_1: result = afb_api_so_v1_make_description_openAPIv3(export->desc.v1, export->api.apiname); break; #endif +#if WITH_LEGACY_BINDING_V2 case Api_Version_2: result = afb_api_so_v2_make_description_openAPIv3(export->desc.v2, export->api.apiname); break; +#endif case Api_Version_3: result = afb_api_v3_make_description_openAPIv3(export->desc.v3, export->api.apiname); break; @@ -1727,12 +1834,13 @@ static int api_service_start_cb(void *closure) return afb_export_start(export); } +#if WITH_AFB_HOOK static void api_update_hooks_cb(void *closure) -{ - struct afb_export *export = closure; + __attribute__((alias("set_interfaces"))); - afb_export_update_hooks(export); -} +void afb_export_update_hooks(struct afb_export *export) + __attribute__((alias("set_interfaces"))); +#endif static int api_get_logmask_cb(void *closure) { @@ -1759,7 +1867,9 @@ static struct afb_api_itf export_api_itf = { .call = api_call_cb, .service_start = api_service_start_cb, +#if WITH_AFB_HOOK .update_hooks = api_update_hooks_cb, +#endif .get_logmask = api_get_logmask_cb, .set_logmask = api_set_logmask_cb, .describe = api_describe_cb,