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>
128a4ee355SMichael Jones #include <cstring>
138a4ee355SMichael 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 
23*25226f3eSMichael Jones bool ProcessStatus::exited_normally() const {
24*25226f3eSMichael Jones   return WIFEXITED(platform_defined);
25af0b0e00SAlex Brachet }
2603689974SAlex Brachet 
27*25226f3eSMichael Jones int ProcessStatus::get_exit_code() const {
28*25226f3eSMichael Jones   assert(exited_normally() && "Abnormal termination, no exit code");
29*25226f3eSMichael Jones   return WEXITSTATUS(platform_defined);
3003689974SAlex Brachet }
3103689974SAlex Brachet 
32*25226f3eSMichael Jones int ProcessStatus::get_fatal_signal() const {
33*25226f3eSMichael Jones   if (exited_normally())
3403689974SAlex Brachet     return 0;
35*25226f3eSMichael Jones   return WTERMSIG(platform_defined);
3603689974SAlex Brachet }
3703689974SAlex Brachet 
38*25226f3eSMichael Jones ProcessStatus invoke_in_subprocess(FunctionCaller *Func, unsigned timeoutMS) {
39af0b0e00SAlex Brachet   std::unique_ptr<FunctionCaller> X(Func);
40af0b0e00SAlex Brachet   int pipeFDs[2];
41af0b0e00SAlex Brachet   if (::pipe(pipeFDs) == -1)
42*25226f3eSMichael Jones     return ProcessStatus::error("pipe(2) failed");
43af0b0e00SAlex Brachet 
4403689974SAlex Brachet   // Don't copy the buffers into the child process and print twice.
458a4ee355SMichael Jones   std::cout.flush();
468a4ee355SMichael Jones   std::cerr.flush();
4703689974SAlex Brachet   pid_t Pid = ::fork();
48af0b0e00SAlex Brachet   if (Pid == -1)
49*25226f3eSMichael Jones     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)
63*25226f3eSMichael Jones     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);
67*25226f3eSMichael Jones     return ProcessStatus::timed_out_ps();
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)
75*25226f3eSMichael Jones     return ProcessStatus::error("waitpid(2) failed");
7617566573SPaula Toth   assert(status == Pid);
77af0b0e00SAlex Brachet   (void)status;
7803689974SAlex Brachet   return {WStatus};
7903689974SAlex Brachet }
8003689974SAlex Brachet 
81*25226f3eSMichael Jones const char *signal_as_string(int Signum) { return ::strsignal(Signum); }
8203689974SAlex Brachet 
8303689974SAlex Brachet } // namespace testutils
8403689974SAlex Brachet } // namespace __llvm_libc
85