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