From b87c909421c7d5dc84301a3f812ab23eb25f3991 Mon Sep 17 00:00:00 2001 From: wangyikai Date: Wed, 28 May 2025 18:00:17 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90init=E3=80=91import=20.cfg=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E6=94=AF=E6=8C=81=E7=9B=B8=E5=AF=B9=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangyikai --- services/init/init_config.c | 44 ++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/services/init/init_config.c b/services/init/init_config.c index f5a1cc5f6..68464802f 100644 --- a/services/init/init_config.c +++ b/services/init/init_config.c @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include #include "init.h" #include "init_jobs_internal.h" #include "init_log.h" @@ -19,8 +20,17 @@ #include "init_utils.h" #include "init_param.h" #include "init_group_manager.h" +#include "securec.h" -static void ParseAllImports(const cJSON *root); +static void ParseAllImports(const cJSON *root, const char *baseDir); + +static bool IsReleative(const char *path) +{ + if (path == NULL) { + return false; + } + return path[0] == '/' ? false : true; +} InitContextType GetConfigContextType(const char *cfgName) { @@ -45,8 +55,14 @@ static void ParseInitCfgContents(const char *cfgName, const cJSON *root) ParseAllServices(root, &context); // parse jobs ParseAllJobs(root, &context); + + char *dupCfgName = strdup(cfgName); + INIT_ERROR_CHECK(dupCfgName != NULL, return, "strdup cfgName failed, cfgName: %s", cfgName); + char *baseDir = dirname(dupCfgName); + INIT_ERROR_CHECK(baseDir != NULL, return, "dirname failed, cfgName: %s", cfgName); // parse imports - ParseAllImports(root); + ParseAllImports(root, baseDir); + free(baseDir); } int ParseInitCfg(const char *configFile, void *context) @@ -66,7 +82,7 @@ int ParseInitCfg(const char *configFile, void *context) return 0; } -static void ParseAllImports(const cJSON *root) +static void ParseAllImports(const cJSON *root, const char *baseDir) { char *tmpParamValue = calloc(PARAM_VALUE_LEN_MAX + 1, sizeof(char)); INIT_ERROR_CHECK(tmpParamValue != NULL, return, "Failed to alloc memory for param"); @@ -88,13 +104,35 @@ static void ParseAllImports(const cJSON *root) INIT_LOGE("cannot get import config file"); break; } + + size_t len = strlen(baseDir) + strlen(importContent) + 2; // separator '/' and '\0' + char *importPath = calloc(len, sizeof(char)); + INIT_ERROR_CHECK(importPath != NULL, return, "calloc failed, importContent: %s", importContent); + + errno_t err = EOK; + if (IsReleative(importContent)) { + err |= strcpy_s(importPath, len, baseDir); + err |= strcat_s(importPath, len, "/"); + err |= strcat_s(importPath, len, importContent); + } else { + err |= strcpy_s(importPath, len, importContent); + } + importContent = importPath; + if (err != EOK) { + INIT_LOGE("concatenate aboluate path failed."); + free(importContent); + continue; + } + int ret = GetParamValue(importContent, strlen(importContent), tmpParamValue, PARAM_VALUE_LEN_MAX); if (ret != 0) { INIT_LOGE("cannot get value for %s", importContent); + free(importContent); continue; } INIT_LOGI("Import %s ...", tmpParamValue); ParseInitCfg(tmpParamValue, NULL); + free(importContent); } free(tmpParamValue); return; -- Gitee