From 64db44f7fb556dcf72f418ab4c62ce85271bf4d2 Mon Sep 17 00:00:00 2001 From: Ivan Kruglov Date: Tue, 7 Jan 2025 12:58:46 +0100 Subject: [PATCH] process-util: read_errno() --- src/basic/process-util.c | 23 +++++++++++++++++++++++ src/basic/process-util.h | 1 + 2 files changed, 24 insertions(+) diff --git a/src/basic/process-util.c b/src/basic/process-util.c index e3c696a3a6c..7a4d0b30e49 100644 --- a/src/basic/process-util.c +++ b/src/basic/process-util.c @@ -2215,3 +2215,26 @@ _noreturn_ void report_errno_and_exit(int errno_fd, int error) { _exit(EXIT_FAILURE); } + +int read_errno(int errno_fd) { + int r; + + assert(errno_fd >= 0); + + ssize_t n = loop_read(errno_fd, &r, sizeof(r), /* do_pool = */ 0); + if (n < 0) { + log_debug_errno(n, "Failed to read errno: %m"); + return -EIO; + } + if (n == sizeof(r)) { + /* child processes reported a error, return it */ + if (r < 0) + return log_debug_errno(r, "Child process failed with errno: %m"); + return log_debug_errno(SYNTHETIC_ERRNO(EIO), "Received a errno, but it's a positive value"); + } + if (n != 0) + return log_debug_errno(SYNTHETIC_ERRNO(EIO), "Received unexpected amount of bytes while reading errno"); + + /* the process exited without reporting a error, assuming success */ + return 0; +} diff --git a/src/basic/process-util.h b/src/basic/process-util.h index b436a7b1b31..8864472af64 100644 --- a/src/basic/process-util.h +++ b/src/basic/process-util.h @@ -269,3 +269,4 @@ int proc_dir_read(DIR *d, pid_t *ret); int proc_dir_read_pidref(DIR *d, PidRef *ret); _noreturn_ void report_errno_and_exit(int errno_fd, int error); +int read_errno(int errno_fd); -- 2.47.3