1 //===------- ExecuteFunction implementation for Unix-like Systems ---------===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "ExecuteFunction.h" 10 #include "llvm/Support/raw_ostream.h" 11 #include <cassert> 12 #include <cstdlib> 13 #include <signal.h> 14 #include <sys/wait.h> 15 #include <unistd.h> 16 17 namespace __llvm_libc { 18 namespace testutils { 19 20 bool ProcessStatus::exitedNormally() { return WIFEXITED(PlatformDefined); } 21 22 int ProcessStatus::getExitCode() { 23 assert(exitedNormally() && "Abnormal termination, no exit code"); 24 return WEXITSTATUS(PlatformDefined); 25 } 26 27 int ProcessStatus::getFatalSignal() { 28 if (exitedNormally()) 29 return 0; 30 return WTERMSIG(PlatformDefined); 31 } 32 33 ProcessStatus invokeInSubprocess(FunctionCaller *Func) { 34 // Don't copy the buffers into the child process and print twice. 35 llvm::outs().flush(); 36 llvm::errs().flush(); 37 pid_t Pid = ::fork(); 38 if (!Pid) { 39 (*Func)(); 40 std::exit(0); 41 } 42 43 int WStatus; 44 ::waitpid(Pid, &WStatus, 0); 45 delete Func; 46 return {WStatus}; 47 } 48 49 const char *signalAsString(int Signum) { return ::strsignal(Signum); } 50 51 } // namespace testutils 52 } // namespace __llvm_libc 53