summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2014-12-17 21:54:00 +0100
committerLennart Poettering <lennart@poettering.net>2014-12-18 01:36:28 +0100
commit20b63d12b533daf2e9b2936ffb03074861e1673e (patch)
tree8c21608e7d9e5a767da287b7fd04d7c432e1e0e4 /src
parent82e6c50c473f4be8df77c7a510577f1975eedddb (diff)
util: in make_stdio() use dup2() rather than dup3()
dup3() allows setting O_CLOEXEC which we are not interested in. However, it also fails if called with the same fd as input and output, which is something we don't want. Hence use dup2(). Also, we need to explicitly turn off O_CLOEXEC for the fds, in case the input fd was O_CLOEXEC and < 3.
Diffstat (limited to 'src')
-rw-r--r--src/shared/util.c12
1 files changed, 8 insertions, 4 deletions
diff --git a/src/shared/util.c b/src/shared/util.c
index ee95a4b6f7..364f61885b 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -2468,9 +2468,9 @@ int make_stdio(int fd) {
assert(fd >= 0);
- r = dup3(fd, STDIN_FILENO, 0);
- s = dup3(fd, STDOUT_FILENO, 0);
- t = dup3(fd, STDERR_FILENO, 0);
+ r = dup2(fd, STDIN_FILENO);
+ s = dup2(fd, STDOUT_FILENO);
+ t = dup2(fd, STDERR_FILENO);
if (fd >= 3)
safe_close(fd);
@@ -2478,7 +2478,11 @@ int make_stdio(int fd) {
if (r < 0 || s < 0 || t < 0)
return -errno;
- /* We rely here that the new fd has O_CLOEXEC not set */
+ /* Explicitly unset O_CLOEXEC, since if fd was < 3, then
+ * dup2() was a NOP and the bit hence possibly set. */
+ fd_cloexec(STDIN_FILENO, false);
+ fd_cloexec(STDOUT_FILENO, false);
+ fd_cloexec(STDERR_FILENO, false);
return 0;
}