diff options
| author | Thiago Macieira <thiago.macieira@intel.com> | 2023-02-28 10:21:05 -0800 |
|---|---|---|
| committer | Thiago Macieira <thiago.macieira@intel.com> | 2023-03-16 16:26:47 -0800 |
| commit | 82b75570f099911076ad0e144927862e8e359fbd (patch) | |
| tree | ea2b2355a62d8052dd94e71996fefe4a99c217d9 /src/corelib/io/qprocess.cpp | |
| parent | 96dc4acb235f13a72bef7c719d005846fe1d9726 (diff) | |
QProcess/Linux: fix file descriptor leak in case of failed child start
If the child failed to start, execChild() would (unnecessarily) store -1
in childStartedPipe[1] after writing the failure message to it and
closing that pipe. This had worked for the previous 20 years of QProcess
existence, because that was run in the child process. However, with 6.5
commit e1a787a76ed462e4ed49db78a40c6d7e272182d7 (cherry-picked to 6.4)
we implemented vfork-like behavior, meaning the child would share memory
with the parent and ran before the parent, so startProcess() failed to
close it:
// parent
// close the ends we don't use and make all pipes non-blocking
qt_safe_close(childStartedPipe[1]);
Also updated the docs to account for the fact that this is a vfork()
environment and, moreover, not using the vfork() function from glibc on
Linux.
[ChangeLog][QtCore][QProcess] Fixed a file descriptor leak in QProcess
when running on Linux 5.4 or later, if the executable being run failed
to start (for example, the file for the executable didn't exist).
Pick-to: 6.4.3 6.4 6.5
Task-number: QTBUG-111243
Change-Id: I7f354474adce419ca6c2fffd17481002e4853cc3
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@gmx.de>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/io/qprocess.cpp')
| -rw-r--r-- | src/corelib/io/qprocess.cpp | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/src/corelib/io/qprocess.cpp b/src/corelib/io/qprocess.cpp index 49ba04068a7..16447033ca1 100644 --- a/src/corelib/io/qprocess.cpp +++ b/src/corelib/io/qprocess.cpp @@ -1574,7 +1574,7 @@ std::function<void(void)> QProcess::childProcessModifier() const Sets the \a modifier function for the child process, for Unix systems (including \macos; for Windows, see setCreateProcessArgumentsModifier()). The function contained by the \a modifier argument will be invoked in the - child process after \c{fork()} is completed and QProcess has set up the + child process after \c{fork()} or \c{vfork()} is completed and QProcess has set up the standard file descriptors for the child process, but before \c{execve()}, inside start(). The modifier is useful to change certain properties of the child process, such as setting up additional file descriptors or closing @@ -1595,6 +1595,15 @@ std::function<void(void)> QProcess::childProcessModifier() const "async-signal-safe" is advised). Most of the Qt API is unsafe inside this callback, including qDebug(), and may lead to deadlocks. + \note On some systems (notably, Linux), QProcess will use \c{vfork()} + semantics to start the child process, so this function must obey even + stricter constraints. First, because it is still sharing memory with the + parent process, it must not write to any non-local variable and must obey + proper ordering semantics when reading from them, to avoid data races. + Second, even more library functions may misbehave; therefore, this function + should only make use of low-level system calls, such as \c{read()}, + \c{write()}, \c{setsid()}, \c{nice()}, and similar. + \sa childProcessModifier() */ void QProcess::setChildProcessModifier(const std::function<void(void)> &modifier) |
