From c0d6f0e3010916d631acd7c5709ec92a7b642f49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=A2=E5=87=AF=E6=98=8E?= Date: Sat, 21 Jun 2025 11:51:01 +0800 Subject: [PATCH] =?UTF-8?q?init=E4=B8=8Econfig=5Fpolicy=E8=A7=A3=E8=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 谢凯明 --- bundle.json | 8 - interfaces/innerkits/BUILD.gn | 6 +- .../BUILD.gn | 12 +- .../config_policy_utils.c | 150 ++++++++++++++++++ .../config_policy_utils.h | 66 ++++++++ services/init/standard/BUILD.gn | 8 +- test/BUILD.gn | 1 + test/unittest/BUILD.gn | 17 +- .../config_policy_utils_unittest.cpp | 98 ++++++++++++ test/unittest/resource/ohos_test.xml | 30 ++++ 10 files changed, 378 insertions(+), 18 deletions(-) rename interfaces/innerkits/{zh_extral_dir => config_policy_static}/BUILD.gn (75%) create mode 100644 interfaces/innerkits/config_policy_static/config_policy_utils.c create mode 100644 interfaces/innerkits/config_policy_static/config_policy_utils.h create mode 100644 test/unittest/config_policy_static/config_policy_utils_unittest.cpp create mode 100644 test/unittest/resource/ohos_test.xml diff --git a/bundle.json b/bundle.json index 4ea02f8e9..b602d948a 100755 --- a/bundle.json +++ b/bundle.json @@ -173,14 +173,6 @@ }, "name": "//base/startup/init/interfaces/innerkits/seccomp:seccomp" }, - { - "header": { - "header_base": "//base/startup/init/interfaces/innerkits/include/param", - "header_files": [ - ] - }, - "name": "//base/startup/init/interfaces/innerkits/zh_extral_dir:export_headers_lib" - }, { "header": { "header_base": "//base/startup/init/services/param/base", diff --git a/interfaces/innerkits/BUILD.gn b/interfaces/innerkits/BUILD.gn index 05dc0de02..1f6d15c17 100755 --- a/interfaces/innerkits/BUILD.gn +++ b/interfaces/innerkits/BUILD.gn @@ -318,7 +318,10 @@ if (defined(ohos_lite)) { "//base/startup/init/services/param/linux:param_client", "//base/startup/init/services/utils:libinit_utils", ] - deps += [ "//base/startup/init/services/param/base:param_base" ] + deps += [ + "//base/startup/init/services/param/base:param_base", + "//base/startup/init/interfaces/innerkits/config_policy_static:config_policy_for_init_static", + ] if (init_begetutil_extra_modules != "") { deps += [ init_begetutil_extra_modules ] } @@ -326,7 +329,6 @@ if (defined(ohos_lite)) { external_deps = [ "bounds_checking_function:libsec_shared", "c_utils:utils", - "config_policy:configpolicy_util_for_init_static", "hilog:libhilog_base", ] public_configs = [ ":exported_header_files" ] diff --git a/interfaces/innerkits/zh_extral_dir/BUILD.gn b/interfaces/innerkits/config_policy_static/BUILD.gn similarity index 75% rename from interfaces/innerkits/zh_extral_dir/BUILD.gn rename to interfaces/innerkits/config_policy_static/BUILD.gn index 34d573261..4b95d592d 100644 --- a/interfaces/innerkits/zh_extral_dir/BUILD.gn +++ b/interfaces/innerkits/config_policy_static/BUILD.gn @@ -12,10 +12,16 @@ # limitations under the License. config("exported_header_files") { - visibility = [ ":*" ] - include_dirs = [ "../../innerkits/include/param" ] + include_dirs = [ + ".", + "../../innerkits/include/param" + ] } -static_library("export_headers_lib") { +static_library("config_policy_for_init_static") { + sources = [ "config_policy_utils.c" ] public_configs = [ ":exported_header_files" ] + external_deps = [ "bounds_checking_function:libsec_shared" ] } + + diff --git a/interfaces/innerkits/config_policy_static/config_policy_utils.c b/interfaces/innerkits/config_policy_static/config_policy_utils.c new file mode 100644 index 000000000..ac6d2e278 --- /dev/null +++ b/interfaces/innerkits/config_policy_static/config_policy_utils.c @@ -0,0 +1,150 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "config_policy_utils.h" + +#include +#include +#include +#include + +#ifndef __LITEOS__ +#include "init_param.h" +#endif + +#define CUST_KEY_POLICY_LAYER "const.cust.config_dir_layer" + +static void FreeIf(void *p) +{ + if (p != NULL) { + free(p); + } +} + +#ifndef __LITEOS__ +static char *CustGetSystemParam(const char *name) +{ + char *value = NULL; + unsigned int len = 0; + + if (SystemGetParameter(name, NULL, &len) != 0 || len <= 0 || len > PARAM_CONST_VALUE_LEN_MAX) { + return NULL; + } + value = (char *)calloc(len, sizeof(char)); + if (value != NULL && SystemGetParameter(name, value, &len) == 0 && value[0]) { + return value; + } + FreeIf(value); + return NULL; +} +#endif + +static void GetCfgDirRealPolicyValue(CfgDir *res) +{ + if (res == NULL) { + return; + } +#ifdef __LITEOS__ + // use default, now do nothing +#else + res->realPolicyValue = CustGetSystemParam(CUST_KEY_POLICY_LAYER); +#endif + if (res->realPolicyValue != NULL && res->realPolicyValue[0]) { + return; + } + res->realPolicyValue = strdup(DEFAULT_LAYER); +} + +void FreeCfgFiles(CfgFiles *res) +{ + if (res == NULL) { + return; + } + for (size_t i = 0; i < MAX_CFG_POLICY_DIRS_CNT; i++) { + if (res->paths[i] != NULL) { + free(res->paths[i]); + res->paths[i] = NULL; + } + } + free(res); +} + +void FreeCfgDirList(CfgDir *res) +{ + if (res == NULL) { + return; + } + if (res->realPolicyValue != NULL) { + free(res->realPolicyValue); + res->realPolicyValue = NULL; + } + free(res); +} + +CfgFiles *GetCfgFiles(const char *pathSuffix) +{ + if (pathSuffix == NULL) { + return NULL; + } + char buf[MAX_PATH_LEN] = {0}; + CfgDir *dirs = GetCfgDirList(); + if (dirs == NULL) { + return NULL; + } + CfgFiles *files = (CfgFiles *)(malloc(sizeof(CfgFiles))); + if (files == NULL) { + FreeCfgDirList(dirs); + return NULL; + } + + (void)memset_s(files, sizeof(CfgFiles), 0, sizeof(CfgFiles)); + int index = 0; + for (size_t i = 0; i < MAX_CFG_POLICY_DIRS_CNT && index < MAX_CFG_POLICY_DIRS_CNT; i++) { + if (dirs->paths[i] == NULL) { + continue; + } + if (snprintf_s(buf, MAX_PATH_LEN, MAX_PATH_LEN - 1, "%s/%s", dirs->paths[i], pathSuffix) > 0 && + access(buf, F_OK) == 0) { + files->paths[index++] = strdup(buf); + } + } + FreeCfgDirList(dirs); + return files; +} + +CfgDir *GetCfgDirList() +{ + CfgDir *res = (CfgDir *)(malloc(sizeof(CfgDir))); + if (res == NULL) { + return NULL; + } + (void)memset_s(res, sizeof(CfgDir), 0, sizeof(CfgDir)); + GetCfgDirRealPolicyValue(res); + char *next = res->realPolicyValue; + if (next == NULL) { + free(res); + return NULL; + } + for (size_t i = 0; i < MAX_CFG_POLICY_DIRS_CNT; i++) { + res->paths[i] = next; + next = strchr(next, ':'); + if (next == NULL) { + break; + } + *next = 0; + next += 1; + } + return res; +} diff --git a/interfaces/innerkits/config_policy_static/config_policy_utils.h b/interfaces/innerkits/config_policy_static/config_policy_utils.h new file mode 100644 index 000000000..20be9503c --- /dev/null +++ b/interfaces/innerkits/config_policy_static/config_policy_utils.h @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef CUSTOMIZATION_CONFIG_POLICY_UTILS_H +#define CUSTOMIZATION_CONFIG_POLICY_UTILS_H + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +#ifndef ROOT_PREFIX +#define ROOT_PREFIX "" +#endif +#define DEFAULT_LAYER ROOT_PREFIX "/system:" ROOT_PREFIX "/chipset:" ROOT_PREFIX "/sys_prod:" ROOT_PREFIX "/chip_prod" + +#define MAX_CFG_POLICY_DIRS_CNT 32 // max number of directories +#define MAX_PATH_LEN 256 // max length of a filepath + +// Config Files +struct CfgFiles { + char *paths[MAX_CFG_POLICY_DIRS_CNT]; +}; + +// Config Directories +struct CfgDir { + char *paths[MAX_CFG_POLICY_DIRS_CNT]; + char *realPolicyValue; +}; + +typedef struct CfgFiles CfgFiles; +typedef struct CfgDir CfgDir; + +// free struct CfgFiles allocated by GetCfgFiles() +void FreeCfgFiles(CfgFiles *res); + +// free struct CfgDir allocated by GetCfgDirList() +void FreeCfgDirList(CfgDir *res); + +// get config files, ordered by priority from low to high +// pathSuffixStr: the relative path of the config file, e.g. "etc/xml/config.xml" +// return: paths of config files +// CAUTION: please use FreeCfgFiles() to avoid memory leak. +CfgFiles *GetCfgFiles(const char *pathSuffix); + +// get config directories, ordered by priority from low to high +// return: paths of config directories +// CAUTION: please use FreeCfgDirList() to avoid memory leak. +CfgDir *GetCfgDirList(void); + +#ifdef __cplusplus +} +#endif // __cplusplus + +#endif // CUSTOMIZATION_CONFIG_POLICY_UTILS_H diff --git a/services/init/standard/BUILD.gn b/services/init/standard/BUILD.gn index 40ca30498..1b23f6f1f 100644 --- a/services/init/standard/BUILD.gn +++ b/services/init/standard/BUILD.gn @@ -118,11 +118,11 @@ ohos_executable("init") { "//base/startup/init/services/sandbox:sandbox", "//base/startup/init/services/utils:libinit_utils", ] - deps += [ "//base/startup/init/services/param/base:param_base" ] - external_deps = [ - "config_policy:configpolicy_util_for_init_static", - "storage_service:libfscryptutils_static", + deps += [ + "//base/startup/init/services/param/base:param_base", + "//base/startup/init/interfaces/innerkits/config_policy_static:config_policy_for_init_static", ] + external_deps = [ "storage_service:libfscryptutils_static" ] if (!defined(ohos_lite)) { include_dirs += [ diff --git a/test/BUILD.gn b/test/BUILD.gn index d292b6b81..0c80b990f 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -24,6 +24,7 @@ group("testgroup") { "unittest:init_dmverify_unittest", "unittest:init_fshvb_unittest", "unittest:init_unittest", + "unittest:initConfigPolicyUtilsTest" ] } else { if (ohos_kernel_type == "liteos_a" || ohos_kernel_type == "linux") { diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 511921163..9278c4887 100755 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -344,7 +344,6 @@ ohos_unittest("init_unittest") { "bounds_checking_function:libsec_static", "cJSON:cjson", "c_utils:utils", - "config_policy:configpolicy_util_for_init_static", "googletest:gmock", "googletest:gtest", "hilog:libhilog", @@ -355,6 +354,7 @@ ohos_unittest("init_unittest") { "storage_service:libfscryptutils_static", "zlib:libz", ] + deps += [ "//base/startup/init/interfaces/innerkits/config_policy_static:config_policy_for_init_static" ] if (defined(global_parts_info) && defined(global_parts_info.security_access_token)) { external_deps += [ @@ -548,3 +548,18 @@ ohos_unittest("init_fshvb_unittest") { } cflags_cc = [ "-fexceptions" ] } + +ohos_unittest("initConfigPolicyUtilsTest") { + module_out_path = "init/init" + resource_config_file = "./resource/ohos_test.xml" + sources = [ + "./config_policy_static/config_policy_utils_unittest.cpp", + ] + deps = [ "//base/startup/init/interfaces/innerkits/config_policy_static:config_policy_for_init_static" ] + external_deps = [ + "c_utils:utils", + "googletest:gtest", + "hilog:libhilog", + "init:libbegetutil" + ] +} \ No newline at end of file diff --git a/test/unittest/config_policy_static/config_policy_utils_unittest.cpp b/test/unittest/config_policy_static/config_policy_utils_unittest.cpp new file mode 100644 index 000000000..9badeeb8d --- /dev/null +++ b/test/unittest/config_policy_static/config_policy_utils_unittest.cpp @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include + +#include "config_policy_utils.h" + +using namespace std; +using namespace testing::ext; + +namespace init_ut { +class ConfigPolicyUtilsUnitTest : public testing::Test { +public: + static void SetUpTestCase(void) {}; + static void TearDownTestCase(void) {}; + void SetUp(void) {}; + void TearDown(void) {}; +}; + +HWTEST_F(ConfigPolicyUtilsUnitTest, GetCfgDirListFuncTest_001, TestSize.Level1) +{ + CfgDir *cfgDir = GetCfgDirList(); + EXPECT_TRUE(cfgDir != nullptr); + bool flag = false; + for (size_t i = 0; i < MAX_CFG_POLICY_DIRS_CNT; i++) { + char *filePath = cfgDir->paths[i]; + if (filePath && *filePath != '\0') { + flag = true; + } + } + FreeCfgDirList(cfgDir); + FreeCfgDirList(nullptr); + EXPECT_TRUE(flag); +} + +HWTEST_F(ConfigPolicyUtilsUnitTest, GetCfgFilesFuncTest_001, TestSize.Level1) +{ + const char *testPathSuffix = "etc/custxmltest/none.xml"; + CfgFiles *cfgFiles = GetCfgFiles(testPathSuffix); + EXPECT_TRUE(cfgFiles != nullptr); + int pathCount = 0; + char *filePath = nullptr; + for (size_t i = 0; i < MAX_CFG_POLICY_DIRS_CNT; i++) { + filePath = cfgFiles->paths[i]; + if (filePath && *filePath != '\0') { + pathCount++; + } + } + FreeCfgFiles(cfgFiles); + FreeCfgFiles(nullptr); + EXPECT_EQ(pathCount, 0); +} + +HWTEST_F(ConfigPolicyUtilsUnitTest, GetCfgFilesFuncTest_002, TestSize.Level1) +{ + const char *testPathSuffix = "etc/custxmltest/system.xml"; + CfgFiles *cfgFiles = GetCfgFiles(testPathSuffix); + EXPECT_TRUE(cfgFiles != nullptr); + int pathCount = 0; + char *filePath = nullptr; + for (size_t i = 0; i < MAX_CFG_POLICY_DIRS_CNT; i++) { + filePath = cfgFiles->paths[i]; + if (filePath && *filePath != '\0') { + pathCount++; + } + } + FreeCfgFiles(cfgFiles); + EXPECT_EQ(pathCount, 1); +} + +HWTEST_F(ConfigPolicyUtilsUnitTest, GetCfgFilesFuncTest_003, TestSize.Level1) +{ + const char *testPathSuffix = "etc/custxmltest/both.xml"; + CfgFiles *cfgFiles = GetCfgFiles(testPathSuffix); + EXPECT_TRUE(cfgFiles != nullptr); + int pathCount = 0; + char *filePath = nullptr; + for (size_t i = 0; i < MAX_CFG_POLICY_DIRS_CNT; i++) { + filePath = cfgFiles->paths[i]; + if (filePath && *filePath != '\0') { + pathCount++; + } + } + FreeCfgFiles(cfgFiles); + EXPECT_EQ(pathCount, 2); +} +} // namespace init_ut diff --git a/test/unittest/resource/ohos_test.xml b/test/unittest/resource/ohos_test.xml new file mode 100644 index 000000000..6fb54ab8f --- /dev/null +++ b/test/unittest/resource/ohos_test.xml @@ -0,0 +1,30 @@ + + + + + + + + + + -- Gitee