xref: /llvm-project/libc/test/UnitTest/ExecuteFunctionUnix.cpp (revision 9426fdd4cbd6812b69c218b865f184cb25342be4)
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 "src/__support/macros/config.h"
11 #include "test/UnitTest/ExecuteFunction.h" // FunctionCaller
12 #include <assert.h>
13 #include <poll.h>
14 #include <signal.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <sys/wait.h>
19 #include <unistd.h>
20 
21 namespace LIBC_NAMESPACE_DECL {
22 namespace testutils {
23 
24 bool ProcessStatus::exited_normally() { return WIFEXITED(platform_defined); }
25 
26 int ProcessStatus::get_exit_code() {
27   assert(exited_normally() && "Abnormal termination, no exit code");
28   return WEXITSTATUS(platform_defined);
29 }
30 
31 int ProcessStatus::get_fatal_signal() {
32   if (exited_normally())
33     return 0;
34   return WTERMSIG(platform_defined);
35 }
36 
37 ProcessStatus invoke_in_subprocess(FunctionCaller *func, unsigned timeout_ms) {
38   int pipe_fds[2];
39   if (::pipe(pipe_fds) == -1) {
40     delete func;
41     return ProcessStatus::error("pipe(2) failed");
42   }
43 
44   // Don't copy the buffers into the child process and print twice.
45   ::fflush(stderr);
46   ::fflush(stdout);
47   pid_t pid = ::fork();
48   if (pid == -1) {
49     delete func;
50     return ProcessStatus::error("fork(2) failed");
51   }
52 
53   if (!pid) {
54     (*func)();
55     delete func;
56     ::exit(0);
57   }
58   ::close(pipe_fds[1]);
59 
60   struct pollfd poll_fd {
61     pipe_fds[0], 0, 0
62   };
63   // No events requested so this call will only return after the timeout or if
64   // the pipes peer was closed, signaling the process exited.
65   if (::poll(&poll_fd, 1, timeout_ms) == -1) {
66     delete func;
67     return ProcessStatus::error("poll(2) failed");
68   }
69   // If the pipe wasn't closed by the child yet then timeout has expired.
70   if (!(poll_fd.revents & POLLHUP)) {
71     ::kill(pid, SIGKILL);
72     delete func;
73     return ProcessStatus::timed_out_ps();
74   }
75 
76   int wstatus = 0;
77   // Wait on the pid of the subprocess here so it gets collected by the system
78   // and doesn't turn into a zombie.
79   pid_t status = ::waitpid(pid, &wstatus, 0);
80   if (status == -1) {
81     delete func;
82     return ProcessStatus::error("waitpid(2) failed");
83   }
84   assert(status == pid);
85   delete func;
86   return {wstatus};
87 }
88 
89 const char *signal_as_string(int signum) { return ::strsignal(signum); }
90 
91 } // namespace testutils
92 } // namespace LIBC_NAMESPACE_DECL
93