source: trunk/doc/src/howtos/unix-signal-handlers.qdoc

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

  • Property svn:eol-style set to native
File size: 4.0 KB
Line 
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