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:
+2
-8
@@ -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:
|
||||
|
||||
@@ -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
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user