util: add new safe_close_above_stdio() wrapper

At various places we only want to close fds if they are not
stdin/stdout/stderr, i.e. fds 0, 1, 2. Let's add a unified helper call
for that, and port everything over.
This commit is contained in:
Lennart Poettering
2018-02-28 10:00:50 +01:00
parent c7f9a8d270
commit e7685a77b4
7 changed files with 50 additions and 21 deletions
+3 -6
View File
@@ -57,10 +57,8 @@ static int spawn_getent(const char *database, const char *key, pid_t *rpid) {
if (dup3(pipe_fds[1], STDOUT_FILENO, 0) < 0)
_exit(EXIT_FAILURE);
if (pipe_fds[0] > 2)
safe_close(pipe_fds[0]);
if (pipe_fds[1] > 2)
safe_close(pipe_fds[1]);
safe_close_above_stdio(pipe_fds[0]);
safe_close_above_stdio(pipe_fds[1]);
nullfd = open("/dev/null", O_RDWR);
if (nullfd < 0)
@@ -72,8 +70,7 @@ static int spawn_getent(const char *database, const char *key, pid_t *rpid) {
if (dup3(nullfd, STDERR_FILENO, 0) < 0)
_exit(EXIT_FAILURE);
if (nullfd > 2)
safe_close(nullfd);
safe_close_above_stdio(nullfd);
close_all_fds(NULL, 0);