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 
exited_normally() const2325226f3eSMichael Jones bool ProcessStatus::exited_normally() const {
2425226f3eSMichael Jones   return WIFEXITED(platform_defined);
25af0b0e00SAlex Brachet }
2603689974SAlex Brachet 
get_exit_code() const2725226f3eSMichael Jones int ProcessStatus::get_exit_code() const {
2825226f3eSMichael Jones   assert(exited_normally() && "Abnormal termination, no exit code");
2925226f3eSMichael Jones   return WEXITSTATUS(platform_defined);
3003689974SAlex Brachet }
3103689974SAlex Brachet 
get_fatal_signal() const3225226f3eSMichael Jones int ProcessStatus::get_fatal_signal() const {
3325226f3eSMichael Jones   if (exited_normally())
3403689974SAlex Brachet     return 0;
3525226f3eSMichael Jones   return WTERMSIG(platform_defined);
3603689974SAlex Brachet }
3703689974SAlex Brachet 
invoke_in_subprocess(FunctionCaller * func,unsigned timeout_ms)38*968c1aa5SAlex Brachet ProcessStatus invoke_in_subprocess(FunctionCaller *func, unsigned timeout_ms) {
39*968c1aa5SAlex Brachet   std::unique_ptr<FunctionCaller> X(func);
40*968c1aa5SAlex Brachet   int pipe_fds[2];
41*968c1aa5SAlex Brachet   if (::pipe(pipe_fds) == -1)
4225226f3eSMichael 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();
47*968c1aa5SAlex Brachet   pid_t pid = ::fork();
48*968c1aa5SAlex Brachet   if (pid == -1)
4925226f3eSMichael Jones     return ProcessStatus::error("fork(2) failed");
50af0b0e00SAlex Brachet 
51*968c1aa5SAlex Brachet   if (!pid) {
52*968c1aa5SAlex Brachet     (*func)();
5303689974SAlex Brachet     std::exit(0);
5403689974SAlex Brachet   }
55*968c1aa5SAlex Brachet   ::close(pipe_fds[1]);
5603689974SAlex Brachet 
57*968c1aa5SAlex Brachet   struct pollfd poll_fd {
58*968c1aa5SAlex Brachet     pipe_fds[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.
62*968c1aa5SAlex Brachet   if (::poll(&poll_fd, 1, timeout_ms) == -1)
6325226f3eSMichael Jones     return ProcessStatus::error("poll(2) failed");
64af0b0e00SAlex Brachet   // If the pipe wasn't closed by the child yet then timeout has expired.
65*968c1aa5SAlex Brachet   if (!(poll_fd.revents & POLLHUP)) {
66*968c1aa5SAlex Brachet     ::kill(pid, SIGKILL);
6725226f3eSMichael Jones     return ProcessStatus::timed_out_ps();
68af0b0e00SAlex Brachet   }
69af0b0e00SAlex Brachet 
70*968c1aa5SAlex 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.
73*968c1aa5SAlex Brachet   pid_t status = ::waitpid(pid, &wstatus, 0);
7417566573SPaula Toth   if (status == -1)
7525226f3eSMichael Jones     return ProcessStatus::error("waitpid(2) failed");
76*968c1aa5SAlex Brachet   assert(status == pid);
77*968c1aa5SAlex Brachet   return {wstatus};
7803689974SAlex Brachet }
7903689974SAlex Brachet 
signal_as_string(int signum)80*968c1aa5SAlex Brachet const char *signal_as_string(int signum) { return ::strsignal(signum); }
8103689974SAlex Brachet 
8203689974SAlex Brachet } // namespace testutils
8303689974SAlex Brachet } // namespace __llvm_libc
84