1 /***************************************************************************
2 * Copyright (C) 2005-2019 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "windowssignalwatcher.h"
28 // This handler is called by Windows in a different thread when a console event happens
29 // FIXME: When the console window is closed, the application is supposedly terminated as soon as
30 // this handler returns. We may want to block and wait for the main thread so set some
31 // condition variable once shutdown is complete...
32 static BOOL WINAPI consoleCtrlHandler(DWORD ctrlType)
35 case CTRL_C_EVENT: // Ctrl+C
36 case CTRL_CLOSE_EVENT: // Closing the console window
37 WindowsSignalWatcher::signalHandler(SIGTERM);
44 WindowsSignalWatcher::WindowsSignalWatcher(QObject* parent)
45 : AbstractSignalWatcher{parent}
46 , Singleton<WindowsSignalWatcher>{this}
48 static bool registered = []() {
49 qRegisterMetaType<AbstractSignalWatcher::Action>();
54 // Use POSIX-style API to register standard signals.
55 // Not sure if this is safe to use, but it has worked so far...
56 signal(SIGTERM, signalHandler);
57 signal(SIGINT, signalHandler);
58 signal(SIGABRT, signalHandler);
59 signal(SIGSEGV, signalHandler);
61 // React on console window events
62 SetConsoleCtrlHandler(consoleCtrlHandler, TRUE);
65 void WindowsSignalWatcher::signalHandler(int signal)
67 qInfo() << "Caught signal" << signal;
72 emit instance()->handleSignal(Action::Terminate);
76 emit instance()->handleSignal(Action::HandleCrash);