X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fafb-ditf.c;h=d3db9bb0721b198fde1126fbe8cd693cbd982ea0;hb=c65113c390a5337924729e21f74f45df8c109291;hp=66bdb9df146ec0fab4876a29049535437c506011;hpb=8a30491d5e60239a338e485311a29c43398726e9;p=src%2Fapp-framework-binder.git diff --git a/src/afb-ditf.c b/src/afb-ditf.c index 66bdb9df..d3db9bb0 100644 --- a/src/afb-ditf.c +++ b/src/afb-ditf.c @@ -46,7 +46,7 @@ static void vverbose_cb(void *closure, int level, const char *file, int line, co char *p; struct afb_ditf *ditf = closure; - if (vasprintf(&p, fmt, args) < 0) + if (!fmt || vasprintf(&p, fmt, args) < 0) vverbose(level, file, line, function, fmt, args); else { verbose(level, file, line, function, "[API %s] %s", ditf->api, p); @@ -56,7 +56,7 @@ static void vverbose_cb(void *closure, int level, const char *file, int line, co static void old_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args) { - vverbose_cb(closure, level, file, line, "?", fmt, args); + vverbose_cb(closure, level, file, line, NULL, fmt, args); } static struct afb_event event_make_cb(void *closure, const char *name) @@ -65,6 +65,14 @@ static struct afb_event event_make_cb(void *closure, const char *name) char *event; struct afb_ditf *ditf = closure; + /* check daemon state */ + if (ditf->state == Daemon_Pre_Init) { + + ERROR("[API %s] Bad call to 'afb_daemon_event_make(%s)', must not be in PreInit", ditf->api, name); + errno = EINVAL; + return (struct afb_event){ .itf = NULL, .closure = NULL }; + } + /* makes the event name */ plen = strlen(ditf->api); nlen = strlen(name); @@ -83,6 +91,14 @@ static int event_broadcast_cb(void *closure, const char *name, struct json_objec char *event; struct afb_ditf *ditf = closure; + /* check daemon state */ + if (ditf->state == Daemon_Pre_Init) { + + ERROR("[API %s] Bad call to 'afb_daemon_event_broadcast(%s, %s)', must not be in PreInit", ditf->api, name, json_object_to_json_string(object)); + errno = EINVAL; + return 0; + } + /* makes the event name */ plen = strlen(ditf->api); nlen = strlen(name); @@ -112,8 +128,31 @@ static struct afb_req unstore_req_cb(void *closure, struct afb_stored_req *sreq) static int require_api_cb(void *closure, const char *name, int initialized) { - struct afb_api a; - return (initialized ? afb_apiset_get_started : afb_apiset_get)(main_apiset, name, &a); + struct afb_ditf *ditf = closure; + if (ditf->state != Daemon_Init) { + ERROR("[API %s] Bad call to 'afb_daemon_require(%s, %d)', must be in Init", ditf->api, name, initialized); + errno = EINVAL; + return -1; + } + return -!(initialized ? afb_apiset_lookup_started : afb_apiset_lookup)(main_apiset, name, 1); +} + +static int rename_api_cb(void *closure, const char *name) +{ + struct afb_ditf *ditf = closure; + if (ditf->state != Daemon_Pre_Init) { + ERROR("[API %s] Bad call to 'afb_daemon_rename(%s)', must be in PreInit", ditf->api, name); + errno = EINVAL; + return -1; + } + if (!afb_api_is_valid_name(name)) { + ERROR("[API %s] Can't rename to %s: bad API name", ditf->api, name); + errno = EINVAL; + return -1; + } + NOTICE("[API %s] renamed to [API %s]", ditf->api, name); + afb_ditf_rename(ditf, name); + return 0; } /********************************************** @@ -131,7 +170,7 @@ static void hooked_vverbose_cb(void *closure, int level, const char *file, int l static void hooked_old_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args) { - hooked_vverbose_cb(closure, level, file, line, "?", fmt, args); + hooked_vverbose_cb(closure, level, file, line, NULL, fmt, args); } static struct afb_event hooked_event_make_cb(void *closure, const char *name) @@ -211,6 +250,14 @@ static int hooked_require_api_cb(void *closure, const char *name, int initialize return afb_hook_ditf_require_api_result(ditf, name, initialized, result); } +static int hooked_rename_api_cb(void *closure, const char *name) +{ + struct afb_ditf *ditf = closure; + const char *oldname = ditf->api; + int result = rename_api_cb(closure, name); + return afb_hook_ditf_rename_api(ditf, oldname, name, result); +} + /********************************************** * vectors **********************************************/ @@ -226,7 +273,8 @@ static const struct afb_daemon_itf daemon_itf = { .rootdir_open_locale = rootdir_open_locale_cb, .queue_job = queue_job_cb, .unstore_req = unstore_req_cb, - .require_api = require_api_cb + .require_api = require_api_cb, + .rename_api = rename_api_cb }; static const struct afb_daemon_itf hooked_daemon_itf = { @@ -241,12 +289,14 @@ static const struct afb_daemon_itf hooked_daemon_itf = { .rootdir_open_locale = hooked_rootdir_open_locale_cb, .queue_job = hooked_queue_job_cb, .unstore_req = hooked_unstore_req_cb, - .require_api = hooked_require_api_cb + .require_api = hooked_require_api_cb, + .rename_api = hooked_rename_api_cb }; void afb_ditf_init_v2(struct afb_ditf *ditf, const char *api, struct afb_binding_data_v2 *data) { ditf->version = 2; + ditf->state = Daemon_Pre_Init; ditf->v2 = data; data->daemon.closure = ditf; afb_ditf_rename(ditf, api); @@ -255,6 +305,7 @@ void afb_ditf_init_v2(struct afb_ditf *ditf, const char *api, struct afb_binding void afb_ditf_init_v1(struct afb_ditf *ditf, const char *api, struct afb_binding_interface_v1 *itf) { ditf->version = 1; + ditf->state = Daemon_Pre_Init; ditf->v1 = itf; itf->verbosity = verbosity; itf->mode = AFB_MODE_LOCAL;