[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [libdebconf-kde] tools: handle sigquit properly
From:       Harald Sitter <null () kde ! org>
Date:       2018-03-21 14:33:02
Message-ID: E1eyenW-00017R-9k () code ! kde ! org
[Download RAW message or body]

Git commit 09927540b3d77388ff81c9428c38f92c47a3da08 by Harald Sitter.
Committed on 21/03/2018 at 14:33.
Pushed by sitter into branch 'master'.

handle sigquit properly

Summary:
this prevents an excessive amount of coredumps being caused by pkcon
which sigquits the helper

Test Plan:
```
debconf-kde-helper --socket-path=/tmp/gpk-26_baceaeca.socket
kill -QUIT `pidof debconf-kde-helper
```
- no crash

Reviewers: apol, dantti

Reviewed By: dantti

Differential Revision: https://phabricator.kde.org/D11546

M  +44   -4    tools/main.cpp

https://commits.kde.org/libdebconf-kde/09927540b3d77388ff81c9428c38f92c47a3da08

diff --git a/tools/main.cpp b/tools/main.cpp
index b8bb8c9..29836c8 100644
--- a/tools/main.cpp
+++ b/tools/main.cpp
@@ -1,6 +1,7 @@
 /*
  * Copyright (C) 2010-2018 Daniel Nicoletti <dantti12@gmail.com>
  *           (C) 2011 Modestas Vainius <modax@debian.org>
+ *           (C) 2018 Harald Sitter <sitter@kde.org>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
@@ -18,23 +19,62 @@
  * Boston, MA 02110-1301, USA.
  */
 
-#include <QtCore/QCommandLineParser>
-#include <QtCore/QDebug>
-#include <QtCore/QRegExp>
-#include <QtWidgets/QApplication>
+#include <QApplication>
+#include <QCommandLineParser>
+#include <QDebug>
+#include <QRegExp>
+#include <QSocketNotifier>
 
 #include <KAboutData>
 #include <KLocalizedString>
 
 #include <iostream>
 
+#include <signal.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <unistd.h>
+
 #include <DebconfGui.h>
 
 using namespace DebconfKde;
 
+// Handle SIGQUIT. Clients (e.g. packagekit) may use QUIT which would otherwise
+// result in a core dump.
+// Qt methods aren't signal-safe, so we'll defer the handling via a socket
+// notification.
+static void setupQuitHandler() {
+    static int quitFD[2];
+    if (socketpair(AF_UNIX, SOCK_STREAM, 0, quitFD)) {
+       qErrnoWarning("Failed to create socket");
+    }
+    auto notifier = new QSocketNotifier(quitFD[1], QSocketNotifier::Read, qApp);
+    QObject::connect(notifier, &QSocketNotifier::activated, [notifier]() {
+        notifier->setEnabled(false);
+        char c;
+        read(quitFD[1], &c, sizeof(c));
+        qApp->quit();
+    });
+
+    struct sigaction sa;
+    sa.sa_handler = [](int) -> void {
+        char c = 1;
+        write(quitFD[0], &c, sizeof(c));
+    };
+    sigemptyset(&sa.sa_mask);
+    sigaddset(&sa.sa_mask, SIGQUIT);
+    sa.sa_flags = 0;
+    sa.sa_flags |= SA_RESTART;
+
+    if (sigaction(SIGQUIT, &sa, 0) != 0) {
+       qErrnoWarning("Failed to set quit handler");
+    }
+}
+
 int main(int argc, char **argv)
 {
     QApplication app(argc, argv);
+    setupQuitHandler();
 
     KLocalizedString::setApplicationDomain("libdebconf-kde");
 
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic