diff options
author | Lennart Poettering <lennart@poettering.net> | 2017-10-26 18:45:54 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2017-11-17 11:13:44 +0100 |
commit | 046a82c1b20f765a8aa704ddea87e1b906d8ded0 (patch) | |
tree | 058313f8238fb73d6ab6baeb4a50d7c290410e44 /src/import/import-common.c | |
parent | 05d69e0294b6bbc7aa348d4fd09bfa847b6e178a (diff) | |
download | systemd-046a82c1b20f765a8aa704ddea87e1b906d8ded0.tar.gz |
fd-util: add new helper move_fd() and make use of it
We are using the same pattern at various places: call dup2() on an fd,
and close the old fd, usually in combination with some O_CLOEXEC
fiddling. Let's add a little helper for this, and port a few obvious
cases over.
Diffstat (limited to 'src/import/import-common.c')
-rw-r--r-- | src/import/import-common.c | 32 |
1 files changed, 12 insertions, 20 deletions
diff --git a/src/import/import-common.c b/src/import/import-common.c index ae71682988..900c0f9591 100644 --- a/src/import/import-common.c +++ b/src/import/import-common.c @@ -103,28 +103,24 @@ int import_fork_tar_x(const char *path, pid_t *ret) { pipefd[1] = safe_close(pipefd[1]); - if (dup2(pipefd[0], STDIN_FILENO) != STDIN_FILENO) { - log_error_errno(errno, "Failed to dup2() fd: %m"); + r = move_fd(pipefd[0], STDIN_FILENO, false); + if (r < 0) { + log_error_errno(r, "Failed to move fd: %m"); _exit(EXIT_FAILURE); } - if (pipefd[0] != STDIN_FILENO) - pipefd[0] = safe_close(pipefd[0]); - null_fd = open("/dev/null", O_WRONLY|O_NOCTTY); if (null_fd < 0) { log_error_errno(errno, "Failed to open /dev/null: %m"); _exit(EXIT_FAILURE); } - if (dup2(null_fd, STDOUT_FILENO) != STDOUT_FILENO) { - log_error_errno(errno, "Failed to dup2() fd: %m"); + r = move_fd(null_fd, STDOUT_FILENO, false); + if (r < 0) { + log_error_errno(r, "Failed to move fd: %m"); _exit(EXIT_FAILURE); } - if (null_fd != STDOUT_FILENO) - null_fd = safe_close(null_fd); - stdio_unset_cloexec(); if (unshare(CLONE_NEWNET) < 0) @@ -175,28 +171,24 @@ int import_fork_tar_c(const char *path, pid_t *ret) { pipefd[0] = safe_close(pipefd[0]); - if (dup2(pipefd[1], STDOUT_FILENO) != STDOUT_FILENO) { - log_error_errno(errno, "Failed to dup2() fd: %m"); + r = move_fd(pipefd[1], STDOUT_FILENO, false); + if (r < 0) { + log_error_errno(r, "Failed to move fd: %m"); _exit(EXIT_FAILURE); } - if (pipefd[1] != STDOUT_FILENO) - pipefd[1] = safe_close(pipefd[1]); - null_fd = open("/dev/null", O_RDONLY|O_NOCTTY); if (null_fd < 0) { log_error_errno(errno, "Failed to open /dev/null: %m"); _exit(EXIT_FAILURE); } - if (dup2(null_fd, STDIN_FILENO) != STDIN_FILENO) { - log_error_errno(errno, "Failed to dup2() fd: %m"); + r = move_fd(null_fd, STDIN_FILENO, false); + if (r < 0) { + log_error_errno(errno, "Failed to move fd: %m"); _exit(EXIT_FAILURE); } - if (null_fd != STDIN_FILENO) - null_fd = safe_close(null_fd); - stdio_unset_cloexec(); if (unshare(CLONE_NEWNET) < 0) |