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 "src/__support/macros/config.h"
21 namespace LIBC_NAMESPACE_DECL
{
24 bool ProcessStatus::exited_normally() { return WIFEXITED(platform_defined
); }
26 int ProcessStatus::get_exit_code() {
27 assert(exited_normally() && "Abnormal termination, no exit code");
28 return WEXITSTATUS(platform_defined
);
31 int ProcessStatus::get_fatal_signal() {
32 if (exited_normally())
34 return WTERMSIG(platform_defined
);
37 ProcessStatus
invoke_in_subprocess(FunctionCaller
*func
, unsigned timeout_ms
) {
38 std::unique_ptr
<FunctionCaller
> X(func
);
40 if (::pipe(pipe_fds
) == -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 poll_fd
{
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(&poll_fd
, 1, timeout_ms
) == -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 (!(poll_fd
.revents
& POLLHUP
)) {
66 return ProcessStatus::timed_out_ps();
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
);
79 const char *signal_as_string(int signum
) { return ::strsignal(signum
); }
81 } // namespace testutils
82 } // namespace LIBC_NAMESPACE_DECL