From 5f85184d3f48ef69381262549e1a3cc9ac963219 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BD=A6=E6=B0=B8=E7=A5=A5?= Date: Thu, 18 Apr 2024 18:28:28 +0800 Subject: [PATCH] Add support for silk enable Signed-off-by: cheyongxiang --- modules/common/BUILD.gn | 7 +- modules/common/appspawn_common.c | 16 ++ modules/common/appspawn_silk.c | 137 ++++++++++++++++++ modules/common/appspawn_silk.h | 22 +++ .../unittest/app_spawn_standard_test/BUILD.gn | 1 + 5 files changed, 182 insertions(+), 1 deletion(-) create mode 100644 modules/common/appspawn_silk.c create mode 100644 modules/common/appspawn_silk.h diff --git a/modules/common/BUILD.gn b/modules/common/BUILD.gn index 22ab1726..a2d43521 100644 --- a/modules/common/BUILD.gn +++ b/modules/common/BUILD.gn @@ -20,6 +20,7 @@ ohos_shared_library("appspawn_common") { "appspawn_cgroup.c", "appspawn_common.c", "appspawn_namespace.c", + "appspawn_silk.c", ] include_dirs = [ ".", @@ -27,13 +28,17 @@ ohos_shared_library("appspawn_common") { "${appspawn_path}/standard", ] cflags = [] - deps = [ "${appspawn_path}/modules/module_engine:libappspawn_module_engine" ] + deps = [ + "${appspawn_path}/modules/module_engine:libappspawn_module_engine", + "${appspawn_path}/util:libappspawn_util", + ] defines = [ "GRAPHIC_PERMISSION_CHECK" ] external_deps = [ "access_token:libtoken_setproc", "access_token:libtokenid_sdk", "cJSON:cjson", "c_utils:utils", + "config_policy:configpolicy_util", "hilog:libhilog", "init:libbegetutil", "netmanager_base:netsys_client", diff --git a/modules/common/appspawn_common.c b/modules/common/appspawn_common.c index 28409e85..428b5e14 100644 --- a/modules/common/appspawn_common.c +++ b/modules/common/appspawn_common.c @@ -41,6 +41,7 @@ #include "appspawn_hook.h" #include "appspawn_msg.h" #include "appspawn_manager.h" +#include "appspawn_silk.h" #include "init_param.h" #include "parameter.h" #include "securec.h" @@ -440,9 +441,17 @@ static int SpawnEnableCache(AppSpawnMgr *content, AppSpawningCtx *property) return ret; } +static void SpawnLoadSilk(const AppSpawnMgr *content, const AppSpawningCtx *property) +{ + const char *processName = GetBundleName(property); + APPSPAWN_CHECK(processName != NULL, return, "Can not get bundle name"); + LoadSilkLibrary(processName); +} + static int SpawnSetProperties(AppSpawnMgr *content, AppSpawningCtx *property) { APPSPAWN_LOGV("Spawning: set child property"); + SpawnLoadSilk(content, property); (void)umask(DEFAULT_UMASK); int ret = SetKeepCapabilities(content, property); APPSPAWN_CHECK_ONLY_EXPER(ret == 0, return ret); @@ -512,10 +521,17 @@ static int SpawnGetSpawningFlag(AppSpawnMgr *content, AppSpawningCtx *property) return 0; } +static int SpawnLoadConfig(AppSpawnMgr *content) +{ + LoadSilkConfig(); + return 0; +} + MODULE_CONSTRUCTOR(void) { APPSPAWN_LOGV("Load common module ..."); AddPreloadHook(HOOK_PRIO_COMMON, PreLoadSetSeccompFilter); + AddPreloadHook(HOOK_PRIO_COMMON, SpawnLoadConfig); AddAppSpawnHook(STAGE_PARENT_PRE_FORK, HOOK_PRIO_HIGHEST, SpawnGetSpawningFlag); AddAppSpawnHook(STAGE_CHILD_PRE_COLDBOOT, HOOK_PRIO_HIGHEST, SpawnInitSpawningEnv); diff --git a/modules/common/appspawn_silk.c b/modules/common/appspawn_silk.c new file mode 100644 index 00000000..c3f7de59 --- /dev/null +++ b/modules/common/appspawn_silk.c @@ -0,0 +1,137 @@ +/* + * Copyright (c) 2024 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 +#include + +#include "appspawn_server.h" +#include "appspawn_silk.h" +#include "appspawn_utils.h" +#include "config_policy_utils.h" +#include "cJSON.h" +#include "json_utils.h" +#include "securec.h" + +#define SILK_JSON_CONFIG_PATH "/vendor/etc/silk/silk.json" +#define SILK_JSON_ENABLE_ITEM "enabled_app_list" +#define SILK_JSON_LIBRARY_PATH "/vendor/lib64/chipsetsdk/libsilk.so.0.1" + +#define SILK_JSON_MAX 128 +#define SILK_JSON_NAME_MAX 256 + +struct SilkConfig { + char **configItems; + int configCursor; +}; + +static struct SilkConfig g_silkConfig = {0}; + +static void ParseSilkConfig(const cJSON *root, struct SilkConfig *config) +{ + cJSON *silkJson = cJSON_GetObjectItemCaseSensitive(root, SILK_JSON_ENABLE_ITEM); + if (silkJson == NULL) { + return; + } + + uint32_t configCount = cJSON_GetArraySize(silkJson); + APPSPAWN_CHECK(configCount <= SILK_JSON_MAX, configCount = SILK_JSON_MAX, + "config count %{public}u is larger than %{public}d", configCount, SILK_JSON_MAX); + config->configItems = (char **)malloc(configCount * sizeof(char *)); + APPSPAWN_CHECK(config->configItems != NULL, return, "Alloc for silk config items failed"); + int ret = memset_s(config->configItems, configCount * sizeof(char *), 0, configCount * sizeof(char *)); + APPSPAWN_CHECK(ret == 0, free(config->configItems); + config->configItems = NULL; return, + "Memset silk config items failed"); + + for (uint32_t i = 0; i < configCount; ++i) { + const char *appName = cJSON_GetStringValue(cJSON_GetArrayItem(silkJson, i)); + APPSPAWN_CHECK(appName != NULL, break, "appName is NULL"); + APPSPAWN_LOGI("Enable silk appName %{public}s", appName); + + int len = strlen(appName) + 1; + APPSPAWN_CHECK(len <= SILK_JSON_NAME_MAX, continue, + "appName %{public}s is larger than the maximum limit", appName); + char **item = &config->configItems[config->configCursor]; + *item = (char *)malloc(len * sizeof(char)); + APPSPAWN_CHECK(*item != NULL, break, "Alloc for config item failed"); + + ret = memset_s(*item, len * sizeof(char), 0, len * sizeof(char)); + APPSPAWN_CHECK(ret == 0, free(*item); + *item = NULL; break, + "Memset config item %{public}s failed", appName); + + ret = strncpy_s(*item, len, appName, len - 1); + APPSPAWN_CHECK(ret == 0, free(*item); + *item = NULL; break, + "Copy config item %{public}s failed", appName); + config->configCursor++; + } +} + +void LoadSilkConfig(void) +{ + cJSON *root = GetJsonObjFromFile(SILK_JSON_CONFIG_PATH); + APPSPAWN_CHECK(root != NULL, return, "Failed to load silk config"); + ParseSilkConfig(root, &g_silkConfig); + cJSON_Delete(root); +} + +static void FreeSilkConfigItems(void) +{ + for (int i = 0; i < g_silkConfig.configCursor; i++) { + if (g_silkConfig.configItems[i] != NULL) { + free(g_silkConfig.configItems[i]); + g_silkConfig.configItems[i] = NULL; + } + } +} + +static void FreeSilkConfig(void) +{ + free(g_silkConfig.configItems); + g_silkConfig.configItems = NULL; + g_silkConfig.configCursor = 0; +} + +static void FreeAllSilkConfig(void) +{ + FreeSilkConfigItems(); + FreeSilkConfig(); +} + +void LoadSilkLibrary(const char *packageName) +{ + APPSPAWN_CHECK(g_silkConfig.configItems != NULL, return, + "Load silk library failed for configItems is NULL"); + APPSPAWN_CHECK(packageName != NULL, FreeAllSilkConfig(); return, + "Load silk library failed for packageName is NULL"); + char **appName = NULL; + void *handle = NULL; + for (int i = 0; i < g_silkConfig.configCursor; i++) { + appName = &g_silkConfig.configItems[i]; + if (*appName == NULL) { + break; + } + if (handle == NULL && strcmp(*appName, packageName) == 0) { + handle = dlopen(SILK_JSON_LIBRARY_PATH, RTLD_NOW); + APPSPAWN_LOGI("Enable Silk AppName %{public}s result:%{public}s", + *appName, handle ? "success" : "failed"); + } + free(*appName); + *appName = NULL; + } + FreeSilkConfig(); +} \ No newline at end of file diff --git a/modules/common/appspawn_silk.h b/modules/common/appspawn_silk.h new file mode 100644 index 00000000..e95a6bf8 --- /dev/null +++ b/modules/common/appspawn_silk.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2024 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 APPSPAWN_SILK_CPP +#define APPSPAWN_SILK_CPP + +void LoadSilkConfig(void); +void LoadSilkLibrary(const char *packageName); + +#endif \ No newline at end of file diff --git a/test/unittest/app_spawn_standard_test/BUILD.gn b/test/unittest/app_spawn_standard_test/BUILD.gn index 6848f8ad..cdd443d3 100644 --- a/test/unittest/app_spawn_standard_test/BUILD.gn +++ b/test/unittest/app_spawn_standard_test/BUILD.gn @@ -89,6 +89,7 @@ ohos_unittest("AppSpawn_ut") { "${appspawn_path}/modules/common/appspawn_cgroup.c", "${appspawn_path}/modules/common/appspawn_common.c", "${appspawn_path}/modules/common/appspawn_namespace.c", + "${appspawn_path}/modules/common/appspawn_silk.c", "${appspawn_path}/modules/nweb_adapter/nwebspawn_adapter.cpp", "${appspawn_path}/modules/sandbox/appspawn_mount_template.c", "${appspawn_path}/modules/sandbox/appspawn_permission.c",