1 //===------- ExecuteFunction implementation for Unix-like Systems ---------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "ExecuteFunction.h"
10 #include "llvm/Support/raw_ostream.h"
19 namespace __llvm_libc {
22 bool ProcessStatus::exitedNormally() const {
23 return WIFEXITED(PlatformDefined);
26 int ProcessStatus::getExitCode() const {
27 assert(exitedNormally() && "Abnormal termination, no exit code");
28 return WEXITSTATUS(PlatformDefined);
31 int ProcessStatus::getFatalSignal() const {
34 return WTERMSIG(PlatformDefined);
37 ProcessStatus invokeInSubprocess(FunctionCaller *Func, unsigned timeoutMS) {
38 std::unique_ptr<FunctionCaller> X(Func);
40 if (::pipe(pipeFDs) == -1)
41 return ProcessStatus::Error("pipe(2) failed");
43 // Don't copy the buffers into the child process and print twice.
48 return ProcessStatus::Error("fork(2) failed");
56 struct pollfd pollFD {
59 // No events requested so this call will only return after the timeout or if
60 // the pipes peer was closed, signaling the process exited.
61 if (::poll(&pollFD, 1, timeoutMS) == -1)
62 return ProcessStatus::Error("poll(2) failed");
63 // If the pipe wasn't closed by the child yet then timeout has expired.
64 if (!(pollFD.revents & POLLHUP)) {
66 return ProcessStatus::TimedOut();
70 // Wait on the pid of the subprocess here so it gets collected by the system
71 // and doesn't turn into a zombie.
72 pid_t status = ::waitpid(Pid, &WStatus, 0);
74 return ProcessStatus::Error("waitpid(2) failed");
75 assert(status == Pid);
80 const char *signalAsString(int Signum) { return ::strsignal(Signum); }
82 } // namespace testutils
83 } // namespace __llvm_libc