diff --git a/src/conmon.c b/src/conmon.c index 2338b713..9365c154 100644 --- a/src/conmon.c +++ b/src/conmon.c @@ -208,7 +208,7 @@ int main(int argc, char *argv[]) if (opt_attach) { ndebug("sending attach message to parent"); - write_sync_fd(attach_pipe_fd, 0, NULL); + write_or_close_sync_fd(&attach_pipe_fd, 0, NULL); ndebug("sent attach message to parent"); } } @@ -380,7 +380,7 @@ int main(int argc, char *argv[]) if (opt_exec && container_status > 0) { to_report = -1 * container_status; } - write_sync_fd(sync_pipe_fd, to_report, buf); + write_or_close_sync_fd(&sync_pipe_fd, to_report, buf); } } nexitf("Failed to create container: exit status %d", get_exit_status(runtime_status)); @@ -407,7 +407,7 @@ int main(int argc, char *argv[]) * Thus, if we are legacy and we are exec, skip this write. */ if ((opt_api_version >= 1 || !opt_exec) && sync_pipe_fd >= 0) - write_sync_fd(sync_pipe_fd, container_pid, NULL); + write_or_close_sync_fd(&sync_pipe_fd, container_pid, NULL); #ifdef __linux__ setup_oom_handling(container_pid); @@ -527,7 +527,7 @@ int main(int argc, char *argv[]) /* Send the command exec exit code back to the parent */ if (opt_exec && sync_pipe_fd >= 0) - write_sync_fd(sync_pipe_fd, exit_status, exit_message); + write_or_close_sync_fd(&sync_pipe_fd, exit_status, exit_message); if (attach_symlink_dir_path != NULL && unlink(attach_symlink_dir_path) == -1 && errno != ENOENT) pexit("Failed to remove symlink for attach socket directory"); diff --git a/src/parent_pipe_fd.c b/src/parent_pipe_fd.c index 4fe6dda3..40e5c2fc 100644 --- a/src/parent_pipe_fd.c +++ b/src/parent_pipe_fd.c @@ -26,7 +26,8 @@ int get_pipe_fd_from_env(const char *envname) return pipe_fd; } -void write_sync_fd(int fd, int res, const char *message) +// Write a message to the sync pipe, or close the file descriptor if it's a broken pipe. +void write_or_close_sync_fd(int *fd, int res, const char *message) { const char *res_key; if (opt_api_version >= 1) @@ -38,7 +39,7 @@ void write_sync_fd(int fd, int res, const char *message) ssize_t len; - if (fd == -1) + if (*fd == -1) return; _cleanup_free_ char *json = NULL; @@ -50,7 +51,12 @@ void write_sync_fd(int fd, int res, const char *message) } len = strlen(json); - if (write_all(fd, json, len) != len) { + if (write_all(*fd, json, len) != len) { + if (errno == EPIPE) { + close(*fd); + *fd = -1; + return; + } pexit("Unable to send container stderr message to parent"); } } diff --git a/src/parent_pipe_fd.h b/src/parent_pipe_fd.h index 87c6357e..944edb76 100644 --- a/src/parent_pipe_fd.h +++ b/src/parent_pipe_fd.h @@ -2,7 +2,7 @@ #define PARENT_PIPE_FD_H -void write_sync_fd(int fd, int res, const char *message); +void write_or_close_sync_fd(int *fd, int res, const char *message); int get_pipe_fd_from_env(const char *envname); extern int sync_pipe_fd;