Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/network/socket/qlocalserver_unix.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||
2 | ** | - | ||||||||||||
3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||
4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||
5 | ** | - | ||||||||||||
6 | ** This file is part of the QtNetwork module of the Qt Toolkit. | - | ||||||||||||
7 | ** | - | ||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||
9 | ** Commercial License Usage | - | ||||||||||||
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||
11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||
12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||
13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||
14 | ** and conditions see http://www.qt.io/terms-conditions. For further | - | ||||||||||||
15 | ** information use the contact form at http://www.qt.io/contact-us. | - | ||||||||||||
16 | ** | - | ||||||||||||
17 | ** GNU Lesser General Public License Usage | - | ||||||||||||
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||
19 | ** General Public License version 2.1 or version 3 as published by the Free | - | ||||||||||||
20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||
21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||
22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||
23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||
24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||
25 | ** | - | ||||||||||||
26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||
27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||
29 | ** | - | ||||||||||||
30 | ** $QT_END_LICENSE$ | - | ||||||||||||
31 | ** | - | ||||||||||||
32 | ****************************************************************************/ | - | ||||||||||||
33 | - | |||||||||||||
34 | #include "qlocalserver.h" | - | ||||||||||||
35 | #include "qlocalserver_p.h" | - | ||||||||||||
36 | #include "qlocalsocket.h" | - | ||||||||||||
37 | #include "qlocalsocket_p.h" | - | ||||||||||||
38 | #include "qnet_unix_p.h" | - | ||||||||||||
39 | #include "qtemporarydir.h" | - | ||||||||||||
40 | - | |||||||||||||
41 | #ifndef QT_NO_LOCALSERVER | - | ||||||||||||
42 | - | |||||||||||||
43 | #include <sys/socket.h> | - | ||||||||||||
44 | #include <sys/un.h> | - | ||||||||||||
45 | - | |||||||||||||
46 | #include <qdebug.h> | - | ||||||||||||
47 | #include <qdir.h> | - | ||||||||||||
48 | #include <qdatetime.h> | - | ||||||||||||
49 | - | |||||||||||||
50 | #ifdef Q_OS_VXWORKS | - | ||||||||||||
51 | # include <selectLib.h> | - | ||||||||||||
52 | #endif | - | ||||||||||||
53 | - | |||||||||||||
54 | QT_BEGIN_NAMESPACE | - | ||||||||||||
55 | - | |||||||||||||
56 | void QLocalServerPrivate::init() | - | ||||||||||||
57 | { | - | ||||||||||||
58 | } | - | ||||||||||||
59 | - | |||||||||||||
60 | bool QLocalServerPrivate::removeServer(const QString &name) | - | ||||||||||||
61 | { | - | ||||||||||||
62 | QString fileName; | - | ||||||||||||
63 | if (name.startsWith(QLatin1Char('/'))) {
| 0-35 | ||||||||||||
64 | fileName = name; | - | ||||||||||||
65 | } else { never executed: end of block | 0 | ||||||||||||
66 | fileName = QDir::cleanPath(QDir::tempPath()); | - | ||||||||||||
67 | fileName += QLatin1Char('/') + name; | - | ||||||||||||
68 | } executed 35 times by 2 tests: end of block Executed by:
| 35 | ||||||||||||
69 | if (QFile::exists(fileName))
| 9-26 | ||||||||||||
70 | return QFile::remove(fileName); executed 9 times by 1 test: return QFile::remove(fileName); Executed by:
| 9 | ||||||||||||
71 | else | - | ||||||||||||
72 | return true; executed 26 times by 2 tests: return true; Executed by:
| 26 | ||||||||||||
73 | } | - | ||||||||||||
74 | - | |||||||||||||
75 | bool QLocalServerPrivate::listen(const QString &requestedServerName) | - | ||||||||||||
76 | { | - | ||||||||||||
77 | Q_Q(QLocalServer); | - | ||||||||||||
78 | - | |||||||||||||
79 | // determine the full server path | - | ||||||||||||
80 | if (requestedServerName.startsWith(QLatin1Char('/'))) {
| 1-47 | ||||||||||||
81 | fullServerName = requestedServerName; | - | ||||||||||||
82 | } else { executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||
83 | fullServerName = QDir::cleanPath(QDir::tempPath()); | - | ||||||||||||
84 | fullServerName += QLatin1Char('/') + requestedServerName; | - | ||||||||||||
85 | } executed 47 times by 3 tests: end of block Executed by:
| 47 | ||||||||||||
86 | serverName = requestedServerName; | - | ||||||||||||
87 | - | |||||||||||||
88 | QByteArray encodedTempPath; | - | ||||||||||||
89 | const QByteArray encodedFullServerName = QFile::encodeName(fullServerName); | - | ||||||||||||
90 | QScopedPointer<QTemporaryDir> tempDir; | - | ||||||||||||
91 | - | |||||||||||||
92 | // Check any of the flags | - | ||||||||||||
93 | if (socketOptions & QLocalServer::WorldAccessOption) {
| 4-44 | ||||||||||||
94 | QFileInfo serverNameFileInfo(fullServerName); | - | ||||||||||||
95 | tempDir.reset(new QTemporaryDir(serverNameFileInfo.absolutePath() + QLatin1Char('/'))); | - | ||||||||||||
96 | if (!tempDir->isValid()) {
| 0-4 | ||||||||||||
97 | setError(QLatin1String("QLocalServer::listen")); | - | ||||||||||||
98 | return false; never executed: return false; | 0 | ||||||||||||
99 | } | - | ||||||||||||
100 | encodedTempPath = QFile::encodeName(tempDir->path() + QLatin1String("/s")); | - | ||||||||||||
101 | } executed 4 times by 1 test: end of block Executed by:
| 4 | ||||||||||||
102 | - | |||||||||||||
103 | // create the unix socket | - | ||||||||||||
104 | listenSocket = qt_safe_socket(PF_UNIX, SOCK_STREAM, 0); | - | ||||||||||||
105 | if (-1 == listenSocket) {
| 0-48 | ||||||||||||
106 | setError(QLatin1String("QLocalServer::listen")); | - | ||||||||||||
107 | closeServer(); | - | ||||||||||||
108 | return false; never executed: return false; | 0 | ||||||||||||
109 | } | - | ||||||||||||
110 | - | |||||||||||||
111 | // Construct the unix address | - | ||||||||||||
112 | struct ::sockaddr_un addr; | - | ||||||||||||
113 | addr.sun_family = PF_UNIX; | - | ||||||||||||
114 | if (sizeof(addr.sun_path) < (uint)encodedFullServerName.size() + 1) {
| 1-47 | ||||||||||||
115 | setError(QLatin1String("QLocalServer::listen")); | - | ||||||||||||
116 | closeServer(); | - | ||||||||||||
117 | return false; executed 1 time by 1 test: return false; Executed by:
| 1 | ||||||||||||
118 | } | - | ||||||||||||
119 | - | |||||||||||||
120 | if (socketOptions & QLocalServer::WorldAccessOption) {
| 4-43 | ||||||||||||
121 | if (sizeof(addr.sun_path) < (uint)encodedTempPath.size() + 1) {
| 0-4 | ||||||||||||
122 | setError(QLatin1String("QLocalServer::listen")); | - | ||||||||||||
123 | closeServer(); | - | ||||||||||||
124 | return false; never executed: return false; | 0 | ||||||||||||
125 | } | - | ||||||||||||
126 | ::memcpy(addr.sun_path, encodedTempPath.constData(), | - | ||||||||||||
127 | encodedTempPath.size() + 1); | - | ||||||||||||
128 | } else { executed 4 times by 1 test: end of block Executed by:
| 4 | ||||||||||||
129 | ::memcpy(addr.sun_path, encodedFullServerName.constData(), | - | ||||||||||||
130 | encodedFullServerName.size() + 1); | - | ||||||||||||
131 | } executed 43 times by 3 tests: end of block Executed by:
| 43 | ||||||||||||
132 | - | |||||||||||||
133 | // bind | - | ||||||||||||
134 | if(-1 == QT_SOCKET_BIND(listenSocket, (sockaddr *)&addr, sizeof(sockaddr_un))) {
| 1-46 | ||||||||||||
135 | setError(QLatin1String("QLocalServer::listen")); | - | ||||||||||||
136 | // if address is in use already, just close the socket, but do not delete the file | - | ||||||||||||
137 | if(errno == EADDRINUSE)
| 0-1 | ||||||||||||
138 | QT_CLOSE(listenSocket); executed 1 time by 1 test: qt_safe_close(listenSocket); Executed by:
| 1 | ||||||||||||
139 | // otherwise, close the socket and delete the file | - | ||||||||||||
140 | else | - | ||||||||||||
141 | closeServer(); never executed: closeServer(); | 0 | ||||||||||||
142 | listenSocket = -1; | - | ||||||||||||
143 | return false; executed 1 time by 1 test: return false; Executed by:
| 1 | ||||||||||||
144 | } | - | ||||||||||||
145 | - | |||||||||||||
146 | // listen for connections | - | ||||||||||||
147 | if (-1 == qt_safe_listen(listenSocket, 50)) {
| 0-46 | ||||||||||||
148 | setError(QLatin1String("QLocalServer::listen")); | - | ||||||||||||
149 | closeServer(); | - | ||||||||||||
150 | listenSocket = -1; | - | ||||||||||||
151 | if (error != QAbstractSocket::AddressInUseError)
| 0 | ||||||||||||
152 | QFile::remove(fullServerName); never executed: QFile::remove(fullServerName); | 0 | ||||||||||||
153 | return false; never executed: return false; | 0 | ||||||||||||
154 | } | - | ||||||||||||
155 | - | |||||||||||||
156 | if (socketOptions & QLocalServer::WorldAccessOption) {
| 4-42 | ||||||||||||
157 | mode_t mode = 000; | - | ||||||||||||
158 | - | |||||||||||||
159 | if (socketOptions & QLocalServer::UserAccessOption)
| 2 | ||||||||||||
160 | mode |= S_IRWXU; executed 2 times by 1 test: mode |= (0400|0200|0100); Executed by:
| 2 | ||||||||||||
161 | - | |||||||||||||
162 | if (socketOptions & QLocalServer::GroupAccessOption)
| 2 | ||||||||||||
163 | mode |= S_IRWXG; executed 2 times by 1 test: mode |= ((0400|0200|0100) >> 3); Executed by:
| 2 | ||||||||||||
164 | - | |||||||||||||
165 | if (socketOptions & QLocalServer::OtherAccessOption)
| 2 | ||||||||||||
166 | mode |= S_IRWXO; executed 2 times by 1 test: mode |= (((0400|0200|0100) >> 3) >> 3); Executed by:
| 2 | ||||||||||||
167 | - | |||||||||||||
168 | if (::chmod(encodedTempPath.constData(), mode) == -1) {
| 0-4 | ||||||||||||
169 | setError(QLatin1String("QLocalServer::listen")); | - | ||||||||||||
170 | closeServer(); | - | ||||||||||||
171 | return false; never executed: return false; | 0 | ||||||||||||
172 | } | - | ||||||||||||
173 | - | |||||||||||||
174 | if (::rename(encodedTempPath.constData(), encodedFullServerName.constData()) == -1) {
| 0-4 | ||||||||||||
175 | setError(QLatin1String("QLocalServer::listen")); | - | ||||||||||||
176 | closeServer(); | - | ||||||||||||
177 | return false; never executed: return false; | 0 | ||||||||||||
178 | } | - | ||||||||||||
179 | } executed 4 times by 1 test: end of block Executed by:
| 4 | ||||||||||||
180 | - | |||||||||||||
181 | Q_ASSERT(!socketNotifier); | - | ||||||||||||
182 | socketNotifier = new QSocketNotifier(listenSocket, | - | ||||||||||||
183 | QSocketNotifier::Read, q); | - | ||||||||||||
184 | q->connect(socketNotifier, SIGNAL(activated(int)), | - | ||||||||||||
185 | q, SLOT(_q_onNewConnection())); | - | ||||||||||||
186 | socketNotifier->setEnabled(maxPendingConnections > 0); | - | ||||||||||||
187 | return true; executed 46 times by 3 tests: return true; Executed by:
| 46 | ||||||||||||
188 | } | - | ||||||||||||
189 | - | |||||||||||||
190 | bool QLocalServerPrivate::listen(qintptr socketDescriptor) | - | ||||||||||||
191 | { | - | ||||||||||||
192 | Q_Q(QLocalServer); | - | ||||||||||||
193 | - | |||||||||||||
194 | // Attach to the localsocket | - | ||||||||||||
195 | listenSocket = socketDescriptor; | - | ||||||||||||
196 | - | |||||||||||||
197 | ::fcntl(listenSocket, F_SETFD, FD_CLOEXEC); | - | ||||||||||||
198 | ::fcntl(listenSocket, F_SETFL, ::fcntl(listenSocket, F_GETFL) | O_NONBLOCK); | - | ||||||||||||
199 | - | |||||||||||||
200 | #ifdef Q_OS_LINUX | - | ||||||||||||
201 | struct ::sockaddr_un addr; | - | ||||||||||||
202 | QT_SOCKLEN_T len = sizeof(addr); | - | ||||||||||||
203 | memset(&addr, 0, sizeof(addr)); | - | ||||||||||||
204 | if (0 == ::getsockname(listenSocket, (sockaddr *)&addr, &len)) {
| 0-4 | ||||||||||||
205 | // check for absract sockets | - | ||||||||||||
206 | if (addr.sun_family == PF_UNIX && addr.sun_path[0] == 0) {
| 0-4 | ||||||||||||
207 | addr.sun_path[0] = '@'; | - | ||||||||||||
208 | } executed 3 times by 1 test: end of block Executed by:
| 3 | ||||||||||||
209 | QString name = QString::fromLatin1(addr.sun_path); | - | ||||||||||||
210 | if (!name.isEmpty()) {
| 0-4 | ||||||||||||
211 | fullServerName = name; | - | ||||||||||||
212 | serverName = fullServerName.mid(fullServerName.lastIndexOf(QLatin1Char('/')) + 1); | - | ||||||||||||
213 | if (serverName.isEmpty()) {
| 0-4 | ||||||||||||
214 | serverName = fullServerName; | - | ||||||||||||
215 | } never executed: end of block | 0 | ||||||||||||
216 | } executed 4 times by 1 test: end of block Executed by:
| 4 | ||||||||||||
217 | } executed 4 times by 1 test: end of block Executed by:
| 4 | ||||||||||||
218 | #else | - | ||||||||||||
219 | serverName.clear(); | - | ||||||||||||
220 | fullServerName.clear(); | - | ||||||||||||
221 | #endif | - | ||||||||||||
222 | - | |||||||||||||
223 | Q_ASSERT(!socketNotifier); | - | ||||||||||||
224 | socketNotifier = new QSocketNotifier(listenSocket, | - | ||||||||||||
225 | QSocketNotifier::Read, q); | - | ||||||||||||
226 | q->connect(socketNotifier, SIGNAL(activated(int)), | - | ||||||||||||
227 | q, SLOT(_q_onNewConnection())); | - | ||||||||||||
228 | socketNotifier->setEnabled(maxPendingConnections > 0); | - | ||||||||||||
229 | return true; executed 4 times by 1 test: return true; Executed by:
| 4 | ||||||||||||
230 | } | - | ||||||||||||
231 | - | |||||||||||||
232 | /*! | - | ||||||||||||
233 | \internal | - | ||||||||||||
234 | - | |||||||||||||
235 | \sa QLocalServer::closeServer() | - | ||||||||||||
236 | */ | - | ||||||||||||
237 | void QLocalServerPrivate::closeServer() | - | ||||||||||||
238 | { | - | ||||||||||||
239 | if (socketNotifier) {
| 1-50 | ||||||||||||
240 | socketNotifier->setEnabled(false); // Otherwise, closed socket is checked before deleter runs | - | ||||||||||||
241 | socketNotifier->deleteLater(); | - | ||||||||||||
242 | socketNotifier = 0; | - | ||||||||||||
243 | } executed 50 times by 3 tests: end of block Executed by:
| 50 | ||||||||||||
244 | - | |||||||||||||
245 | if (-1 != listenSocket)
| 0-51 | ||||||||||||
246 | QT_CLOSE(listenSocket); executed 51 times by 3 tests: qt_safe_close(listenSocket); Executed by:
| 51 | ||||||||||||
247 | listenSocket = -1; | - | ||||||||||||
248 | - | |||||||||||||
249 | if (!fullServerName.isEmpty())
| 0-51 | ||||||||||||
250 | QFile::remove(fullServerName); executed 51 times by 3 tests: QFile::remove(fullServerName); Executed by:
| 51 | ||||||||||||
251 | } executed 51 times by 3 tests: end of block Executed by:
| 51 | ||||||||||||
252 | - | |||||||||||||
253 | /*! | - | ||||||||||||
254 | \internal | - | ||||||||||||
255 | - | |||||||||||||
256 | We have received a notification that we can read on the listen socket. | - | ||||||||||||
257 | Accept the new socket. | - | ||||||||||||
258 | */ | - | ||||||||||||
259 | void QLocalServerPrivate::_q_onNewConnection() | - | ||||||||||||
260 | { | - | ||||||||||||
261 | Q_Q(QLocalServer); | - | ||||||||||||
262 | if (-1 == listenSocket)
| 0-77 | ||||||||||||
263 | return; never executed: return; | 0 | ||||||||||||
264 | - | |||||||||||||
265 | ::sockaddr_un addr; | - | ||||||||||||
266 | QT_SOCKLEN_T length = sizeof(sockaddr_un); | - | ||||||||||||
267 | int connectedSocket = qt_safe_accept(listenSocket, (sockaddr *)&addr, &length); | - | ||||||||||||
268 | if(-1 == connectedSocket) {
| 0-77 | ||||||||||||
269 | setError(QLatin1String("QLocalSocket::activated")); | - | ||||||||||||
270 | closeServer(); | - | ||||||||||||
271 | } else { never executed: end of block | 0 | ||||||||||||
272 | socketNotifier->setEnabled(pendingConnections.size() | - | ||||||||||||
273 | <= maxPendingConnections); | - | ||||||||||||
274 | q->incomingConnection(connectedSocket); | - | ||||||||||||
275 | } executed 77 times by 3 tests: end of block Executed by:
| 77 | ||||||||||||
276 | } | - | ||||||||||||
277 | - | |||||||||||||
278 | void QLocalServerPrivate::waitForNewConnection(int msec, bool *timedOut) | - | ||||||||||||
279 | { | - | ||||||||||||
280 | fd_set readfds; | - | ||||||||||||
281 | FD_ZERO(&readfds); | - | ||||||||||||
282 | FD_SET(listenSocket, &readfds); | - | ||||||||||||
283 | - | |||||||||||||
284 | struct timespec timeout; | - | ||||||||||||
285 | timeout.tv_sec = msec / 1000; | - | ||||||||||||
286 | timeout.tv_nsec = (msec % 1000) * 1000 * 1000; | - | ||||||||||||
287 | - | |||||||||||||
288 | int result = -1; | - | ||||||||||||
289 | result = qt_safe_select(listenSocket + 1, &readfds, 0, 0, (msec == -1) ? 0 : &timeout); | - | ||||||||||||
290 | if (-1 == result) {
| 0-79 | ||||||||||||
291 | setError(QLatin1String("QLocalServer::waitForNewConnection")); | - | ||||||||||||
292 | closeServer(); | - | ||||||||||||
293 | } never executed: end of block | 0 | ||||||||||||
294 | if (result > 0)
| 2-77 | ||||||||||||
295 | _q_onNewConnection(); executed 77 times by 3 tests: _q_onNewConnection(); Executed by:
| 77 | ||||||||||||
296 | if (timedOut)
| 25-54 | ||||||||||||
297 | *timedOut = (result == 0); executed 54 times by 1 test: *timedOut = (result == 0); Executed by:
| 54 | ||||||||||||
298 | } executed 79 times by 3 tests: end of block Executed by:
| 79 | ||||||||||||
299 | - | |||||||||||||
300 | void QLocalServerPrivate::setError(const QString &function) | - | ||||||||||||
301 | { | - | ||||||||||||
302 | if (EAGAIN == errno)
| 0-2 | ||||||||||||
303 | return; never executed: return; | 0 | ||||||||||||
304 | - | |||||||||||||
305 | switch (errno) { | - | ||||||||||||
306 | case EACCES: never executed: case 13: | 0 | ||||||||||||
307 | errorString = QLocalServer::tr("%1: Permission denied").arg(function); | - | ||||||||||||
308 | error = QAbstractSocket::SocketAccessError; | - | ||||||||||||
309 | break; never executed: break; | 0 | ||||||||||||
310 | case ELOOP: never executed: case 40: | 0 | ||||||||||||
311 | case ENOENT: never executed: case 2: | 0 | ||||||||||||
312 | case ENAMETOOLONG: executed 1 time by 1 test: case 36: Executed by:
| 1 | ||||||||||||
313 | case EROFS: never executed: case 30: | 0 | ||||||||||||
314 | case ENOTDIR: never executed: case 20: | 0 | ||||||||||||
315 | errorString = QLocalServer::tr("%1: Name error").arg(function); | - | ||||||||||||
316 | error = QAbstractSocket::HostNotFoundError; | - | ||||||||||||
317 | break; executed 1 time by 1 test: break; Executed by:
| 1 | ||||||||||||
318 | case EADDRINUSE: executed 1 time by 1 test: case 98: Executed by:
| 1 | ||||||||||||
319 | errorString = QLocalServer::tr("%1: Address in use").arg(function); | - | ||||||||||||
320 | error = QAbstractSocket::AddressInUseError; | - | ||||||||||||
321 | break; executed 1 time by 1 test: break; Executed by:
| 1 | ||||||||||||
322 | - | |||||||||||||
323 | default: never executed: default: | 0 | ||||||||||||
324 | errorString = QLocalServer::tr("%1: Unknown error %2") | - | ||||||||||||
325 | .arg(function).arg(errno); | - | ||||||||||||
326 | error = QAbstractSocket::UnknownSocketError; | - | ||||||||||||
327 | #if defined QLOCALSERVER_DEBUG | - | ||||||||||||
328 | qWarning() << errorString << "fullServerName:" << fullServerName; | - | ||||||||||||
329 | #endif | - | ||||||||||||
330 | } never executed: end of block | 0 | ||||||||||||
331 | } | - | ||||||||||||
332 | - | |||||||||||||
333 | QT_END_NAMESPACE | - | ||||||||||||
334 | - | |||||||||||||
335 | #endif // QT_NO_LOCALSERVER | - | ||||||||||||
Source code | Switch to Preprocessed file |