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:BSD$
|
---|
10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
11 | **
|
---|
12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
13 | ** modification, are permitted provided that the following conditions are
|
---|
14 | ** met:
|
---|
15 | ** * Redistributions of source code must retain the above copyright
|
---|
16 | ** notice, this list of conditions and the following disclaimer.
|
---|
17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
18 | ** notice, this list of conditions and the following disclaimer in
|
---|
19 | ** the documentation and/or other materials provided with the
|
---|
20 | ** distribution.
|
---|
21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
22 | ** the names of its contributors may be used to endorse or promote
|
---|
23 | ** products derived from this software without specific prior written
|
---|
24 | ** permission.
|
---|
25 | **
|
---|
26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
37 | ** $QT_END_LICENSE$
|
---|
38 | **
|
---|
39 | ****************************************************************************/
|
---|
40 |
|
---|
41 | //! [0]
|
---|
42 | class MyDaemon : public QObject
|
---|
43 | {
|
---|
44 | Q_OBJECT
|
---|
45 |
|
---|
46 | public:
|
---|
47 | MyDaemon(QObject *parent = 0, const char *name = 0);
|
---|
48 | ~MyDaemon();
|
---|
49 |
|
---|
50 | // Unix signal handlers.
|
---|
51 | static void hupSignalHandler(int unused);
|
---|
52 | static void termSignalHandler(int unused);
|
---|
53 |
|
---|
54 | public slots:
|
---|
55 | // Qt signal handlers.
|
---|
56 | void handleSigHup();
|
---|
57 | void handleSigTerm();
|
---|
58 |
|
---|
59 | private:
|
---|
60 | static int sighupFd[2];
|
---|
61 | static int sigtermFd[2];
|
---|
62 |
|
---|
63 | QSocketNotifier *snHup;
|
---|
64 | QSocketNotifier *snTerm;
|
---|
65 | };
|
---|
66 | //! [0]
|
---|
67 |
|
---|
68 |
|
---|
69 | //! [1]
|
---|
70 | MyDaemon::MyDaemon(QObject *parent, const char *name)
|
---|
71 | : QObject(parent,name)
|
---|
72 | {
|
---|
73 | if (::socketpair(AF_UNIX, SOCK_STREAM, 0, sighupFd))
|
---|
74 | qFatal("Couldn't create HUP socketpair");
|
---|
75 |
|
---|
76 | if (::socketpair(AF_UNIX, SOCK_STREAM, 0, sigtermFd))
|
---|
77 | qFatal("Couldn't create TERM socketpair");
|
---|
78 | snHup = new QSocketNotifier(sighupFd[1], QSocketNotifier::Read, this);
|
---|
79 | connect(snHup, SIGNAL(activated(int)), this, SLOT(handleSigHup()));
|
---|
80 | snTerm = new QSocketNotifier(sigtermFd[1], QSocketNotifier::Read, this);
|
---|
81 | connect(snTerm, SIGNAL(activated(int)), this, SLOT(handleSigTerm()));
|
---|
82 |
|
---|
83 | ...
|
---|
84 | }
|
---|
85 | //! [1]
|
---|
86 |
|
---|
87 |
|
---|
88 | //! [2]
|
---|
89 | static int setup_unix_signal_handlers()
|
---|
90 | {
|
---|
91 | struct sigaction hup, term;
|
---|
92 |
|
---|
93 | hup.sa_handler = MyDaemon::hupSignalHandler;
|
---|
94 | sigemptyset(&hup.sa_mask);
|
---|
95 | hup.sa_flags = 0;
|
---|
96 | hup.sa_flags |= SA_RESTART;
|
---|
97 |
|
---|
98 | if (sigaction(SIGHUP, &hup, 0) > 0)
|
---|
99 | return 1;
|
---|
100 |
|
---|
101 | term.sa_handler = MyDaemon::termSignalHandler;
|
---|
102 | sigemptyset(&term.sa_mask);
|
---|
103 | term.sa_flags |= SA_RESTART;
|
---|
104 |
|
---|
105 | if (sigaction(SIGTERM, &term, 0) > 0)
|
---|
106 | return 2;
|
---|
107 |
|
---|
108 | return 0;
|
---|
109 | }
|
---|
110 | //! [2]
|
---|
111 |
|
---|
112 |
|
---|
113 | //! [3]
|
---|
114 | void MyDaemon::hupSignalHandler(int)
|
---|
115 | {
|
---|
116 | char a = 1;
|
---|
117 | ::write(sighupFd[0], &a, sizeof(a));
|
---|
118 | }
|
---|
119 |
|
---|
120 | void MyDaemon::termSignalHandler(int)
|
---|
121 | {
|
---|
122 | char a = 1;
|
---|
123 | ::write(sigtermFd[0], &a, sizeof(a));
|
---|
124 | }
|
---|
125 | //! [3]
|
---|
126 |
|
---|
127 |
|
---|
128 | //! [4]
|
---|
129 | void MyDaemon::handleSigTerm()
|
---|
130 | {
|
---|
131 | snTerm->setEnabled(false);
|
---|
132 | char tmp;
|
---|
133 | ::read(sigtermFd[1], &tmp, sizeof(tmp));
|
---|
134 |
|
---|
135 | // do Qt stuff
|
---|
136 |
|
---|
137 | snTerm->setEnabled(true);
|
---|
138 | }
|
---|
139 |
|
---|
140 | void MyDaemon::handleSigHup()
|
---|
141 | {
|
---|
142 | snHup->setEnabled(false);
|
---|
143 | char tmp;
|
---|
144 | ::read(sighupFd[1], &tmp, sizeof(tmp));
|
---|
145 |
|
---|
146 | // do Qt stuff
|
---|
147 |
|
---|
148 | snHup->setEnabled(true);
|
---|
149 | }
|
---|
150 | //! [4]
|
---|