diff options
author | Joerg Bornemann <joerg.bornemann@qt.io> | 2017-05-30 13:53:33 +0200 |
---|---|---|
committer | Joerg Bornemann <joerg.bornemann@qt.io> | 2017-06-13 16:49:38 +0000 |
commit | 7ad55ca65f42351e231f31f7a9253ae6eaf1ebb3 (patch) | |
tree | 580e4f422f13456c5019449e342b664e917e3afd /src/corelib/io/qprocess_unix.cpp | |
parent | 424d9e9e56314bae09a0dcbf09be0e3c0c9e0ac6 (diff) |
Support standard channel redirection in QProcess::startDetached
[ChangeLog][QtCore][QProcess] Added support for standard channel
redirection using setStandard{Input|Output|Error}File to
QProcess::startDetached.
Task-number: QTBUG-2058
Task-number: QTBUG-37656
Change-Id: Iafb9bd7899f752d0305e3410ad4dcb7ef598dc79
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/io/qprocess_unix.cpp')
-rw-r--r-- | src/corelib/io/qprocess_unix.cpp | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/corelib/io/qprocess_unix.cpp b/src/corelib/io/qprocess_unix.cpp index 251f68c9b4..2b47f3e5b1 100644 --- a/src/corelib/io/qprocess_unix.cpp +++ b/src/corelib/io/qprocess_unix.cpp @@ -915,6 +915,17 @@ bool QProcessPrivate::startDetached(qint64 *pid) return false; } + if ((stdinChannel.type == Channel::Redirect && !openChannel(stdinChannel)) + || (stdoutChannel.type == Channel::Redirect && !openChannel(stdoutChannel)) + || (stderrChannel.type == Channel::Redirect && !openChannel(stderrChannel))) { + closeChannel(&stdinChannel); + closeChannel(&stdoutChannel); + closeChannel(&stderrChannel); + qt_safe_close(startedPipe[0]); + qt_safe_close(startedPipe[1]); + return false; + } + pid_t childPid = fork(); if (childPid == 0) { struct sigaction noaction; @@ -931,6 +942,18 @@ bool QProcessPrivate::startDetached(qint64 *pid) if (doubleForkPid == 0) { qt_safe_close(pidPipe[1]); + // copy the stdin socket if asked to (without closing on exec) + if (inputChannelMode != QProcess::ForwardedInputChannel) + qt_safe_dup2(stdinChannel.pipe[0], STDIN_FILENO, 0); + + // copy the stdout and stderr if asked to + if (processChannelMode != QProcess::ForwardedChannels) { + if (processChannelMode != QProcess::ForwardedOutputChannel) + qt_safe_dup2(stdoutChannel.pipe[1], STDOUT_FILENO, 0); + if (processChannelMode != QProcess::ForwardedErrorChannel) + qt_safe_dup2(stderrChannel.pipe[1], STDERR_FILENO, 0); + } + if (!encodedWorkingDirectory.isEmpty()) { if (QT_CHDIR(encodedWorkingDirectory.constData()) == -1) qWarning("QProcessPrivate::startDetached: failed to chdir to %s", encodedWorkingDirectory.constData()); @@ -992,6 +1015,9 @@ bool QProcessPrivate::startDetached(qint64 *pid) ::_exit(1); } + closeChannel(&stdinChannel); + closeChannel(&stdoutChannel); + closeChannel(&stderrChannel); qt_safe_close(startedPipe[1]); qt_safe_close(pidPipe[1]); |