Skip to content

Commit 2b2fec7

Browse files
committed
util: split out errno related stuff
1 parent e56f9ff commit 2b2fec7

File tree

20 files changed

+48
-34
lines changed

20 files changed

+48
-34
lines changed

src/activate/activate.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "sd-daemon.h"
1111

1212
#include "alloc-util.h"
13+
#include "errno-util.h"
1314
#include "escape.h"
1415
#include "fd-util.h"
1516
#include "log.h"

src/basic/async.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <unistd.h>
77

88
#include "async.h"
9+
#include "errno-util.h"
910
#include "fd-util.h"
1011
#include "log.h"
1112
#include "macro.h"

src/basic/errno-util.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* SPDX-License-Identifier: LGPL-2.1+ */
2+
#pragma once
3+
4+
#include "macro.h"
5+
6+
static inline void _reset_errno_(int *saved_errno) {
7+
if (*saved_errno < 0) /* Invalidated by UNPROTECT_ERRNO? */
8+
return;
9+
10+
errno = *saved_errno;
11+
}
12+
13+
#define PROTECT_ERRNO \
14+
_cleanup_(_reset_errno_) _unused_ int _saved_errno_ = errno
15+
16+
#define UNPROTECT_ERRNO \
17+
do { \
18+
errno = _saved_errno_; \
19+
_saved_errno_ = -1; \
20+
} while (false)
21+
22+
static inline int negative_errno(void) {
23+
/* This helper should be used to shut up gcc if you know 'errno' is
24+
* negative. Instead of "return -errno;", use "return negative_errno();"
25+
* It will suppress bogus gcc warnings in case it assumes 'errno' might
26+
* be 0 and thus the caller's error-handling might not be triggered. */
27+
assert_return(errno > 0, -EINVAL);
28+
return -errno;
29+
}

src/basic/fs-util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
#include <sys/types.h>
1111
#include <unistd.h>
1212

13+
#include "errno-util.h"
1314
#include "time-util.h"
14-
#include "util.h"
1515

1616
int unlink_noerrno(const char *path);
1717

src/basic/log.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "sd-messages.h"
2020

2121
#include "alloc-util.h"
22+
#include "errno-util.h"
2223
#include "fd-util.h"
2324
#include "format-util.h"
2425
#include "io-util.h"
@@ -37,7 +38,6 @@
3738
#include "terminal-util.h"
3839
#include "time-util.h"
3940
#include "utf8.h"
40-
#include "util.h"
4141

4242
#define SNDBUF_SIZE (8*1024*1024)
4343

src/basic/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ basic_sources = files('''
4545
env-util.h
4646
errno-list.c
4747
errno-list.h
48+
errno-util.h
4849
escape.c
4950
escape.h
5051
ether-addr-util.c

src/basic/rm-rf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include <sys/stat.h>
55

6-
#include "util.h"
6+
#include "errno-util.h"
77

88
typedef enum RemoveFlags {
99
REMOVE_ONLY_DIRECTORIES = 1 << 0,

src/basic/selinux-util.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
#endif
1717

1818
#include "alloc-util.h"
19+
#include "errno-util.h"
1920
#include "fd-util.h"
2021
#include "log.h"
2122
#include "macro.h"
2223
#include "path-util.h"
2324
#include "selinux-util.h"
2425
#include "stdio-util.h"
2526
#include "time-util.h"
26-
#include "util.h"
2727

2828
#if HAVE_SELINUX
2929
DEFINE_TRIVIAL_CLEANUP_FUNC(char*, freecon);

src/basic/util.h

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -63,31 +63,6 @@ void in_initrd_force(bool value);
6363

6464
int on_ac_power(void);
6565

66-
static inline void _reset_errno_(int *saved_errno) {
67-
if (*saved_errno < 0) /* Invalidated by UNPROTECT_ERRNO? */
68-
return;
69-
70-
errno = *saved_errno;
71-
}
72-
73-
#define PROTECT_ERRNO \
74-
_cleanup_(_reset_errno_) _unused_ int _saved_errno_ = errno
75-
76-
#define UNPROTECT_ERRNO \
77-
do { \
78-
errno = _saved_errno_; \
79-
_saved_errno_ = -1; \
80-
} while (false)
81-
82-
static inline int negative_errno(void) {
83-
/* This helper should be used to shut up gcc if you know 'errno' is
84-
* negative. Instead of "return -errno;", use "return negative_errno();"
85-
* It will suppress bogus gcc warnings in case it assumes 'errno' might
86-
* be 0 and thus the caller's error-handling might not be triggered. */
87-
assert_return(errno > 0, -EINVAL);
88-
return -errno;
89-
}
90-
9166
static inline unsigned u64log2(uint64_t n) {
9267
#if __SIZEOF_LONG_LONG__ == 8
9368
return (n > 1) ? (unsigned) __builtin_clzll(n) ^ 63U : 0;

src/journal/journal-send.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
#include "sd-journal.h"
1414

1515
#include "alloc-util.h"
16+
#include "errno-util.h"
1617
#include "fd-util.h"
1718
#include "io-util.h"
1819
#include "memfd-util.h"
1920
#include "socket-util.h"
2021
#include "stdio-util.h"
2122
#include "string-util.h"
2223
#include "tmpfile-util.h"
23-
#include "util.h"
2424

2525
#define SNDBUF_SIZE (8*1024*1024)
2626

0 commit comments

Comments
 (0)