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