1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qwssignalhandler_p.h"
43 
44 #ifndef QT_NO_QWS_SIGNALHANDLER
45 
46 #include "qlock_p.h"
47 #include "qwslock_p.h"
48 
49 #include <sys/types.h>
50 #include <signal.h>
51 
52 QT_BEGIN_NAMESPACE
53 
54 class QWSSignalHandlerPrivate : public QWSSignalHandler
55 {
56 public:
QWSSignalHandlerPrivate()57     QWSSignalHandlerPrivate() : QWSSignalHandler() {}
58 };
59 
60 
61 Q_GLOBAL_STATIC(QWSSignalHandlerPrivate, signalHandlerInstance);
62 
63 
instance()64 QWSSignalHandler* QWSSignalHandler::instance()
65 {
66     return signalHandlerInstance();
67 }
68 
QWSSignalHandler()69 QWSSignalHandler::QWSSignalHandler()
70 {
71     const int signums[] = { SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGABRT, SIGFPE,
72                             SIGSEGV, SIGTERM, SIGBUS };
73     const int n = sizeof(signums)/sizeof(int);
74 
75     for (int i = 0; i < n; ++i) {
76         const int signum = signums[i];
77         qt_sighandler_t old = signal(signum, handleSignal);
78         if (old == SIG_IGN) // don't remove shm and semaphores when ignored
79             signal(signum, old);
80         else
81             oldHandlers[signum] = (old == SIG_ERR ? SIG_DFL : old);
82     }
83 }
84 
~QWSSignalHandler()85 QWSSignalHandler::~QWSSignalHandler()
86 {
87     clear();
88 }
89 
clear()90 void QWSSignalHandler::clear()
91 {
92 #if !defined(QT_NO_QWS_MULTIPROCESS)
93     // it is safe to call d-tors directly here since, on normal exit,
94     // lists should be empty; otherwise, we don't care about semi-alive objects
95     // and the only important thing here is to unregister the system semaphores.
96     while (!locks.isEmpty())
97         locks.takeLast()->~QLock();
98     while (!wslocks.isEmpty())
99         wslocks.takeLast()->~QWSLock();
100 #endif
101     objects.clear();
102 }
103 
handleSignal(int signum)104 void QWSSignalHandler::handleSignal(int signum)
105 {
106     QWSSignalHandler *h = instance();
107     if (h) {
108         signal(signum, h->oldHandlers[signum]);
109         h->clear();
110     }
111     raise(signum);
112 }
113 
114 QT_END_NAMESPACE
115 
116 #endif // QT_NO_QWS_SIGNALHANDLER
117