1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include <stdlib.h>
5 #include <string.h>
6
7 #include "macro.h"
8
_reset_errno_(int * saved_errno)9 static inline void _reset_errno_(int *saved_errno) {
10 if (*saved_errno < 0) /* Invalidated by UNPROTECT_ERRNO? */
11 return;
12
13 errno = *saved_errno;
14 }
15
16 #define PROTECT_ERRNO \
17 _cleanup_(_reset_errno_) _unused_ int _saved_errno_ = errno
18
19 #define UNPROTECT_ERRNO \
20 do { \
21 errno = _saved_errno_; \
22 _saved_errno_ = -1; \
23 } while (false)
24
negative_errno(void)25 static inline int negative_errno(void) {
26 /* This helper should be used to shut up gcc if you know 'errno' is
27 * negative. Instead of "return -errno;", use "return negative_errno();"
28 * It will suppress bogus gcc warnings in case it assumes 'errno' might
29 * be 0 and thus the caller's error-handling might not be triggered. */
30 assert_return(errno > 0, -EINVAL);
31 return -errno;
32 }
33
RET_NERRNO(int ret)34 static inline int RET_NERRNO(int ret) {
35
36 /* Helper to wrap system calls in to make them return negative errno errors. This brings system call
37 * error handling in sync with how we usually handle errors in our own code, i.e. with immediate
38 * returning of negative errno. Usage is like this:
39 *
40 * …
41 * r = RET_NERRNO(unlink(t));
42 * …
43 *
44 * or
45 *
46 * …
47 * fd = RET_NERRNO(open("/etc/fstab", O_RDONLY|O_CLOEXEC));
48 * …
49 */
50
51 if (ret < 0)
52 return negative_errno();
53
54 return ret;
55 }
56
strerror_safe(int error)57 static inline const char *strerror_safe(int error) {
58 /* 'safe' here does NOT mean thread safety. */
59 return strerror(abs(error)); /* lgtm [cpp/potentially-dangerous-function] */
60 }
61
errno_or_else(int fallback)62 static inline int errno_or_else(int fallback) {
63 /* To be used when invoking library calls where errno handling is not defined clearly: we return
64 * errno if it is set, and the specified error otherwise. The idea is that the caller initializes
65 * errno to zero before doing an API call, and then uses this helper to retrieve a somewhat useful
66 * error code */
67 if (errno > 0)
68 return -errno;
69
70 return -abs(fallback);
71 }
72
73 /* For send()/recv() or read()/write(). */
ERRNO_IS_TRANSIENT(int r)74 static inline bool ERRNO_IS_TRANSIENT(int r) {
75 return IN_SET(abs(r),
76 EAGAIN,
77 EINTR);
78 }
79
80 /* Hint #1: ENETUNREACH happens if we try to connect to "non-existing" special IP addresses, such as ::5.
81 *
82 * Hint #2: The kernel sends e.g., EHOSTUNREACH or ENONET to userspace in some ICMP error cases. See the
83 * icmp_err_convert[] in net/ipv4/icmp.c in the kernel sources.
84 *
85 * Hint #3: When asynchronous connect() on TCP fails because the host never acknowledges a single packet,
86 * kernel tells us that with ETIMEDOUT, see tcp(7). */
ERRNO_IS_DISCONNECT(int r)87 static inline bool ERRNO_IS_DISCONNECT(int r) {
88 return IN_SET(abs(r),
89 ECONNABORTED,
90 ECONNREFUSED,
91 ECONNRESET,
92 EHOSTDOWN,
93 EHOSTUNREACH,
94 ENETDOWN,
95 ENETRESET,
96 ENETUNREACH,
97 ENONET,
98 ENOPROTOOPT,
99 ENOTCONN,
100 EPIPE,
101 EPROTO,
102 ESHUTDOWN,
103 ETIMEDOUT);
104 }
105
106 /* Transient errors we might get on accept() that we should ignore. As per error handling comment in
107 * the accept(2) man page. */
ERRNO_IS_ACCEPT_AGAIN(int r)108 static inline bool ERRNO_IS_ACCEPT_AGAIN(int r) {
109 return ERRNO_IS_DISCONNECT(r) ||
110 ERRNO_IS_TRANSIENT(r) ||
111 abs(r) == EOPNOTSUPP;
112 }
113
114 /* Resource exhaustion, could be our fault or general system trouble */
ERRNO_IS_RESOURCE(int r)115 static inline bool ERRNO_IS_RESOURCE(int r) {
116 return IN_SET(abs(r),
117 EMFILE,
118 ENFILE,
119 ENOMEM);
120 }
121
122 /* Seven different errors for "operation/system call/ioctl/socket feature not supported" */
ERRNO_IS_NOT_SUPPORTED(int r)123 static inline bool ERRNO_IS_NOT_SUPPORTED(int r) {
124 return IN_SET(abs(r),
125 EOPNOTSUPP,
126 ENOTTY,
127 ENOSYS,
128 EAFNOSUPPORT,
129 EPFNOSUPPORT,
130 EPROTONOSUPPORT,
131 ESOCKTNOSUPPORT);
132 }
133
134 /* Two different errors for access problems */
ERRNO_IS_PRIVILEGE(int r)135 static inline bool ERRNO_IS_PRIVILEGE(int r) {
136 return IN_SET(abs(r),
137 EACCES,
138 EPERM);
139 }
140
141 /* Three different errors for "not enough disk space" */
ERRNO_IS_DISK_SPACE(int r)142 static inline bool ERRNO_IS_DISK_SPACE(int r) {
143 return IN_SET(abs(r),
144 ENOSPC,
145 EDQUOT,
146 EFBIG);
147 }
148
149 /* Three different errors for "this device does not quite exist" */
ERRNO_IS_DEVICE_ABSENT(int r)150 static inline bool ERRNO_IS_DEVICE_ABSENT(int r) {
151 return IN_SET(abs(r),
152 ENODEV,
153 ENXIO,
154 ENOENT);
155 }
156