From d22104c2a1beab2077848aea3c6a7aa63dbd1cd4 Mon Sep 17 00:00:00 2001 From: Romain Forlot Date: Fri, 18 May 2018 00:46:39 +0200 Subject: str Change-Id: I6f240bf971b7b4d45dc6cab6a56ef6157d00fe8f --- ctl-lib/ctl-config.c | 30 +++++++++++++++++++----------- ctl-lib/ctl-lua.c | 31 +++++++++++++++++++------------ ctl-lib/ctl-lua.h | 5 ----- ctl-lib/ctl-plugin.c | 12 +++++++----- 4 files changed, 45 insertions(+), 33 deletions(-) (limited to 'ctl-lib') diff --git a/ctl-lib/ctl-config.c b/ctl-lib/ctl-config.c index c99c3d6..d508b78 100644 --- a/ctl-lib/ctl-config.c +++ b/ctl-lib/ctl-config.c @@ -42,7 +42,7 @@ int CtlConfigMagicNew() { return ((long)rand()); } - json_object* CtlConfigScan(const char *dirList, const char *prefix) { +json_object* CtlConfigScan(const char *dirList, const char *prefix) { char controlFile[CONTROL_MAXPATH_LEN]; const char *binderName = GetBinderName(); @@ -64,33 +64,39 @@ int CtlConfigMagicNew() { char* ConfigSearch(AFB_ApiT apiHandle, json_object *responseJ) { // We load 1st file others are just warnings - char filepath[CONTROL_MAXPATH_LEN]; + size_t p_length; + char *filepath; + const char *filename; + const char*fullpath; + for (int index = 0; index < json_object_array_length(responseJ); index++) { json_object *entryJ = json_object_array_get_idx(responseJ, index); - char *filename; - char*fullpath; int err = wrap_json_unpack(entryJ, "{s:s, s:s !}", "fullpath", &fullpath, "filename", &filename); if (err) { AFB_ApiError(apiHandle, "CTL-INIT HOOPs invalid JSON entry= %s", json_object_get_string(entryJ)); } if (index == 0) { - strncpy(filepath, fullpath, CONTROL_MAXPATH_LEN - 1); - strncat(filepath, "/", CONTROL_MAXPATH_LEN - strlen(filepath) - 1); - strncat(filepath, filename, CONTROL_MAXPATH_LEN - strlen(filepath) - 1); + p_length = strlen(fullpath) + 1 + strlen(filename); + filepath = malloc(p_length + 1); + + strncpy(filepath, fullpath, p_length); + strncat(filepath, "/", p_length - strlen(filepath)); + strncat(filepath, filename, p_length - strlen(filepath)); } } json_object_put(responseJ); - return strndup(filepath, sizeof(filepath)); + return filepath; } char* CtlConfigSearch(AFB_ApiT apiHandle, const char *dirList, const char *prefix) { // search for default dispatch config file json_object* responseJ = CtlConfigScan (dirList, prefix); - if(responseJ) return ConfigSearch(apiHandle, responseJ); + if(responseJ) + return ConfigSearch(apiHandle, responseJ); return NULL; } @@ -179,6 +185,7 @@ json_object* LoadAdditionalsFiles(AFB_ApiT apiHandle, CtlConfigT *ctlHandle, con json_object* CtlUpdateSectionConfig(AFB_ApiT apiHandle, CtlConfigT *ctlHandle, const char *key, json_object *sectionJ, json_object *filesJ) { json_object *sectionArrayJ; + char *oneFile; const char *bindingPath = GetBindingDirPath(apiHandle); if(! json_object_is_type(sectionJ, json_type_array)) { @@ -203,7 +210,7 @@ json_object* CtlUpdateSectionConfig(AFB_ApiT apiHandle, CtlConfigT *ctlHandle, c AFB_ApiError(apiHandle, "No config files found in search path. No changes has been made\n -- %s\n -- %s", CONTROL_CONFIG_PATH, bindingPath); return sectionArrayJ; } - const char *oneFile = ConfigSearch(apiHandle, responseJ); + oneFile = ConfigSearch(apiHandle, responseJ); if (oneFile) { json_object *newSectionJ, *newFileJ = json_object_from_file(oneFile); json_object_object_get_ex(newFileJ, key, &newSectionJ); @@ -222,12 +229,13 @@ json_object* CtlUpdateSectionConfig(AFB_ApiT apiHandle, CtlConfigT *ctlHandle, c AFB_ApiError(apiHandle, "No config files found in search path. No changes has been made\n -- %s\n -- %s", CONTROL_CONFIG_PATH, bindingPath); return sectionArrayJ; } - const char *oneFile = ConfigSearch(apiHandle, responseJ); + oneFile = ConfigSearch(apiHandle, responseJ); json_object *newSectionJ = json_object_from_file(oneFile); LoadAdditionalsFiles(apiHandle, ctlHandle, key, newSectionJ); wrap_json_optarray_for_all(newSectionJ, wrap_json_array_add, sectionArrayJ); } + free(oneFile); return sectionArrayJ; } diff --git a/ctl-lib/ctl-lua.c b/ctl-lib/ctl-lua.c index 3abf330..449ae09 100644 --- a/ctl-lib/ctl-lua.c +++ b/ctl-lib/ctl-lua.c @@ -738,14 +738,13 @@ int LuaLoadScript(const char *luaScriptPath) { } static int LuaDoScript(json_object *queryJ, CtlSourceT *source) { - const char *uid = NULL, *func = NULL; - char luaScriptPath[CONTROL_MAXPATH_LEN]; - char *filename, *fullpath; + const char *uid = NULL, *func = NULL, *filename = NULL, *fullpath = NULL; + char *luaScriptPath = NULL; int index, err = 0; + size_t p_length = 0; json_object *argsJ = NULL; static json_object *luaScriptPathJ = NULL; - luaScriptPath[CONTROL_MAXPATH_LEN - 1] = '\0'; if (!queryJ) { return -1; @@ -758,15 +757,20 @@ static int LuaDoScript(json_object *queryJ, CtlSourceT *source) { "args", &argsJ); if (err) { + AFB_ApiError(source->api, "LUA-DOSCRIPT-SCAN: Miss something in JSON object uid|[spath]|action|[args]: %s", json_object_get_string(queryJ)); return -1; } - // search for filename=script in CONTROL_LUA_PATH + // search for filename=uid*.lua or fallback on bindername*.lua in current directory if (!luaScriptPathJ) { - strncpy(luaScriptPath, CONTROL_DOSCRIPT_PRE, CONTROL_MAXPATH_LEN - 1); - strncat(luaScriptPath, "-", CONTROL_MAXPATH_LEN - strlen(luaScriptPath) - 1); - strncat(luaScriptPath, uid, CONTROL_MAXPATH_LEN - strlen(luaScriptPath) - 1); - luaScriptPathJ = ScanForConfig(luaScriptPath, CTL_SCAN_RECURSIVE, luaScriptPath, ".lua"); + luaScriptPathJ = ScanForConfig(".", CTL_SCAN_RECURSIVE, uid, ".lua"); + if (!luaScriptPathJ) + luaScriptPathJ = ScanForConfig(".", CTL_SCAN_RECURSIVE, GetBinderName(), ".lua"); + } + + if(!luaScriptPathJ) { + AFB_ApiError(source->api, "LUA-DOSCRIPT-SCAN: No script found"); + return -1; } for (index = 0; index < json_object_array_length(luaScriptPathJ); index++) { @@ -775,11 +779,14 @@ static int LuaDoScript(json_object *queryJ, CtlSourceT *source) { err = wrap_json_unpack(entryJ, "{s:s, s:s !}", "fullpath", &fullpath, "filename", &filename); if (err) { AFB_ApiError(source->api, "LUA-DOSCRIPT-SCAN:HOOPs invalid config file path = %s", json_object_get_string(entryJ)); - return -2; + return -1; } // Ignoring other found script. Only take the first one. if (!index) { + p_length = strlen(fullpath) + 1 + strlen(filename); + luaScriptPath = malloc(p_length + 1); + strncpy(luaScriptPath, fullpath, CONTROL_MAXPATH_LEN - 1); strncat(luaScriptPath, "/", CONTROL_MAXPATH_LEN - strlen(luaScriptPath) - 1); strncat(luaScriptPath, filename, CONTROL_MAXPATH_LEN - strlen(luaScriptPath) - 1); @@ -800,7 +807,7 @@ static int LuaDoScript(json_object *queryJ, CtlSourceT *source) { } if (!func) { AFB_ApiError(source->api, "LUA-DOSCRIPT:FAIL to deduct funcname from %s", filename); - return -5; + return -1; } // load function (should exist in CONTROL_PATH_LUA @@ -809,7 +816,7 @@ static int LuaDoScript(json_object *queryJ, CtlSourceT *source) { // Push AFB client context on the stack LuaAfbSourceT *afbSource = LuaSourcePush(luaState, source); if (!afbSource) - return -6; + return -1; return 0; } diff --git a/ctl-lib/ctl-lua.h b/ctl-lib/ctl-lua.h index e1c88f6..ba40991 100644 --- a/ctl-lib/ctl-lua.h +++ b/ctl-lib/ctl-lua.h @@ -29,11 +29,6 @@ extern "C" { #define _GNU_SOURCE #endif -// prefix start debug script -#ifndef CONTROL_DOSCRIPT_PRE -#define CONTROL_DOSCRIPT_PRE "debug" -#endif - // default event name used by LUA #ifndef CONTROL_LUA_EVENT #define CONTROL_LUA_EVENT "luaevt" diff --git a/ctl-lib/ctl-plugin.c b/ctl-lib/ctl-plugin.c index 2097afa..d20950f 100644 --- a/ctl-lib/ctl-plugin.c +++ b/ctl-lib/ctl-plugin.c @@ -112,10 +112,12 @@ static int PluginLoadCOne(AFB_ApiT apiHandle, const char *pluginpath, json_objec int Lua2cAddOne(luaL_Reg *l2cFunc, const char* l2cName, int index) { if(ctlPlugin->ctlL2cFunc->l2cCount) {index += ctlPlugin->ctlL2cFunc->l2cCount+1;} - char funcName[CONTROL_MAXPATH_LEN]; - funcName[CONTROL_MAXPATH_LEN - 1] = '\0'; - strncpy(funcName, "lua2c_", CONTROL_MAXPATH_LEN - 1); - strncat(funcName, l2cName, CONTROL_MAXPATH_LEN - strlen (funcName) - 1); + char *funcName; + size_t p_length = 6 + strlen(l2cName); + funcName = malloc(p_length + 1); + + strncpy(funcName, "lua2c_", p_length); + strncat(funcName, l2cName, p_length - strlen (funcName)); Lua2cFunctionT l2cFunction = (Lua2cFunctionT) dlsym(dlHandle, funcName); if (!l2cFunction) { @@ -213,13 +215,13 @@ static int LoadFoundPlugins(AFB_ApiT apiHandle, json_object *scanResult, json_ob return -1; } - /* Make sure you don't load two found libraries */ ext = strrchr(filename, '.'); strncpy(pluginpath, fullpath, CONTROL_MAXPATH_LEN - 1); strncat(pluginpath, "/", CONTROL_MAXPATH_LEN - strlen(pluginpath) - 1); strncat(pluginpath, filename, CONTROL_MAXPATH_LEN - strlen (pluginpath) - 1); if(!strcasecmp(ext, CTL_PLUGIN_EXT)) { + /* Make sure you don't load two found libraries */ if(ext && !strcasecmp(ext, CTL_PLUGIN_EXT) && i > 0) { AFB_ApiWarning(apiHandle, "Plugin multiple instances in searchpath will use %s/%s", fullpath, filename); return 0; -- cgit 1.2.3-korg