From: Ivan Kruglov Date: Tue, 7 Jan 2025 11:58:46 +0000 (+0100) Subject: process-util: read_errno() X-Git-Tag: v258-rc1~1653^2~5 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=64db44f7fb556dcf72f418ab4c62ce85271bf4d2;p=thirdparty%2Fsystemd.git process-util: read_errno() --- 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);