macro: introduce new TAKE_FD() macro

This is similar to TAKE_PTR() but operates on file descriptors, and thus
assigns -1 to the fd parameter after returning it.

Removes 60 lines from our codebase. Pretty good too I think.
This commit is contained in:
Lennart Poettering
2018-03-22 20:30:40 +01:00
parent 2f4cefe6ce
commit c10d6bdb89
25 changed files with 64 additions and 130 deletions
+2 -8
View File
@@ -484,10 +484,7 @@ int acquire_data_fd(const void *data, size_t size, unsigned flags) {
if (r < 0)
return r;
r = fd;
fd = -1;
return r;
return TAKE_FD(fd);
}
try_pipe:
@@ -524,10 +521,7 @@ try_pipe:
(void) fd_nonblock(pipefds[0], false);
r = pipefds[0];
pipefds[0] = -1;
return r;
return TAKE_FD(pipefds[0]);
}
try_dev_shm:
+8
View File
@@ -105,3 +105,11 @@ int rearrange_stdio(int original_input_fd, int original_output_fd, int original_
static inline int make_null_stdio(void) {
return rearrange_stdio(-1, -1, -1);
}
/* Like TAKE_PTR() but for file descriptors, resetting them to -1 */
#define TAKE_FD(fd) \
({ \
int _fd_ = (fd); \
(fd) = -1; \
_fd_; \
})
+3 -10
View File
@@ -730,8 +730,7 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags,
}
safe_close(fd);
fd = fd_parent;
fd_parent = -1;
fd = TAKE_FD(fd_parent);
continue;
}
@@ -849,8 +848,7 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags,
/* And iterate again, but go one directory further down. */
safe_close(fd);
fd = child;
child = -1;
fd = TAKE_FD(child);
}
if (!done) {
@@ -864,16 +862,11 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags,
*ret = TAKE_PTR(done);
if (flags & CHASE_OPEN) {
int q;
/* Return the O_PATH fd we currently are looking to the caller. It can translate it to a proper fd by
* opening /proc/self/fd/xyz. */
assert(fd >= 0);
q = fd;
fd = -1;
return q;
return TAKE_FD(fd);
}
return exists;
+1 -4
View File
@@ -168,8 +168,5 @@ int memfd_new_and_map(const char *name, size_t sz, void **p) {
if (r < 0)
return r;
r = fd;
fd = -1;
return r;
return TAKE_FD(fd);
}
+1 -4
View File
@@ -496,10 +496,7 @@ int acquire_terminal(
fd = safe_close(fd);
}
r = fd;
fd = -1;
return r;
return TAKE_FD(fd);
}
int release_terminal(void) {