summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2021-11-14 22:31:27 +0100
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2021-11-16 08:04:09 +0100
commitef470ffa234c8c824897488195fb2fc9a4a520e6 (patch)
tree8228fb8b72994716f8bbf86d8ea543cbd6d0f484 /src
parentcb3763d57ba9a7e89d4accbf0aff074c294d29be (diff)
downloadsystemd-ef470ffa234c8c824897488195fb2fc9a4a520e6.tar.gz
macro: add new helper RET_NERRNO()
This new helper converts libc style syscall return values into systemd-kernel (actually: kernel style) negative errno values. It's implemented as macro-like inline function, and propagates return values >= 0 as themselves and returns -errno for negative error returns. THis is supposed to be little more than syntactic sugar so that we can reduce a lot of (short, but still) boilerplate code whever we convert libc style error handling into our own. As discussed here: https://github.com/systemd/systemd/pull/21326#discussion_r748413537
Diffstat (limited to 'src')
-rw-r--r--src/basic/errno-util.h23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/basic/errno-util.h b/src/basic/errno-util.h
index 3f2d0af56d..6d79723b39 100644
--- a/src/basic/errno-util.h
+++ b/src/basic/errno-util.h
@@ -31,6 +31,29 @@ static inline int negative_errno(void) {
return -errno;
}
+static inline int RET_NERRNO(int ret) {
+
+ /* Helper to wrap system calls in to make them return negative errno errors. This brings system call
+ * error handling in sync with how we usually handle errors in our own code, i.e. with immediate
+ * returning of negative errno. Usage is like this:
+ *
+ * …
+ * r = RET_NERRNO(unlink(t));
+ * …
+ *
+ * or
+ *
+ * …
+ * fd = RET_NERRNO(open("/etc/fstab", O_RDONLY|O_CLOEXEC));
+ * …
+ */
+
+ if (ret < 0)
+ return negative_errno();
+
+ return ret;
+}
+
static inline const char *strerror_safe(int error) {
/* 'safe' here does NOT mean thread safety. */
return strerror(abs(error)); /* lgtm [cpp/potentially-dangerous-function] */