|
| 1 | +#include "child_process.h" |
| 2 | + |
| 3 | +#include <cerrno> |
| 4 | +#include <cstdlib> |
| 5 | +#include <fcntl.h> |
| 6 | +#include <sys/wait.h> |
| 7 | +#include <unistd.h> |
| 8 | +#include <cassert> |
| 9 | +#include <cstdio> |
| 10 | +#include <cstring> |
| 11 | +#include <string> |
| 12 | +#include <vector> |
| 13 | + |
| 14 | +#if defined(__ANDROID__) |
| 15 | +#include <android/api-level.h> |
| 16 | +#endif |
| 17 | + |
| 18 | +#if !defined(__ANDROID__) |
| 19 | +#include <spawn.h> |
| 20 | +#elif (__ANDROID_API__ >= 29) |
| 21 | +#include <spawn.h> |
| 22 | +#endif |
| 23 | + |
| 24 | +#ifndef VerifyElseExit |
| 25 | +#define VerifyElseExit(condition) \ |
| 26 | + do { \ |
| 27 | + if (!(condition)) { \ |
| 28 | + ExitOnError(#condition, nullptr); \ |
| 29 | + } \ |
| 30 | + } while (false) |
| 31 | +#endif |
| 32 | + |
| 33 | +#ifndef VerifyElseExitWithCleanup |
| 34 | +#define VerifyElseExitWithCleanup(condition, actions_ptr) \ |
| 35 | + do { \ |
| 36 | + if (!(condition)) { \ |
| 37 | + ExitOnError(#condition, actions_ptr); \ |
| 38 | + } \ |
| 39 | + } while (false) |
| 40 | +#endif |
| 41 | + |
| 42 | +#if defined(__ANDROID__) && (__ANDROID_API__ < 29) |
| 43 | + |
| 44 | +namespace node_api_tests { |
| 45 | + |
| 46 | +ProcessResult SpawnSync(std::string_view /*command*/, |
| 47 | + std::vector<std::string> /*args*/) { |
| 48 | + ProcessResult result{}; |
| 49 | + result.status = -1; |
| 50 | + result.std_error = "child_process.spawnSync is not supported on this platform."; |
| 51 | + result.std_output.clear(); |
| 52 | + return result; |
| 53 | +} |
| 54 | + |
| 55 | +} // namespace node_api_tests |
| 56 | + |
| 57 | +#else |
| 58 | + |
| 59 | +extern char** environ; |
| 60 | + |
| 61 | +namespace node_api_tests { |
| 62 | + |
| 63 | +namespace { |
| 64 | + |
| 65 | +std::string ReadFromFd(int fd); |
| 66 | +void ExitOnError(const char* message, posix_spawn_file_actions_t* actions); |
| 67 | + |
| 68 | +} // namespace |
| 69 | + |
| 70 | +ProcessResult SpawnSync(std::string_view command, |
| 71 | + std::vector<std::string> args) { |
| 72 | + ProcessResult result{}; |
| 73 | + |
| 74 | + // These int arrays each comprise two file descriptors: { readEnd, writeEnd }. |
| 75 | + int stdout_pipe[2], stderr_pipe[2]; |
| 76 | + VerifyElseExit(pipe(stdout_pipe) == 0); |
| 77 | + VerifyElseExit(pipe(stderr_pipe) == 0); |
| 78 | + |
| 79 | + posix_spawn_file_actions_t actions; |
| 80 | + VerifyElseExit(posix_spawn_file_actions_init(&actions) == 0); |
| 81 | + |
| 82 | + VerifyElseExitWithCleanup(posix_spawn_file_actions_adddup2( |
| 83 | + &actions, stdout_pipe[1], STDOUT_FILENO) == 0, |
| 84 | + &actions); |
| 85 | + VerifyElseExitWithCleanup(posix_spawn_file_actions_adddup2( |
| 86 | + &actions, stderr_pipe[1], STDERR_FILENO) == 0, |
| 87 | + &actions); |
| 88 | + |
| 89 | + VerifyElseExitWithCleanup( |
| 90 | + posix_spawn_file_actions_addclose(&actions, stdout_pipe[0]) == 0, |
| 91 | + &actions); |
| 92 | + VerifyElseExitWithCleanup( |
| 93 | + posix_spawn_file_actions_addclose(&actions, stderr_pipe[0]) == 0, |
| 94 | + &actions); |
| 95 | + |
| 96 | + std::vector<char*> argv; |
| 97 | + argv.push_back(strdup(std::string(command).c_str())); |
| 98 | + for (const std::string& arg : args) { |
| 99 | + argv.push_back(strdup(arg.c_str())); |
| 100 | + } |
| 101 | + argv.push_back(nullptr); |
| 102 | + |
| 103 | + pid_t pid; |
| 104 | + VerifyElseExitWithCleanup( |
| 105 | + posix_spawnp(&pid, argv[0], &actions, nullptr, argv.data(), environ) == 0, |
| 106 | + &actions); |
| 107 | + |
| 108 | + posix_spawn_file_actions_destroy(&actions); |
| 109 | + |
| 110 | + // Close the write ends of the pipes. |
| 111 | + close(stdout_pipe[1]); |
| 112 | + close(stderr_pipe[1]); |
| 113 | + |
| 114 | + int wait_status; |
| 115 | + pid_t waited_pid; |
| 116 | + do { |
| 117 | + waited_pid = waitpid(pid, &wait_status, 0); |
| 118 | + } while (waited_pid == -1 && errno == EINTR); |
| 119 | + |
| 120 | + VerifyElseExit(waited_pid == pid); |
| 121 | + |
| 122 | + if (WIFEXITED(wait_status)) { |
| 123 | + result.status = WEXITSTATUS(wait_status); |
| 124 | + } else if (WIFSIGNALED(wait_status)) { |
| 125 | + result.status = 128 + WTERMSIG(wait_status); |
| 126 | + } else { |
| 127 | + result.status = 1; |
| 128 | + } |
| 129 | + result.std_output = ReadFromFd(stdout_pipe[0]); |
| 130 | + result.std_error = ReadFromFd(stderr_pipe[0]); |
| 131 | + |
| 132 | + // Close the read ends of the pipes. |
| 133 | + close(stdout_pipe[0]); |
| 134 | + close(stderr_pipe[0]); |
| 135 | + |
| 136 | + for (char* arg : argv) { |
| 137 | + free(arg); |
| 138 | + } |
| 139 | + |
| 140 | + return result; |
| 141 | +} |
| 142 | + |
| 143 | +namespace { |
| 144 | + |
| 145 | +std::string ReadFromFd(int fd) { |
| 146 | + std::string result; |
| 147 | + constexpr size_t bufferSize = 4096; |
| 148 | + char buffer[bufferSize]; |
| 149 | + ssize_t bytesRead; |
| 150 | + while (true) { |
| 151 | + bytesRead = read(fd, buffer, bufferSize); |
| 152 | + if (bytesRead > 0) { |
| 153 | + result.append(buffer, bytesRead); |
| 154 | + continue; |
| 155 | + } |
| 156 | + |
| 157 | + if (bytesRead == 0) { |
| 158 | + break; |
| 159 | + } |
| 160 | + |
| 161 | + if (errno == EINTR) { |
| 162 | + continue; |
| 163 | + } |
| 164 | + |
| 165 | + ExitOnError("read", nullptr); |
| 166 | + } |
| 167 | + return result; |
| 168 | +} |
| 169 | + |
| 170 | +// Format a readable error message, print it to console, and exit from the |
| 171 | +// application. |
| 172 | +void ExitOnError(const char* message, posix_spawn_file_actions_t* actions) { |
| 173 | + int err = errno; |
| 174 | + const char* err_msg = strerror(err); |
| 175 | + |
| 176 | + fprintf(stderr, "%s failed with error %d: %s\n", message, err, err_msg); |
| 177 | + |
| 178 | + if (actions != nullptr) { |
| 179 | + posix_spawn_file_actions_destroy(actions); |
| 180 | + } |
| 181 | + |
| 182 | + exit(1); |
| 183 | +} |
| 184 | + |
| 185 | +} // namespace |
| 186 | + |
| 187 | +} // namespace node_api_tests |
| 188 | + |
| 189 | +#endif // __ANDROID__ |
0 commit comments