Does QProcess::close() raise the unix SIGTERM signal on the process?

你。 提交于 2020-01-15 03:12:13

问题


In Linux/Qt I have a GUI application. The GUI starts up additional child processes using QProcess. To close the child processes I use QProcess::close().

Does QProcess::close() raise the unix SIGTERM signal for the child process?

(I have linked to the Qt documentation for QProcess and close() because I can not tell from the documentation if a unix signal is raised...)

UPDATE: changed question to ask about a specific unix signal: SIGTERM.


回答1:


Today I found out that QProcess::close() does not raise the SIGTERM signal. To debug this issue I am capturing the stderr and stdout of the child process. Spoiler: QProcess::terminate() does raise the SIGTERM signal.


Child process code to handle unix SIGTERM signal:

static void unixSignalHandler(int signum) {
    qDebug("DBG: main.cpp::unixSignalHandler(). signal = %s\n", strsignal(signum));

    /*
     * Make sure your Qt application gracefully quits.
     * NOTE - purpose for calling qApp->exit(0):
     *      1. Forces the Qt framework's "main event loop `qApp->exec()`" to quit looping.
     *      2. Also emits the QCoreApplication::aboutToQuit() signal. This signal is used for cleanup code.
     */
    qApp->exit(0);
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    MAINOBJECT mainobject;

    /*
     * Setup UNIX signal handlers for some of the common signals.
     * NOTE common signals:
     *      SIGINT:     The user started the process on the command line and user ctrl-C.
     *      SIGTERM:    The user kills the process using the `kill` command.
     *                  OR
     *                  The process is started using QProcess and SIGTERM is
     *                  issued when QProcess::close() is used to close the process.
     */
    if (signal(SIGINT, unixSignalHandler) == SIG_ERR) {
        qFatal("ERR - %s(%d): An error occurred while setting a signal handler.\n", __FILE__,__LINE__);
    }
    if (signal(SIGTERM, unixSignalHandler) == SIG_ERR) {
        qFatal("ERR - %s(%d): An error occurred while setting a signal handler.\n", __FILE__,__LINE__);
    }
    // executes mainbobject.cleanupSlot() when the Qt framework emits aboutToQuit() signal.
    QObject::connect(qApp,          SIGNAL(aboutToQuit()),
                     &mainobject,   SLOT(cleanupSlot()));

    return a.exec();
}

Parent process code that handles close() or terminate() of the child:

if(this->childProcess1!=NULL && this->childProcess1->state()==QProcess::Running) {
    this->childProcess1->terminate(); // raise unix SIGTERM signal on the process (let's it execute cleanup code)
    if(childProcess1->waitForFinished() == false) {
        qDebug("DBG - MainWindow::close(): Failed to close childProcess1.");
        qDebug() << "DBG - childProcess1->exitCode     =" << childProcess1->exitCode();
        qDebug() << "DBG - childProcess1->ExitStatus   =" << childProcess1->exitStatus();
        qDebug() << "DBG - childProcess1->error()      =" << childProcess1->error();
        qDebug() << "DBG - childProcess1->errorString()=" << childProcess1->errorString();
    }
}
if(this->childProcess2!=NULL && this->childProcess2->state()== QProcess::Running) {
    this->childProcess2->terminate(); // raise unix SIGTERM signal on the process (let's it execute cleanup code)
    if(childProcess2->waitForFinished() == false) {
        qDebug("DBG - MainWindow::close(): Failed to close childProcess2.");
        qDebug() << "DBG - childProcess2->exitCode     =" << childProcess2->exitCode();
        qDebug() << "DBG - childProcess2->ExitStatus   =" << childProcess2->exitStatus();
        qDebug() << "DBG - childProcess2->error()      =" << childProcess2->error();
        qDebug() << "DBG - childProcess2->errorString()=" << childProcess2->errorString();
    }
}

When the parent uses QProcess::terminate(), the child process output is:

"DBG: main.cpp::unixSignalHandler(). signal = Terminated

When the parent uses `QProcess::close(), the child process output is:

DBG - PARENTOBJECT::close(): Failed to close childProcess.
DBG - childProcess->exitCode     = 0 
DBG - childProcess->ExitStatus   = 1 
DBG - childProcess->error()      = 1 
DBG - childProcess->errorString()= "Unknown error" 

The output from the terminate() experiment proves that the child process is getting a SIGTERM signal.

The output from the close() experiment proves that the child process is not getting the SIGTERM signal.


Conclusion:

If you want to send the child process the SIGTERM signal, use QProcess::terminate().




回答2:


It does, I'm looking at the source for qprocess.cpp and it goes through some motions to nicely wind the process down, then calls off to a generic handler, then passes to terminateProcess in qprocess_unix which SIGTERM's the process.



来源:https://stackoverflow.com/questions/7501762/does-qprocessclose-raise-the-unix-sigterm-signal-on-the-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!