summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/io
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2023-05-12 19:50:51 -0700
committerThiago Macieira <thiago.macieira@intel.com>2023-07-08 15:03:23 -0700
commit13a1995e9dc987d1560b38d16b76442261b4aa8d (patch)
treebb2ba2d6ce159b1123721756c616cb10c8170c1a /tests/auto/corelib/io
parent8f1df9aaa87f1d899babce42a8fdbb28c13ae604 (diff)
QProcess/Unix: add a few, basic session & terminal management flags
Doing setsid() and disconnecting from the controlling terminal are, in addition to resetting the standard file descriptors to /dev/null, a common task that daemons do. These options allow a QProcess to force a child to be a daemon. QProcess ensures that the operations are done in the correct order. Change-Id: I3e3bfef633af4130a03afffd175e9451d2716d7a Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'tests/auto/corelib/io')
-rw-r--r--tests/auto/corelib/io/qprocess/testUnixProcessParameters/main.cpp16
-rw-r--r--tests/auto/corelib/io/qprocess/tst_qprocess.cpp44
2 files changed, 60 insertions, 0 deletions
diff --git a/tests/auto/corelib/io/qprocess/testUnixProcessParameters/main.cpp b/tests/auto/corelib/io/qprocess/testUnixProcessParameters/main.cpp
index 96425eca2f..962a1ee1c0 100644
--- a/tests/auto/corelib/io/qprocess/testUnixProcessParameters/main.cpp
+++ b/tests/auto/corelib/io/qprocess/testUnixProcessParameters/main.cpp
@@ -80,6 +80,22 @@ int main(int argc, char **argv)
return EXIT_SUCCESS;
}
+ if (cmd == "noctty") {
+ int fd = open("/dev/tty", O_RDONLY);
+ if (fd == -1)
+ return EXIT_SUCCESS;
+ fprintf(stderr, "Could open /dev/tty\n");
+ return EXIT_FAILURE;
+ }
+
+ if (cmd == "setsid") {
+ pid_t pgid = getpgrp();
+ if (pgid == getpid())
+ return EXIT_SUCCESS;
+ fprintf(stderr, "Process group was %d\n", pgid);
+ return EXIT_FAILURE;
+ }
+
fprintf(stderr, "Unknown command \"%s\"", cmd.data());
return EXIT_FAILURE;
}
diff --git a/tests/auto/corelib/io/qprocess/tst_qprocess.cpp b/tests/auto/corelib/io/qprocess/tst_qprocess.cpp
index b4012c69c3..1e091d1178 100644
--- a/tests/auto/corelib/io/qprocess/tst_qprocess.cpp
+++ b/tests/auto/corelib/io/qprocess/tst_qprocess.cpp
@@ -126,6 +126,8 @@ private slots:
void raiseInChildProcessModifier();
void unixProcessParameters_data();
void unixProcessParameters();
+ void impossibleUnixProcessParameters_data();
+ void impossibleUnixProcessParameters();
void unixProcessParametersAndChildModifier();
void unixProcessParametersOtherFileDescriptors();
#endif
@@ -1730,6 +1732,10 @@ void tst_QProcess::unixProcessParameters_data()
addRow("reset-sighand", P::ResetSignalHandlers);
addRow("ignore-sigpipe", P::IgnoreSigPipe);
addRow("file-descriptors", P::CloseFileDescriptors);
+ addRow("setsid", P::CreateNewSession);
+
+ // On FreeBSD, we need to be session leader to disconnect from the CTTY
+ addRow("noctty", P::DisconnectControllingTerminal | P::CreateNewSession);
}
void tst_QProcess::unixProcessParameters()
@@ -1778,6 +1784,13 @@ void tst_QProcess::unixProcessParameters()
}
} scope;
+ if (params.flags & QProcess::UnixProcessFlag::DisconnectControllingTerminal) {
+ if (int fd = open("/dev/tty", O_RDONLY); fd < 0) {
+ qInfo("Process has no controlling terminal; this test will do nothing");
+ close(fd);
+ }
+ }
+
QProcess process;
process.setUnixProcessParameters(params);
process.setStandardInputFile(QProcess::nullDevice()); // so we can't mess with SIGPIPE
@@ -1798,6 +1811,31 @@ void tst_QProcess::unixProcessParameters()
QCOMPARE(process.exitStatus(), QProcess::NormalExit);
}
+void tst_QProcess::impossibleUnixProcessParameters_data()
+{
+ using P = QProcess::UnixProcessParameters;
+ QTest::addColumn<P>("params");
+ QTest::newRow("setsid") << P{ QProcess::UnixProcessFlag::CreateNewSession };
+}
+
+void tst_QProcess::impossibleUnixProcessParameters()
+{
+ QFETCH(QProcess::UnixProcessParameters, params);
+
+ QProcess process;
+ if (params.flags & QProcess::UnixProcessFlag::CreateNewSession) {
+ process.setChildProcessModifier([]() {
+ // double setsid() should cause the second to fail
+ setsid();
+ });
+ }
+ process.setUnixProcessParameters(params);
+ process.start("testProcessNormal/testProcessNormal");
+
+ QVERIFY(!process.waitForStarted(5000));
+ qDebug() << process.errorString();
+}
+
void tst_QProcess::unixProcessParametersAndChildModifier()
{
static constexpr char message[] = "Message from the handler function\n";
@@ -1806,6 +1844,8 @@ void tst_QProcess::unixProcessParametersAndChildModifier()
QAtomicInt vforkControl;
int pipes[2];
+ pid_t oldpgid = getpgrp();
+
QVERIFY2(pipe(pipes) == 0, qPrintable(qt_error_string()));
auto pipeGuard0 = qScopeGuard([=] { close(pipes[0]); });
{
@@ -1813,10 +1853,14 @@ void tst_QProcess::unixProcessParametersAndChildModifier()
// verify that our modifier runs before the parameters are applied
process.setChildProcessModifier([=, &vforkControl] {
+ const char *pgidmsg = "PGID mismatch. ";
+ if (getpgrp() != oldpgid)
+ write(pipes[1], pgidmsg, strlen(pgidmsg));
write(pipes[1], message, strlen(message));
vforkControl.storeRelaxed(1);
});
auto flags = QProcess::UnixProcessFlag::CloseFileDescriptors |
+ QProcess::UnixProcessFlag::CreateNewSession |
QProcess::UnixProcessFlag::UseVFork;
process.setUnixProcessParameters({ flags });
process.setProgram("testUnixProcessParameters/testUnixProcessParameters");