166d00febSPaula 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 <cassert>
1103689974SAlex Brachet #include <cstdlib>
12*8a4ee355SMichael Jones #include <cstring>
13*8a4ee355SMichael Jones #include <iostream>
14af0b0e00SAlex Brachet #include <memory>
15af0b0e00SAlex Brachet #include <poll.h>
1603689974SAlex Brachet #include <signal.h>
1703689974SAlex Brachet #include <sys/wait.h>
1803689974SAlex Brachet #include <unistd.h>
1903689974SAlex Brachet 
2003689974SAlex Brachet namespace __llvm_libc {
2103689974SAlex Brachet namespace testutils {
2203689974SAlex Brachet 
23af0b0e00SAlex Brachet bool ProcessStatus::exitedNormally() const {
24af0b0e00SAlex Brachet   return WIFEXITED(PlatformDefined);
25af0b0e00SAlex Brachet }
2603689974SAlex Brachet 
27af0b0e00SAlex Brachet int ProcessStatus::getExitCode() const {
2803689974SAlex Brachet   assert(exitedNormally() && "Abnormal termination, no exit code");
2903689974SAlex Brachet   return WEXITSTATUS(PlatformDefined);
3003689974SAlex Brachet }
3103689974SAlex Brachet 
32af0b0e00SAlex Brachet int ProcessStatus::getFatalSignal() const {
3303689974SAlex Brachet   if (exitedNormally())
3403689974SAlex Brachet     return 0;
3503689974SAlex Brachet   return WTERMSIG(PlatformDefined);
3603689974SAlex Brachet }
3703689974SAlex Brachet 
38af0b0e00SAlex Brachet ProcessStatus invokeInSubprocess(FunctionCaller *Func, unsigned timeoutMS) {
39af0b0e00SAlex Brachet   std::unique_ptr<FunctionCaller> X(Func);
40af0b0e00SAlex Brachet   int pipeFDs[2];
41af0b0e00SAlex Brachet   if (::pipe(pipeFDs) == -1)
42af0b0e00SAlex Brachet     return ProcessStatus::Error("pipe(2) failed");
43af0b0e00SAlex Brachet 
4403689974SAlex Brachet   // Don't copy the buffers into the child process and print twice.
45*8a4ee355SMichael Jones   std::cout.flush();
46*8a4ee355SMichael Jones   std::cerr.flush();
4703689974SAlex Brachet   pid_t Pid = ::fork();
48af0b0e00SAlex Brachet   if (Pid == -1)
49af0b0e00SAlex Brachet     return ProcessStatus::Error("fork(2) failed");
50af0b0e00SAlex Brachet 
5103689974SAlex Brachet   if (!Pid) {
5203689974SAlex Brachet     (*Func)();
5303689974SAlex Brachet     std::exit(0);
5403689974SAlex Brachet   }
55af0b0e00SAlex Brachet   ::close(pipeFDs[1]);
5603689974SAlex Brachet 
57af0b0e00SAlex Brachet   struct pollfd pollFD {
58af0b0e00SAlex Brachet     pipeFDs[0], 0, 0
59af0b0e00SAlex Brachet   };
60af0b0e00SAlex Brachet   // No events requested so this call will only return after the timeout or if
61af0b0e00SAlex Brachet   // the pipes peer was closed, signaling the process exited.
62af0b0e00SAlex Brachet   if (::poll(&pollFD, 1, timeoutMS) == -1)
63af0b0e00SAlex Brachet     return ProcessStatus::Error("poll(2) failed");
64af0b0e00SAlex Brachet   // If the pipe wasn't closed by the child yet then timeout has expired.
65af0b0e00SAlex Brachet   if (!(pollFD.revents & POLLHUP)) {
66af0b0e00SAlex Brachet     ::kill(Pid, SIGKILL);
67af0b0e00SAlex Brachet     return ProcessStatus::TimedOut();
68af0b0e00SAlex Brachet   }
69af0b0e00SAlex Brachet 
70af0b0e00SAlex Brachet   int WStatus = 0;
7117566573SPaula Toth   // Wait on the pid of the subprocess here so it gets collected by the system
7217566573SPaula Toth   // and doesn't turn into a zombie.
7317566573SPaula Toth   pid_t status = ::waitpid(Pid, &WStatus, 0);
7417566573SPaula Toth   if (status == -1)
7517566573SPaula Toth     return ProcessStatus::Error("waitpid(2) failed");
7617566573SPaula Toth   assert(status == Pid);
77af0b0e00SAlex Brachet   (void)status;
7803689974SAlex Brachet   return {WStatus};
7903689974SAlex Brachet }
8003689974SAlex Brachet 
8103689974SAlex Brachet const char *signalAsString(int Signum) { return ::strsignal(Signum); }
8203689974SAlex Brachet 
8303689974SAlex Brachet } // namespace testutils
8403689974SAlex Brachet } // namespace __llvm_libc
85