1 /****************************************************************************
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
6 ** This file is part of the documentation of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:FDL$
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.
17 ** GNU Free Documentation License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Free
19 ** Documentation License version 1.3 as published by the Free Software
20 ** Foundation and appearing in the file included in the packaging of
21 ** this file. Please review the following information to ensure
22 ** the GNU Free Documentation License version 1.3 requirements
23 ** will be met: http://www.gnu.org/copyleft/fdl.html.
26 ****************************************************************************/
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...
33 \ingroup platform-specific
34 \ingroup best-practices
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.
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.
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
62 \snippet doc/src/snippets/code/doc_src_unix-signal-handlers.cpp 0
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.
71 \snippet doc/src/snippets/code/doc_src_unix-signal-handlers.cpp 1
73 Somewhere else in your startup code, you install your Unix signal
74 handlers with sigaction(2).
76 \snippet doc/src/snippets/code/doc_src_unix-signal-handlers.cpp 2
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.
83 \snippet doc/src/snippets/code/doc_src_unix-signal-handlers.cpp 3
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.
90 \snippet doc/src/snippets/code/doc_src_unix-signal-handlers.cpp 4