]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
process-util: read_errno()
authorIvan Kruglov <mail@ikruglov.com>
Tue, 7 Jan 2025 11:58:46 +0000 (12:58 +0100)
committerIvan Kruglov <mail@ikruglov.com>
Thu, 9 Jan 2025 09:47:24 +0000 (10:47 +0100)
src/basic/process-util.c
src/basic/process-util.h

index e3c696a3a6c7d398fbc380525cdaa5df70a6b6c4..7a4d0b30e49f7065c957e8902e9dd7336fbe71c6 100644 (file)
@@ -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;
+}
index b436a7b1b31d940ee35327034cce30227d33f237..8864472af64440e8e560e993d6e52f37776d9c9f 100644 (file)
@@ -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);