tree-wide: always use TAKE_FD() when calling rearrange_stdio()

rearrange_stdio() invalidates specified fds even on failure, which means
we should always invalidate the fds we pass in no matter what. Let's
make this explicit by using TAKE_FD() for that everywhere.

Note that in many places we such invalidation doesnt get us much
behaviour-wise, since we don't use the variables anymore later. But
TAKE_FD() in a way is also documentation, it encodes explicitly that the
fds are invalidated here, so I think it's a good thing to always make
this explicit here.
This commit is contained in:
Lennart Poettering
2021-11-03 23:05:26 +00:00
committed by Luca Boccassi
parent 829b86bc0f
commit aedec452b9
11 changed files with 28 additions and 25 deletions
+11 -10
View File
@@ -756,12 +756,16 @@ static int chown_terminal(int fd, uid_t uid) {
return 1;
}
static int setup_confirm_stdio(const char *vc, int *_saved_stdin, int *_saved_stdout) {
static int setup_confirm_stdio(
const char *vc,
int *ret_saved_stdin,
int *ret_saved_stdout) {
_cleanup_close_ int fd = -1, saved_stdin = -1, saved_stdout = -1;
int r;
assert(_saved_stdin);
assert(_saved_stdout);
assert(ret_saved_stdin);
assert(ret_saved_stdout);
saved_stdin = fcntl(STDIN_FILENO, F_DUPFD, 3);
if (saved_stdin < 0)
@@ -783,16 +787,13 @@ static int setup_confirm_stdio(const char *vc, int *_saved_stdin, int *_saved_st
if (r < 0)
return r;
r = rearrange_stdio(fd, fd, STDERR_FILENO);
fd = -1;
r = rearrange_stdio(fd, fd, STDERR_FILENO); /* Invalidates 'fd' also on failure */
TAKE_FD(fd);
if (r < 0)
return r;
*_saved_stdin = saved_stdin;
*_saved_stdout = saved_stdout;
saved_stdin = saved_stdout = -1;
*ret_saved_stdin = TAKE_FD(saved_stdin);
*ret_saved_stdout = TAKE_FD(saved_stdout);
return 0;
}