xref: /llvm-project/libc/test/UnitTest/ExecuteFunctionUnix.cpp (revision e3645eadb8f28a522243d628dcc0d37e5af9aecf)
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 
20 namespace __llvm_libc {
21 namespace testutils {
22 
23 bool ProcessStatus::exited_normally() { return WIFEXITED(platform_defined); }
24 
25 int ProcessStatus::get_exit_code() {
26   assert(exited_normally() && "Abnormal termination, no exit code");
27   return WEXITSTATUS(platform_defined);
28 }
29 
30 int ProcessStatus::get_fatal_signal() {
31   if (exited_normally())
32     return 0;
33   return WTERMSIG(platform_defined);
34 }
35 
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");
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("fork(2) failed");
48 
49   if (!pid) {
50     (*func)();
51     std::exit(0);
52   }
53   ::close(pipe_fds[1]);
54 
55   struct pollfd poll_fd {
56     pipe_fds[0], 0, 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(&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();
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, &wstatus, 0);
72   if (status == -1)
73     return ProcessStatus::error("waitpid(2) failed");
74   assert(status == pid);
75   return {wstatus};
76 }
77 
78 const char *signal_as_string(int signum) { return ::strsignal(signum); }
79 
80 } // namespace testutils
81 } // namespace __llvm_libc
82