2
* Copyright (C) 2006 Justin Karneges
4
* This library is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU Lesser General Public
6
* License as published by the Free Software Foundation; either
7
* version 2.1 of the License, or (at your option) any later version.
9
* This library is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
* Lesser General Public License for more details.
14
* You should have received a copy of the GNU Lesser General Public
15
* License along with this library; if not, write to the Free Software
16
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21
#include "processquit.h"
24
# include "irisnetglobal_p.h"
28
# include <QApplication>
44
Q_GLOBAL_STATIC(QMutex, pq_mutex)
45
static ProcessQuit *g_pq = 0;
47
inline bool is_gui_app()
50
return (QApplication::type() != QApplication::Tty);
56
class ProcessQuit::Private : public QObject
68
QSocketNotifier *sig_notifier;
71
Private(ProcessQuit *_q) : QObject(_q), q(_q)
75
use_handler = !is_gui_app();
77
SetConsoleCtrlHandler((PHANDLER_ROUTINE)winHandler, TRUE);
81
sig_notifier = new QSocketNotifier(sig_pipe[0], QSocketNotifier::Read, this);
82
connect(sig_notifier, SIGNAL(activated(int)), SLOT(sig_activated(int)));
85
unixWatchAdd(SIGTERM);
93
SetConsoleCtrlHandler((PHANDLER_ROUTINE)winHandler, FALSE);
96
unixWatchRemove(SIGINT);
97
unixWatchRemove(SIGHUP);
98
unixWatchRemove(SIGTERM);
106
static BOOL winHandler(DWORD ctrlType)
109
QMetaObject::invokeMethod(g_pq->d, "ctrl_ready", Qt::QueuedConnection);
115
static void unixHandler(int sig)
119
::write(g_pq->d->sig_pipe[1], &c, 1);
122
void unixWatchAdd(int sig)
125
sigaction(sig, NULL, &sa);
126
// if the signal is ignored, don't take it over. this is
127
// recommended by the glibc manual
128
if(sa.sa_handler == SIG_IGN)
130
sigemptyset(&(sa.sa_mask));
132
sa.sa_handler = unixHandler;
133
sigaction(sig, &sa, 0);
136
void unixWatchRemove(int sig)
139
sigaction(sig, NULL, &sa);
140
// ignored means we skipped it earlier, so we should
142
if(sa.sa_handler == SIG_IGN)
144
sigemptyset(&(sa.sa_mask));
146
sa.sa_handler = SIG_DFL;
147
sigaction(sig, &sa, 0);
159
void sig_activated(int)
163
::read(sig_pipe[0], &c, 1);
180
ProcessQuit::ProcessQuit(QObject *parent)
183
d = new Private(this);
186
ProcessQuit::~ProcessQuit()
191
ProcessQuit *ProcessQuit::instance()
193
QMutexLocker locker(pq_mutex());
196
g_pq = new ProcessQuit;
197
g_pq->moveToThread(QCoreApplication::instance()->thread());
199
irisNetAddPostRoutine(cleanup);
205
void ProcessQuit::reset()
207
QMutexLocker locker(pq_mutex());
209
g_pq->d->done = false;
212
void ProcessQuit::cleanup()
222
#include "processquit.moc"