diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2020-02-29 14:47:43 -0800 |
---|---|---|
committer | Thiago Macieira <thiago.macieira@intel.com> | 2020-09-08 21:00:29 -0700 |
commit | 7e93870401852623b316a9e707baf67cbaa6d722 (patch) | |
tree | f26caef521e35e3c3b3827794ffea832d4fd0491 /src/corelib/doc/snippets | |
parent | 3deb154d2207c5b38675ee75b4a843a100abf6a9 (diff) |
QProcess/Unix: introduce setChildProcessModifier()
[ChangeLog][Source-Incompatible Changes] QProcess::setupChildProcess()
was removed. To execute code in a child process, use
QProcess::setChildProcessModifier()
[ChangeLog][QtCore][QProcess] Added setChildProcessModifier() function
with which one can provide code to be run in the Unix child process
between fork() and execve(). With this function, it is no longer
necessary to derive from QProcess in order to execute actions in the
child process.
Another reason is that we can tell whether the std::function carries a
valid target much more easily than we can tell whether QProcess was
overridden.
The setupChildProcess() virtual function does not need to be marked
final, since no overrider could ever return an inaccessible private
class. This also makes sure the error presented to the user is about the
return type, not about attempting to override a final.
Clang:
error: virtual function 'f' has a different return type ('void') than the function it overrides (which has return type 'QProcess::Use_setChildProcessModifier_Instead')
GCC:
error: conflicting return type specified for 'virtual void MyProcess::setupChildProcess()'
note: overridden function is 'virtual QProcess::Use_setChildProcessModifier_Instead QProcess::setupChildProcess()'
ICC:
error: return type is neither identical to nor covariant with return type "QProcess::Use_setChildProcessModifier_Instead" of overridden virtual function "QProcess::setupChildProcess"
MSVC is not relevant since it doesn't compile to Unix.
Change-Id: Ia8b65350cd5d49debca9fffd15f801161363aea7
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@gmx.de>
Diffstat (limited to 'src/corelib/doc/snippets')
-rw-r--r-- | src/corelib/doc/snippets/code/src_corelib_io_qprocess.cpp | 34 |
1 files changed, 14 insertions, 20 deletions
diff --git a/src/corelib/doc/snippets/code/src_corelib_io_qprocess.cpp b/src/corelib/doc/snippets/code/src_corelib_io_qprocess.cpp index 8a2825cd10..5bbbd1b2cd 100644 --- a/src/corelib/doc/snippets/code/src_corelib_io_qprocess.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_io_qprocess.cpp @@ -90,28 +90,22 @@ process2.start("command2"); //! [4] -class SandboxProcess : public QProcess +void runSandboxed(const QString &name, const QStringList &arguments) { - ... - protected: - void setupChildProcess() override; - ... -}; - -void SandboxProcess::setupChildProcess() -{ - // Drop all privileges in the child process, and enter - // a chroot jail. -#if defined Q_OS_UNIX - ::setgroups(0, 0); - ::chroot("/etc/safe"); - ::chdir("/"); - ::setgid(safeGid); - ::setuid(safeUid); - ::umask(0); -#endif + QProcess proc; + proc.setChildProcessModifier([] { + // Drop all privileges in the child process, and enter + // a chroot jail. + ::setgroups(0, 0); + ::chroot("/etc/safe"); + ::chdir("/"); + ::setgid(safeGid); + ::setuid(safeUid); + ::umask(0); + }); + proc.start(name, arguments); + proc.waitForFinished(); } - //! [4] |