| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2018 The Qt Company Ltd and/or its subsidiary(-ies). |
| 4 | ** Contact: http://www.qt-project.org/legal |
| 5 | ** |
| 6 | ** This file is part of the QtSystems 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 "qremoteserviceregister_p.h" |
| 41 | #include "qremoteserviceregister_ls_p.h" |
| 42 | #include "ipcendpoint_p.h" |
| 43 | #include "objectendpoint_p.h" |
| 44 | #include "qserviceclientcredentials_p.h" |
| 45 | #include "qserviceclientcredentials.h" |
| 46 | |
| 47 | #include <QLocalServer> |
| 48 | #include <QEventLoop> |
| 49 | #include <QLocalSocket> |
| 50 | #include <QDataStream> |
| 51 | #include <QTimer> |
| 52 | #include <QProcess> |
| 53 | #include <QFile> |
| 54 | #include <QCoreApplication> |
| 55 | #include <QScopedPointer> |
| 56 | #include <QDir> |
| 57 | |
| 58 | #include <time.h> |
| 59 | #include <sys/types.h> |
| 60 | |
| 61 | #ifndef Q_OS_WIN |
| 62 | #include <sys/un.h> |
| 63 | #include <sys/socket.h> |
| 64 | #include <errno.h> |
| 65 | #else |
| 66 | // Needed for ::Sleep, while we wait for a better solution |
| 67 | #include <windows.h> |
| 68 | #include <winbase.h> |
| 69 | #endif |
| 70 | |
| 71 | #ifdef LOCAL_PEERCRED /* from sys/un.h */ |
| 72 | #include <sys/ucred.h> |
| 73 | #endif |
| 74 | |
| 75 | QT_BEGIN_NAMESPACE |
| 76 | |
| 77 | //IPC based on QLocalSocket |
| 78 | |
| 79 | class LocalSocketEndPoint : public QServiceIpcEndPoint |
| 80 | { |
| 81 | Q_OBJECT |
| 82 | public: |
| 83 | LocalSocketEndPoint(QLocalSocket* s, QObject* parent = 0) |
| 84 | : QServiceIpcEndPoint(parent), |
| 85 | socket(s), |
| 86 | pending_bytes(0) |
| 87 | |
| 88 | { |
| 89 | Q_ASSERT(socket); |
| 90 | socket->setParent(this); |
| 91 | |
| 92 | connect(s, SIGNAL(readyRead()), this, SLOT(readIncoming())); |
| 93 | connect(s, SIGNAL(disconnected()), this, SIGNAL(disconnected())); |
| 94 | connect(s, SIGNAL(disconnected()), this, SLOT(ipcfault())); |
| 95 | connect(s, SIGNAL(error(QLocalSocket::LocalSocketError)), |
| 96 | this, SLOT(socketError(QLocalSocket::LocalSocketError))); |
| 97 | |
| 98 | if (socket->bytesAvailable()) |
| 99 | QTimer::singleShot(0, this, SLOT(readIncoming())); |
| 100 | |
| 101 | } |
| 102 | |
| 103 | ~LocalSocketEndPoint() |
| 104 | { |
| 105 | disconnect(this, SLOT(ipcfault())); |
| 106 | socket->close(); |
| 107 | } |
| 108 | |
| 109 | void getSecurityCredentials(QServiceClientCredentials &creds) |
| 110 | { |
| 111 | //LocalSocketEndPoint owns socket |
| 112 | int fd = socket->socketDescriptor(); |
| 113 | #if defined(LOCAL_PEERCRED) |
| 114 | struct xucred xuc; |
| 115 | socklen_t len = sizeof(struct xucred); |
| 116 | |
| 117 | if (getsockopt(fd, SOL_SOCKET, LOCAL_PEERCRED, &xuc, &len) == 0) { |
| 118 | creds.d->pid = -1; // No PID on bsd |
| 119 | creds.d->uid = xuc.cr_uid; |
| 120 | creds.d->gid = xuc.cr_gid; |
| 121 | |
| 122 | } else { |
| 123 | qDebug("SFW getsockopt failed: %s", qPrintable(qt_error_string(errno))); |
| 124 | } |
| 125 | #elif defined(SO_PEERCRED) |
| 126 | struct ucred uc; |
| 127 | socklen_t len = sizeof(struct ucred); |
| 128 | |
| 129 | if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &uc, &len) == 0) { |
| 130 | creds.d->pid = uc.pid; |
| 131 | creds.d->uid = uc.uid; |
| 132 | creds.d->gid = uc.gid; |
| 133 | } else { |
| 134 | qDebug("SFW getsockopt failed: %s", qPrintable(qt_error_string(errno))); |
| 135 | } |
| 136 | #else |
| 137 | Q_UNUSED(creds); |
| 138 | Q_UNUSED(fd); |
| 139 | #endif |
| 140 | } |
| 141 | |
| 142 | void terminateConnection() |
| 143 | { |
| 144 | socket->close(); |
| 145 | } |
| 146 | |
| 147 | |
| 148 | Q_SIGNALS: |
| 149 | void errorUnrecoverableIPCFault(QService::UnrecoverableIPCError); |
| 150 | |
| 151 | |
| 152 | protected: |
| 153 | void flushPackage(const QServicePackage& package) |
| 154 | { |
| 155 | QByteArray block; |
| 156 | QDataStream out(&block, QIODevice::WriteOnly); |
| 157 | out.setVersion(QDataStream::Qt_4_6); |
| 158 | out << package; |
| 159 | |
| 160 | QByteArray sizeblock; |
| 161 | QDataStream outsize(&sizeblock, QIODevice::WriteOnly); |
| 162 | outsize.setVersion(QDataStream::Qt_4_6); |
| 163 | |
| 164 | quint32 size = block.length(); |
| 165 | outsize << size; |
| 166 | |
| 167 | int bytes = socket->write(sizeblock); |
| 168 | if (bytes != sizeof(quint32)) { |
| 169 | qWarning() << "Failed to write length"; |
| 170 | socket->close(); |
| 171 | return; |
| 172 | } |
| 173 | bytes = socket->write(block); |
| 174 | if (bytes != block.length()){ |
| 175 | qWarning() << "Can't send package, socket error" << block.length() << bytes; |
| 176 | socket->close(); |
| 177 | return; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | protected Q_SLOTS: |
| 182 | void readIncoming() |
| 183 | { |
| 184 | while (socket->bytesAvailable()) { |
| 185 | |
| 186 | if (pending_bytes == 0) { /* New packet */ |
| 187 | QByteArray data = socket->read(4 - pending_header.length()); |
| 188 | pending_header.append(data); |
| 189 | if (pending_header.length() == 4) { |
| 190 | QDataStream in_size(&pending_header, QIODevice::ReadOnly); |
| 191 | in_size.setVersion(QDataStream::Qt_4_6); |
| 192 | in_size >> pending_bytes; |
| 193 | pending_buf.clear(); |
| 194 | pending_header.clear(); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | if (pending_bytes) { /* read any new data and add to buffer */ |
| 199 | int readsize = pending_bytes; |
| 200 | if (socket->bytesAvailable() < readsize) { |
| 201 | readsize = socket->bytesAvailable(); |
| 202 | } |
| 203 | pending_buf.append(socket->read(readsize)); |
| 204 | pending_bytes -= readsize; |
| 205 | } |
| 206 | |
| 207 | if (pending_bytes == 0 && !pending_buf.isEmpty()) { |
| 208 | QDataStream in(pending_buf); |
| 209 | in.setVersion(QDataStream::Qt_4_6); |
| 210 | QServicePackage package; |
| 211 | in >> package; |
| 212 | incoming.enqueue(package); |
| 213 | pending_buf.clear(); |
| 214 | emit readyRead(); |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | void socketError(QLocalSocket::LocalSocketError err) |
| 220 | { |
| 221 | Q_UNUSED(err) |
| 222 | // qWarning() << "Socket error!" << err << socket->errorString(); |
| 223 | } |
| 224 | |
| 225 | protected Q_SLOTS: |
| 226 | void ipcfault() |
| 227 | { |
| 228 | emit errorUnrecoverableIPCFault(QService::ErrorServiceNoLongerAvailable); |
| 229 | } |
| 230 | |
| 231 | private: |
| 232 | QLocalSocket* socket; |
| 233 | QRemoteServiceRegisterLocalSocketPrivate *serviceRegPriv; |
| 234 | QByteArray pending_header; |
| 235 | QByteArray pending_buf; |
| 236 | quint32 pending_bytes; |
| 237 | }; |
| 238 | |
| 239 | QRemoteServiceRegisterLocalSocketPrivate::QRemoteServiceRegisterLocalSocketPrivate(QObject* parent) |
| 240 | : QRemoteServiceRegisterPrivate(parent) |
| 241 | { |
| 242 | } |
| 243 | |
| 244 | void QRemoteServiceRegisterLocalSocketPrivate::publishServices( const QString& ident) |
| 245 | { |
| 246 | createServiceEndPoint(ident); |
| 247 | } |
| 248 | |
| 249 | void QRemoteServiceRegisterLocalSocketPrivate::processIncoming() |
| 250 | { |
| 251 | if (localServer->hasPendingConnections()) { |
| 252 | QLocalSocket* s = localServer->nextPendingConnection(); |
| 253 | |
| 254 | //LocalSocketEndPoint owns socket |
| 255 | LocalSocketEndPoint* ipcEndPoint = new LocalSocketEndPoint(s, this); |
| 256 | |
| 257 | if (getSecurityFilter()){ |
| 258 | QServiceClientCredentials creds; |
| 259 | ipcEndPoint->getSecurityCredentials(creds); |
| 260 | |
| 261 | qDebug() << "Security filter call"; |
| 262 | getSecurityFilter()(&creds); |
| 263 | if (!creds.isClientAccepted()) { |
| 264 | s->close(); |
| 265 | return; |
| 266 | } |
| 267 | } |
| 268 | ObjectEndPoint* endpoint = new ObjectEndPoint(ObjectEndPoint::Service, ipcEndPoint, this); |
| 269 | Q_UNUSED(endpoint); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | /* |
| 274 | Creates endpoint on service side. |
| 275 | */ |
| 276 | bool QRemoteServiceRegisterLocalSocketPrivate::createServiceEndPoint(const QString& ident) |
| 277 | { |
| 278 | |
| 279 | localServer = new QLocalServer(this); |
| 280 | connect(localServer, SIGNAL(newConnection()), this, SLOT(processIncoming())); |
| 281 | |
| 282 | //other IPC mechanisms such as dbus may have to publish the |
| 283 | //meta object definition for all registered service types |
| 284 | QLocalServer::removeServer(ident); |
| 285 | |
| 286 | if ( !localServer->listen(ident) ) { |
| 287 | qWarning() << "Cannot create local socket endpoint"; |
| 288 | return false; |
| 289 | } |
| 290 | |
| 291 | if (localServer->hasPendingConnections()) |
| 292 | QMetaObject::invokeMethod(this, "processIncoming", Qt::QueuedConnection); |
| 293 | |
| 294 | return true; |
| 295 | } |
| 296 | |
| 297 | QRemoteServiceRegisterPrivate* QRemoteServiceRegisterPrivate::constructPrivateObject(QObject *parent) |
| 298 | { |
| 299 | return new QRemoteServiceRegisterLocalSocketPrivate(parent); |
| 300 | } |
| 301 | |
| 302 | /* |
| 303 | Creates endpoint on client side. |
| 304 | */ |
| 305 | QObject* QRemoteServiceRegisterPrivate::proxyForService(const QRemoteServiceRegister::Entry& entry, const QString& location) |
| 306 | { |
| 307 | qWarning() << "(all ok) starting SFW proxyForService" << location; |
| 308 | |
| 309 | QLocalSocket* socket = new QLocalSocket(); |
| 310 | |
| 311 | socket->connectToServer(location); |
| 312 | |
| 313 | if (!socket->waitForConnected()){ |
| 314 | if (!socket->isValid()) { |
| 315 | QString path = location; |
| 316 | qWarning() << "Cannot connect to remote service, trying to start service " << path; |
| 317 | |
| 318 | qint64 pid = 0; |
| 319 | // Start the service as a detached process |
| 320 | if (QProcess::startDetached(path, QStringList(), QString(), &pid)){ |
| 321 | int i; |
| 322 | socket->connectToServer(location); |
| 323 | for (i = 0; !socket->isValid() && i < 1000; i++){ |
| 324 | // Temporary hack till we can improve startup signaling |
| 325 | #ifdef Q_OS_WIN |
| 326 | ::Sleep(10); |
| 327 | #else |
| 328 | struct timespec tm; |
| 329 | tm.tv_sec = 0; |
| 330 | tm.tv_nsec = 1000000; |
| 331 | nanosleep(&tm, 0x0); |
| 332 | #endif /* Q_OS_WIN */ |
| 333 | socket->connectToServer(location); |
| 334 | // keep trying for a while |
| 335 | } |
| 336 | if (!socket->isValid()){ |
| 337 | qWarning() << "Server failed to start within waiting period"; |
| 338 | return false; |
| 339 | } |
| 340 | } |
| 341 | else { |
| 342 | qWarning() << "Server could not be started"; |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | if (socket->isValid()){ |
| 348 | LocalSocketEndPoint* ipcEndPoint = new LocalSocketEndPoint(socket); |
| 349 | ObjectEndPoint* endPoint = new ObjectEndPoint(ObjectEndPoint::Client, ipcEndPoint); |
| 350 | |
| 351 | QObject *proxy = endPoint->constructProxy(entry); |
| 352 | if (proxy){ |
| 353 | QObject::connect(proxy, SIGNAL(destroyed()), endPoint, SLOT(deleteLater())); |
| 354 | QObject::connect(ipcEndPoint, SIGNAL(errorUnrecoverableIPCFault(QService::UnrecoverableIPCError)), |
| 355 | proxy, SIGNAL(errorUnrecoverableIPCFault(QService::UnrecoverableIPCError))); |
| 356 | } |
| 357 | ipcEndPoint->setParent(proxy); |
| 358 | endPoint->setParent(proxy); |
| 359 | qWarning() << "SFW create object"; |
| 360 | return proxy; |
| 361 | } |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | /*! |
| 366 | Returns true if the service is running |
| 367 | */ |
| 368 | |
| 369 | bool QRemoteServiceRegisterPrivate::isServiceRunning(const QRemoteServiceRegister::Entry & /*entry*/, const QString &location) |
| 370 | { |
| 371 | QLocalSocket* socket = new QLocalSocket(); |
| 372 | socket->connectToServer(location); |
| 373 | |
| 374 | // give a short timeout to block, no running services should fail almost instantly |
| 375 | return socket->waitForConnected(1000); |
| 376 | } |
| 377 | |
| 378 | #include "moc_qremoteserviceregister_ls_p.cpp" |
| 379 | #include "qremoteserviceregister_ls_p.moc" |
| 380 | QT_END_NAMESPACE |