summaryrefslogtreecommitdiffstats
path: root/src/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 /src/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 'src/corelib/io')
-rw-r--r--src/corelib/io/qprocess.cpp16
-rw-r--r--src/corelib/io/qprocess.h2
-rw-r--r--src/corelib/io/qprocess_unix.cpp37
3 files changed, 53 insertions, 2 deletions
diff --git a/src/corelib/io/qprocess.cpp b/src/corelib/io/qprocess.cpp
index ab7219a6cd..78d2de7779 100644
--- a/src/corelib/io/qprocess.cpp
+++ b/src/corelib/io/qprocess.cpp
@@ -849,6 +849,22 @@ void QProcessPrivate::Channel::clear()
child. The \c stdin, \c stdout, and \c stderr file descriptors are
never closed.
+ \value [since 6.7] CreateNewSession Starts a new process session, by calling
+ \c{setsid(2)}. This allows the child process to outlive the session
+ the current process is in. This is one of the steps that
+ startDetached() takes to allow the process to detach, and is also one
+ of the steps to daemonize a process.
+
+ \value [since 6.7] DisconnectControllingTerminal Requests that the process
+ disconnect from its controlling terminal, if it has one. If it has
+ none, nothing happens. Processes still connected to a controlling
+ terminal may get a Hang Up (\c SIGHUP) signal if the terminal
+ closes, or one of the other terminal-control signals (\c SIGTSTP, \c
+ SIGTTIN, \c SIGTTOU). Note that on some operating systems, a process
+ may only disconnect from the controlling terminal if it is the
+ session leader, meaning the \c CreateNewSession flag may be
+ required. Like it, this is one of the steps to daemonize a process.
+
\value IgnoreSigPipe Always sets the \c SIGPIPE signal to ignored
(\c SIG_IGN), even if the \c ResetSignalHandlers flag was set. By
default, if the child attempts to write to its standard output or
diff --git a/src/corelib/io/qprocess.h b/src/corelib/io/qprocess.h
index 770be0cbf0..42bee0691b 100644
--- a/src/corelib/io/qprocess.h
+++ b/src/corelib/io/qprocess.h
@@ -182,6 +182,8 @@ public:
// some room if we want to add IgnoreSigHup or so
CloseFileDescriptors = 0x0010,
UseVFork = 0x0020, // like POSIX_SPAWN_USEVFORK
+ CreateNewSession = 0x0040, // like POSIX_SPAWN_SETSID
+ DisconnectControllingTerminal = 0x0080,
};
Q_DECLARE_FLAGS(UnixProcessFlags, UnixProcessFlag)
struct UnixProcessParameters
diff --git a/src/corelib/io/qprocess_unix.cpp b/src/corelib/io/qprocess_unix.cpp
index 64f48accb2..02438359f2 100644
--- a/src/corelib/io/qprocess_unix.cpp
+++ b/src/corelib/io/qprocess_unix.cpp
@@ -39,6 +39,9 @@
#include <sys/resource.h>
#include <unistd.h>
+#if __has_include(<paths.h>)
+# include <paths.h>
+#endif
#if __has_include(<linux/close_range.h>)
// FreeBSD's is in <unistd.h>
# include <linux/close_range.h>
@@ -51,6 +54,12 @@
#ifndef O_PATH
# define O_PATH 0
#endif
+#ifndef _PATH_DEV
+# define _PATH_DEV "/dev/"
+#endif
+#ifndef _PATH_TTY
+# define _PATH_TTY _PATH_DEV "tty"
+#endif
#ifdef Q_OS_FREEBSD
__attribute__((weak))
@@ -774,7 +783,7 @@ void QProcess::failChildProcessModifier(const char *description, int error) noex
}
// See IMPORTANT notice below
-static void applyProcessParameters(const QProcess::UnixProcessParameters &params)
+static const char *applyProcessParameters(const QProcess::UnixProcessParameters &params)
{
// Apply Unix signal handler parameters.
// We don't expect signal() to fail, so we ignore its return value
@@ -817,6 +826,29 @@ static void applyProcessParameters(const QProcess::UnixProcessParameters &params
close(fd);
}
}
+
+ // Apply session and process group settings. This may fail.
+ if (params.flags.testFlag(QProcess::UnixProcessFlag::CreateNewSession)) {
+ if (setsid() < 0)
+ return "setsid";
+ }
+
+ // Disconnect from the controlling TTY. This probably won't fail. Must be
+ // done after the session settings from above.
+ if (params.flags.testFlag(QProcess::UnixProcessFlag::DisconnectControllingTerminal)) {
+ if (int fd = open(_PATH_TTY, O_RDONLY | O_NOCTTY); fd >= 0) {
+ // we still have a controlling TTY; give it up
+ int r = ioctl(fd, TIOCNOTTY);
+ int savedErrno = errno;
+ close(fd);
+ if (r != 0) {
+ errno = savedErrno;
+ return "ioctl";
+ }
+ }
+ }
+
+ return nullptr;
}
// the noexcept here adds an extra layer of protection
@@ -857,7 +889,8 @@ void QChildProcess::startProcess() const noexcept
callChildProcessModifier(d);
// then we apply our other user-provided parameters
- applyProcessParameters(d->unixExtras->processParameters);
+ if (const char *what = applyProcessParameters(d->unixExtras->processParameters))
+ failChildProcess(d, what, errno);
auto flags = d->unixExtras->processParameters.flags;
using P = QProcess::UnixProcessFlag;