From afdb59fd8297ce3d345da20da160c82c72cdf576 Mon Sep 17 00:00:00 2001 From: xionglei6 Date: Thu, 24 Mar 2022 14:02:40 +0800 Subject: [PATCH 1/3] fix: pipe error Signed-off-by: xionglei6 --- src/appspawn_server.cpp | 30 +++++++++++++++++++++++++----- src/socket/client_socket.cpp | 9 +++++---- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/appspawn_server.cpp b/src/appspawn_server.cpp index 0c6814d4..6ebc8f47 100644 --- a/src/appspawn_server.cpp +++ b/src/appspawn_server.cpp @@ -70,6 +70,8 @@ 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"); @@ -343,6 +345,26 @@ int AppSpawnServer::DoColdStartApp(ClientSocket::AppProperty *appProperty, int f return 0; } +static int WaitChild(int fd, int pid, ClientSocket::AppProperty *appProperty) +{ + int result = 0; + int count = 0; + while (count < RETRY_TIME) { // wait child process resutl + if (read(fd, &result, sizeof(result)) == sizeof(result)){ + break; + } + usleep(DELAY_US); + count++; + } + if (count >= RETRY_TIME) { + APPSPAWN_LOGI("Time out for child %d %s ", appProperty->processName, pid); + result = ERR_OK; + } + HiLog::Info(LABEL, "child process %{public}s %{public}s pid %{public}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 +372,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_NONBLOCK); InstallSigHandler(); pid = fork(); @@ -383,12 +405,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() diff --git a/src/socket/client_socket.cpp b/src/socket/client_socket.cpp index 308f389c..699bbaf9 100644 --- a/src/socket/client_socket.cpp +++ b/src/socket/client_socket.cpp @@ -63,20 +63,17 @@ int ClientSocket::ConnectSocket(int connectFd) } if (PackSocketAddr() != 0) { - CloseSocket(connectFd); return -1; } if ((setsockopt(connectFd, SOL_SOCKET, SO_RCVTIMEO, &SOCKET_TIMEOUT, sizeof(SOCKET_TIMEOUT)) != 0) || (setsockopt(connectFd, SOL_SOCKET, SO_SNDTIMEO, &SOCKET_TIMEOUT, sizeof(SOCKET_TIMEOUT)) != 0)) { HiLog::Warn(LABEL, "Client: Failed to set opt of socket %d, err %d", connectFd, errno); - CloseSocket(connectFd); return -1; } if (connect(connectFd, reinterpret_cast(&socketAddr_), socketAddrLen_) < 0) { HiLog::Warn(LABEL, "Client: Connect on socket fd %d, failed: %d", connectFd, errno); - CloseSocket(connectFd); return -1; } @@ -86,7 +83,11 @@ int ClientSocket::ConnectSocket(int connectFd) int ClientSocket::ConnectSocket() { - return ConnectSocket(socketFd_); + int ret = ConnectSocket(socketFd_); + if (ret != 0) { + CloseClient(); + } + return ret; } int ClientSocket::WriteSocketMessage(const void *buf, int len) -- Gitee From 8068c955da94a639fe5f054a9aa391054d53cb7c Mon Sep 17 00:00:00 2001 From: xionglei6 Date: Thu, 24 Mar 2022 14:15:29 +0800 Subject: [PATCH 2/3] fix: pipe error Signed-off-by: xionglei6 --- src/appspawn_server.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/appspawn_server.cpp b/src/appspawn_server.cpp index 6ebc8f47..aa785949 100644 --- a/src/appspawn_server.cpp +++ b/src/appspawn_server.cpp @@ -350,7 +350,8 @@ static int WaitChild(int fd, int pid, ClientSocket::AppProperty *appProperty) int result = 0; int count = 0; while (count < RETRY_TIME) { // wait child process resutl - if (read(fd, &result, sizeof(result)) == sizeof(result)){ + int readLen = read(fd, &result, sizeof(result)); + if (readLen == sizeof(result)){ break; } usleep(DELAY_US); -- Gitee From 285c9b0495c5beaf574cb2e9ec2838cf4204c17b Mon Sep 17 00:00:00 2001 From: xionglei6 Date: Thu, 24 Mar 2022 14:29:35 +0800 Subject: [PATCH 3/3] fix: pipe error Signed-off-by: xionglei6 --- src/appspawn_server.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/appspawn_server.cpp b/src/appspawn_server.cpp index aa785949..f1c32ba8 100644 --- a/src/appspawn_server.cpp +++ b/src/appspawn_server.cpp @@ -351,7 +351,7 @@ static int WaitChild(int fd, int pid, ClientSocket::AppProperty *appProperty) int count = 0; while (count < RETRY_TIME) { // wait child process resutl int readLen = read(fd, &result, sizeof(result)); - if (readLen == sizeof(result)){ + if (readLen == sizeof(result)) { break; } usleep(DELAY_US); -- Gitee