summaryrefslogtreecommitdiff
path: root/doc/src/howtos/unix-signal-handlers.qdoc
blob: e936ea29497c3547038b45a00ce2941ab645bc00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the documentation of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

/*!
    \page unix-signals.html
    \title Calling Qt Functions From Unix Signal Handlers
    \brief You can't. But don't despair, there is a way...

    \ingroup platform-specific
    \ingroup best-practices

    You \e can't call Qt functions from Unix signal handlers. The
    standard POSIX rule applies: You can only call async-signal-safe
    functions from signal handlers. See \l
    {http://www.opengroup.org/onlinepubs/000095399/functions/xsh_chap02_04.html#tag_02_04_01}
    {Signal Actions} for the complete list of functions you can call
    from Unix signal handlers.

    But don't despair, there is a way to use Unix signal handlers with
    Qt. The strategy is to have your Unix signal handler do something
    that will eventually cause a Qt signal to be emitted, and then you
    simply return from your Unix signal handler. Back in your Qt
    program, that Qt signal gets emitted and then received by your Qt
    slot function, where you can safely do whatever Qt stuff you
    weren't allowed to do in the Unix signal handler.

    One simple way to make this happen is to declare a socket pair in
    your class for each Unix signal you want to handle. The socket
    pairs are declared as static data members. You also create a
    QSocketNotifier to monitor the \e read end of each socket pair,
    declare your Unix signal handlers to be static class methods, and
    declare a slot function corresponding to each of your Unix signal
    handlers. In this example, we intend to handle both the SIGHUP and
    SIGTERM signals. Note: You should read the socketpair(2) and the
    sigaction(2) man pages before plowing through the following code
    snippets.
    
    \snippet doc/src/snippets/code/doc_src_unix-signal-handlers.qdoc 0

    In the MyDaemon constructor, use the socketpair(2) function to
    initialize each file descriptor pair, and then create the
    QSocketNotifier to monitor the \e read end of each pair. The
    activated() signal of each QSocketNotifier is connected to the
    appropriate slot function, which effectively converts the Unix
    signal to the QSocketNotifier::activated() signal.

    \snippet doc/src/snippets/code/doc_src_unix-signal-handlers.qdoc 1

    Somewhere else in your startup code, you install your Unix signal
    handlers with sigaction(2).

    \snippet doc/src/snippets/code/doc_src_unix-signal-handlers.qdoc 2

    In your Unix signal handlers, you write a byte to the \e write end
    of a socket pair and return. This will cause the corresponding
    QSocketNotifier to emit its activated() signal, which will in turn
    cause the appropriate Qt slott function to run.

    \snippet doc/src/snippets/code/doc_src_unix-signal-handlers.qdoc 3

    In the slot functions connected to the
    QSocketNotifier::activated() signals, you \e read the byte. Now
    you are safely back in Qt with your signal, and you can do all the
    Qt stuff you weren'tr allowed to do in the Unix signal handler.

    \snippet doc/src/snippets/code/doc_src_unix-signal-handlers.qdoc 4
*/