From 07446b44c7e2fd45e76cd1e89b86e4cd3f2b1266 Mon Sep 17 00:00:00 2001 From: xlgitee Date: Sun, 8 Dec 2024 16:55:14 +0800 Subject: [PATCH] enable hap carsh kernel snapshot Signed-off-by: xlgitee --- standard/appspawn_service.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/standard/appspawn_service.c b/standard/appspawn_service.c index ca514784..9c84903a 100644 --- a/standard/appspawn_service.c +++ b/standard/appspawn_service.c @@ -745,6 +745,40 @@ static int SetPreforkProcessName(AppSpawnContent *content) return 0; } +static void EnableKernelSnapshot() +{ + const char *filePath = "/proc/self/unexpecterd_die_catch"; + int dieCatchFd = open(filePath, O_RDWR); + if (dieCatchFd < 0) { + APPSPAWN_LOGE("Failed to open unexpecterd_die_catch %{public}d", errno); + return; + } + do { + char val[10] = {0}; + if (read(dieCatchFd, val, sizeof(val)) < 0) { + APPSPAWN_LOGE("Failed to read unexpecterd_die_catch %{public}d", errno); + break; + } + if (lseek(dieCatchFd, 0, SEEK_SET) < 0) { + APPSPAWN_LOGE("Failed to lseek unexpecterd_die_catch %{public}d", errno); + break; + } + + int num = (int)strtol(val, NULL, 16); // 16 : val is hex + num |= 8; // 8 : kernel snapshot enable falg + + (void)memset_s(val, sizeof(val), 0, sizeof(val)); + if (snprintf_s(val, sizeof(val), sizeof(val) - 1, "%d", num) < 0) { + APPSPAWN_LOGE("Failed to format unexpecterd_die_catch val %{public}d", errno); + break; + } + if (write(dieCatchFd, val, sizeof(val)) < 0) { + APPSPAWN_LOGE("Failed to write unexpecterd_die_catch %{public}d", errno); + } + } while (false); + close(dieCatchFd); +} + static void ProcessPreFork(AppSpawnContent *content, AppSpawningCtx *property) { APPSPAWN_CHECK(pipe(content->preforkFd) == 0, return, "prefork with prefork pipe failed %{public}d", errno); @@ -759,6 +793,7 @@ static void ProcessPreFork(AppSpawnContent *content, AppSpawningCtx *property) content->reservedPid, content->preforkFd[0], content->preforkFd[1], content->parentToChildFd[0], content->parentToChildFd[1]); if (content->reservedPid == 0) { + EnableKernelSnapshot(); (void)close(property->forkCtx.fd[0]); (void)close(property->forkCtx.fd[1]); int isRet = SetPreforkProcessName(content); -- Gitee