Q_Q(QProcess);
// copy the stdin socket (without closing on exec)
- qt_safe_dup2(stdinChannel.pipe[0], STDIN_FILENO, 0);
+ qt_safe_dup2(stdinChannel.pipe[0], STDIN_FILENO);
// copy the stdout and stderr if asked to
if (processChannelMode != QProcess::ForwardedChannels) {
- qt_safe_dup2(stdoutChannel.pipe[1], STDOUT_FILENO, 0);
+ qt_safe_dup2(stdoutChannel.pipe[1], STDOUT_FILENO);
// merge stdout and stderr if asked to
if (processChannelMode == QProcess::MergedChannels) {
- qt_safe_dup2(STDOUT_FILENO, STDERR_FILENO, 0);
+ qt_safe_dup2(STDOUT_FILENO, STDERR_FILENO);
} else {
- qt_safe_dup2(stderrChannel.pipe[1], STDERR_FILENO, 0);
+ qt_safe_dup2(stderrChannel.pipe[1], STDERR_FILENO);
}
}
}
// don't call dup or fcntl(F_DUPFD)
-static inline int qt_safe_dup(int oldfd, int atleast = 0, int flags = FD_CLOEXEC)
+static inline int qt_safe_dup(int oldfd)
{
- Q_ASSERT(flags == FD_CLOEXEC || flags == 0);
-
int ret;
#ifdef F_DUPFD_CLOEXEC
// use this fcntl
- if (flags & FD_CLOEXEC) {
- ret = ::fcntl(oldfd, F_DUPFD_CLOEXEC, atleast);
- if (ret != -1 || errno != EINVAL)
- return ret;
- }
+ ret = ::fcntl(oldfd, F_DUPFD_CLOEXEC, 0);
+ if (ret != -1 || errno != EINVAL)
+ return ret;
#endif
// use F_DUPFD
- ret = ::fcntl(oldfd, F_DUPFD, atleast);
+ ret = ::fcntl(oldfd, F_DUPFD, 0);
- if (flags && ret != -1)
- ::fcntl(ret, F_SETFD, flags);
+ if (ret != -1)
+ ::fcntl(ret, F_SETFD, FD_CLOEXEC);
return ret;
}
// don't call dup2
// call qt_safe_dup2
-static inline int qt_safe_dup2(int oldfd, int newfd, int flags = FD_CLOEXEC)
+static inline int qt_safe_dup2(int oldfd, int newfd)
{
- Q_ASSERT(flags == FD_CLOEXEC || flags == 0);
-
int ret;
-#if defined(Q_OS_LINUX) && defined(O_CLOEXEC)
- // use dup3
- if (flags & FD_CLOEXEC) {
- EINTR_LOOP(ret, ::dup3(oldfd, newfd, O_CLOEXEC));
- if (ret == 0 || errno != ENOSYS)
- return ret;
- }
-#endif
EINTR_LOOP(ret, ::dup2(oldfd, newfd));
if (ret == -1)
return -1;
-
- if (flags)
- ::fcntl(newfd, F_SETFD, flags);
return 0;
}