diff options
author | Lennart Poettering <lennart@poettering.net> | 2019-07-04 17:55:36 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2019-07-11 23:20:31 +0200 |
commit | fed813778fb89d2c7890845a2e35bca95d4897cf (patch) | |
tree | 8fe84cb4377ff0a617a7e4db5766ef77665546e4 /src/basic | |
parent | 4747b645c6690c058ca1c8353cd69d061f98cf4f (diff) | |
download | systemd-fed813778fb89d2c7890845a2e35bca95d4897cf.tar.gz |
errno-util: add new errno_or_else() helper
Diffstat (limited to 'src/basic')
-rw-r--r-- | src/basic/errno-util.h | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/src/basic/errno-util.h b/src/basic/errno-util.h index 3ad732b91e..34859d6d8a 100644 --- a/src/basic/errno-util.h +++ b/src/basic/errno-util.h @@ -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 |