X Tutup
Skip to content

Commit ef470ff

Browse files
poetteringkeszybz
authored andcommitted
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: systemd#21326 (comment)
1 parent cb3763d commit ef470ff

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/basic/errno-util.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,29 @@ static inline int negative_errno(void) {
3131
return -errno;
3232
}
3333

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+
3457
static inline const char *strerror_safe(int error) {
3558
/* 'safe' here does NOT mean thread safety. */
3659
return strerror(abs(error)); /* lgtm [cpp/potentially-dangerous-function] */

0 commit comments

Comments
 (0)
X Tutup