103689974SAlex Brachet //===------- 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>
13*af0b0e00SAlex Brachet #include <memory>
14*af0b0e00SAlex 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 
22*af0b0e00SAlex Brachet bool ProcessStatus::exitedNormally() const {
23*af0b0e00SAlex Brachet   return WIFEXITED(PlatformDefined);
24*af0b0e00SAlex Brachet }
2503689974SAlex Brachet 
26*af0b0e00SAlex Brachet int ProcessStatus::getExitCode() const {
2703689974SAlex Brachet   assert(exitedNormally() && "Abnormal termination, no exit code");
2803689974SAlex Brachet   return WEXITSTATUS(PlatformDefined);
2903689974SAlex Brachet }
3003689974SAlex Brachet 
31*af0b0e00SAlex Brachet int ProcessStatus::getFatalSignal() const {
3203689974SAlex Brachet   if (exitedNormally())
3303689974SAlex Brachet     return 0;
3403689974SAlex Brachet   return WTERMSIG(PlatformDefined);
3503689974SAlex Brachet }
3603689974SAlex Brachet 
37*af0b0e00SAlex Brachet ProcessStatus invokeInSubprocess(FunctionCaller *Func, unsigned timeoutMS) {
38*af0b0e00SAlex Brachet   std::unique_ptr<FunctionCaller> X(Func);
39*af0b0e00SAlex Brachet   int pipeFDs[2];
40*af0b0e00SAlex Brachet   if (::pipe(pipeFDs) == -1)
41*af0b0e00SAlex Brachet     return ProcessStatus::Error("pipe(2) failed");
42*af0b0e00SAlex 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();
47*af0b0e00SAlex Brachet   if (Pid == -1)
48*af0b0e00SAlex Brachet     return ProcessStatus::Error("fork(2) failed");
49*af0b0e00SAlex Brachet 
5003689974SAlex Brachet   if (!Pid) {
5103689974SAlex Brachet     (*Func)();
5203689974SAlex Brachet     std::exit(0);
5303689974SAlex Brachet   }
54*af0b0e00SAlex Brachet   ::close(pipeFDs[1]);
5503689974SAlex Brachet 
56*af0b0e00SAlex Brachet   struct pollfd pollFD {
57*af0b0e00SAlex Brachet     pipeFDs[0], 0, 0
58*af0b0e00SAlex Brachet   };
59*af0b0e00SAlex Brachet   // No events requested so this call will only return after the timeout or if
60*af0b0e00SAlex Brachet   // the pipes peer was closed, signaling the process exited.
61*af0b0e00SAlex Brachet   if (::poll(&pollFD, 1, timeoutMS) == -1)
62*af0b0e00SAlex Brachet     return ProcessStatus::Error("poll(2) failed");
63*af0b0e00SAlex Brachet   // If the pipe wasn't closed by the child yet then timeout has expired.
64*af0b0e00SAlex Brachet   if (!(pollFD.revents & POLLHUP)) {
65*af0b0e00SAlex Brachet     ::kill(Pid, SIGKILL);
66*af0b0e00SAlex Brachet     return ProcessStatus::TimedOut();
67*af0b0e00SAlex Brachet   }
68*af0b0e00SAlex Brachet 
69*af0b0e00SAlex Brachet   int WStatus = 0;
70*af0b0e00SAlex Brachet   int status = ::waitpid(Pid, &WStatus, WNOHANG);
71*af0b0e00SAlex Brachet   assert(status == Pid && "wait call should not block here");
72*af0b0e00SAlex Brachet   (void)status;
7303689974SAlex Brachet   return {WStatus};
7403689974SAlex Brachet }
7503689974SAlex Brachet 
7603689974SAlex Brachet const char *signalAsString(int Signum) { return ::strsignal(Signum); }
7703689974SAlex Brachet 
7803689974SAlex Brachet } // namespace testutils
7903689974SAlex Brachet } // namespace __llvm_libc
80