From b6b17ec360a75bc0e9a770e62a75f97e242cedde Mon Sep 17 00:00:00 2001 From: lie Date: Mon, 9 May 2022 16:39:00 +0800 Subject: [PATCH] fix get render process termination status failed in nwebspawn Signed-off-by: lie Change-Id: I34714c68f1af56b689598c8eefdc794bed126a7b --- adapter/appspawn_adapter.h | 2 -- adapter/appspawn_nweb.cpp | 55 -------------------------------- standard/appspawn_service.c | 62 +++++++++++++++++++++++-------------- 3 files changed, 39 insertions(+), 80 deletions(-) diff --git a/adapter/appspawn_adapter.h b/adapter/appspawn_adapter.h index 22168570..a1df6de0 100644 --- a/adapter/appspawn_adapter.h +++ b/adapter/appspawn_adapter.h @@ -29,8 +29,6 @@ void SetAppAccessToken(struct AppSpawnContent_ *content, AppSpawnClient *client) void LoadExtendLib(AppSpawnContent *content); void RunChildProcessor(AppSpawnContent *content, AppSpawnClient *client); void RegisterAppSandbox(struct AppSpawnContent_ *content, AppSpawnClient *client); -int GetRenderProcessTerminationStatus(int32_t pid, int *status); -void RecordRenderProcessExitedStatus(pid_t pid, int status); void LoadAppSandboxConfig(void); #ifdef __cplusplus } diff --git a/adapter/appspawn_nweb.cpp b/adapter/appspawn_nweb.cpp index 471a9ee2..b46ccee5 100644 --- a/adapter/appspawn_nweb.cpp +++ b/adapter/appspawn_nweb.cpp @@ -18,18 +18,6 @@ #include #include -#ifdef NWEB_SPAWN -#define RENDER_PROCESS_MAX_NUM 16 -#define RENDER_PROCESS_ARRAY_IDLE 0 - -typedef struct { - int32_t pid; - int exitStatus; -} RenderProcessNode; - -static RenderProcessNode g_renderProcessArray[RENDER_PROCESS_MAX_NUM]; -#endif - void *g_nwebHandle = nullptr; void LoadExtendLib(AppSpawnContent *content) @@ -73,46 +61,3 @@ void RunChildProcessor(AppSpawnContent *content, AppSpawnClient *client) } funcNWebRenderMain(appProperty->property.renderCmd); } - -static void DumpRenderProcessExitedArray() -{ - APPSPAWN_LOGI("dump render process exited array:"); - for (int i = 0; i < RENDER_PROCESS_MAX_NUM; i++) { - APPSPAWN_LOGI("[pid, exitedStatus] = [%d, %d]", - g_renderProcessArray[i].pid, g_renderProcessArray[i].exitStatus); - } -} - -void RecordRenderProcessExitedStatus(pid_t pid, int status) -{ - int i = 0; - for (; i < RENDER_PROCESS_MAX_NUM; i++) { - if (g_renderProcessArray[i].pid == RENDER_PROCESS_ARRAY_IDLE) { - g_renderProcessArray[i].exitStatus = status; - g_renderProcessArray[i].pid = pid; - break; - } - } - if (i == RENDER_PROCESS_MAX_NUM) { - APPSPAWN_LOGE("no empty space in render process exited array"); - DumpRenderProcessExitedArray(); - } -} - -int GetRenderProcessTerminationStatus(int32_t pid, int *status) -{ - if (status == nullptr) { - return -1; - } - - for (int i = 0; i < RENDER_PROCESS_MAX_NUM; i++) { - if (g_renderProcessArray[i].pid == pid) { - *status = g_renderProcessArray[i].exitStatus; - g_renderProcessArray[i].pid = RENDER_PROCESS_ARRAY_IDLE; - return 0; - } - } - APPSPAWN_LOGE("not find pid[%d] in render process exited arrary", pid); - DumpRenderProcessExitedArray(); - return -1; -} diff --git a/standard/appspawn_service.c b/standard/appspawn_service.c index 898042f7..06208dd6 100644 --- a/standard/appspawn_service.c +++ b/standard/appspawn_service.c @@ -136,15 +136,15 @@ static void SignalHandler(const struct signalfd_siginfo *siginfo) APPSPAWN_LOGI("SignalHandler signum %d", siginfo->ssi_signo); switch (siginfo->ssi_signo) { case SIGCHLD: { // delete pid from app map +#ifndef NWEB_SPAWN + // nwebspawn will invoke waitpid and remove appinfo at GetRenderProcessTerminationStatus. pid_t pid; int status; while ((pid = waitpid(-1, &status, WNOHANG)) > 0) { APPSPAWN_LOGI("SignalHandler pid %d status %d", pid, status); RemoveAppInfo(pid); -#ifdef NWEB_SPAWN - RecordRenderProcessExitedStatus(pid, status); -#endif } +#endif break; } case SIGTERM: { // appswapn killed, use kill without parameter @@ -221,29 +221,41 @@ static void StartColdApp(AppSpawnClientExt *appProperty) } } -static int GetProcessTerminationStatus(AppSpawnClientExt *appProperty) -{ #ifdef NWEB_SPAWN - if (appProperty == NULL) { +static int GetRenderProcessTerminationStatus(int32_t pid, int *status) +{ + if (status == NULL) { return -1; } - if (appProperty->property.code == GET_RENDER_TERMINATION_STATUS) { - int exitStatus = 0; - int ret = GetRenderProcessTerminationStatus(appProperty->property.pid, &exitStatus); - if (ret) { - SendResponse(appProperty, (char *)&ret, sizeof(ret)); - } else { - SendResponse(appProperty, (char *)&exitStatus, sizeof(exitStatus)); - } - APPSPAWN_LOGI("AppSpawnServer::get render process termination status, status = %d pid = %d uid %d %s %s", - exitStatus, appProperty->property.pid, appProperty->property.uid, - appProperty->property.processName, appProperty->property.bundleName); - return 0; + + if (kill(pid, SIGKILL) != 0) { + APPSPAWN_LOGE("unable to kill render process, pid: %d", pid); } -#endif - return -1; + + pid_t exitPid = waitpid(pid, status, 0); + if (exitPid != pid) { + APPSPAWN_LOGE("waitpid failed, return : %d, pid: %d, status: %d", exitPid, pid, *status); + return -1; + } + RemoveAppInfo(pid); + return 0; } +static void GetProcessTerminationStatus(AppSpawnClientExt *appProperty) +{ + int exitStatus = 0; + int ret = GetRenderProcessTerminationStatus(appProperty->property.pid, &exitStatus); + if (ret) { + SendResponse(appProperty, (char *)&ret, sizeof(ret)); + } else { + SendResponse(appProperty, (char *)&exitStatus, sizeof(exitStatus)); + } + APPSPAWN_LOGI("AppSpawnServer::get render process termination status, status = %d pid = %d uid %d %s %s", + exitStatus, appProperty->property.pid, appProperty->property.uid, + appProperty->property.processName, appProperty->property.bundleName); +} +#endif + static void OnReceiveRequest(const TaskHandle taskHandle, const uint8_t *buffer, uint32_t buffLen) { APPSPAWN_CHECK(buffer != NULL && buffLen >= sizeof(AppParameter), LE_CloseTask(LE_GetDefaultLoop(), taskHandle); @@ -254,6 +266,13 @@ static void OnReceiveRequest(const TaskHandle taskHandle, const uint8_t *buffer, int ret = memcpy_s(&appProperty->property, sizeof(appProperty->property), buffer, buffLen); APPSPAWN_CHECK(ret == 0, LE_CloseTask(LE_GetDefaultLoop(), taskHandle); return, "Invalid buffer buffLen %u", buffLen); +#ifdef NWEB_SPAWN + // get render process termination status, only nwebspawn need this logic. + if (appProperty->property.code == GET_RENDER_TERMINATION_STATUS) { + GetProcessTerminationStatus(appProperty); + return; + } +#endif APPSPAWN_CHECK(appProperty->property.gidCount <= APP_MAX_GIDS && strlen(appProperty->property.processName) > 0, LE_CloseTask(LE_GetDefaultLoop(), taskHandle); return, "Invalid property %u", appProperty->property.gidCount); @@ -279,9 +298,6 @@ static void OnReceiveRequest(const TaskHandle taskHandle, const uint8_t *buffer, fcntl(appProperty->fd[0], F_SETFL, O_NONBLOCK); - // get render process termination status - APPSPAWN_CHECK(GetProcessTerminationStatus(appProperty) != 0, return, "Invalid appspawn content"); - pid_t pid = 0; int result = AppSpawnProcessMsg(&g_appSpawnContent->content, &appProperty->client, &pid); if (result == 0) { // wait child process resutl -- Gitee