Skip to content

Commit 5c737a7

Browse files
committed
fix(engine): reliably report POSIX child process errors
1 parent a71ab99 commit 5c737a7

1 file changed

Lines changed: 24 additions & 5 deletions

File tree

‎src/ProcessPosix.cpp‎

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <algorithm>
66
#include <cerrno>
7+
#include <cstddef>
78
#include <cstring>
89
#include <fcntl.h>
910
#include <map>
@@ -26,6 +27,24 @@ namespace vix::engine::process
2627
::close(fd);
2728
}
2829

30+
static void write_child_error_code_or_exit(int fd, int code) noexcept
31+
{
32+
const auto *bytes = reinterpret_cast<const unsigned char *>(&code);
33+
std::size_t offset = 0;
34+
while (offset < sizeof(code))
35+
{
36+
const ssize_t written = ::write(fd, bytes + offset, sizeof(code) - offset);
37+
if (written > 0)
38+
{
39+
offset += static_cast<std::size_t>(written);
40+
continue;
41+
}
42+
if (written < 0 && errno == EINTR)
43+
continue;
44+
_exit(127);
45+
}
46+
}
47+
2948
static bool set_close_on_exec(int fd) noexcept
3049
{
3150
const int flags = ::fcntl(fd, F_GETFD);
@@ -238,7 +257,7 @@ namespace vix::engine::process
238257
if (::dup2(stdoutPipe[1], STDOUT_FILENO) < 0)
239258
{
240259
const int code = errno;
241-
(void)::write(errorPipe[1], &code, sizeof(code));
260+
write_child_error_code_or_exit(errorPipe[1], code);
242261
_exit(127);
243262
}
244263

@@ -247,7 +266,7 @@ namespace vix::engine::process
247266
if (::dup2(stdoutPipe[1], STDERR_FILENO) < 0)
248267
{
249268
const int code = errno;
250-
(void)::write(errorPipe[1], &code, sizeof(code));
269+
write_child_error_code_or_exit(errorPipe[1], code);
251270
_exit(127);
252271
}
253272
}
@@ -257,7 +276,7 @@ namespace vix::engine::process
257276
if (::dup2(stderrPipe[1], STDERR_FILENO) < 0)
258277
{
259278
const int code = errno;
260-
(void)::write(errorPipe[1], &code, sizeof(code));
279+
write_child_error_code_or_exit(errorPipe[1], code);
261280
_exit(127);
262281
}
263282
}
@@ -269,7 +288,7 @@ namespace vix::engine::process
269288
::chdir(command.workingDirectory.c_str()) != 0)
270289
{
271290
const int code = errno;
272-
(void)::write(errorPipe[1], &code, sizeof(code));
291+
write_child_error_code_or_exit(errorPipe[1], code);
273292
_exit(127);
274293
}
275294

@@ -296,7 +315,7 @@ namespace vix::engine::process
296315
}
297316

298317
const int code = errno;
299-
(void)::write(errorPipe[1], &code, sizeof(code));
318+
write_child_error_code_or_exit(errorPipe[1], code);
300319
_exit(127);
301320
}
302321

0 commit comments

Comments
 (0)