]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
errno-util: add new errno_or_else() helper
authorLennart Poettering <lennart@poettering.net>
Thu, 4 Jul 2019 15:55:36 +0000 (17:55 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 11 Jul 2019 21:20:31 +0000 (23:20 +0200)
src/basic/errno-util.h

index 3ad732b91ee2ea6ad8022491ebd1e178b5563e80..34859d6d8a970fa4c62bc6bbbaf557d894030eeb 100644 (file)
@@ -36,6 +36,17 @@ static inline char *strerror_safe(int error) {
         return strerror(abs(error));
 }
 
+static inline int errno_or_else(int fallback) {
+        /* To be used when invoking library calls where errno handling is not defined clearly: we return
+         * errno if it is set, and the specified error otherwise. The idea is that the caller initializes
+         * errno to zero before doing an API call, and then uses this helper to retrieve a somewhat useful
+         * error code */
+        if (errno > 0)
+                return -errno;
+
+        return -abs(fallback);
+}
+
 /* Hint #1: ENETUNREACH happens if we try to connect to "non-existing" special IP addresses, such as ::5.
  *
  * Hint #2: The kernel sends e.g., EHOSTUNREACH or ENONET to userspace in some ICMP error cases.  See the