diff --git a/common/appspawn_server.c b/common/appspawn_server.c index 2b7b2ceacbc0390fa0600a3ffa60646e6d5077b1..c1d41ccdeac2f043c1a4b22948406f7d47bc2024 100644 --- a/common/appspawn_server.c +++ b/common/appspawn_server.c @@ -37,9 +37,9 @@ static void NotifyResToParent(struct AppSpawnContent *content, AppSpawnClient *c { StartAppspawnTrace("NotifyResToParent"); APPSPAWN_LOGI("NotifyResToParent: %{public}d", result); - if (content->notifyResToParent != NULL) { - content->notifyResToParent(content, client, result); - } + APPSPAWN_CHECK(content != NULL && content->notifyResToParent != NULL, FinishAppspawnTrace(); + return, "invalid param"); + content->notifyResToParent(content, client, result); FinishAppspawnTrace(); } @@ -104,8 +104,9 @@ int AppSpawnChild(AppSpawnContent *content, AppSpawnClient *client) AppSpawnEnvClear(content, client); return 0); - // notify success to father process and start app process - NotifyResToParent(content, client, 0); + bool isAppspawn = (content->mode == MODE_FOR_APP_SPAWN || content->mode == MODE_FOR_APP_COLD_RUN); + APPSPAWN_CHECK_ONLY_EXPER(isAppspawn, + NotifyResToParent(content, client, 0)); StartAppspawnTrace("AppSpawnExecutePostReplyHook"); (void)AppSpawnExecutePostReplyHook(content, client); @@ -115,6 +116,8 @@ int AppSpawnChild(AppSpawnContent *content, AppSpawnClient *client) ret = content->runChildProcessor(content, client); } if (ret != 0) { + APPSPAWN_CHECK_ONLY_EXPER(!isAppspawn, + NotifyResToParent(content, client, ret)); AppSpawnEnvClear(content, client); } return 0; diff --git a/modules/ace_adapter/ace_adapter.cpp b/modules/ace_adapter/ace_adapter.cpp index eb42eacd1db3f7b707767803ba364b6bf0438d4a..e38f114df9b55e5fa3ee41c0908a699ae1f7f7a3 100644 --- a/modules/ace_adapter/ace_adapter.cpp +++ b/modules/ace_adapter/ace_adapter.cpp @@ -220,6 +220,20 @@ static int BuildFdInfoMap(const AppSpawnMsgNode *message, std::mapforkCtx.fd[1]; + property->forkCtx.fd[1] = -1; + AppSpawnEnvClear(content, client); + APPSPAWN_CHECK(fd >= 0, return, "invalid fd for notify parent"); + int ret = 0; + ssize_t written = write(fd, &ret, sizeof(ret)); + (void)close(fd); + APPSPAWN_LOGI("ClearEnvAndReturnSuccess %{public}u %{public}zd", client->id, written); +} + APPSPAWN_STATIC int RunChildThread(const AppSpawnMgr *content, const AppSpawningCtx *property) { std::string checkExit; @@ -230,10 +244,10 @@ APPSPAWN_STATIC int RunChildThread(const AppSpawnMgr *content, const AppSpawning if (CheckAppMsgFlagsSet(property, APP_FLAGS_CHILDPROCESS)) { std::map fdMap; BuildFdInfoMap(property->message, fdMap, IsColdRunMode(content)); - AppSpawnEnvClear((AppSpawnContent *)&content->content, (AppSpawnClient *)&property->client); + ClearEnvAndReturnSuccess((AppSpawnContent *)&content->content, (AppSpawnClient *)&property->client); OHOS::AppExecFwk::MainThread::StartChild(fdMap); } else { - AppSpawnEnvClear((AppSpawnContent *)&content->content, (AppSpawnClient *)&property->client); + ClearEnvAndReturnSuccess((AppSpawnContent *)&content->content, (AppSpawnClient *)&property->client); OHOS::AppExecFwk::MainThread::Start(); } unsetenv(APPSPAWN_CHECK_EXIT); @@ -265,7 +279,7 @@ APPSPAWN_STATIC int RunChildByRenderCmd(const AppSpawnMgr *content, const AppSpa } options.push_back(nullptr); // clear appspawn env, do not user any content and property - AppSpawnEnvClear((AppSpawnContent *)&content->content, (AppSpawnClient *)&property->client); + ClearEnvAndReturnSuccess((AppSpawnContent *)&content->content, (AppSpawnClient *)&property->client); execvp(args[0].c_str(), options.data()); // If it succeeds calling execvp, it never returns. int err = errno; diff --git a/standard/appspawn_service.c b/standard/appspawn_service.c index 8a0a598931cb40ce3b9c529320b81ae0aa879681..5f9bceb93c440e46f1ce0e401c80bb310e3f138e 100644 --- a/standard/appspawn_service.c +++ b/standard/appspawn_service.c @@ -1115,6 +1115,7 @@ static void ProcessChildResponse(const WatcherHandle taskHandle, int fd, uint32_ static void NotifyResToParent(AppSpawnContent *content, AppSpawnClient *client, int result) { AppSpawningCtx *property = (AppSpawningCtx *)client; + APPSPAWN_CHECK(property != NULL && client != NULL, return, "invalid param"); int fd = property->forkCtx.fd[1]; if (fd >= 0) { (void)write(fd, &result, sizeof(result)); diff --git a/test/unittest/app_spawn_standard_test/app_spawn_cold_run_test.cpp b/test/unittest/app_spawn_standard_test/app_spawn_cold_run_test.cpp index 77d3fcce0c2b13110fcaaf8f50793dc187a0d246..ebc0e2929a04a46d18262ade60298c4adfc2c177 100644 --- a/test/unittest/app_spawn_standard_test/app_spawn_cold_run_test.cpp +++ b/test/unittest/app_spawn_standard_test/app_spawn_cold_run_test.cpp @@ -262,9 +262,9 @@ HWTEST_F(AppSpawnColdRunTest, App_Spawn_Cold_Run_005, TestSize.Level0) appProperty.message->msgHeader.msgLen = 1024; char msg[] = "test-xxx-xxx"; appProperty.message->buffer = (uint8_t *)msg; - AppSpawnClient client = {0, 1}; struct ListNode node; - appProperty.client = client; + appProperty.client.id = 0; + appProperty.client.flags = 1; appProperty.node = node; appProperty.forkCtx.fd[0] = 0; appProperty.forkCtx.fd[1] = 1; @@ -275,8 +275,8 @@ HWTEST_F(AppSpawnColdRunTest, App_Spawn_Cold_Run_005, TestSize.Level0) AppSpawnMgr spawnMgr; spawnMgr.content = content; - ret = AppSpawnColdStartApp(&content, &client); - ASSERT_EQ(ret, APPSPAWN_SYSTEM_ERROR); + ret = AppSpawnColdStartApp(&content, &appProperty.client); + ASSERT_EQ(ret, 0); free(appProperty.message); } } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/app_spawn_test_helper.cpp b/test/unittest/app_spawn_test_helper.cpp index b85686d7c65dfa27957c62b257b3e730260a2825..61f730e1d7a4aaa6a20be5dd134aa3e9103c569c 100644 --- a/test/unittest/app_spawn_test_helper.cpp +++ b/test/unittest/app_spawn_test_helper.cpp @@ -58,6 +58,14 @@ uint32_t AppSpawnTestServer::serverId = 0; static int TestChildLoopRun(AppSpawnContent *content, AppSpawnClient *client) { APPSPAWN_LOGV("ChildLoopRun ..."); + AppSpawningCtx *property = (AppSpawningCtx *)client; + APPSPAWN_CHECK(content != NULL && property != NULL, return 0, "invlid param in clearEnv"); + int fd = property->forkCtx.fd[1]; + property->forkCtx.fd[1] = -1; + APPSPAWN_CHECK(fd >= 0, return 0, "invalid fd for notify parent"); + int ret = 0; + (void)write(fd, &ret, sizeof(ret)); + (void)close(fd); sleep(1); return 0; }