From eabae24ea592420de46e36f0b1af5d39eee5b8a4 Mon Sep 17 00:00:00 2001 From: Romain Forlot Date: Thu, 14 Sep 2017 19:31:42 +0200 Subject: Attach and recursion check working Change-Id: I2f9509d4b6aa63a16df8db2187810337fd802ef4 Signed-off-by: Romain Forlot --- controller/ctl-config.c | 81 ++++++++++++++++++++++++++++++++++++------------- controller/ctl-config.h | 11 ++++--- controller/ctl-plugin.c | 23 +++++++++----- 3 files changed, 81 insertions(+), 34 deletions(-) (limited to 'controller') diff --git a/controller/ctl-config.c b/controller/ctl-config.c index 95f6db8..948f0d1 100644 --- a/controller/ctl-config.c +++ b/controller/ctl-config.c @@ -28,12 +28,16 @@ // Load control config file -char* CtlConfigSearch(const char *dirList) { +char* CtlConfigSearch(const char *dirList, const char* fileName) { int index; char controlFile [CONTROL_MAXPATH_LEN]; - strncpy(controlFile, CONTROL_CONFIG_PRE "-", CONTROL_MAXPATH_LEN); - strncat(controlFile, GetBinderName(), CONTROL_MAXPATH_LEN); + if(fileName) { + strncpy(controlFile, fileName, CONTROL_MAXPATH_LEN); + } else { + strncpy(controlFile, CONTROL_CONFIG_PRE "-", CONTROL_MAXPATH_LEN); + strncat(controlFile, GetBinderName(), CONTROL_MAXPATH_LEN); + } // search for default dispatch config file json_object* responseJ = ScanForConfig(dirList, CTL_SCAN_RECURSIVE, controlFile, ".json"); @@ -97,9 +101,6 @@ int CtlConfigExec(CtlConfigT *ctlConfig) { errcount += ctlConfig->sections[idx].loadCB(&ctlConfig->sections[idx], NULL); } return errcount; - -OnErrorExit: - return 1; } CtlConfigT *CtlConfigLoad(const char* filepath, CtlSectionT *sections) { @@ -112,18 +113,22 @@ CtlConfigT *CtlConfigLoad(const char* filepath, CtlSectionT *sections) { if (err) goto OnErrorExit; #endif - // Search for config in filepath - filepath = CtlConfigSearch(filepath); - - if (!filepath) { - AFB_ERROR("CTL-LOAD-CONFIG No JSON Config found invalid JSON %s ", filepath); - goto OnErrorExit; + json_object* loadJSON(const char* fileName) + { + // Search for config in filepath + const char* filepathFound = CtlConfigSearch(filepath, fileName); + if(!filepathFound) + { + AFB_ERROR("CTL-LOAD-CONFIG No JSON Config found in %s", filepath); + return NULL; + } + return json_object_from_file(filepathFound); } // Load JSON file - ctlConfigJ = json_object_from_file(filepath); + ctlConfigJ = loadJSON(NULL); if (!ctlConfigJ) { - AFB_ERROR("CTL-LOAD-CONFIG Not invalid JSON %s ", filepath); + AFB_ERROR("CTL-LOAD-CONFIG Invalid JSON %s ", filepath); goto OnErrorExit; } @@ -132,9 +137,13 @@ CtlConfigT *CtlConfigLoad(const char* filepath, CtlSectionT *sections) { json_object *metadataJ; int done = json_object_object_get_ex(ctlConfigJ, "metadata", &metadataJ); if (done) { - CtlConfigT *ctlConfig = calloc(1, sizeof (CtlConfigT)); - err = wrap_json_unpack(metadataJ, "{ss,ss,ss,s?s,s?o !}", "label", &ctlConfig->label, "version", &ctlConfig->version - , "api", &ctlConfig->api, "info", &ctlConfig->info, "require", &ctlConfig->requireJ); + err = wrap_json_unpack(metadataJ, "{ss,ss,ss,s?s,s?o,s?o !}", + "label", &ctlConfig->label, + "version", &ctlConfig->version, + "api", &ctlConfig->api, + "info", &ctlConfig->info, + "require", &ctlConfig->requireJ, + "files",&ctlConfig->filesJ); if (err) { AFB_ERROR("CTL-LOAD-CONFIG:METADATA Missing something label|api|version|[info]|[require] in:\n-- %s", json_object_get_string(metadataJ)); goto OnErrorExit; @@ -145,22 +154,52 @@ CtlConfigT *CtlConfigLoad(const char* filepath, CtlSectionT *sections) { err = afb_daemon_rename_api(ctlConfig->api); if (err) AFB_WARNING("Fail to rename api to:%s", ctlConfig->api); } - } //load config sections err = 0; ctlConfig->sections = sections; + for (int idx = 0; sections[idx].key != NULL; idx++) { json_object * sectionJ; int done = json_object_object_get_ex(ctlConfigJ, sections[idx].key, §ionJ); if (!done) { - AFB_ERROR("CtlConfigLoad: fail to find '%s' section in config '%s'", sections[idx].key, filepath); - err++; + json_object* configJ = NULL; + const char* fileName; + AFB_DEBUG("CtlConfigLoad: fail to find '%s' section in config '%s'. Searching deeper", sections[idx].key, filepath); + if (json_object_get_type(ctlConfig->filesJ) == json_type_array) { + int filesEnd = json_object_array_length(ctlConfig->filesJ); + for (int jdx = 0; jdx < filesEnd; jdx++) { + fileName = json_object_get_string(json_object_array_get_idx(ctlConfig->filesJ, jdx)); + configJ = loadJSON(fileName); + if(json_object_object_get_ex(configJ, sections[idx].key, §ionJ)) + { + const char* k = sections[idx].key; + err += sections[idx].loadCB(§ions[idx], sectionJ); + done++; + } + } + if(!done) { + AFB_ERROR("CtlConfigLoad: fail to find '%s' section in config '%s' with '%s' in its name", sections[idx].key, filepath, fileName); + err++; + } + if(err) {goto OnErrorExit;} + } else { + fileName = json_object_get_string(ctlConfig->filesJ); + configJ = loadJSON(fileName); + if(json_object_object_get_ex(configJ, sections[idx].key, §ionJ)) { + err += sections[idx].loadCB(§ions[idx], sectionJ); + done++; + } + else { + AFB_ERROR("CtlConfigLoad: fail to find '%s' section in config '%s' with '%s' in its name", sections[idx].key, filepath, fileName); + err++; + } + if(err) {goto OnErrorExit;} + } } else { err += sections[idx].loadCB(§ions[idx], sectionJ); } - } if (err) goto OnErrorExit; diff --git a/controller/ctl-config.h b/controller/ctl-config.h index 3b39f53..7c3631c 100644 --- a/controller/ctl-config.h +++ b/controller/ctl-config.h @@ -75,11 +75,11 @@ typedef struct { } DispatchHandleT; typedef struct ConfigSectionS { - const char *key; - const char *label; - const char *info; - int (*loadCB)(struct ConfigSectionS *section, json_object *sectionJ); - void *handle; + const char *key; + const char *label; + const char *info; + int (*loadCB)(struct ConfigSectionS *section, json_object *sectionJ); + void *handle; } CtlSectionT; typedef struct { @@ -88,6 +88,7 @@ typedef struct { const char *info; const char *version; json_object *requireJ; + json_object *filesJ; CtlSectionT *sections; } CtlConfigT; diff --git a/controller/ctl-plugin.c b/controller/ctl-plugin.c index 748c78b..ae203af 100644 --- a/controller/ctl-plugin.c +++ b/controller/ctl-plugin.c @@ -44,8 +44,7 @@ int PluginGetCB (CtlActionT *action , json_object *callbackJ) { goto OnErrorExit; } - - int err = wrap_json_unpack(callbackJ, "{ss,ss,s?s,s?o!}", "plugin", &plugin, "function", &function, "args", &argsJ); + int err = wrap_json_unpack(callbackJ, "{ss,ss,s?o!}", "plugin", &plugin, "function", &function, "args", &argsJ); if (err) { AFB_ERROR("PluginGet missing plugin|function|[args] in %s", json_object_get_string(callbackJ)); goto OnErrorExit; @@ -87,7 +86,8 @@ STATIC int DispatchOneL2c(lua_State* luaState, char *funcname, Lua2cFunctionT ca STATIC int PluginLoadOne (CtlPluginT *ctlPlugin, json_object *pluginJ, void* handle) { json_object *lua2csJ = NULL, *actionsJ = NULL; - const char*ldSearchPath = NULL, *basename = NULL; + const char *basename = NULL; + char *ldSearchPath = NULL; void *dlHandle; @@ -95,15 +95,23 @@ STATIC int PluginLoadOne (CtlPluginT *ctlPlugin, json_object *pluginJ, void* han if (!pluginJ) return 0; int err = wrap_json_unpack(pluginJ, "{ss,s?s,s?s,s?s,ss,s?o,s?o!}", - "label", &ctlPlugin->label, "version", &ctlPlugin->version, "info", &ctlPlugin->info, "ldpath", &ldSearchPath, "basename", &basename, "lua2c", &lua2csJ, "actions", &actionsJ); + "label", &ctlPlugin->label, + "version", &ctlPlugin->version, + "info", &ctlPlugin->info, + "ldpath", &ldSearchPath, + "basename", &basename, + "lua2c", &lua2csJ, + "actions", &actionsJ); if (err) { AFB_ERROR("CTL-PLUGIN-LOADONE Plugin missing label|basename|version|[info]|[ldpath]|[lua2c]|[actions] in:\n-- %s", json_object_get_string(pluginJ)); goto OnErrorExit; } // if search path not in Json config file, then try default binding dir - ldSearchPath = getenv("CONTROL_PLUGIN_PATH"); - if (!ldSearchPath) ldSearchPath = strncat(GetBindingDirPath(), "/lib", sizeof(GetBindingDirPath()) - strlen(GetBindingDirPath()) - 1); + if (!ldSearchPath) ldSearchPath = getenv("CONTROL_PLUGIN_PATH"); + if (!ldSearchPath) { + ldSearchPath = strncat(GetBindingDirPath(), "/lib", sizeof(GetBindingDirPath()) - strlen(GetBindingDirPath()) - 1); + } // search for default policy config file json_object *pluginPathJ = ScanForConfig(ldSearchPath, CTL_SCAN_RECURSIVE, basename, CTL_PLUGIN_EXT); @@ -145,7 +153,7 @@ STATIC int PluginLoadOne (CtlPluginT *ctlPlugin, json_object *pluginJ, void* han // store dlopen handle to enable onload action at exec time ctlPlugin->dlHandle = dlHandle; - // Jose hack to make verbosity visible from sharelib + // Jose hack to make verbosity visible from sharedlib struct afb_binding_data_v2 *afbHidenData = dlsym(dlHandle, "afbBindingV2data"); if (afbHidenData) *afbHidenData = afbBindingV2data; @@ -208,7 +216,6 @@ OnErrorExit: return 1; } - int PluginConfig(CtlSectionT *section, json_object *pluginsJ) { int err=0; -- cgit 1.2.3-korg