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