From e3a6f05b20640d6f5f485252e05c6ebd1fb8c54a Mon Sep 17 00:00:00 2001 From: xionglei6 Date: Fri, 25 Mar 2022 13:06:47 +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 38008a33..d4e65d1e 100644 --- a/src/appspawn_server.cpp +++ b/src/appspawn_server.cpp @@ -315,6 +315,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(1, &rd, nullptr, nullptr, &tv); + if (ret == 0) { // timeout + APPSPAWN_LOGI("Time out for child %d %s ", appProperty->processName, pid); + result = ERR_OK; + } else if (ret == -1) { + APPSPAWN_LOGI("Error for child %d %s ", 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) { @@ -322,11 +346,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(); @@ -354,12 +378,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 59d36cc2d4cb2aa6778a28cacd66bc24b8b51da6 Mon Sep 17 00:00:00 2001 From: xionglei6 Date: Fri, 25 Mar 2022 14:14:23 +0800 Subject: [PATCH 2/2] fix: pipe error Signed-off-by: xionglei6 --- src/appspawn_server.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/appspawn_server.cpp b/src/appspawn_server.cpp index d4e65d1e..576a119e 100644 --- a/src/appspawn_server.cpp +++ b/src/appspawn_server.cpp @@ -324,12 +324,12 @@ static int WaitChild(int fd, int pid, ClientSocket::AppProperty *appProperty) FD_SET(fd, &rd); tv.tv_sec = 1; tv.tv_usec = 0; - int ret = select(1, &rd, nullptr, nullptr, &tv); + int ret = select(fd + 1, &rd, nullptr, nullptr, &tv); if (ret == 0) { // timeout - APPSPAWN_LOGI("Time out for child %d %s ", appProperty->processName, pid); + APPSPAWN_LOGI("Time out for child %s %d", appProperty->processName, pid); result = ERR_OK; } else if (ret == -1) { - APPSPAWN_LOGI("Error for child %d %s ", appProperty->processName, pid); + APPSPAWN_LOGI("Error for child %s %d", appProperty->processName, pid); result = ERR_OK; } else { ret = read(fd, &result, sizeof(result)); -- Gitee