1 /***************************************************************************
2 * Copyright (C) 2005-2018 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 #include "logmessage.h"
30 // This handler is called by Windows in a different thread when a console event happens
31 // FIXME: When the console window is closed, the application is supposedly terminated as soon as
32 // this handler returns. We may want to block and wait for the main thread so set some
33 // condition variable once shutdown is complete...
34 static BOOL WINAPI consoleCtrlHandler(DWORD ctrlType)
37 case CTRL_C_EVENT: // Ctrl+C
38 case CTRL_CLOSE_EVENT: // Closing the console window
39 WindowsSignalWatcher::signalHandler(SIGTERM);
46 WindowsSignalWatcher::WindowsSignalWatcher(QObject *parent)
47 : AbstractSignalWatcher{parent}
48 , Singleton<WindowsSignalWatcher>{this}
50 static bool registered = []() {
51 qRegisterMetaType<AbstractSignalWatcher::Action>();
56 // Use POSIX-style API to register standard signals.
57 // Not sure if this is safe to use, but it has worked so far...
58 signal(SIGTERM, signalHandler);
59 signal(SIGINT, signalHandler);
60 signal(SIGABRT, signalHandler);
61 signal(SIGSEGV, signalHandler);
63 // React on console window events
64 SetConsoleCtrlHandler(consoleCtrlHandler, TRUE);
67 void WindowsSignalWatcher::signalHandler(int signal)
69 quInfo() << "Caught signal" << signal;
74 emit instance()->handleSignal(Action::Terminate);
78 emit instance()->handleSignal(Action::HandleCrash);