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"
20 namespace LIBC_NAMESPACE
{
23 bool ProcessStatus::exited_normally() { return WIFEXITED(platform_defined
); }
25 int ProcessStatus::get_exit_code() {
26 assert(exited_normally() && "Abnormal termination, no exit code");
27 return WEXITSTATUS(platform_defined
);
30 int ProcessStatus::get_fatal_signal() {
31 if (exited_normally())
33 return WTERMSIG(platform_defined
);
36 ProcessStatus
invoke_in_subprocess(FunctionCaller
*func
, unsigned timeout_ms
) {
37 std::unique_ptr
<FunctionCaller
> X(func
);
39 if (::pipe(pipe_fds
) == -1)
40 return ProcessStatus::error("pipe(2) failed");
42 // Don't copy the buffers into the child process and print twice.
47 return ProcessStatus::error("fork(2) failed");
55 struct pollfd poll_fd
{
58 // No events requested so this call will only return after the timeout or if
59 // the pipes peer was closed, signaling the process exited.
60 if (::poll(&poll_fd
, 1, timeout_ms
) == -1)
61 return ProcessStatus::error("poll(2) failed");
62 // If the pipe wasn't closed by the child yet then timeout has expired.
63 if (!(poll_fd
.revents
& POLLHUP
)) {
65 return ProcessStatus::timed_out_ps();
69 // Wait on the pid of the subprocess here so it gets collected by the system
70 // and doesn't turn into a zombie.
71 pid_t status
= ::waitpid(pid
, &wstatus
, 0);
73 return ProcessStatus::error("waitpid(2) failed");
74 assert(status
== pid
);
78 const char *signal_as_string(int signum
) { return ::strsignal(signum
); }
80 } // namespace testutils
81 } // namespace LIBC_NAMESPACE