1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | /*!
|
---|
43 | \page unix-signals.html
|
---|
44 | \title Calling Qt Functions From Unix Signal Handlers
|
---|
45 | \ingroup platform-notes
|
---|
46 | \brief You can't. But don't despair, there is a way...
|
---|
47 |
|
---|
48 | You \e can't call Qt functions from Unix signal handlers. The
|
---|
49 | standard POSIX rule applies: You can only call async-signal-safe
|
---|
50 | functions from signal handlers. See \l
|
---|
51 | {http://www.opengroup.org/onlinepubs/000095399/functions/xsh_chap02_04.html#tag_02_04_01}
|
---|
52 | {Signal Actions} for the complete list of functions you can call
|
---|
53 | from Unix signal handlers.
|
---|
54 |
|
---|
55 | But don't despair, there is a way to use Unix signal handlers with
|
---|
56 | Qt. The strategy is to have your Unix signal handler do something
|
---|
57 | that will eventually cause a Qt signal to be emitted, and then you
|
---|
58 | simply return from your Unix signal handler. Back in your Qt
|
---|
59 | program, that Qt signal gets emitted and then received by your Qt
|
---|
60 | slot function, where you can safely do whatever Qt stuff you
|
---|
61 | weren't allowed to do in the Unix signal handler.
|
---|
62 |
|
---|
63 | One simple way to make this happen is to declare a socket pair in
|
---|
64 | your class for each Unix signal you want to handle. The socket
|
---|
65 | pairs are declared as static data members. You also create a
|
---|
66 | QSocketNotifier to monitor the \e read end of each socket pair,
|
---|
67 | declare your Unix signal handlers to be static class methods, and
|
---|
68 | declare a slot function corresponding to each of your Unix signal
|
---|
69 | handlers. In this example, we intend to handle both the SIGHUP and
|
---|
70 | SIGTERM signals. Note: You should read the socketpair(2) and the
|
---|
71 | sigaction(2) man pages before plowing through the following code
|
---|
72 | snippets.
|
---|
73 |
|
---|
74 | \snippet doc/src/snippets/code/doc_src_unix-signal-handlers.qdoc 0
|
---|
75 |
|
---|
76 | In the MyDaemon constructor, use the socketpair(2) function to
|
---|
77 | initialize each file descriptor pair, and then create the
|
---|
78 | QSocketNotifier to monitor the \e read end of each pair. The
|
---|
79 | activated() signal of each QSocketNotifier is connected to the
|
---|
80 | appropriate slot function, which effectively converts the Unix
|
---|
81 | signal to the QSocketNotifier::activated() signal.
|
---|
82 |
|
---|
83 | \snippet doc/src/snippets/code/doc_src_unix-signal-handlers.qdoc 1
|
---|
84 |
|
---|
85 | Somewhere else in your startup code, you install your Unix signal
|
---|
86 | handlers with sigaction(2).
|
---|
87 |
|
---|
88 | \snippet doc/src/snippets/code/doc_src_unix-signal-handlers.qdoc 2
|
---|
89 |
|
---|
90 | In your Unix signal handlers, you write a byte to the \e write end
|
---|
91 | of a socket pair and return. This will cause the corresponding
|
---|
92 | QSocketNotifier to emit its activated() signal, which will in turn
|
---|
93 | cause the appropriate Qt slott function to run.
|
---|
94 |
|
---|
95 | \snippet doc/src/snippets/code/doc_src_unix-signal-handlers.qdoc 3
|
---|
96 |
|
---|
97 | In the slot functions connected to the
|
---|
98 | QSocketNotifier::activated() signals, you \e read the byte. Now
|
---|
99 | you are safely back in Qt with your signal, and you can do all the
|
---|
100 | Qt stuff you weren'tr allowed to do in the Unix signal handler.
|
---|
101 |
|
---|
102 | \snippet doc/src/snippets/code/doc_src_unix-signal-handlers.qdoc 4
|
---|
103 | */
|
---|