/usr/share/qt5/doc/qtdoc/unix-signals.html is in qt5-doc-html 5.9.5-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- unix-signal-handlers.qdoc -->
<title>Calling Qt Functions From Unix Signal Handlers | Qt 5.9</title>
<link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
<script type="text/javascript">
document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
// loading style sheet breaks anchors that were jumped to before
// so force jumping to anchor again
setTimeout(function() {
var anchor = location.hash;
// need to jump to different anchor first (e.g. none)
location.hash = "#";
setTimeout(function() {
location.hash = anchor;
}, 0);
}, 0);
</script>
</head>
<body>
<div class="header" id="qtdocheader">
<div class="main">
<div class="main-rounded">
<div class="navigationbar">
<table><tr>
<td ><a href="index.html">Qt 5.9</a></td><td ><a href="overviews-main.html#best-practices">Best Practice Guides</a></td><td >Calling Qt Functions From Unix Signal Handlers</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.9.5 Reference Documentation</td>
</tr></table>
</div>
</div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar"><div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Calling Qt Functions From Unix Signal Handlers</h1>
<span class="subtitle"></span>
<!-- $$$unix-signals.html-description -->
<div class="descr"> <a name="details"></a>
<p>You <i>can't</i> call Qt functions from Unix signal handlers. The standard POSIX rule applies: You can only call async-signal-safe functions from signal handlers. See <a href="http://www.opengroup.org/onlinepubs/000095399/functions/xsh_chap02_04.html#tag_02_04_01">Signal Actions</a> for the complete list of functions you can call from Unix signal handlers.</p>
<p>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.</p>
<p>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 <a href="../qtcore/qsocketnotifier.html">QSocketNotifier</a> to monitor the <i>read</i> 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.</p>
<pre class="cpp">
<span class="keyword">class</span> MyDaemon : <span class="keyword">public</span> <span class="type"><a href="../qtcore/qobject.html">QObject</a></span>
{
Q_OBJECT
<span class="keyword">public</span>:
MyDaemon(<span class="type"><a href="../qtcore/qobject.html">QObject</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);
<span class="operator">~</span>MyDaemon();
<span class="comment">// Unix signal handlers.</span>
<span class="keyword">static</span> <span class="type">void</span> hupSignalHandler(<span class="type">int</span> unused);
<span class="keyword">static</span> <span class="type">void</span> termSignalHandler(<span class="type">int</span> unused);
<span class="keyword">public</span> <span class="keyword">slots</span>:
<span class="comment">// Qt signal handlers.</span>
<span class="type">void</span> handleSigHup();
<span class="type">void</span> handleSigTerm();
<span class="keyword">private</span>:
<span class="keyword">static</span> <span class="type">int</span> sighupFd<span class="operator">[</span><span class="number">2</span><span class="operator">]</span>;
<span class="keyword">static</span> <span class="type">int</span> sigtermFd<span class="operator">[</span><span class="number">2</span><span class="operator">]</span>;
<span class="type"><a href="../qtcore/qsocketnotifier.html">QSocketNotifier</a></span> <span class="operator">*</span>snHup;
<span class="type"><a href="../qtcore/qsocketnotifier.html">QSocketNotifier</a></span> <span class="operator">*</span>snTerm;
};
</pre>
<p>In the MyDaemon constructor, use the socketpair(2) function to initialize each file descriptor pair, and then create the <a href="../qtcore/qsocketnotifier.html">QSocketNotifier</a> to monitor the <i>read</i> end of each pair. The activated() signal of each <a href="../qtcore/qsocketnotifier.html">QSocketNotifier</a> is connected to the appropriate slot function, which effectively converts the Unix signal to the <a href="../qtcore/qsocketnotifier.html#activated">QSocketNotifier::activated</a>() signal.</p>
<pre class="cpp">
MyDaemon<span class="operator">::</span>MyDaemon(<span class="type"><a href="../qtcore/qobject.html">QObject</a></span> <span class="operator">*</span>parent)
: <span class="type"><a href="../qtcore/qobject.html">QObject</a></span>(parent)
{
<span class="keyword">if</span> (<span class="operator">::</span>socketpair(AF_UNIX<span class="operator">,</span> SOCK_STREAM<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> sighupFd))
<a href="../qtcore/qtglobal.html#qFatal">qFatal</a>(<span class="string">"Couldn't create HUP socketpair"</span>);
<span class="keyword">if</span> (<span class="operator">::</span>socketpair(AF_UNIX<span class="operator">,</span> SOCK_STREAM<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> sigtermFd))
<a href="../qtcore/qtglobal.html#qFatal">qFatal</a>(<span class="string">"Couldn't create TERM socketpair"</span>);
snHup <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtcore/qsocketnotifier.html">QSocketNotifier</a></span>(sighupFd<span class="operator">[</span><span class="number">1</span><span class="operator">]</span><span class="operator">,</span> <span class="type"><a href="../qtcore/qsocketnotifier.html">QSocketNotifier</a></span><span class="operator">::</span>Read<span class="operator">,</span> <span class="keyword">this</span>);
connect(snHup<span class="operator">,</span> SIGNAL(activated(<span class="type">int</span>))<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(handleSigHup()));
snTerm <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtcore/qsocketnotifier.html">QSocketNotifier</a></span>(sigtermFd<span class="operator">[</span><span class="number">1</span><span class="operator">]</span><span class="operator">,</span> <span class="type"><a href="../qtcore/qsocketnotifier.html">QSocketNotifier</a></span><span class="operator">::</span>Read<span class="operator">,</span> <span class="keyword">this</span>);
connect(snTerm<span class="operator">,</span> SIGNAL(activated(<span class="type">int</span>))<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(handleSigTerm()));
<span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
}
</pre>
<p>Somewhere else in your startup code, you install your Unix signal handlers with sigaction(2).</p>
<pre class="cpp">
<span class="keyword">static</span> <span class="type">int</span> setup_unix_signal_handlers()
{
<span class="keyword">struct</span> sigaction hup<span class="operator">,</span> term;
hup<span class="operator">.</span>sa_handler <span class="operator">=</span> MyDaemon<span class="operator">::</span>hupSignalHandler;
sigemptyset(<span class="operator">&</span>hup<span class="operator">.</span>sa_mask);
hup<span class="operator">.</span>sa_flags <span class="operator">=</span> <span class="number">0</span>;
hup<span class="operator">.</span>sa_flags <span class="operator">|</span><span class="operator">=</span> SA_RESTART;
<span class="keyword">if</span> (sigaction(SIGHUP<span class="operator">,</span> <span class="operator">&</span>hup<span class="operator">,</span> <span class="number">0</span>))
<span class="keyword">return</span> <span class="number">1</span>;
term<span class="operator">.</span>sa_handler <span class="operator">=</span> MyDaemon<span class="operator">::</span>termSignalHandler;
sigemptyset(<span class="operator">&</span>term<span class="operator">.</span>sa_mask);
term<span class="operator">.</span>sa_flags <span class="operator">|</span><span class="operator">=</span> SA_RESTART;
<span class="keyword">if</span> (sigaction(SIGTERM<span class="operator">,</span> <span class="operator">&</span>term<span class="operator">,</span> <span class="number">0</span>))
<span class="keyword">return</span> <span class="number">2</span>;
<span class="keyword">return</span> <span class="number">0</span>;
}
</pre>
<p>In your Unix signal handlers, you write a byte to the <i>write</i> end of a socket pair and return. This will cause the corresponding <a href="../qtcore/qsocketnotifier.html">QSocketNotifier</a> to emit its activated() signal, which will in turn cause the appropriate Qt slot function to run.</p>
<pre class="cpp">
<span class="type">void</span> MyDaemon<span class="operator">::</span>hupSignalHandler(<span class="type">int</span>)
{
<span class="type">char</span> a <span class="operator">=</span> <span class="number">1</span>;
<span class="operator">::</span>write(sighupFd<span class="operator">[</span><span class="number">0</span><span class="operator">]</span><span class="operator">,</span> <span class="operator">&</span>a<span class="operator">,</span> <span class="keyword">sizeof</span>(a));
}
<span class="type">void</span> MyDaemon<span class="operator">::</span>termSignalHandler(<span class="type">int</span>)
{
<span class="type">char</span> a <span class="operator">=</span> <span class="number">1</span>;
<span class="operator">::</span>write(sigtermFd<span class="operator">[</span><span class="number">0</span><span class="operator">]</span><span class="operator">,</span> <span class="operator">&</span>a<span class="operator">,</span> <span class="keyword">sizeof</span>(a));
}
</pre>
<p>In the slot functions connected to the <a href="../qtcore/qsocketnotifier.html#activated">QSocketNotifier::activated</a>() signals, you <i>read</i> 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.</p>
<pre class="cpp">
<span class="type">void</span> MyDaemon<span class="operator">::</span>handleSigTerm()
{
snTerm<span class="operator">-</span><span class="operator">></span>setEnabled(<span class="keyword">false</span>);
<span class="type">char</span> tmp;
<span class="operator">::</span>read(sigtermFd<span class="operator">[</span><span class="number">1</span><span class="operator">]</span><span class="operator">,</span> <span class="operator">&</span>tmp<span class="operator">,</span> <span class="keyword">sizeof</span>(tmp));
<span class="comment">// do Qt stuff</span>
snTerm<span class="operator">-</span><span class="operator">></span>setEnabled(<span class="keyword">true</span>);
}
<span class="type">void</span> MyDaemon<span class="operator">::</span>handleSigHup()
{
snHup<span class="operator">-</span><span class="operator">></span>setEnabled(<span class="keyword">false</span>);
<span class="type">char</span> tmp;
<span class="operator">::</span>read(sighupFd<span class="operator">[</span><span class="number">1</span><span class="operator">]</span><span class="operator">,</span> <span class="operator">&</span>tmp<span class="operator">,</span> <span class="keyword">sizeof</span>(tmp));
<span class="comment">// do Qt stuff</span>
snHup<span class="operator">-</span><span class="operator">></span>setEnabled(<span class="keyword">true</span>);
}
</pre>
</div>
<!-- @@@unix-signals.html -->
</div>
</div>
</div>
</div>
</div>
<div class="footer">
<p>
<acronym title="Copyright">©</acronym> 2017 The Qt Company Ltd.
Documentation contributions included herein are the copyrights of
their respective owners.<br> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br> Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property
of their respective owners. </p>
</div>
</body>
</html>
|