Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / test / UnitTest / ExecuteFunctionUnix.cpp
blobb244337f9c6fcd6508e689f932a5fe88bc22f451
1 //===-- ExecuteFunction implementation for Unix-like Systems --------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
9 #include "ExecuteFunction.h"
10 #include <cassert>
11 #include <cstdlib>
12 #include <cstring>
13 #include <iostream>
14 #include <memory>
15 #include <poll.h>
16 #include <signal.h>
17 #include <sys/wait.h>
18 #include <unistd.h>
20 namespace LIBC_NAMESPACE {
21 namespace testutils {
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())
32 return 0;
33 return WTERMSIG(platform_defined);
36 ProcessStatus invoke_in_subprocess(FunctionCaller *func, unsigned timeout_ms) {
37 std::unique_ptr<FunctionCaller> X(func);
38 int pipe_fds[2];
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.
43 std::cout.flush();
44 std::cerr.flush();
45 pid_t pid = ::fork();
46 if (pid == -1)
47 return ProcessStatus::error("fork(2) failed");
49 if (!pid) {
50 (*func)();
51 std::exit(0);
53 ::close(pipe_fds[1]);
55 struct pollfd poll_fd {
56 pipe_fds[0], 0, 0
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)) {
64 ::kill(pid, SIGKILL);
65 return ProcessStatus::timed_out_ps();
68 int wstatus = 0;
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);
72 if (status == -1)
73 return ProcessStatus::error("waitpid(2) failed");
74 assert(status == pid);
75 return {wstatus};
78 const char *signal_as_string(int signum) { return ::strsignal(signum); }
80 } // namespace testutils
81 } // namespace LIBC_NAMESPACE