Skip to content

Commit

Permalink
refactor: Enhance handling of sync pipes and broken pipes
Browse files Browse the repository at this point in the history
change the write_sync_fd function to handle broken pipes.  When a
broken pipe is detected, the file descriptor is closed and conmon
continues gracefully instead of exiting.

Closes: containers#438

Signed-off-by: Giuseppe Scrivano <[email protected]>
  • Loading branch information
giuseppe committed Aug 9, 2023
1 parent 75f8ceb commit 6e48ceb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/conmon.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
Expand Down Expand Up @@ -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));
Expand All @@ -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);
Expand Down Expand Up @@ -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");
Expand Down
12 changes: 9 additions & 3 deletions src/parent_pipe_fd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
Expand All @@ -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");
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/parent_pipe_fd.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 6e48ceb

Please sign in to comment.