1*66d00febSPaula Toth //===-- ExecuteFunction implementation for Unix-like Systems --------------===// 203689974SAlex Brachet // 303689974SAlex Brachet // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 403689974SAlex Brachet // See https://llvm.org/LICENSE.txt for license information. 503689974SAlex Brachet // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 603689974SAlex Brachet // 703689974SAlex Brachet //===----------------------------------------------------------------------===// 803689974SAlex Brachet 903689974SAlex Brachet #include "ExecuteFunction.h" 1003689974SAlex Brachet #include "llvm/Support/raw_ostream.h" 1103689974SAlex Brachet #include <cassert> 1203689974SAlex Brachet #include <cstdlib> 13af0b0e00SAlex Brachet #include <memory> 14af0b0e00SAlex Brachet #include <poll.h> 1503689974SAlex Brachet #include <signal.h> 1603689974SAlex Brachet #include <sys/wait.h> 1703689974SAlex Brachet #include <unistd.h> 1803689974SAlex Brachet 1903689974SAlex Brachet namespace __llvm_libc { 2003689974SAlex Brachet namespace testutils { 2103689974SAlex Brachet 22af0b0e00SAlex Brachet bool ProcessStatus::exitedNormally() const { 23af0b0e00SAlex Brachet return WIFEXITED(PlatformDefined); 24af0b0e00SAlex Brachet } 2503689974SAlex Brachet 26af0b0e00SAlex Brachet int ProcessStatus::getExitCode() const { 2703689974SAlex Brachet assert(exitedNormally() && "Abnormal termination, no exit code"); 2803689974SAlex Brachet return WEXITSTATUS(PlatformDefined); 2903689974SAlex Brachet } 3003689974SAlex Brachet 31af0b0e00SAlex Brachet int ProcessStatus::getFatalSignal() const { 3203689974SAlex Brachet if (exitedNormally()) 3303689974SAlex Brachet return 0; 3403689974SAlex Brachet return WTERMSIG(PlatformDefined); 3503689974SAlex Brachet } 3603689974SAlex Brachet 37af0b0e00SAlex Brachet ProcessStatus invokeInSubprocess(FunctionCaller *Func, unsigned timeoutMS) { 38af0b0e00SAlex Brachet std::unique_ptr<FunctionCaller> X(Func); 39af0b0e00SAlex Brachet int pipeFDs[2]; 40af0b0e00SAlex Brachet if (::pipe(pipeFDs) == -1) 41af0b0e00SAlex Brachet return ProcessStatus::Error("pipe(2) failed"); 42af0b0e00SAlex Brachet 4303689974SAlex Brachet // Don't copy the buffers into the child process and print twice. 4403689974SAlex Brachet llvm::outs().flush(); 4503689974SAlex Brachet llvm::errs().flush(); 4603689974SAlex Brachet pid_t Pid = ::fork(); 47af0b0e00SAlex Brachet if (Pid == -1) 48af0b0e00SAlex Brachet return ProcessStatus::Error("fork(2) failed"); 49af0b0e00SAlex Brachet 5003689974SAlex Brachet if (!Pid) { 5103689974SAlex Brachet (*Func)(); 5203689974SAlex Brachet std::exit(0); 5303689974SAlex Brachet } 54af0b0e00SAlex Brachet ::close(pipeFDs[1]); 5503689974SAlex Brachet 56af0b0e00SAlex Brachet struct pollfd pollFD { 57af0b0e00SAlex Brachet pipeFDs[0], 0, 0 58af0b0e00SAlex Brachet }; 59af0b0e00SAlex Brachet // No events requested so this call will only return after the timeout or if 60af0b0e00SAlex Brachet // the pipes peer was closed, signaling the process exited. 61af0b0e00SAlex Brachet if (::poll(&pollFD, 1, timeoutMS) == -1) 62af0b0e00SAlex Brachet return ProcessStatus::Error("poll(2) failed"); 63af0b0e00SAlex Brachet // If the pipe wasn't closed by the child yet then timeout has expired. 64af0b0e00SAlex Brachet if (!(pollFD.revents & POLLHUP)) { 65af0b0e00SAlex Brachet ::kill(Pid, SIGKILL); 66af0b0e00SAlex Brachet return ProcessStatus::TimedOut(); 67af0b0e00SAlex Brachet } 68af0b0e00SAlex Brachet 69af0b0e00SAlex Brachet int WStatus = 0; 7017566573SPaula Toth // Wait on the pid of the subprocess here so it gets collected by the system 7117566573SPaula Toth // and doesn't turn into a zombie. 7217566573SPaula Toth pid_t status = ::waitpid(Pid, &WStatus, 0); 7317566573SPaula Toth if (status == -1) 7417566573SPaula Toth return ProcessStatus::Error("waitpid(2) failed"); 7517566573SPaula Toth assert(status == Pid); 76af0b0e00SAlex Brachet (void)status; 7703689974SAlex Brachet return {WStatus}; 7803689974SAlex Brachet } 7903689974SAlex Brachet 8003689974SAlex Brachet const char *signalAsString(int Signum) { return ::strsignal(Signum); } 8103689974SAlex Brachet 8203689974SAlex Brachet } // namespace testutils 8303689974SAlex Brachet } // namespace __llvm_libc 84