From c79b9d69997b8e18410c28d8bd5af4c40acc2b56 Mon Sep 17 00:00:00 2001 From: wangfeng Date: Thu, 5 Dec 2024 09:41:26 +0800 Subject: [PATCH 1/4] Adjust the mounting timing of the storage/Users directory Signed-off-by: wangfeng --- appdata-sandbox-app.json | 22 ++++++++++++++++---- appdata-sandbox.json | 33 +++++++++++++++++++++++++++++- modules/sandbox/appspawn_sandbox.c | 10 +++++++-- modules/sandbox/sandbox_utils.cpp | 14 ++++++++++--- 4 files changed, 69 insertions(+), 10 deletions(-) diff --git a/appdata-sandbox-app.json b/appdata-sandbox-app.json index b75c1579..a41ba2a4 100755 --- a/appdata-sandbox-app.json +++ b/appdata-sandbox-app.json @@ -178,7 +178,10 @@ "name": "ohos.permission.FILE_ACCESS_MANAGER", "sandbox-switch": "ON", "gids": ["file_manager", "user_data_rw"], - "mount-paths": [], + "mount-paths": [{ + "src-path": "/mnt/user//nosharefs/docs", + "sandbox-path": "/storage/Users" + }], "mount-groups": [ "user-public", "file_manager_recent" ] }, { "name": "ohos.permission.READ_IMAGEVIDEO", @@ -192,11 +195,11 @@ "name": "ohos.permission.FILE_CROSS_APP", "gids": ["file_manager"], "mount-paths": [], - "mount-groups": [ "user-public" ] + "mount-groups": [ "user-public", "storage_users" ] }, { "name": "ohos.permission.FILE_ACCESS_COMMON_DIR", "mount-paths": [], - "mount-groups": [ "user-public" ] + "mount-groups": [ "user-public", "storage_users" ] }, { "name": "ohos.permission.ACTIVATE_THEME_PACKAGE", "gids": ["wallpaper"], @@ -451,13 +454,24 @@ "src-path": "/storage/media//local/files/Docs", "sandbox-path": "/currentUser" }] - }, { + }, { "name": "file_manager_recent", "type": "app-variable", "mount-paths": [{ "src-path": "/storage/media//local/files/.Recent", "sandbox-path": "/storage/Users//.Recent" }] + }, { + "name": "storage_users", + "type": "app-variable", + "mount-paths": [{ + "src-path": "/mnt/user//sharefs/docs", + "sandbox-path": "/mnt/storage/Users" + }, { + "src-path": "/mnt/sandbox//app-root/mnt/storage/Users", + "sandbox-path": "/storage/Users", + "category": "dac_override_delete" + }] }, { "name": "el2", "type": "app-variable", diff --git a/appdata-sandbox.json b/appdata-sandbox.json index f428321e..5b809e76 100755 --- a/appdata-sandbox.json +++ b/appdata-sandbox.json @@ -790,6 +790,11 @@ "sandbox-path": "/storage/Users//.Recent", "sandbox-flags": [ "bind", "rec" ] }, + { + "src-path": "/mnt/user//nosharefs/docs", + "sandbox-path": "/storage/Users", + "sandbox-flags": [ "bind", "rec" ] + }, { "src-path": "/mnt/data//hmdfs", "sandbox-path": "/storage/hmdfs", @@ -813,12 +818,38 @@ "src-path": "/mnt/data/external", "sandbox-path": "/storage/External", "sandbox-flags": [ "bind", "rec" ] + }, + { + "src-path": "/mnt/user//sharefs/docs", + "sandbox-path": "/mnt/storage/Users", + "sandbox-flags": [ "bind", "rec" ] + }, + { + "src-path": "/mnt/sandbox///mnt/storage/Users", + "sandbox-path": "/storage/Users", + "sandbox-flags-customized": [ "MS_NODEV"], + "dac-override-sensitive": "true", + "fs-type": "sharefs", + "options": "override" } ] }], "ohos.permission.FILE_ACCESS_COMMON_DIR":[{ "sandbox-switch": "ON", - "mount-paths": [] + "mount-paths": [{ + "src-path": "/mnt/user//sharefs/docs", + "sandbox-path": "/mnt/storage/Users", + "sandbox-flags": [ "bind", "rec" ] + }, + { + "src-path": "/mnt/sandbox///mnt/storage/Users", + "sandbox-path": "/storage/Users", + "sandbox-flags-customized": [ "MS_NODEV"], + "dac-override-sensitive": "true", + "fs-type": "sharefs", + "options": "override" + } + ] }], "ohos.permission.ACTIVATE_THEME_PACKAGE":[{ "sandbox-switch": "ON", diff --git a/modules/sandbox/appspawn_sandbox.c b/modules/sandbox/appspawn_sandbox.c index ea236388..b9c960e9 100644 --- a/modules/sandbox/appspawn_sandbox.c +++ b/modules/sandbox/appspawn_sandbox.c @@ -659,6 +659,7 @@ static int SharedMountInSharefs(const AppSpawnMsgDacInfo *info, const char *root char currentUserPath[MAX_SANDBOX_BUFFER] = {0}; int ret = snprintf_s(currentUserPath, MAX_SANDBOX_BUFFER, MAX_SANDBOX_BUFFER - 1, "%s/currentUser", target); if (ret <= 0) { + APPSPAWN_LOGE("snprintf_s currentUserPath failed, errno %{public}d", errno); return APPSPAWN_ERROR_UTILS_MEM_FAIL; } @@ -675,6 +676,7 @@ static int SharedMountInSharefs(const AppSpawnMsgDacInfo *info, const char *root ret = snprintf_s(options, OPTIONS_MAX_LEN, OPTIONS_MAX_LEN - 1, "override_support_delete,user_id=%d", info->uid / UID_BASE); if (ret <= 0) { + APPSPAWN_LOGE("snprintf_s options failed, errno %{public}d", errno); return APPSPAWN_ERROR_UTILS_MEM_FAIL; } @@ -704,6 +706,7 @@ static void UpdateStorageDir(const SandboxContext *context, AppSpawnSandboxCfg * int ret = snprintf_s(nosharefsDocsDir, MAX_SANDBOX_BUFFER, MAX_SANDBOX_BUFFER - 1, "%s/%d/%s", mntUser, info->uid / UID_BASE, nosharefsDocs); if (ret <= 0) { + APPSPAWN_LOGE("snprintf_s nosharefsDocsDir failed, errno %{public}d", errno); return; } @@ -712,6 +715,7 @@ static void UpdateStorageDir(const SandboxContext *context, AppSpawnSandboxCfg * ret = snprintf_s(sharefsDocsDir, MAX_SANDBOX_BUFFER, MAX_SANDBOX_BUFFER - 1, "%s/%d/%s", mntUser, info->uid / UID_BASE, sharefsDocs); if (ret <= 0) { + APPSPAWN_LOGE("snprintf_s sharefsDocsDir failed, errno %{public}d", errno); return; } @@ -722,6 +726,7 @@ static void UpdateStorageDir(const SandboxContext *context, AppSpawnSandboxCfg * ret = snprintf_s(storageUserPath, MAX_SANDBOX_BUFFER, MAX_SANDBOX_BUFFER - 1, "%s/%d/app-root/%s", rootPath, info->uid / UID_BASE, userPath); if (ret <= 0) { + APPSPAWN_LOGE("snprintf_s storageUserPath failed, errno %{public}d", errno); return; } /* mount /mnt/user//sharefs/docs to /mnt/sandbox//app-root/storage/Users */ @@ -733,6 +738,7 @@ static void UpdateStorageDir(const SandboxContext *context, AppSpawnSandboxCfg * if (ret != 0) { APPSPAWN_LOGE("Update storage dir, ret %{public}d", ret); } + APPSPAWN_LOGI("Update %{public}s storage dir success", res == 0 ? "sharefs dir" : "no sharefs dir"); } static void MountDirToShared(const SandboxContext *context, AppSpawnSandboxCfg *sandbox) @@ -746,8 +752,6 @@ static void MountDirToShared(const SandboxContext *context, AppSpawnSandboxCfg * return; } - UpdateStorageDir(context, sandbox, info); - MountDir(info, appRootName, rootPath, nwebPath); MountDir(info, appRootName, rootPath, nwebTmpPath); @@ -755,6 +759,8 @@ static void MountDirToShared(const SandboxContext *context, AppSpawnSandboxCfg * return; } + UpdateStorageDir(context, sandbox, info); + int length = sizeof(MOUNT_SHARED_MAP) / sizeof(MOUNT_SHARED_MAP[0]); for (int i = 0; i < length; i++) { if (MOUNT_SHARED_MAP[i].permission == NULL) { diff --git a/modules/sandbox/sandbox_utils.cpp b/modules/sandbox/sandbox_utils.cpp index f2d755a7..3adb954f 100644 --- a/modules/sandbox/sandbox_utils.cpp +++ b/modules/sandbox/sandbox_utils.cpp @@ -274,7 +274,7 @@ int32_t SandboxUtils::DoAppSandboxMountOnce(const char *originPath, const char * struct timespec mountStart = {0}; clock_gettime(CLOCK_MONOTONIC_COARSE, &mountStart); APPSPAWN_LOGV("Bind mount %{public}s to %{public}s '%{public}s' '%{public}lu' '%{public}s' '%{public}u'", - originPath, destinationPath, fsType, mountFlags, options, mountSharedFlag); + originPath, destinationPath, fsType, mountFlags, options, mountSharedFlag); ret = mount(originPath, destinationPath, fsType, mountFlags, options); struct timespec mountEnd = {0}; clock_gettime(CLOCK_MONOTONIC_COARSE, &mountEnd); @@ -1954,12 +1954,14 @@ static int MountInShared(const AppSpawningCtx *property, const char *rootPath, c int ret = snprintf_s(path, PATH_MAX_LEN, PATH_MAX_LEN - 1, "%s/%u/%s/%s", rootPath, info->uid / UID_BASE, bundleName, target); if (ret <= 0) { + APPSPAWN_LOGE("snprintf_s path failed, errno %{public}d", errno); return APPSPAWN_ERROR_UTILS_MEM_FAIL; } char currentUserPath[PATH_MAX_LEN] = {0}; ret = snprintf_s(currentUserPath, PATH_MAX_LEN, PATH_MAX_LEN - 1, "%s/currentUser", path); if (ret <= 0) { + APPSPAWN_LOGE("snprintf_s currentUserPath failed, errno %{public}d", errno); return APPSPAWN_ERROR_UTILS_MEM_FAIL; } @@ -1995,6 +1997,7 @@ static int SharedMountInSharefs(const AppSpawningCtx *property, const char *root char currentUserPath[PATH_MAX_LEN] = {0}; int ret = snprintf_s(currentUserPath, PATH_MAX_LEN, PATH_MAX_LEN - 1, "%s/currentUser", target); if (ret <= 0) { + APPSPAWN_LOGE("snprintf_s currentUserPath failed, errno %{public}d", errno); return APPSPAWN_ERROR_UTILS_MEM_FAIL; } @@ -2011,6 +2014,7 @@ static int SharedMountInSharefs(const AppSpawningCtx *property, const char *root ret = snprintf_s(options, PATH_MAX_LEN, PATH_MAX_LEN - 1, "override_support_delete,user_id=%u", info->uid / UID_BASE); if (ret <= 0) { + APPSPAWN_LOGE("snprintf_s options failed, errno %{public}d", errno); return APPSPAWN_ERROR_UTILS_MEM_FAIL; } @@ -2045,6 +2049,7 @@ static void UpdateStorageDir(const AppSpawningCtx *property) int ret = snprintf_s(nosharefsDocsDir, PATH_MAX_LEN, PATH_MAX_LEN - 1, "%s/%u/%s", mntUser, info->uid / UID_BASE, nosharefsDocs); if (ret <= 0) { + APPSPAWN_LOGE("snprintf_s nosharefsDocsDir failed, errno %{public}d", errno); return; } @@ -2053,6 +2058,7 @@ static void UpdateStorageDir(const AppSpawningCtx *property) ret = snprintf_s(sharefsDocsDir, PATH_MAX_LEN, PATH_MAX_LEN - 1, "%s/%u/%s", mntUser, info->uid / UID_BASE, sharefsDocs); if (ret <= 0) { + APPSPAWN_LOGE("snprintf_s sharefsDocsDir failed, errno %{public}d", errno); return; } @@ -2064,6 +2070,7 @@ static void UpdateStorageDir(const AppSpawningCtx *property) ret = snprintf_s(storageUserPath, PATH_MAX_LEN, PATH_MAX_LEN - 1, "%s/%u/%s/%s", rootPath, info->uid / UID_BASE, bundleName, userPath); if (ret <= 0) { + APPSPAWN_LOGE("snprintf_s storageUserPath failed, errno %{public}d", errno); return; } /* mount /mnt/user//sharefs/docs to /mnt/sandbox///storage/Users */ @@ -2075,6 +2082,7 @@ static void UpdateStorageDir(const AppSpawningCtx *property) if (ret != 0) { APPSPAWN_LOGE("Update storage dir, ret %{public}d", ret); } + APPSPAWN_LOGI("Update %{public}s storage dir success", res == 0 ? "sharefs dir" : "no sharefs dir"); } static void MountDirToShared(const AppSpawningCtx *property) @@ -2088,8 +2096,6 @@ static void MountDirToShared(const AppSpawningCtx *property) return; } - UpdateStorageDir(property); - string sourcePath = "/data/app/el1/bundle/public/" + string(bundleName); MountDir(property, rootPath, sourcePath.c_str(), el1Path); size_t bundleNameLen = strlen(bundleName); @@ -2097,6 +2103,8 @@ static void MountDirToShared(const AppSpawningCtx *property) return; } + UpdateStorageDir(property); + int length = sizeof(MOUNT_SHARED_MAP) / sizeof(MOUNT_SHARED_MAP[0]); for (int i = 0; i < length; i++) { if (MOUNT_SHARED_MAP[i].permission == nullptr) { -- Gitee From dc09a9ba1d2030ebb9693918e335214f74faa8f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=BE=BE?= Date: Mon, 9 Dec 2024 15:35:06 +0800 Subject: [PATCH 2/4] =?UTF-8?q?appspawn=E8=A7=A3=E8=80=A6=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E7=A0=81=E5=85=BC=E5=AE=B9=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 王达 --- service/hnp/BUILD.gn | 3 ++- test/unittest/hnp_test/BUILD.gn | 5 ++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/service/hnp/BUILD.gn b/service/hnp/BUILD.gn index 89e48e85..d2f675ea 100644 --- a/service/hnp/BUILD.gn +++ b/service/hnp/BUILD.gn @@ -44,7 +44,8 @@ if (!defined(ohos_lite)) { defines = [] if (defined(global_parts_info) && - defined(global_parts_info.security_code_signature)) { + defined(global_parts_info.security_code_signature) && + appspawn_support_code_signature) { defines += [ "CODE_SIGNATURE_ENABLE" ] } install_enable = true diff --git a/test/unittest/hnp_test/BUILD.gn b/test/unittest/hnp_test/BUILD.gn index af235c35..7855c298 100644 --- a/test/unittest/hnp_test/BUILD.gn +++ b/test/unittest/hnp_test/BUILD.gn @@ -56,11 +56,14 @@ if (!defined(ohos_lite)) { external_deps = [ "bounds_checking_function:libsec_shared", "cJSON:cjson", - "code_signature:libcode_sign_utils", "hilog:libhilog", "init:libbegetutil", "selinux_adapter:librestorecon", "zlib:shared_libz", ] + + if (appspawn_support_code_signature) { + external_deps += [ "code_signature:libcode_sign_utils" ] + } } } -- Gitee From 09968922660ccab0b4e2dcf58b6e84d54a6ed6d5 Mon Sep 17 00:00:00 2001 From: whoselittlelion Date: Mon, 9 Dec 2024 20:39:49 +0800 Subject: [PATCH 3/4] add hisysevent Signed-off-by: whoselittlelion --- bundle.json | 1 + hisysevent.yaml | 27 +++ modules/sandbox/BUILD.gn | 6 + modules/sandbox/sandbox_utils.cpp | 3 + modules/sysevent/hisysevent_adapter.cpp | 156 ++++++++++++++++++ modules/sysevent/hisysevent_adapter.h | 102 ++++++++++++ standard/BUILD.gn | 15 +- standard/appspawn_appmgr.c | 6 + standard/appspawn_manager.h | 6 + standard/appspawn_service.c | 32 +++- .../unittest/app_spawn_standard_test/BUILD.gn | 1 + 11 files changed, 350 insertions(+), 5 deletions(-) create mode 100644 hisysevent.yaml create mode 100644 modules/sysevent/hisysevent_adapter.cpp create mode 100644 modules/sysevent/hisysevent_adapter.h diff --git a/bundle.json b/bundle.json index a0608e6a..878786fa 100644 --- a/bundle.json +++ b/bundle.json @@ -31,6 +31,7 @@ "rom": "296KB", "ram": "13125KB", "hisysevent_config": [ + "//base/startup/appspawn/hisysevent.yaml", "//base/startup/appspawn/startup_events.yaml" ], "deps": { diff --git a/hisysevent.yaml b/hisysevent.yaml new file mode 100644 index 00000000..38bcc464 --- /dev/null +++ b/hisysevent.yaml @@ -0,0 +1,27 @@ +# 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. + +domain: APPSPAWN + +SPAWN_CHILD_PROCESS_FAIL: + __BASE: {type: FAULT, level: CRITICAL, desc: Spawn Child Process Fail} + PROCESS_NAME: {type: STRING, desc: Process Name} + ERROR_CODE: {type: INT32, desc: Error Code} + +SPAWN_PROCESS_DURATION: + __BASE: {type: STATISTIC, level: CRITICAL, desc: Boot Process Duration} + MAXDURATION: {type: INT64, desc: Max Spawn Process Duration} + MINDURATION: {type: INT64, desc: Min Spawn Process Duration} + TOTALDURATION: {type: INT64, desc: Total Spawn Process Duration} + EVENTCOUNT: {type: INT64, desc: Total Spawn Process Count} + STAGE: {type: STRING, desc: Boot Stage Or BootFinished Stage} \ No newline at end of file diff --git a/modules/sandbox/BUILD.gn b/modules/sandbox/BUILD.gn index ad320a82..279ffdb0 100644 --- a/modules/sandbox/BUILD.gn +++ b/modules/sandbox/BUILD.gn @@ -114,6 +114,12 @@ if (defined(appspawn_sandbox_new) && appspawn_sandbox_new) { external_deps += [ "selinux_adapter:librestorecon" ] } } + if (appspawn_report_event) { + defines += [ "APPSPAWN_HISYSEVENT" ] + external_deps += [ "hisysevent:libhisysevent" ] + include_dirs += [ "${appspawn_path}/modules/sysevent" ] + sources += [ "${appspawn_path}/modules/sysevent/hisysevent_adapter.cpp" ] + } subsystem_name = "${subsystem_name}" part_name = "${part_name}" diff --git a/modules/sandbox/sandbox_utils.cpp b/modules/sandbox/sandbox_utils.cpp index 3adb954f..e7d1a35a 100644 --- a/modules/sandbox/sandbox_utils.cpp +++ b/modules/sandbox/sandbox_utils.cpp @@ -43,6 +43,9 @@ #include "parameter.h" #include "parameters.h" #include "securec.h" +#ifdef APPSPAWN_HISYSEVENT +#include "hisysevent_adapter.h" +#endif #ifdef WITH_SELINUX #include "hap_restorecon.h" diff --git a/modules/sysevent/hisysevent_adapter.cpp b/modules/sysevent/hisysevent_adapter.cpp new file mode 100644 index 00000000..323ec1ae --- /dev/null +++ b/modules/sysevent/hisysevent_adapter.cpp @@ -0,0 +1,156 @@ +/* + * 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 "hisysevent_adapter.h" +#include "securec.h" +#include "hisysevent.h" +#include "appspawn_utils.h" + +using namespace OHOS::HiviewDFX; +namespace { +// event +constexpr const char* SPAWN_PROCESS_DURATION = "SPAWN_PROCESS_DURATION"; +constexpr const char* SPAWN_CHILD_PROCESS_FAIL = "SPAWN_CHILD_PROCESS_FAIL"; + +// param +constexpr const char* PROCESS_NAME = "PROCESS_NAME"; +constexpr const char* ERROR_CODE = "ERROR_CODE"; +constexpr const char* MAXDURATION = "MAXDURATION"; +constexpr const char* MINDURATION = "MINDURATION"; +constexpr const char* TOTALDURATION = "TOTALDURATION"; +constexpr const char* EVENTCOUNT = "EVENTCOUNT"; +constexpr const char* STAGE = "STAGE"; +constexpr const char* BOOTSTAGE = "BOOTSTAGE"; +constexpr const char* BOOTFINISHEDSTAGE = "BOOTFINISHEDSTAGE"; +} + +static void AddStatisticEvent(AppSpawnHisysevent *event, uint32_t duration) +{ + event->eventCount++; + event->totalDuration += duration; + if (duration > event->maxDuration) { + event->maxDuration = duration; + } + if (duration < event->minDuration) { + event->minDuration = duration; + } + APPSPAWN_LOGV("event->maxDuration is: %{public}d, event->minDuration is: %{public}d", + event->maxDuration, event->minDuration); +} + +void AddStatisticEventInfo(AppSpawnHisyseventInfo *hisyseventInfo, uint32_t duration, bool stage) +{ + APPSPAWN_CHECK(hisyseventInfo != NULL, return, "fail to get AppSpawnHisyseventInfo"); + if (stage) { + AddStatisticEvent(&hisyseventInfo->bootEvent, duration); + } else { + AddStatisticEvent(&hisyseventInfo->manualEvent, duration); + } +} + +static void InitStatisticEvent(AppSpawnHisysevent *event) +{ + event->eventCount = 0; + event->maxDuration = 0; + event->minDuration = UINT32_MAX; + event->totalDuration = 0; +} + +static void InitStatisticEventInfo(AppSpawnHisyseventInfo *appSpawnHisysInfo) +{ + InitStatisticEvent(&appSpawnHisysInfo->bootEvent); + InitStatisticEvent(&appSpawnHisysInfo->manualEvent); +} + +static int CreateHisysTimerLoop(AppSpawnHisyseventInfo *hisyseventInfo) +{ + LoopHandle loop = LE_GetDefaultLoop(); + TimerHandle timer = NULL; + int ret = LE_CreateTimer(loop, &timer, ReportSpawnStatisticDuration, (void *)hisyseventInfo); + APPSPAWN_CHECK(ret == 0, return -1, "fail to create HisysTimer, ret is: %{public}d", ret); + // start a timer to report event every 24h + ret = LE_StartTimer(loop, timer, APPSPAWN_HISYSEVENT_REPORT_TIME, INT64_MAX); + APPSPAWN_CHECK(ret == 0, return -1, "fail to start HisysTimer, ret is: %{public}d", ret); + return ret; +} + +AppSpawnHisyseventInfo *GetAppSpawnHisyseventInfo() +{ + AppSpawnHisyseventInfo *hisyseventInfo = + static_cast(malloc(sizeof(AppSpawnHisyseventInfo))); + APPSPAWN_CHECK(hisyseventInfo != NULL, return NULL, "fail to alloc memory for hisyseventInfo"); + int ret = memset_s(hisyseventInfo, sizeof(AppSpawnHisyseventInfo), 0, sizeof(AppSpawnHisyseventInfo)); + if (ret != 0) { + free(hisyseventInfo); + hisyseventInfo = NULL; + APPSPAWN_LOGE("Failed to memset hisyseventInfo"); + return NULL; + } + InitStatisticEventInfo(hisyseventInfo); + return hisyseventInfo; +} + +void DeleteHisyseventInfo(AppSpawnHisyseventInfo *hisyseventInfo) +{ + APPSPAWN_CHECK_ONLY_EXPER(hisyseventInfo != NULL, return); + free(hisyseventInfo); + hisyseventInfo = NULL; +} + +AppSpawnHisyseventInfo *InitHisyseventTimer() +{ + AppSpawnHisyseventInfo *hisyseventInfo = GetAppSpawnHisyseventInfo(); + APPSPAWN_CHECK(hisyseventInfo != NULL, return NULL, "fail to init hisyseventInfo"); + int ret = CreateHisysTimerLoop(hisyseventInfo); + if (ret != 0) { + DeleteHisyseventInfo(hisyseventInfo); + hisyseventInfo = NULL; + APPSPAWN_LOGE("fail to create hisys timer loop, ret: %{public}d", ret); + } + return hisyseventInfo; +} + +void ReportSpawnChildProcessFail(const char* processName, int32_t errorCode) +{ + int ret = HiSysEventWrite(HiSysEvent::Domain::APPSPAWN, SPAWN_CHILD_PROCESS_FAIL, + HiSysEvent::EventType::FAULT, + PROCESS_NAME, processName, + ERROR_CODE, errorCode); + if (ret != 0) { + APPSPAWN_LOGE("HiSysEventWrite error, ret: %{public}d", ret); + } +} + +void ReportSpawnProcessDuration(AppSpawnHisysevent *hisysevent, const char* stage) +{ + int ret = HiSysEventWrite(HiSysEvent::Domain::APPSPAWN, SPAWN_PROCESS_DURATION, + HiSysEvent::EventType::STATISTIC, + MAXDURATION, hisysevent->maxDuration, + MINDURATION, hisysevent->minDuration, + TOTALDURATION, hisysevent->totalDuration, + EVENTCOUNT, hisysevent->eventCount, + STAGE, stage); + if (ret != 0) { + APPSPAWN_LOGE("HiSysEventWrite error, ret: %{public}d", ret); + } +} + +void ReportSpawnStatisticDuration(const TimerHandle taskHandle, void *content) +{ + AppSpawnHisyseventInfo *hisyseventInfo = static_cast(content); + ReportSpawnProcessDuration(&hisyseventInfo->bootEvent, BOOTSTAGE); + ReportSpawnProcessDuration(&hisyseventInfo->manualEvent, BOOTFINISHEDSTAGE); + + InitStatisticEventInfo(hisyseventInfo); +} \ No newline at end of file diff --git a/modules/sysevent/hisysevent_adapter.h b/modules/sysevent/hisysevent_adapter.h new file mode 100644 index 00000000..649842a3 --- /dev/null +++ b/modules/sysevent/hisysevent_adapter.h @@ -0,0 +1,102 @@ +/* + * 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_HISYSEVENT_ADAPTER_H +#define APPSPAWN_HISYSEVENT_ADAPTER_H + +#include "appspawn_utils.h" +#include "loop_event.h" + +#ifdef __cplusplus +extern "C" { +#endif + + +// 错误码定义规范:子系统ID(28-21位)| 模块ID(20-16位)| 具体错误ID(15-0位) +#define APPSPAWN_HISYSEVENT_REPORT_TIME (24 * 60 * 60 * 1000) // 24h +#define SUBSYS_STARTUP_ID 39 +#define SUBSYSTEM_BIT_NUM 21 +#define MODULE_BIT_NUM 16 +#define ERR_MODULE_APPSPAWN 0x00 +#define ERR_MODULE_INIT 0x10 +#define ERR_APPSPAWN_BASE ((SUBSYS_STARTUP_ID << SUBSYSTEM_BIT_NUM) | (ERR_MODULE_APPSPAWN << MODULE_BIT_NUM)) +#define ERR_INIT_BASE ((SUBSYS_STARTUP_ID << SUBSYSTEM_BIT_NUM) | (ERR_MODULE_INIT << MODULE_BIT_NUM)) + +typedef enum { + // errcode for handle msg, reserved 128, 0x0001-0x0080 + ERR_APPSPAWN_MSG_TOO_LONG = ERR_APPSPAWN_BASE + 0x0001, // 81788929 + ERR_APPSPAWN_MSG_INVALID_HANDLE, + ERR_APPSPAWN_MSG_CREATE_MSG_FAILED, + ERR_APPSPAWN_COPY_MSG_FAILED, + ERR_APPSPAWN_ALLOC_MEMORY_FAILED, + ERR_APPSPAWN_MSG_DECODE_MSG_FAILED, + + // errcode for hnp, reserved 128, 0x0081 - 0x0100 + ERR_APPSPAWN_HNP = ERR_APPSPAWN_BASE + 0x0081, + + // errcode for device_dug, reserved 128, 0x0101 - 0x0180 + ERR_APPSPAWN_DEVICE_DUG = ERR_APPSPAWN_BASE + 0x0101, + + // errcode for process_exit, reserved 128, 0x0181 - 0x0200 + ERR_APPSPAWN_PROCESS_EXIT = ERR_APPSPAWN_BASE + 0x0181, + + // errcode for prefork, reserved 128, 0x0201 - 0x0280 + ERR_APPSPAWN_PREFORK = ERR_APPSPAWN_BASE + 0x0201, + + // errcode for sandbox, reserved 128, 0x0281 - 0x0300 + ERR_APPSPAWN_SANDBOX = ERR_APPSPAWN_BASE + 0x0281, + + // errcode for set permission, reserved 128, 0x0301 - 0x0380 + ERR_APPSPAWN_SET_PERMISSION = ERR_APPSPAWN_BASE + 0x0301, + ERR_APPSPAWN_SET_INTE_PERMISSION_FAIL, + ERR_APPSPAWN_SET_PROPERTIES_FAIL_SET_KEEP_CAP_FAIL, + ERR_APPSPAWN_SET_PROPERTIES_FAIL_SET_XPM_FAIL, + ERR_APPSPAWN_SET_PROPERTIES_FAIL_SET_PROCESSNAME_FAIL, + ERR_APPSPAWN_SET_PROPERTIES_FAIL_SET_UIDGID_FAIL, + ERR_APPSPAWN_SET_PROPERTIES_FAIL_SET_FD_FAIL, + ERR_APPSPAWN_SET_PROPERTIES_FAIL_SET_CAP_FAIL, + ERR_APPSPAWN_SET_PROPERTIES_FAIL_SET_SELINUXCON_FAIL, + + // errorcode for spawn child process fail, reserved 128, 0x0381 - 0x0400 + ERR_APPSPAWN_SPAWN_FAIL = ERR_APPSPAWN_BASE + 0x0381, + ERR_APPSPAWN_SPAWN_TIMEOUT, + ERR_APPSPAWN_CHILD_CRASH, + ERR_APPSPAWN_MAX_FAILURES_EXCEEDED, +} AppSpawnHisysErrorCode; + +typedef struct { + uint32_t eventCount; + uint32_t maxDuration; + uint32_t minDuration; + uint32_t totalDuration; +} AppSpawnHisysevent; + +typedef struct { + AppSpawnHisysevent bootEvent; // bootStage + AppSpawnHisysevent manualEvent; // bootFinished +} AppSpawnHisyseventInfo; + +AppSpawnHisyseventInfo *InitHisyseventTimer(); +AppSpawnHisyseventInfo *GetAppSpawnHisyseventInfo(); +void AddStatisticEventInfo(AppSpawnHisyseventInfo *hisyseventInfo, uint32_t duration, bool stage); +void DeleteHisyseventInfo(AppSpawnHisyseventInfo *hisyseventInfo); + +void ReportSpawnChildProcessFail(const char* processName, int32_t errorCode); +void ReportSpawnStatisticDuration(const TimerHandle taskHandle, void* content); + +#ifdef __cplusplus +} +#endif +#endif // APPSPAWN_HISYSEVENT_ADAPTER_H \ No newline at end of file diff --git a/standard/BUILD.gn b/standard/BUILD.gn index 44263767..06e35539 100644 --- a/standard/BUILD.gn +++ b/standard/BUILD.gn @@ -87,7 +87,10 @@ ohos_executable("appspawn") { if (appspawn_report_event) { defines += [ "APPSPAWN_HISYSEVENT" ] external_deps += [ "hisysevent:libhisysevent" ] - sources += [ "${appspawn_path}/modules/sysevent/appspawn_hisysevent.cpp" ] + sources += [ + "${appspawn_path}/modules/sysevent/appspawn_hisysevent.cpp", + "${appspawn_path}/modules/sysevent/hisysevent_adapter.cpp", + ] } if (build_selinux) { defines += [ "WITH_SELINUX" ] @@ -195,7 +198,10 @@ ohos_executable("cjappspawn") { if (appspawn_report_event) { defines += [ "APPSPAWN_HISYSEVENT" ] external_deps += [ "hisysevent:libhisysevent" ] - sources += [ "${appspawn_path}/modules/sysevent/appspawn_hisysevent.cpp" ] + sources += [ + "${appspawn_path}/modules/sysevent/appspawn_hisysevent.cpp", + "${appspawn_path}/modules/sysevent/hisysevent_adapter.cpp", + ] } if (build_selinux) { defines += [ "WITH_SELINUX" ] @@ -267,7 +273,10 @@ ohos_executable("nativespawn") { if (appspawn_report_event) { defines += [ "APPSPAWN_HISYSEVENT" ] external_deps += [ "hisysevent:libhisysevent" ] - sources += [ "${appspawn_path}/modules/sysevent/appspawn_hisysevent.cpp" ] + sources += [ + "${appspawn_path}/modules/sysevent/appspawn_hisysevent.cpp", + "${appspawn_path}/modules/sysevent/hisysevent_adapter.cpp", + ] } if (build_selinux) { defines += [ "WITH_SELINUX" ] diff --git a/standard/appspawn_appmgr.c b/standard/appspawn_appmgr.c index 248d5df7..d538192c 100644 --- a/standard/appspawn_appmgr.c +++ b/standard/appspawn_appmgr.c @@ -55,6 +55,9 @@ AppSpawnMgr *CreateAppSpawnMgr(int mode) appMgr->servicePid = getpid(); appMgr->server = NULL; appMgr->sigHandler = NULL; +#ifdef APPSPAWN_HISYSEVENT + appMgr->hisyseventInfo = NULL; +#endif OH_ListInit(&appMgr->appQueue); OH_ListInit(&appMgr->diedQueue); OH_ListInit(&appMgr->appSpawnQueue); @@ -98,6 +101,9 @@ void DeleteAppSpawnMgr(AppSpawnMgr *mgr) OH_ListRemoveAll(&mgr->diedQueue, NULL); OH_ListRemoveAll(&mgr->appSpawnQueue, SpawningQueueDestroy); OH_ListRemoveAll(&mgr->extData, ExtDataDestroy); +#ifdef APPSPAWN_HISYSEVENT + DeleteHisyseventInfo(mgr->hisyseventInfo); +#endif APPSPAWN_LOGV("DeleteAppSpawnMgr %{public}d %{public}d", mgr->servicePid, getpid()); free(mgr); diff --git a/standard/appspawn_manager.h b/standard/appspawn_manager.h index 9c78948c..5c5280ac 100644 --- a/standard/appspawn_manager.h +++ b/standard/appspawn_manager.h @@ -27,6 +27,9 @@ #include "appspawn_utils.h" #include "list.h" #include "loop_event.h" +#ifdef APPSPAWN_HISYSEVENT +#include "hisysevent_adapter.h" +#endif #ifdef __cplusplus extern "C" { @@ -120,6 +123,9 @@ typedef struct TagAppSpawnMgr { struct timespec perLoadEnd; struct ListNode extData; struct SpawnTime spawnTime; +#ifdef APPSPAWN_HISYSEVENT + AppSpawnHisyseventInfo *hisyseventInfo; +#endif } AppSpawnMgr; /** diff --git a/standard/appspawn_service.c b/standard/appspawn_service.c index 30f6512e..d5cd01c0 100644 --- a/standard/appspawn_service.c +++ b/standard/appspawn_service.c @@ -45,6 +45,7 @@ #include "cJSON.h" #ifdef APPSPAWN_HISYSEVENT #include "appspawn_hisysevent.h" +#include "hisysevent_adapter.h" #endif #define PARAM_BUFFER_SIZE 10 #define PATH_SIZE 256 @@ -929,9 +930,13 @@ static uint32_t g_crashTimes = 0; static void WaitChildDied(pid_t pid) { AppSpawningCtx *property = GetAppSpawningCtxByPid(pid); - if (property != NULL && property->state == APP_STATE_SPAWNING) { + if (property != NULL && property->message != NULL && property->state == APP_STATE_SPAWNING) { + const char *processName = GetProcessName(property); APPSPAWN_LOGI("Child process %{public}s fail \'child crash \'pid %{public}d appId: %{public}d", - GetProcessName(property), property->pid, property->client.id); + processName, property->pid, property->client.id); +#ifdef APPSPAWN_HISYSEVENT + ReportSpawnChildProcessFail(processName, ERR_APPSPAWN_CHILD_CRASH); +#endif if (property->client.id == g_lastDiedAppId + 1) { g_crashTimes++; } else { @@ -944,6 +949,9 @@ static void WaitChildDied(pid_t pid) if (g_crashTimes >= MAX_CRASH_TIME) { APPSPAWN_LOGW("Continuous failures in spawning the app, restart appspawn"); +#ifdef APPSPAWN_HISYSEVENT + ReportSpawnChildProcessFail(processName, ERR_APPSPAWN_MAX_FAILURES_EXCEEDED); +#endif StopAppSpawn(); } } @@ -960,6 +968,9 @@ static void WaitChildTimeout(const TimerHandle taskHandle, void *context) #endif kill(property->pid, SIGKILL); } +#ifdef APPSPAWN_HISYSEVENT + ReportSpawnChildProcessFail(GetProcessName(property), ERR_APPSPAWN_SPAWN_TIMEOUT); +#endif SendResponse(property->message->connection, &property->message->msgHeader, APPSPAWN_SPAWN_TIMEOUT, 0); DeleteAppSpawningCtx(property); } @@ -973,6 +984,9 @@ static int ProcessChildFdCheck(int fd, AppSpawningCtx *property) APPSPAWN_CHECK(property->message != NULL, return -1, "Invalid message in ctx %{public}d", property->client.id); if (result != 0) { +#ifdef APPSPAWN_HISYSEVENT + ReportSpawnChildProcessFail(GetProcessName(property), ERR_APPSPAWN_SPAWN_FAIL); +#endif SendResponse(property->message->connection, &property->message->msgHeader, result, property->pid); DeleteAppSpawningCtx(property); return -1; @@ -1007,6 +1021,17 @@ static void ProcessChildResponse(const WatcherHandle taskHandle, int fd, uint32_ clock_gettime(CLOCK_MONOTONIC, &appInfo->spawnEnd); // add max info } + +#ifdef APPSPAWN_HISYSEVENT + //add process spawn duration into hisysevent,(ms) + uint32_t spawnProcessDuration = (appInfo->spawnEnd.tv_sec - appInfo->spawnStart.tv_sec) * (APPSPAWN_USEC_TO_NSEC) + + (uint32_t)((appInfo->spawnEnd.tv_nsec - appInfo->spawnStart.tv_nsec)/(APPSPAWN_MSEC_TO_NSEC)); + AppSpawnMgr *appspawnMgr = GetAppSpawnMgr(); + if (appspawnMgr != NULL) { + AddStatisticEventInfo(appspawnMgr->hisyseventInfo, spawnProcessDuration, IsBootFinished()); + } +#endif + WatchChildProcessFd(property); ProcessMgrHookExecute(STAGE_SERVER_APP_ADD, GetAppSpawnContent(), appInfo); // response @@ -1254,6 +1279,9 @@ AppSpawnContent *AppSpawnCreateContent(const char *socketName, char *longProcNam AppSpawnMgr *appSpawnContent = CreateAppSpawnMgr(mode); APPSPAWN_CHECK(appSpawnContent != NULL, return NULL, "Failed to alloc memory for appspawn"); +#ifdef APPSPAWN_HISYSEVENT + appSpawnContent->hisyseventInfo = InitHisyseventTimer(); +#endif appSpawnContent->content.longProcName = longProcName; appSpawnContent->content.longProcNameLen = nameLen; appSpawnContent->content.notifyResToParent = NotifyResToParent; diff --git a/test/unittest/app_spawn_standard_test/BUILD.gn b/test/unittest/app_spawn_standard_test/BUILD.gn index ae2b9d8e..e12873c2 100644 --- a/test/unittest/app_spawn_standard_test/BUILD.gn +++ b/test/unittest/app_spawn_standard_test/BUILD.gn @@ -203,6 +203,7 @@ ohos_unittest("AppSpawn_ut") { sources += [ "${appspawn_path}/modules/sysevent/appspawn_hisysevent.cpp", "${appspawn_path}/modules/sysevent/event_reporter.cpp", + "${appspawn_path}/modules/sysevent/hisysevent_adapter.cpp", ] } -- Gitee From 6c47947dbf6442d07eb806ec495de5a16aae2f98 Mon Sep 17 00:00:00 2001 From: mengguo Date: Tue, 10 Dec 2024 15:59:28 +0800 Subject: [PATCH 4/4] =?UTF-8?q?Revert=20"!1757=20=E5=BA=94=E7=94=A8?= =?UTF-8?q?=E8=AE=BE=E7=BD=AEMCS=E6=A0=87=E7=AD=BE"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit ae79473850456bd513cdbfe6802b0d551ddb6c70, reversing changes made to 69df0929bebcf49e2e6362a82bd91740dc7d5528. Signed-off-by: mengguo --- modules/common/appspawn_adapter.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/modules/common/appspawn_adapter.cpp b/modules/common/appspawn_adapter.cpp index 2a4b6aa5..74ea6d78 100644 --- a/modules/common/appspawn_adapter.cpp +++ b/modules/common/appspawn_adapter.cpp @@ -106,13 +106,11 @@ int SetSelinuxCon(const AppSpawnMgr *content, const AppSpawningCtx *property) reinterpret_cast(GetAppProperty(property, TLV_DOMAIN_INFO)); APPSPAWN_CHECK(msgDomainInfo != NULL, return APPSPAWN_TLV_NONE, "No domain info in req form %{public}s", GetProcessName(property)); - AppDacInfo *appInfo = reinterpret_cast(GetAppProperty(property, TLV_DAC_INFO)); HapContext hapContext; HapDomainInfo hapDomainInfo; hapDomainInfo.apl = msgDomainInfo->apl; hapDomainInfo.packageName = GetBundleName(property); hapDomainInfo.hapFlags = msgDomainInfo->hapFlags; - hapDomainInfo.uid = appInfo == NULL ? 0 : appInfo->uid; //The value of 0 is invalid. Its purpose is to initialize. if (CheckAppMsgFlagsSet(property, APP_FLAGS_DEBUGGABLE)) { hapDomainInfo.hapFlags |= SELINUX_HAP_DEBUGGABLE; } -- Gitee