!914 fix the issue of killing process in same group when native process exits

Merge pull request !914 from nianyuu/master
This commit is contained in:
openharmony_ci 2024-01-31 11:37:30 +00:00 committed by Gitee
commit 9e663eb792
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 13 additions and 9 deletions

View File

@ -85,13 +85,14 @@ static void AppInfoHashNodeFree(const HashNode *node, void *context)
free(testNode);
}
APPSPAWN_STATIC void AddAppInfo(pid_t pid, const char *processName)
APPSPAWN_STATIC void AddAppInfo(pid_t pid, const char *processName, AppOperateType code)
{
size_t len = strlen(processName) + 1;
AppInfo *node = (AppInfo *)malloc(sizeof(AppInfo) + len + 1);
APPSPAWN_CHECK(node != NULL, return, "Failed to malloc for appinfo");
node->pid = pid;
node->code = code;
int ret = strcpy_s(node->name, len, processName);
APPSPAWN_CHECK(ret == 0, free(node);
return, "Failed to strcpy process name");
@ -99,12 +100,12 @@ APPSPAWN_STATIC void AddAppInfo(pid_t pid, const char *processName)
ret = OH_HashMapAdd(g_appSpawnContent->appMap, &node->node);
APPSPAWN_CHECK(ret == 0, free(node);
return, "Failed to add appinfo to hash");
APPSPAWN_LOGI("Add %{public}s, pid=%{public}d success", processName, pid);
APPSPAWN_LOGI("Add %{public}s, pid = %{public}d code = %{public}d success", processName, pid, code);
}
void AddNwebInfo(pid_t pid, const char *processName)
{
AddAppInfo(pid, processName);
AddAppInfo(pid, processName, DEFAULT);
}
static AppInfo *GetAppInfo(pid_t pid)
@ -310,8 +311,10 @@ static void MountAppEl2Dir(const AppSpawnClient* client)
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 (appInfo->code != SPAWN_NATIVE_PROCESS) {
KillProcessesByCGroup(uid, appInfo);
}
if (WIFSIGNALED(status)) {
APPSPAWN_LOGW("%{public}s with pid %{public}d exit with signal:%{public}d",
appInfo->name, pid, WTERMSIG(status));
@ -579,7 +582,7 @@ static int HandleMessage(AppSpawnClientExt *appProperty)
APPSPAWN_LOGI("child process %{public}s %{public}s pid %{public}d",
appProperty->property.processName, (result == 0) ? "success" : "fail", appProperty->pid);
if (result == 0) {
AddAppInfo(appProperty->pid, appProperty->property.processName);
AddAppInfo(appProperty->pid, appProperty->property.processName, appProperty->property.code);
SendResponse(appProperty, (char *)&appProperty->pid, sizeof(appProperty->pid));
} else {
SendResponse(appProperty, (char *)&result, sizeof(result));

View File

@ -57,6 +57,7 @@ typedef struct {
typedef struct {
HashNode node;
pid_t pid;
AppOperateType code;
char name[0];
} AppInfo;

View File

@ -51,7 +51,7 @@ using nlohmann::json;
TaskHandle AcceptClient(const LoopHandle loopHandle, const TaskHandle server, uint32_t flags);
bool ReceiveRequestData(const TaskHandle taskHandle, AppSpawnClientExt *appProperty,
const uint8_t *buffer, uint32_t buffLen);
void AddAppInfo(pid_t pid, const char *processName);
void AddAppInfo(pid_t pid, const char *processName, AppOperateType code);
void SignalHandler(const struct signalfd_siginfo *siginfo);
#ifdef __cplusplus
}
@ -707,9 +707,9 @@ HWTEST(AppSpawnStandardTest, App_Spawn_Standard_07, TestSize.Level0)
GTEST_LOG_(INFO) << "App_Spawn_Standard_07 start";
AppSpawnContentExt *content = TestClient(0, DEFAULT, "ohos.test.testapp", "test007");
EXPECT_TRUE(content != nullptr);
AddAppInfo(111, "111");
AddAppInfo(65, "112");
AddAppInfo(97, "113");
AddAppInfo(111, "111", DEFAULT);
AddAppInfo(65, "112", DEFAULT);
AddAppInfo(97, "113", DEFAULT);
struct signalfd_siginfo siginfo = {};
siginfo.ssi_signo = SIGCHLD;