From 158b3869d0c8faeb9f7b2724ba75e335ce43e0b3 Mon Sep 17 00:00:00 2001 From: handyohos Date: Wed, 23 Aug 2023 19:21:23 +0800 Subject: [PATCH] Bugfix: kill all children processes when app exited 1) read children processes from cgroup file 2) invoke kill for SIGCHLD of app processes to kill all children processes Signed-off-by: handyohos Change-Id: I660ec91efc21c33aad1a54492051f70d6f8ea0c0 #I7U7MU --- standard/appspawn_service.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/standard/appspawn_service.c b/standard/appspawn_service.c index 3a7ad9bc..c3965c88 100644 --- a/standard/appspawn_service.c +++ b/standard/appspawn_service.c @@ -24,6 +24,7 @@ #include #include #include +#include #include "init_hashmap.h" #include "init_socket.h" @@ -170,9 +171,33 @@ static int SendResponse(AppSpawnClientExt *client, const char *buff, size_t buff return LE_Send(LE_GetDefaultLoop(), client->stream, handle, buffSize); } +static void KillProcessesByCGroup(uid_t uid, const AppInfo *appInfo) +{ + if (appInfo == NULL) { + return; + } + + int userId = uid / 200000; + char buf[PATH_MAX]; + + snprintf_s(buf, sizeof(buf), sizeof(buf) - 1, "/dev/memcg/%d/%s/cgroup.procs", userId, appInfo->name); + FILE *file = fopen(buf, "r"); + APPSPAWN_CHECK(file != NULL, return, "%{public}s not exists.", buf); + pid_t pid; + while (fscanf_s(file, "%d\n", &pid) == 1 && pid > 0) { + if (pid == getpid()) { + continue; + } + APPSPAWN_LOGI("Kill app pid %{public}d now ...", pid); + kill(pid, SIGKILL); + } + fclose(file); +} + static void HandleDiedPid(pid_t pid, uid_t uid, int status) { AppInfo *appInfo = GetAppInfo(pid); + KillProcessesByCGroup(uid, appInfo); APPSPAWN_CHECK(appInfo != NULL, return, "Can not find app info for %{public}d", pid); if (WIFSIGNALED(status)) { APPSPAWN_LOGW("%{public}s with pid %{public}d exit with signal:%{public}d", -- Gitee