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//===----------------------------------------------------------------------===//
8
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>
19
20namespace LIBC_NAMESPACE {
21namespace testutils {
22
23bool ProcessStatus::exited_normally() { return WIFEXITED(platform_defined); }
24
25int ProcessStatus::get_exit_code() {
26 assert(exited_normally() && "Abnormal termination, no exit code");
27 return WEXITSTATUS(platform_defined);
28}
29
30int ProcessStatus::get_fatal_signal() {
31 if (exited_normally())
32 return 0;
33 return WTERMSIG(platform_defined);
34}
35
36ProcessStatus invoke_in_subprocess(FunctionCaller *func, unsigned timeout_ms) {
37 std::unique_ptr<FunctionCaller> X(func);
38 int pipe_fds[2];
39 if (::pipe(pipedes: pipe_fds) == -1)
40 return ProcessStatus::error(error: "pipe(2) failed");
41
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(error: "fork(2) failed");
48
49 if (!pid) {
50 (*func)();
51 std::exit(status: 0);
52 }
53 ::close(fd: pipe_fds[1]);
54
55 struct pollfd poll_fd {
56 .fd: pipe_fds[0], .events: 0, .revents: 0
57 };
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(fds: &poll_fd, nfds: 1, timeout: timeout_ms) == -1)
61 return ProcessStatus::error(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: pid, SIGKILL);
65 return ProcessStatus::timed_out_ps();
66 }
67
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: pid, stat_loc: &wstatus, options: 0);
72 if (status == -1)
73 return ProcessStatus::error(error: "waitpid(2) failed");
74 assert(status == pid);
75 return {.platform_defined: wstatus};
76}
77
78const char *signal_as_string(int signum) { return ::strsignal(sig: signum); }
79
80} // namespace testutils
81} // namespace LIBC_NAMESPACE
82

source code of libc/test/UnitTest/ExecuteFunctionUnix.cpp