udev: String substitutions can be done in ENV, too
[systemd_ALT.git] / src / basic / errno-util.h
blob091f99c5902ad644f3264513384e05a4d310f8ef
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
4 #include <stdlib.h>
5 #include <string.h>
7 #include "macro.h"
9 /* strerror(3) says that glibc uses a maximum length of 1024 bytes. */
10 #define ERRNO_BUF_LEN 1024
12 /* Note: the lifetime of the compound literal is the immediately surrounding block,
13 * see C11 §6.5.2.5, and
14 * https://stackoverflow.com/questions/34880638/compound-literal-lifetime-and-if-blocks
16 * Note that we use the GNU variant of strerror_r() here. */
17 #define STRERROR(errnum) strerror_r(abs(errnum), (char[ERRNO_BUF_LEN]){}, ERRNO_BUF_LEN)
19 /* A helper to print an error message or message for functions that return 0 on EOF.
20 * Note that we can't use ({ … }) to define a temporary variable, so errnum is
21 * evaluated twice. */
22 #define STRERROR_OR_EOF(errnum) ((errnum) != 0 ? STRERROR(errnum) : "Unexpected EOF")
24 static inline void _reset_errno_(int *saved_errno) {
25 if (*saved_errno < 0) /* Invalidated by UNPROTECT_ERRNO? */
26 return;
28 errno = *saved_errno;
31 #define PROTECT_ERRNO \
32 _cleanup_(_reset_errno_) _unused_ int _saved_errno_ = errno
34 #define UNPROTECT_ERRNO \
35 do { \
36 errno = _saved_errno_; \
37 _saved_errno_ = -1; \
38 } while (false)
40 #define LOCAL_ERRNO(value) \
41 PROTECT_ERRNO; \
42 errno = abs(value)
44 static inline int negative_errno(void) {
45 /* This helper should be used to shut up gcc if you know 'errno' is
46 * negative. Instead of "return -errno;", use "return negative_errno();"
47 * It will suppress bogus gcc warnings in case it assumes 'errno' might
48 * be 0 and thus the caller's error-handling might not be triggered. */
49 assert_return(errno > 0, -EINVAL);
50 return -errno;
53 static inline int RET_NERRNO(int ret) {
55 /* Helper to wrap system calls in to make them return negative errno errors. This brings system call
56 * error handling in sync with how we usually handle errors in our own code, i.e. with immediate
57 * returning of negative errno. Usage is like this:
59 * …
60 * r = RET_NERRNO(unlink(t));
61 * …
63 * or
65 * …
66 * fd = RET_NERRNO(open("/etc/fstab", O_RDONLY|O_CLOEXEC));
67 * …
70 if (ret < 0)
71 return negative_errno();
73 return ret;
76 static inline int errno_or_else(int fallback) {
77 /* To be used when invoking library calls where errno handling is not defined clearly: we return
78 * errno if it is set, and the specified error otherwise. The idea is that the caller initializes
79 * errno to zero before doing an API call, and then uses this helper to retrieve a somewhat useful
80 * error code */
81 if (errno > 0)
82 return -errno;
84 return -abs(fallback);
87 /* For send()/recv() or read()/write(). */
88 static inline bool ERRNO_IS_TRANSIENT(int r) {
89 return IN_SET(abs(r),
90 EAGAIN,
91 EINTR);
94 /* Hint #1: ENETUNREACH happens if we try to connect to "non-existing" special IP addresses, such as ::5.
96 * Hint #2: The kernel sends e.g., EHOSTUNREACH or ENONET to userspace in some ICMP error cases. See the
97 * icmp_err_convert[] in net/ipv4/icmp.c in the kernel sources.
99 * Hint #3: When asynchronous connect() on TCP fails because the host never acknowledges a single packet,
100 * kernel tells us that with ETIMEDOUT, see tcp(7). */
101 static inline bool ERRNO_IS_DISCONNECT(int r) {
102 return IN_SET(abs(r),
103 ECONNABORTED,
104 ECONNREFUSED,
105 ECONNRESET,
106 EHOSTDOWN,
107 EHOSTUNREACH,
108 ENETDOWN,
109 ENETRESET,
110 ENETUNREACH,
111 ENONET,
112 ENOPROTOOPT,
113 ENOTCONN,
114 EPIPE,
115 EPROTO,
116 ESHUTDOWN,
117 ETIMEDOUT);
120 /* Transient errors we might get on accept() that we should ignore. As per error handling comment in
121 * the accept(2) man page. */
122 static inline bool ERRNO_IS_ACCEPT_AGAIN(int r) {
123 return ERRNO_IS_DISCONNECT(r) ||
124 ERRNO_IS_TRANSIENT(r) ||
125 abs(r) == EOPNOTSUPP;
128 /* Resource exhaustion, could be our fault or general system trouble */
129 static inline bool ERRNO_IS_RESOURCE(int r) {
130 return IN_SET(abs(r),
131 EMFILE,
132 ENFILE,
133 ENOMEM);
136 /* Seven different errors for "operation/system call/ioctl/socket feature not supported" */
137 static inline bool ERRNO_IS_NOT_SUPPORTED(int r) {
138 return IN_SET(abs(r),
139 EOPNOTSUPP,
140 ENOTTY,
141 ENOSYS,
142 EAFNOSUPPORT,
143 EPFNOSUPPORT,
144 EPROTONOSUPPORT,
145 ESOCKTNOSUPPORT);
148 /* Two different errors for access problems */
149 static inline bool ERRNO_IS_PRIVILEGE(int r) {
150 return IN_SET(abs(r),
151 EACCES,
152 EPERM);
155 /* Three different errors for "not enough disk space" */
156 static inline bool ERRNO_IS_DISK_SPACE(int r) {
157 return IN_SET(abs(r),
158 ENOSPC,
159 EDQUOT,
160 EFBIG);
163 /* Three different errors for "this device does not quite exist" */
164 static inline bool ERRNO_IS_DEVICE_ABSENT(int r) {
165 return IN_SET(abs(r),
166 ENODEV,
167 ENXIO,
168 ENOENT);
171 /* Quite often we want to handle cases where the backing FS doesn't support extended attributes at all and
172 * where it simply doesn't have the requested xattr the same way */
173 static inline bool ERRNO_IS_XATTR_ABSENT(int r) {
174 return abs(r) == ENODATA ||
175 ERRNO_IS_NOT_SUPPORTED(r);