diff --git a/interfaces/innerkits/include/client_socket.h b/interfaces/innerkits/include/client_socket.h index dc459bc778869272552f405f535ea73936cb30df..67c189e245e1a03c8406cd4d0a682db4505d7ced 100644 --- a/interfaces/innerkits/include/client_socket.h +++ b/interfaces/innerkits/include/client_socket.h @@ -92,6 +92,11 @@ public: static constexpr int RENDER_CMD_MAX_LEN = 1024; static constexpr int APPSPAWN_COLD_BOOT = 0x01; + enum AppOperateCode { + DEFAULT = 0, + GET_RENDER_TERMINATION_STATUS, + }; + struct AppProperty { uint32_t uid; // the UNIX uid that the child process setuid() to after fork() uint32_t gid; // the UNIX gid that the child process setgid() to after fork() @@ -104,6 +109,8 @@ public: char apl[APL_MAX_LEN]; char renderCmd[RENDER_CMD_MAX_LEN]; uint32_t flags; + int32_t pid; // query render process exited status by render process pid + AppOperateCode code; }; private: diff --git a/src/appspawn_server.cpp b/src/appspawn_server.cpp index 54f66450b81e493f08225361784dd1bb60dd144b..ba31235b25ec4880c8f1d7cf9f605575a0c72f45 100644 --- a/src/appspawn_server.cpp +++ b/src/appspawn_server.cpp @@ -82,13 +82,36 @@ static constexpr HiLogLabel LABEL = {LOG_CORE, 0, "AppSpawnServer"}; extern "C" { #endif +#ifdef NWEB_SPAWN +static int GetRenderProcessTerminationStatus(int32_t pid, int *status) +{ + if (status == nullptr) { + return -EINVAL; + } + + if (kill(pid, SIGKILL) != 0) { + APPSPAWN_LOGE("unable to kill render process, pid: %d", pid); + } + + pid_t exitPid = waitpid(pid, status, 0); + if (exitPid != pid) { + APPSPAWN_LOGE("SignalHandler HandleSignal: %d, status: %d", pid, status); + return -EINVAL; + } + + return 0; +} +#endif + static void SignalHandler([[maybe_unused]] int sig) { +#ifndef NWEB_SPAWN pid_t pid; int status; while ((pid = waitpid(-1, &status, WNOHANG)) > 0) { } +#endif } static void InstallSigHandler() @@ -207,12 +230,13 @@ void AppSpawnServer::HandleSignal() if (ret != sizeof(fdsi) || fdsi.ssi_signo != SIGCHLD) { continue; } +#ifndef NWEB_SPAWN pid_t pid; int status; while ((pid = waitpid(-1, &status, WNOHANG)) > 0) { APPSPAWN_LOGE("HandleSignal: %d", pid); } - +#endif std::lock_guard lock(mut_); isChildDie_ = true; childPid_ = fdsi.ssi_pid; @@ -419,6 +443,37 @@ void AppSpawnServer::QuickExitMain() return; } +void AppSpawnServer::ProcessAppSpawnMsg(char *longProcName, int64_t longProcNameLen, + const std::unique_ptr &msg) +{ + int connectFd = msg->GetConnectFd(); + ClientSocket::AppProperty *appProperty = msg->GetMsg(); +#ifdef NWEB_SPAWN + if (appProperty->code == ClientSocket::GET_RENDER_TERMINATION_STATUS) { + int exitStatus = 0; + int ret = GetRenderProcessTerminationStatus(appProperty->pid, &exitStatus); + if (ret) { + msg->Response(ret); + } else { + msg->Response(exitStatus); + } + APPSPAWN_LOGI("AppSpawnServer::get render process termination status, status = %d pid = %d uid %d %s %s", + exitStatus, appProperty->pid, appProperty->uid, appProperty->processName, appProperty->bundleName); + return; + } +#endif + pid_t pid = 0; + int ret = StartApp(longProcName, longProcNameLen, appProperty, connectFd, pid); + if (ret) { + msg->Response(ret); + } else { + msg->Response(pid); + appMap_[pid] = appProperty->processName; + } + APPSPAWN_LOGI("AppSpawnServer::parent process create app finish, pid = %d uid %d %s %s", + pid, appProperty->uid, appProperty->processName, appProperty->bundleName); +} + bool AppSpawnServer::ServerMain(char *longProcName, int64_t longProcNameLen) { if (socket_->RegisterServerSocket() != 0) { @@ -450,18 +505,8 @@ bool AppSpawnServer::ServerMain(char *longProcName, int64_t longProcNameLen) std::unique_ptr msg = std::move(appQueue_.front()); appQueue_.pop(); int connectFd = msg->GetConnectFd(); - ClientSocket::AppProperty *appProperty = msg->GetMsg(); - pid_t pid = 0; - int ret = StartApp(longProcName, longProcNameLen, appProperty, connectFd, pid); - if (ret != 0) { - msg->Response(ret); - } else { - msg->Response(pid); - appMap_[pid] = appProperty->processName; - } + ProcessAppSpawnMsg(longProcName, longProcNameLen, msg); socket_->CloseConnection(connectFd); // close socket connection - APPSPAWN_LOGI("AppSpawnServer::parent process create app finish, pid = %d uid %d %s %s", - pid, appProperty->uid, appProperty->processName, appProperty->bundleName); } while (appMap_.size() > 0) { diff --git a/src/include/appspawn_server.h b/src/include/appspawn_server.h index 5836b32d065d7c21c2c54d74ac6282d14f4e8358..2ec656cfc9d768088f634f4db91397ca807caaf8 100644 --- a/src/include/appspawn_server.h +++ b/src/include/appspawn_server.h @@ -195,6 +195,10 @@ private: void HandleSignal(); void QuickExitMain(); + + void ProcessAppSpawnMsg(char *longProcName, int64_t longProcNameLen, + const std::unique_ptr &msg); + private: const std::string deviceNull_ = "/dev/null"; std::string socketName_ {};