From b8c2dc5fde5c7d6a070808f87f6d037710e27659 Mon Sep 17 00:00:00 2001 From: xionglei6 Date: Fri, 25 Mar 2022 15:09:30 +0800 Subject: [PATCH 1/2] fix: pipe error Signed-off-by: xionglei6 --- src/appspawn_server.cpp | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/appspawn_server.cpp b/src/appspawn_server.cpp index 0c6814d4..cc159e82 100644 --- a/src/appspawn_server.cpp +++ b/src/appspawn_server.cpp @@ -343,6 +343,30 @@ int AppSpawnServer::DoColdStartApp(ClientSocket::AppProperty *appProperty, int f return 0; } +static int WaitChild(int fd, int pid, ClientSocket::AppProperty *appProperty) +{ + int result = ERR_OK; + fd_set rd; + struct timeval tv; + FD_ZERO(&rd); + FD_SET(fd, &rd); + tv.tv_sec = 1; + tv.tv_usec = 0; + int ret = select(fd + 1, &rd, nullptr, nullptr, &tv); + if (ret == 0) { // timeout + APPSPAWN_LOGI("Time out for child %s %d", appProperty->processName, pid); + result = ERR_OK; + } else if (ret == -1) { + APPSPAWN_LOGI("Error for child %s %d", appProperty->processName, pid); + result = ERR_OK; + } else { + ret = read(fd, &result, sizeof(result)); + } + APPSPAWN_LOGI("child process %s %s pid %d", + appProperty->processName, (result == ERR_OK) ? "success" : "fail", pid); + return (result == ERR_OK) ? 0 : result; +} + int AppSpawnServer::StartApp(char *longProcName, int64_t longProcNameLen, ClientSocket::AppProperty *appProperty, int connectFd, pid_t &pid) { @@ -350,11 +374,11 @@ int AppSpawnServer::StartApp(char *longProcName, int64_t longProcNameLen, return -EINVAL; } int32_t fd[FDLEN2] = {FD_INIT_VALUE, FD_INIT_VALUE}; - int32_t buff = 0; if (pipe(fd) == -1) { HiLog::Error(LABEL, "create pipe fail, errno = %{public}d", errno); return ERR_PIPE_FAIL; } + fcntl(fd[0], F_SETFL, O_NDELAY); InstallSigHandler(); pid = fork(); @@ -383,12 +407,10 @@ int AppSpawnServer::StartApp(char *longProcName, int64_t longProcNameLen, } _exit(0); } - read(fd[0], &buff, sizeof(buff)); // wait child process resutl + int ret = WaitChild(fd[0], pid, appProperty); close(fd[0]); close(fd[1]); - - HiLog::Info(LABEL, "child process init %{public}s", (buff == ERR_OK) ? "success" : "fail"); - return (buff == ERR_OK) ? 0 : buff; + return ret; } void AppSpawnServer::QuickExitMain() -- Gitee From e54e5f87b4ef6dd2ab25e73130ef14b0fda492f2 Mon Sep 17 00:00:00 2001 From: xionglei6 Date: Fri, 25 Mar 2022 17:09:45 +0800 Subject: [PATCH 2/2] fix: pipe error Signed-off-by: xionglei6 --- src/appspawn_server.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/appspawn_server.cpp b/src/appspawn_server.cpp index f6c0598d..7baf1926 100644 --- a/src/appspawn_server.cpp +++ b/src/appspawn_server.cpp @@ -70,8 +70,6 @@ constexpr int32_t GID_USER_DATA_RW = 1008; constexpr int32_t MAX_GIDS = 64; constexpr int32_t UID_BASE = 200000; constexpr int32_t WAIT_PARAM_TIME = 5; -constexpr int32_t RETRY_TIME = 10; -constexpr int32_t DELAY_US = 10 * 1000; // 10ms constexpr std::string_view BUNDLE_NAME_MEDIA_LIBRARY("com.ohos.medialibrary.MediaLibraryDataA"); constexpr std::string_view BUNDLE_NAME_SCANNER("com.ohos.medialibrary.MediaScannerAbilityA"); -- Gitee