Skip to content

Commit 69c38ec

Browse files
author
someguy
committed
Copy and use fd_nonblock and RET_NERRNO from systemd source.
1 parent a893462 commit 69c38ec

File tree

5 files changed

+33
-13
lines changed

5 files changed

+33
-13
lines changed

src/netlog/netlog-network.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ int manager_open_network_socket(Manager *m) {
220220
return r;
221221
}
222222

223-
r = fd_status_flag(m->socket, O_NONBLOCK, 1);
223+
r = fd_nonblock(m->socket, 1);
224224
if (r < 0)
225225
goto fail;
226226

src/share/fd-util.c

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ int fd_cloexec(int fd, bool cloexec) {
121121
return 0;
122122
}
123123

124-
int fd_status_flag(int fd, int flag, bool new_value) {
124+
int fd_nonblock(int fd, bool nonblock) {
125125
int flags, nflags;
126126

127127
assert(fd >= 0);
@@ -130,18 +130,11 @@ int fd_status_flag(int fd, int flag, bool new_value) {
130130
if (flags < 0)
131131
return -errno;
132132

133-
if (new_value)
134-
nflags = flags | flag;
135-
else
136-
nflags = flags & ~flag;
137-
133+
nflags = UPDATE_FLAG(flags, O_NONBLOCK, nonblock);
138134
if (nflags == flags)
139135
return 0;
140136

141-
if (fcntl(fd, F_SETFL, nflags) < 0)
142-
return -errno;
143-
144-
return 0;
137+
return RET_NERRNO(fcntl(fd, F_SETFL, nflags));
145138
}
146139

147140
void stdio_unset_cloexec(void) {

src/share/fd-util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(DIR*, closedir);
3838
#define _cleanup_close_pair_ _cleanup_(close_pairp)
3939

4040
int fd_cloexec(int fd, bool cloexec);
41-
int fd_status_flag(int fd, int flag, bool new_value);
41+
int fd_nonblock(int fd, bool nonblock);
4242
void stdio_unset_cloexec(void);
4343

4444

src/share/macro.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,12 @@ static inline unsigned long ALIGN_POWER2(unsigned long u) {
312312
ans; \
313313
})
314314

315+
#define UPDATE_FLAG(orig, flag, b) \
316+
((b) ? ((orig) | (flag)) : ((orig) & ~(flag)))
315317
#define SET_FLAG(v, flag, b) \
316-
(v) = (b) ? ((v) | (flag)) : ((v) & ~(flag))
318+
(v) = UPDATE_FLAG(v, flag, b)
319+
#define FLAGS_SET(v, flags) \
320+
((~(v) & (flags)) == 0)
317321

318322
#define CASE_F(X) case X:
319323
#define CASE_F_1(CASE, X) CASE_F(X)

src/share/util.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,29 @@ static inline int negative_errno(void) {
103103
return -errno;
104104
}
105105

106+
static inline int RET_NERRNO(int ret) {
107+
108+
/* Helper to wrap system calls in to make them return negative errno errors. This brings system call
109+
* error handling in sync with how we usually handle errors in our own code, i.e. with immediate
110+
* returning of negative errno. Usage is like this:
111+
*
112+
* …
113+
* r = RET_NERRNO(unlink(t));
114+
* …
115+
*
116+
* or
117+
*
118+
* …
119+
* fd = RET_NERRNO(open("/etc/fstab", O_RDONLY|O_CLOEXEC));
120+
* …
121+
*/
122+
123+
if (ret < 0)
124+
return negative_errno();
125+
126+
return ret;
127+
}
128+
106129
static inline unsigned u64log2(uint64_t n) {
107130
#if __SIZEOF_LONG_LONG__ == 8
108131
return (n > 1) ? (unsigned) __builtin_clzll(n) ^ 63U : 0;

0 commit comments

Comments
 (0)