| 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_unix_p.h" |
| 42 | #include "ipcendpoint_p.h" |
| 43 | #include "objectendpoint_p.h" |
| 44 | #include "qserviceclientcredentials_p.h" |
| 45 | #include "qserviceclientcredentials.h" |
| 46 | #include "qservicedebuglog_p.h" |
| 47 | |
| 48 | #include <QDataStream> |
| 49 | #include <QTimer> |
| 50 | #include <QProcess> |
| 51 | #include <QFile> |
| 52 | #include <QCoreApplication> |
| 53 | #include <QScopedPointer> |
| 54 | #include <QDir> |
| 55 | #include <QEvent> |
| 56 | #include <QThreadStorage> |
| 57 | #include <QStandardPaths> |
| 58 | #include <QString> |
| 59 | |
| 60 | #include <time.h> |
| 61 | #include <sys/types.h> |
| 62 | #include <sys/un.h> |
| 63 | #include <sys/socket.h> |
| 64 | #include <errno.h> |
| 65 | #include <fcntl.h> |
| 66 | |
| 67 | #include <signal.h> |
| 68 | |
| 69 | #include "qservicemanager.h" |
| 70 | #include <QCoreApplication> |
| 71 | #include <QTime> |
| 72 | #include <QTextStream> |
| 73 | #include <QFileSystemWatcher> |
| 74 | #include <QSocketNotifier> |
| 75 | #include <sys/stat.h> |
| 76 | #include <stdio.h> |
| 77 | #ifndef Q_OS_MAC |
| 78 | #include <sys/inotify.h> |
| 79 | #endif |
| 80 | #include <unistd.h> |
| 81 | #include <fcntl.h> |
| 82 | |
| 83 | |
| 84 | #ifdef LOCAL_PEERCRED /* from sys/un.h */ |
| 85 | #include <sys/ucred.h> |
| 86 | #endif |
| 87 | |
| 88 | QT_BEGIN_NAMESPACE |
| 89 | |
| 90 | //IPC based on unix domain sockets |
| 91 | |
| 92 | class UnixEndPoint; |
| 93 | class Waiter; |
| 94 | |
| 95 | static void qt_ignore_sigpipe() |
| 96 | { |
| 97 | #ifndef Q_NO_POSIX_SIGNALS |
| 98 | // Set to ignore SIGPIPE once only. |
| 99 | static QBasicAtomicInt atom = Q_BASIC_ATOMIC_INITIALIZER(0); |
| 100 | if (atom.testAndSetRelaxed(0, 1)) { |
| 101 | struct sigaction noaction; |
| 102 | memset(&noaction, 0, sizeof(noaction)); |
| 103 | noaction.sa_handler = SIG_IGN; |
| 104 | ::sigaction(SIGPIPE, &noaction, 0); |
| 105 | qServiceLog() << "disable_sigpipe" << 1; |
| 106 | } |
| 107 | #else |
| 108 | // Posix signals are not supported by the underlying platform |
| 109 | qServiceLog() << "disable_sigpipe" << 0 |
| 110 | << "reason" << QString::fromLatin1("no posix signals"); |
| 111 | #endif |
| 112 | } |
| 113 | |
| 114 | Q_GLOBAL_STATIC(QThreadStorage<QList<UnixEndPoint *> >, _q_unixendpoints); |
| 115 | Q_GLOBAL_STATIC(QThreadStorage<QList<QRemoteServiceRegisterUnixPrivate *> >, _q_remoteservice); |
| 116 | Q_GLOBAL_STATIC(QThreadStorage<QList<Waiter *> >, _q_connectionfds); |
| 117 | |
| 118 | class Waiter |
| 119 | { |
| 120 | public: |
| 121 | enum Type { |
| 122 | Reader = 0, |
| 123 | Writer, |
| 124 | Timer |
| 125 | }; |
| 126 | |
| 127 | Waiter(Type t, int fd) |
| 128 | : fd(fd), |
| 129 | done(false), |
| 130 | type(t), |
| 131 | timeout(0), |
| 132 | enabled(true) |
| 133 | { |
| 134 | QList<Waiter *> &conn = _q_connectionfds()->localData(); |
| 135 | conn.append(this); |
| 136 | } |
| 137 | |
| 138 | ~Waiter() |
| 139 | { |
| 140 | if (_q_connectionfds()) { |
| 141 | QList<Waiter *> &conn = _q_connectionfds()->localData(); |
| 142 | conn.removeAll(this); |
| 143 | } else { |
| 144 | qWarning("%s:%d: waiter destroyed after _q_connectionfds", |
| 145 | __FILE__, __LINE__); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | void setEnabled(bool enabled) |
| 150 | { |
| 151 | if (enabled == this->enabled) |
| 152 | return; |
| 153 | |
| 154 | if (enabled) { |
| 155 | QList<Waiter *> &conn = _q_connectionfds()->localData(); |
| 156 | conn.append(this); |
| 157 | } else { |
| 158 | QList<Waiter *> &conn = _q_connectionfds()->localData(); |
| 159 | conn.removeAll(this); |
| 160 | } |
| 161 | this->enabled = enabled; |
| 162 | } |
| 163 | |
| 164 | int fd; |
| 165 | bool done; |
| 166 | Type type; |
| 167 | int timeout; |
| 168 | QString name; |
| 169 | |
| 170 | private: |
| 171 | bool enabled; |
| 172 | }; |
| 173 | |
| 174 | class UnixEndPoint : public QServiceIpcEndPoint |
| 175 | { |
| 176 | Q_OBJECT |
| 177 | public: |
| 178 | UnixEndPoint(int client_fd, QObject* parent = 0); |
| 179 | ~UnixEndPoint(); |
| 180 | |
| 181 | void getSecurityCredentials(QServiceClientCredentials &creds); |
| 182 | |
| 183 | bool event(QEvent *e); |
| 184 | void terminateConnection(bool error); |
| 185 | |
| 186 | int waitForData(); |
| 187 | static int last_packet_size; |
| 188 | static int operation_sequence; |
| 189 | static QStringList op_log; |
| 190 | |
| 191 | static int runLocalEventLoop(int msec = 5000); |
| 192 | Q_SIGNALS: |
| 193 | void errorUnrecoverableIPCFault(QService::UnrecoverableIPCError); |
| 194 | |
| 195 | |
| 196 | protected: |
| 197 | void flushPackage(const QServicePackage& package); |
| 198 | protected Q_SLOTS: |
| 199 | void readIncoming(); |
| 200 | void flushWriteBuffer(); |
| 201 | |
| 202 | void registerWithThreadData(); |
| 203 | void socketError(const QString &error); |
| 204 | protected Q_SLOTS: |
| 205 | void ipcfault(); |
| 206 | |
| 207 | private: |
| 208 | |
| 209 | int write(const char *data, int len); |
| 210 | |
| 211 | int client_fd; |
| 212 | bool connection_open; |
| 213 | QSocketNotifier *readNotifier; |
| 214 | QSocketNotifier *writeNotifier; |
| 215 | QByteArray pending_write; |
| 216 | |
| 217 | QRemoteServiceRegisterUnixPrivate *serviceRegPriv; |
| 218 | QByteArray pending_header; |
| 219 | QByteArray pending_buf; |
| 220 | quint32 pending_bytes; |
| 221 | }; |
| 222 | |
| 223 | UnixEndPoint::UnixEndPoint(int client_fd, QObject* parent) |
| 224 | : QServiceIpcEndPoint(parent), |
| 225 | client_fd(client_fd), |
| 226 | connection_open(true), |
| 227 | pending_bytes(0) |
| 228 | |
| 229 | { |
| 230 | qt_ignore_sigpipe(); |
| 231 | |
| 232 | registerWithThreadData(); |
| 233 | readNotifier = new QSocketNotifier(client_fd, QSocketNotifier::Read, this); |
| 234 | QObject::connect(readNotifier, SIGNAL(activated(int)), this, SLOT(readIncoming())); |
| 235 | |
| 236 | writeNotifier = new QSocketNotifier(client_fd, QSocketNotifier::Write, this); |
| 237 | QObject::connect(writeNotifier, SIGNAL(activated(int)), this, SLOT(flushWriteBuffer())); |
| 238 | |
| 239 | int flags = fcntl(client_fd, F_GETFL, 0); |
| 240 | fcntl(client_fd, F_SETFL, flags|O_NONBLOCK); |
| 241 | fcntl(client_fd, F_SETFD, FD_CLOEXEC); |
| 242 | |
| 243 | qServiceLog() << "class" << "unixep" |
| 244 | << "event" << "new" |
| 245 | << "client_fd" << client_fd |
| 246 | << "name" << objectName(); |
| 247 | } |
| 248 | |
| 249 | UnixEndPoint::~UnixEndPoint() |
| 250 | { |
| 251 | qServiceLog() << "class" << "unixep" |
| 252 | << "event" << "delete" |
| 253 | << "client_fd" << client_fd |
| 254 | << "name" << objectName() |
| 255 | << "was_open" << (connection_open ? 1 : 0); |
| 256 | if (connection_open) |
| 257 | terminateConnection(false); |
| 258 | } |
| 259 | |
| 260 | void UnixEndPoint::getSecurityCredentials(QServiceClientCredentials &creds) |
| 261 | { |
| 262 | //LocalSocketEndPoint owns socket |
| 263 | #if defined(LOCAL_PEERCRED) |
| 264 | struct xucred xuc; |
| 265 | socklen_t len = sizeof(struct xucred); |
| 266 | |
| 267 | if (getsockopt(client_fd, SOL_SOCKET, LOCAL_PEERCRED, &xuc, &len) == 0) { |
| 268 | creds.d->pid = -1; // No PID on bsd |
| 269 | creds.d->uid = xuc.cr_uid; |
| 270 | creds.d->gid = xuc.cr_gid; |
| 271 | |
| 272 | } else { |
| 273 | qServiceLog() << "class" << "unixep" |
| 274 | << "event" << "getsockopt failed" |
| 275 | << "errno" << qt_error_string(errno); |
| 276 | qDebug("SFW getsockopt failed: %s", qPrintable(qt_error_string(errno))); |
| 277 | } |
| 278 | #elif defined(SO_PEERCRED) |
| 279 | struct ucred uc; |
| 280 | socklen_t len = sizeof(struct ucred); |
| 281 | |
| 282 | if (getsockopt(client_fd, SOL_SOCKET, SO_PEERCRED, &uc, &len) == 0) { |
| 283 | creds.d->pid = uc.pid; |
| 284 | creds.d->uid = uc.uid; |
| 285 | creds.d->gid = uc.gid; |
| 286 | qServiceLog() << "class" << "unixep" |
| 287 | << "event" << "identified" |
| 288 | << "client_fd" << client_fd |
| 289 | << "pid" << uc.pid; |
| 290 | } else { |
| 291 | qServiceLog() << "class" << "unixep" |
| 292 | << "event" << "getsockopt failed" |
| 293 | << "errno" << qt_error_string(errno); |
| 294 | qDebug("SFW getsockopt failed: %s", qPrintable(qt_error_string(errno))); |
| 295 | } |
| 296 | #else |
| 297 | Q_UNUSED(creds); |
| 298 | Q_UNUSED(fd); |
| 299 | #endif |
| 300 | } |
| 301 | |
| 302 | bool UnixEndPoint::event(QEvent *e) |
| 303 | { |
| 304 | if (e->type() == QEvent::ThreadChange && _q_unixendpoints()) { |
| 305 | QList<UnixEndPoint *> &endp = _q_unixendpoints()->localData(); |
| 306 | endp.removeAll(this); |
| 307 | QMetaObject::invokeMethod(this, "registerWithThreadData", Qt::QueuedConnection); |
| 308 | } |
| 309 | |
| 310 | return QServiceIpcEndPoint::event(e); |
| 311 | } |
| 312 | |
| 313 | void UnixEndPoint::terminateConnection(bool error) |
| 314 | { |
| 315 | if (connection_open) { |
| 316 | if (_q_unixendpoints()) { |
| 317 | QList<UnixEndPoint *> &endp = _q_unixendpoints()->localData(); |
| 318 | endp.removeAll(this); |
| 319 | } |
| 320 | qServiceLog() << "class" << "unixep" |
| 321 | << "event" << "terminate" |
| 322 | << "client_fd" << client_fd |
| 323 | << "name" << objectName(); |
| 324 | readNotifier->setEnabled(false); |
| 325 | delete readNotifier; |
| 326 | writeNotifier->setEnabled(false); |
| 327 | delete writeNotifier; |
| 328 | ::close(client_fd); |
| 329 | client_fd = -1; |
| 330 | connection_open = false; |
| 331 | emit disconnected(); |
| 332 | if (error) |
| 333 | ipcfault(); |
| 334 | } else { |
| 335 | qServiceLog() << "class" << "unixep" |
| 336 | << "event" << "double terminate" |
| 337 | << "name" << objectName(); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | int UnixEndPoint::waitForData() |
| 342 | { |
| 343 | /* no point waiting around for a dead client */ |
| 344 | if (client_fd == -1) |
| 345 | return -1; |
| 346 | |
| 347 | return UnixEndPoint::runLocalEventLoop(); |
| 348 | } |
| 349 | |
| 350 | int UnixEndPoint::runLocalEventLoop(int msec) { |
| 351 | fd_set reader; |
| 352 | fd_set writer; |
| 353 | struct timeval tv; |
| 354 | |
| 355 | QTime total_time; |
| 356 | total_time.start(); |
| 357 | |
| 358 | FD_ZERO(&reader); |
| 359 | FD_ZERO(&writer); |
| 360 | |
| 361 | if (!_q_unixendpoints() || !_q_remoteservice() || !_q_connectionfds()) { |
| 362 | qWarning("%s:%d: runLocalEventLoop called but global statics are invalid!", |
| 363 | __FILE__, __LINE__); |
| 364 | } |
| 365 | |
| 366 | QList<UnixEndPoint *> &endp = _q_unixendpoints()->localData(); |
| 367 | |
| 368 | int n = 0; |
| 369 | |
| 370 | foreach (UnixEndPoint *e, endp) { |
| 371 | FD_SET(e->client_fd, &reader); |
| 372 | if (!e->pending_write.isEmpty()) { |
| 373 | FD_SET(e->client_fd, &writer); |
| 374 | } |
| 375 | if (n <= e->client_fd) { |
| 376 | n = e->client_fd+1; |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | QList<QRemoteServiceRegisterUnixPrivate *> &endpu = _q_remoteservice()->localData(); |
| 381 | |
| 382 | foreach (QRemoteServiceRegisterUnixPrivate *e, endpu) { |
| 383 | FD_SET(e->server_fd, &reader); |
| 384 | if (n <= e->server_fd) { |
| 385 | n = e->server_fd+1; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | QList<Waiter *> &conn = _q_connectionfds()->localData(); |
| 390 | |
| 391 | foreach (Waiter *w, conn) { |
| 392 | if (w->type != Waiter::Timer) { |
| 393 | if (w->type == Waiter::Reader) |
| 394 | FD_SET(w->fd, &reader); |
| 395 | else |
| 396 | FD_SET(w->fd, &writer); |
| 397 | if (n <= w->fd) { |
| 398 | n = w->fd+1; |
| 399 | } |
| 400 | } |
| 401 | if (w->timeout && w->timeout < msec) { |
| 402 | msec = w->timeout; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | tv.tv_usec = msec*1000; |
| 407 | // OSX does not support usec > 1 million |
| 408 | tv.tv_sec = tv.tv_usec%1000000; |
| 409 | tv.tv_usec /= 1000000; |
| 410 | |
| 411 | // QServiceDebugLog::instance()->appendToLog(QStringLiteral("<!> select")); |
| 412 | |
| 413 | int ret = ::select(n, &reader, &writer, 0, &tv); |
| 414 | if (ret < 0) { |
| 415 | qServiceLog() << "class" << "unixep:static" |
| 416 | << "event" << "select failed" |
| 417 | << "errno" << qt_error_string(errno); |
| 418 | if (errno == EBADF) { |
| 419 | struct stat buf; |
| 420 | foreach (UnixEndPoint *e, endp) { |
| 421 | if (fstat(e->client_fd, &buf) == -1) { |
| 422 | qServiceLog() << "class" << "unixep:static" |
| 423 | << "event" << "holding uep with bad fd" |
| 424 | << "client_fd" << e->client_fd; |
| 425 | e->terminateConnection(true); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | foreach (QRemoteServiceRegisterUnixPrivate *e, endpu) { |
| 430 | if (fstat(e->server_fd, &buf) == -1) { |
| 431 | qServiceLog() << "class" << "unixep:static" |
| 432 | << "event" << "holding qrsrup with bad fd" |
| 433 | << "server_fd" << e->server_fd; |
| 434 | endpu.removeAll(e); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | foreach (Waiter *w, conn) { |
| 439 | if (w->type != Waiter::Timer) { |
| 440 | if (fstat(w->fd, &buf) == -1) { |
| 441 | qServiceLog() << "class" << "unixep:static" |
| 442 | << "event" << "holding waiter with bad fd" |
| 443 | << "fd" << w->fd; |
| 444 | conn.removeAll(w); |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | } else if (errno == EINTR) { |
| 449 | /* no error, play it again sam */ |
| 450 | return 0; |
| 451 | } |
| 452 | return 0; |
| 453 | } else if (ret == 0) { |
| 454 | /* timeout */ |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | foreach (UnixEndPoint *e, endp) { |
| 459 | if (FD_ISSET(e->client_fd, &reader)) { |
| 460 | e->readIncoming(); |
| 461 | } |
| 462 | if (!e->pending_write.isEmpty() && FD_ISSET(e->client_fd, &writer)) { |
| 463 | e->flushWriteBuffer(); |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | foreach (QRemoteServiceRegisterUnixPrivate *e, endpu) { |
| 468 | if (FD_ISSET(e->server_fd, &reader)) { |
| 469 | e->processIncoming(); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | foreach (Waiter *w, conn) { |
| 474 | if ((w->type == Waiter::Writer && FD_ISSET(w->fd, &writer)) || |
| 475 | (w->type == Waiter::Reader && FD_ISSET(w->fd, &reader))) { |
| 476 | w->done = true; |
| 477 | w->setEnabled(false); |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | #ifdef QT_SFW_IPC_DEBUG |
| 482 | const char *times_str = ::getenv("SFW_BLOCKING_TIMES"); |
| 483 | if (times_str) { |
| 484 | int times = QString::fromLatin1(times_str).toInt(); |
| 485 | if (total_time.elapsed() > times) { |
| 486 | qServiceLog() << "class" << "unixep:static" |
| 487 | << "event" << "spun local loop" |
| 488 | << "time_ms" << total_time.elapsed(); |
| 489 | } |
| 490 | } |
| 491 | #endif |
| 492 | |
| 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | |
| 497 | void UnixEndPoint::flushPackage(const QServicePackage& package) |
| 498 | { |
| 499 | QByteArray block; |
| 500 | QDataStream out(&block, QIODevice::WriteOnly); |
| 501 | out.setVersion(QDataStream::Qt_4_6); |
| 502 | out << package; |
| 503 | |
| 504 | QByteArray sizeblock; |
| 505 | QDataStream outsize(&sizeblock, QIODevice::WriteOnly); |
| 506 | outsize.setVersion(QDataStream::Qt_4_6); |
| 507 | |
| 508 | quint32 size = block.length(); |
| 509 | outsize << size; |
| 510 | |
| 511 | #ifdef QT_SFW_IPC_DEBUG |
| 512 | const QMetaObject *mo = &QServicePackage::staticMetaObject; |
| 513 | |
| 514 | const QMetaEnum typeEnum = mo->enumerator( |
| 515 | mo->indexOfEnumerator("Type")); |
| 516 | const char *type = typeEnum.valueToKey(package.d->packageType); |
| 517 | |
| 518 | const QMetaEnum rtypeEnum = mo->enumerator( |
| 519 | mo->indexOfEnumerator("ResponseType")); |
| 520 | const char *rtype = rtypeEnum.valueToKey(package.d->responseType); |
| 521 | |
| 522 | qServiceLog() << "class" << "unixep" |
| 523 | << "event" << "write" |
| 524 | << "fd" << client_fd |
| 525 | << "size" << (qint32)size |
| 526 | << "name" << objectName() |
| 527 | << "packageType" << type |
| 528 | << "respType" << rtype; |
| 529 | #endif |
| 530 | |
| 531 | if (!connection_open) |
| 532 | return; |
| 533 | |
| 534 | int bytes = write(sizeblock.constData(), sizeblock.length()); |
| 535 | if (bytes != sizeof(quint32)) { |
| 536 | qWarning() << "SFW Failed to write length" << client_fd << bytes; |
| 537 | terminateConnection(true); |
| 538 | return; |
| 539 | } |
| 540 | bytes = write(block.constData(), block.length()); |
| 541 | if (bytes != block.length()){ |
| 542 | qWarning() << "SFW Can't send package, socket error" << block.length() << bytes; |
| 543 | terminateConnection(true); |
| 544 | return; |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | void UnixEndPoint::readIncoming() |
| 549 | { |
| 550 | if (!connection_open) { |
| 551 | qServiceLog() << "class" << "unixep" |
| 552 | << "event" << "read on closed socket" |
| 553 | << "client_fd" << client_fd |
| 554 | << "name" << objectName(); |
| 555 | return; |
| 556 | } |
| 557 | |
| 558 | readNotifier->setEnabled(false); |
| 559 | |
| 560 | QByteArray raw_data; |
| 561 | raw_data.resize(4096); |
| 562 | int bytes = ::read(client_fd, raw_data.data(), 4096); |
| 563 | if (bytes <= 0) { |
| 564 | /* Linux can give us a spurious EAGAIN, only check on error */ |
| 565 | /* No Comment */ |
| 566 | if ((bytes < 0) && (errno == EAGAIN || errno == EWOULDBLOCK)) { |
| 567 | qServiceLog() << "class" << "unixep" |
| 568 | << "event" << "spurious eagain" |
| 569 | << "client_fd" << client_fd |
| 570 | << "name" << objectName(); |
| 571 | readNotifier->setEnabled(true); |
| 572 | return; |
| 573 | } |
| 574 | |
| 575 | socketError(qt_error_string(errno)); |
| 576 | terminateConnection(true); |
| 577 | return; |
| 578 | } |
| 579 | readNotifier->setEnabled(true); |
| 580 | raw_data.resize(bytes); |
| 581 | |
| 582 | while (!raw_data.isEmpty()) { |
| 583 | |
| 584 | if (pending_bytes == 0) { /* New packet */ |
| 585 | int bytes = 4 - pending_header.length(); |
| 586 | pending_header.append(raw_data.left(bytes)); |
| 587 | raw_data.remove(0, bytes); |
| 588 | if (pending_header.length() == 4) { |
| 589 | QDataStream in_size(&pending_header, QIODevice::ReadOnly); |
| 590 | in_size.setVersion(QDataStream::Qt_4_6); |
| 591 | in_size >> pending_bytes; |
| 592 | pending_buf.clear(); |
| 593 | pending_header.clear(); |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | if (pending_bytes) { /* read any new data and add to buffer */ |
| 598 | int readsize = pending_bytes; |
| 599 | if (raw_data.length() < readsize) { |
| 600 | readsize = raw_data.length(); |
| 601 | } |
| 602 | pending_buf.append(raw_data.left(readsize)); |
| 603 | raw_data.remove(0, readsize); |
| 604 | pending_bytes -= readsize; |
| 605 | } |
| 606 | |
| 607 | if (pending_bytes == 0 && !pending_buf.isEmpty()) { |
| 608 | QDataStream in(pending_buf); |
| 609 | in.setVersion(QDataStream::Qt_4_6); |
| 610 | QServicePackage package; |
| 611 | in >> package; |
| 612 | |
| 613 | #ifdef QT_SFW_IPC_DEBUG |
| 614 | int size = pending_buf.length(); |
| 615 | const QMetaObject *mo = &QServicePackage::staticMetaObject; |
| 616 | |
| 617 | const QMetaEnum typeEnum = mo->enumerator( |
| 618 | mo->indexOfEnumerator("Type")); |
| 619 | const char *type = typeEnum.valueToKey(package.d->packageType); |
| 620 | |
| 621 | const QMetaEnum rtypeEnum = mo->enumerator( |
| 622 | mo->indexOfEnumerator("ResponseType")); |
| 623 | const char *rtype = rtypeEnum.valueToKey(package.d->responseType); |
| 624 | |
| 625 | qServiceLog() << "class" << "unixep" |
| 626 | << "event" << "read" |
| 627 | << "fd" << client_fd |
| 628 | << "size" << (qint32)size |
| 629 | << "name" << objectName() |
| 630 | << "packageType" << type |
| 631 | << "respType" << rtype; |
| 632 | #endif |
| 633 | |
| 634 | incoming.enqueue(package); |
| 635 | pending_buf.clear(); |
| 636 | emit readyRead(); |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | Q_ASSERT(raw_data.isEmpty()); |
| 641 | } |
| 642 | |
| 643 | void UnixEndPoint::registerWithThreadData() |
| 644 | { |
| 645 | if (!_q_unixendpoints()) { |
| 646 | qWarning("%s:%d: registerWithThreadData called, no thread local data!", |
| 647 | __FILE__, __LINE__); |
| 648 | return; |
| 649 | } |
| 650 | |
| 651 | QList<UnixEndPoint *> &endp = _q_unixendpoints()->localData(); |
| 652 | endp.append(this); |
| 653 | } |
| 654 | |
| 655 | void UnixEndPoint::socketError(const QString &error) |
| 656 | { |
| 657 | Q_UNUSED(error) |
| 658 | } |
| 659 | |
| 660 | void UnixEndPoint::ipcfault() |
| 661 | { |
| 662 | emit errorUnrecoverableIPCFault(QService::ErrorServiceNoLongerAvailable); |
| 663 | // QMetaObject::invokeMethod(this, "errorUnrecoverableIPCFault", Qt::QueuedConnection, Q_ARG(QService::UnrecoverableIPCError, QService::ErrorServiceNoLongerAvailable)); |
| 664 | } |
| 665 | |
| 666 | int UnixEndPoint::write(const char *data, int len) |
| 667 | { |
| 668 | pending_write.append(data, len); |
| 669 | flushWriteBuffer(); |
| 670 | return len; |
| 671 | } |
| 672 | |
| 673 | void UnixEndPoint::flushWriteBuffer() |
| 674 | { |
| 675 | writeNotifier->setEnabled(false); |
| 676 | |
| 677 | if (!pending_write.isEmpty()) { |
| 678 | int ret = ::write(client_fd, pending_write.constData(), pending_write.length()); |
| 679 | |
| 680 | if (ret > 0) { |
| 681 | pending_write.remove(0, ret); |
| 682 | if (!pending_write.isEmpty()) { |
| 683 | writeNotifier->setEnabled(true); |
| 684 | } |
| 685 | |
| 686 | qServiceLog() << "class" << "unixep" |
| 687 | << "event" << "flush ok" |
| 688 | << "client_fd" << client_fd |
| 689 | << "wrote" << ret |
| 690 | << "pending" << pending_write.size(); |
| 691 | |
| 692 | } else if ((ret == -1) && (errno == EAGAIN || errno == EWOULDBLOCK)) { |
| 693 | /* no data writen, but socket is open */ |
| 694 | qServiceLog() << "class" << "unixep" |
| 695 | << "event" << "flush eagain" |
| 696 | << "client_fd" << client_fd |
| 697 | << "pending" << pending_write.size(); |
| 698 | |
| 699 | writeNotifier->setEnabled(true); |
| 700 | } else { |
| 701 | qServiceLog() << "class" << "unixep" |
| 702 | << "event" << "flush FAIL" |
| 703 | << "client_fd" << client_fd |
| 704 | << "pending" << pending_write.size() |
| 705 | << "errno" << qt_error_string(errno); |
| 706 | } |
| 707 | } else { |
| 708 | writeNotifier->setEnabled(false); |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | QRemoteServiceRegisterUnixPrivate::QRemoteServiceRegisterUnixPrivate(QObject* parent) |
| 713 | : QRemoteServiceRegisterPrivate(parent), server_fd(-1), server_notifier(0) |
| 714 | { |
| 715 | } |
| 716 | |
| 717 | QRemoteServiceRegisterUnixPrivate::~QRemoteServiceRegisterUnixPrivate() |
| 718 | { |
| 719 | qServiceLog() << "class" << "qrsrup" |
| 720 | << "event" << "delete start" |
| 721 | << "name" << objectName(); |
| 722 | |
| 723 | if (_q_remoteservice()) { |
| 724 | QList<QRemoteServiceRegisterUnixPrivate *> &endp = |
| 725 | _q_remoteservice()->localData(); |
| 726 | endp.removeAll(this); |
| 727 | } |
| 728 | |
| 729 | qServiceLog() << "class" << "qrsrup" |
| 730 | << "event" << "delete done" |
| 731 | << "name" << objectName(); |
| 732 | } |
| 733 | |
| 734 | void QRemoteServiceRegisterUnixPrivate::publishServices( const QString& ident) |
| 735 | { |
| 736 | createServiceEndPoint(ident); |
| 737 | } |
| 738 | |
| 739 | void QRemoteServiceRegisterUnixPrivate::processIncoming() |
| 740 | { |
| 741 | struct sockaddr_un name; |
| 742 | int len = sizeof(struct sockaddr_un); |
| 743 | |
| 744 | int client_fd = ::accept(server_fd, (struct sockaddr *) &name, (socklen_t *) &len); |
| 745 | |
| 746 | int flags = ::fcntl(client_fd, F_GETFL, 0); |
| 747 | ::fcntl(client_fd, F_SETFL, flags|O_NONBLOCK); |
| 748 | ::fcntl(client_fd, F_SETFD, FD_CLOEXEC); |
| 749 | |
| 750 | qServiceLog() << "class" << "qrsrup" |
| 751 | << "event" << "accept" |
| 752 | << "client_fd" << client_fd |
| 753 | << "name" << objectName(); |
| 754 | |
| 755 | if (client_fd != -1) { |
| 756 | //LocalSocketEndPoint owns socket |
| 757 | UnixEndPoint* ipcEndPoint = new UnixEndPoint(client_fd, this); |
| 758 | |
| 759 | ipcEndPoint->setObjectName(objectName() + QString(QLatin1String(" instance on fd %1")).arg(client_fd)); |
| 760 | |
| 761 | QServiceClientCredentials creds; |
| 762 | ipcEndPoint->getSecurityCredentials(creds); |
| 763 | |
| 764 | if (getSecurityFilter()){ |
| 765 | getSecurityFilter()(&creds); |
| 766 | if (!creds.isClientAccepted()) { |
| 767 | ipcEndPoint->terminateConnection(true); |
| 768 | return; |
| 769 | } |
| 770 | } |
| 771 | ObjectEndPoint* endpoint = new ObjectEndPoint(ObjectEndPoint::Service, ipcEndPoint, this); |
| 772 | endpoint->setObjectName(objectName() + QString(QLatin1String(" instance on fd %1")).arg(client_fd)); |
| 773 | Q_UNUSED(endpoint); |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | /* |
| 778 | Creates endpoint on service side. |
| 779 | */ |
| 780 | bool QRemoteServiceRegisterUnixPrivate::createServiceEndPoint(const QString& ident) |
| 781 | { |
| 782 | setObjectName(ident); |
| 783 | server_fd = ::socket(PF_UNIX, SOCK_STREAM, 0); |
| 784 | if (server_fd == -1) { |
| 785 | qWarning() << "SFW Failed to create server socket" << ident << qt_error_string(errno); |
| 786 | return false; |
| 787 | } |
| 788 | |
| 789 | int flags = fcntl(server_fd, F_GETFL, 0); |
| 790 | fcntl(server_fd, F_SETFL, flags|O_NONBLOCK); |
| 791 | fcntl(server_fd, F_SETFD, FD_CLOEXEC); |
| 792 | |
| 793 | QString location = ident; |
| 794 | location = QDir::cleanPath(QDir::tempPath()); |
| 795 | location += QLatin1Char('/') + ident; |
| 796 | |
| 797 | qServiceLog() << "class" << "qrsrup" |
| 798 | << "event" << "createservice start" |
| 799 | << "server_fd" << server_fd |
| 800 | << "name" << objectName(); |
| 801 | |
| 802 | // Note safe, but temporary code |
| 803 | QString tempLocation = location + QStringLiteral(".temp"); |
| 804 | QString pidLocation = location + QStringLiteral(".pid"); |
| 805 | |
| 806 | ::unlink(location.toLatin1()); |
| 807 | ::unlink(tempLocation.toLatin1()); |
| 808 | ::unlink(pidLocation.toLatin1()); |
| 809 | |
| 810 | struct sockaddr_un name; |
| 811 | name.sun_family = PF_UNIX; |
| 812 | |
| 813 | ::memcpy(name.sun_path, tempLocation.toLatin1().data(), |
| 814 | tempLocation.toLatin1().size() + 1); |
| 815 | |
| 816 | if (-1 == ::bind(server_fd, (const sockaddr *)&name, sizeof(struct sockaddr_un))) { |
| 817 | qWarning() << "Failed to bind to server socket" << tempLocation << qt_error_string(errno); |
| 818 | return false; |
| 819 | } |
| 820 | |
| 821 | if (-1 == ::chmod(tempLocation.toLatin1(), |
| 822 | S_IWUSR|S_IRUSR|S_IWGRP|S_IRGRP|S_IWOTH|S_IROTH)) { |
| 823 | qWarning("SFW failed to chmod %s: %s", qPrintable(tempLocation), |
| 824 | ::strerror(errno)); |
| 825 | } |
| 826 | int uid = getuid(); |
| 827 | int gid = getgid(); |
| 828 | bool doChown = false; |
| 829 | if (isBaseUserIdentifierSet()) { |
| 830 | uid = getBaseUserIdentifier(); |
| 831 | doChown = true; |
| 832 | } |
| 833 | if (isBaseGroupIdentifierSet()) { |
| 834 | gid = getBaseGroupIdentifier(); |
| 835 | doChown = true; |
| 836 | } |
| 837 | if (doChown && (-1 == ::chown(tempLocation.toLatin1(), uid, gid))) { |
| 838 | qWarning() << "Failed to chown socket to request uid/gid" << uid << gid << qt_error_string(errno); |
| 839 | return false; |
| 840 | } |
| 841 | |
| 842 | if (-1 == ::listen(server_fd, 50)) { |
| 843 | qWarning() << "Failed to listen on server socket" << tempLocation << qt_error_string(errno); |
| 844 | return false; |
| 845 | } |
| 846 | |
| 847 | if (-1 == ::rename(tempLocation.toLatin1(), location.toLatin1())) { |
| 848 | qWarning("Failed to rename %s to %s", qPrintable(tempLocation), |
| 849 | qPrintable(location)); |
| 850 | } |
| 851 | |
| 852 | server_notifier = new QSocketNotifier(server_fd, QSocketNotifier::Read, this); |
| 853 | connect(server_notifier, SIGNAL(activated(int)), this, SLOT(processIncoming())); |
| 854 | |
| 855 | registerWithThreadData(); |
| 856 | |
| 857 | qServiceLog() << "class" << "qrsrup" |
| 858 | << "event" << "createservice done" |
| 859 | << "server_fd" << server_fd |
| 860 | << "name" << objectName(); |
| 861 | |
| 862 | FILE *f = ::fopen(pidLocation.toLatin1(), "w"); |
| 863 | if (f) { |
| 864 | ::fprintf(f, "%d\n", ::getpid()); |
| 865 | ::fclose(f); |
| 866 | } else { |
| 867 | qWarning() << "Failed to create pid file for location" << pidLocation; |
| 868 | } |
| 869 | |
| 870 | return true; |
| 871 | } |
| 872 | |
| 873 | bool QRemoteServiceRegisterUnixPrivate::event(QEvent *e) |
| 874 | { |
| 875 | if (e->type() == QEvent::ThreadChange && server_fd != -1 && _q_remoteservice()) { |
| 876 | QList<QRemoteServiceRegisterUnixPrivate *> &endp = _q_remoteservice()->localData(); |
| 877 | endp.removeAll(this); |
| 878 | QMetaObject::invokeMethod(this, "registerWithThreadData", Qt::QueuedConnection); |
| 879 | } |
| 880 | |
| 881 | return QRemoteServiceRegisterPrivate::event(e); |
| 882 | } |
| 883 | |
| 884 | void QRemoteServiceRegisterUnixPrivate::registerWithThreadData() |
| 885 | { |
| 886 | if (server_fd != -1 && _q_remoteservice()) { |
| 887 | QList<QRemoteServiceRegisterUnixPrivate *> &endp = _q_remoteservice()->localData(); |
| 888 | endp.append(this); |
| 889 | } |
| 890 | } |
| 891 | |
| 892 | QRemoteServiceRegisterPrivate* QRemoteServiceRegisterPrivate::constructPrivateObject(QObject *parent) |
| 893 | { |
| 894 | return new QRemoteServiceRegisterUnixPrivate(parent); |
| 895 | } |
| 896 | |
| 897 | #define SFW_PROCESS_TIMEOUT 10000 |
| 898 | |
| 899 | int doStart(const QString &location) { |
| 900 | |
| 901 | QLatin1String fmt("hh:mm:ss.zzz"); |
| 902 | QTime total_time; |
| 903 | total_time.start(); |
| 904 | |
| 905 | qServiceLog() << "class" << "doStart" |
| 906 | << "event" << "start" |
| 907 | << "location" << location; |
| 908 | |
| 909 | #ifndef Q_OS_MAC |
| 910 | int fd = ::inotify_init1(IN_NONBLOCK|IN_CLOEXEC); |
| 911 | int wd = ::inotify_add_watch(fd, "/tmp/", IN_MOVED_TO|IN_CREATE); |
| 912 | |
| 913 | Waiter *w_inotify = new Waiter(Waiter::Reader, fd); |
| 914 | w_inotify->name = QStringLiteral("inotifier waiter for") + location; |
| 915 | #else |
| 916 | Waiter *w_inotify = new Waiter(Waiter::Timer, -1); |
| 917 | w_inotify->timeout = 100; |
| 918 | #endif |
| 919 | |
| 920 | int socketfd = ::socket(PF_UNIX, SOCK_STREAM, 0); |
| 921 | if (socketfd < 0) { |
| 922 | qWarning("socket(2) failed: %s", ::strerror(errno)); |
| 923 | delete w_inotify; |
| 924 | return -1; |
| 925 | } |
| 926 | // set non blocking so we can try to connect and it wont wait |
| 927 | int flags = ::fcntl(socketfd, F_GETFL, 0); |
| 928 | if (flags < 0) { |
| 929 | qWarning("fcntl(F_GETFL) failed: %s", ::strerror(errno)); |
| 930 | delete w_inotify; |
| 931 | return -1; |
| 932 | } |
| 933 | if (fcntl(socketfd, F_SETFL, flags | O_NONBLOCK)) { |
| 934 | qWarning("fcntl(F_SETFL) failed: %s", ::strerror(errno)); |
| 935 | delete w_inotify; |
| 936 | return -1; |
| 937 | } |
| 938 | fcntl(socketfd, F_SETFD, FD_CLOEXEC); |
| 939 | |
| 940 | struct sockaddr_un name; |
| 941 | name.sun_family = PF_UNIX; |
| 942 | |
| 943 | QString fullPath = QDir::cleanPath(QDir::tempPath()); |
| 944 | fullPath += QLatin1Char('/') + location; |
| 945 | |
| 946 | ::memcpy(name.sun_path, fullPath.toLatin1().data(), |
| 947 | fullPath.toLatin1().size() + 1); |
| 948 | |
| 949 | Waiter *w = new Waiter(Waiter::Reader, socketfd); |
| 950 | w->name = QStringLiteral("connect waiter for") + location; |
| 951 | |
| 952 | int ret = ::connect(socketfd, (struct sockaddr *) &name, sizeof(struct sockaddr_un)); |
| 953 | |
| 954 | if (ret == -1 && errno == EAGAIN) { |
| 955 | QTime e; |
| 956 | e.start(); |
| 957 | |
| 958 | qServiceLog() << "class" << "doStart" |
| 959 | << "event" << "found process" |
| 960 | << "location" << location; |
| 961 | |
| 962 | while ((e.elapsed() < 5000) && !w->done) { |
| 963 | UnixEndPoint::runLocalEventLoop(); |
| 964 | } |
| 965 | |
| 966 | if (w->done) { |
| 967 | #ifndef Q_OS_MAC |
| 968 | ::inotify_rm_watch(fd, wd); |
| 969 | ::close(fd); |
| 970 | #endif |
| 971 | delete w; |
| 972 | delete w_inotify; |
| 973 | return socketfd; |
| 974 | } |
| 975 | } else if (ret == 0) { |
| 976 | #ifndef Q_OS_MAC |
| 977 | ::inotify_rm_watch(fd, wd); |
| 978 | ::close(fd); |
| 979 | #endif |
| 980 | delete w; |
| 981 | delete w_inotify; |
| 982 | return socketfd; |
| 983 | } |
| 984 | |
| 985 | qWarning() << QTime::currentTime().toString(fmt) |
| 986 | << "SFW unable to connect to service, trying to start it." << ret << qt_error_string(errno); |
| 987 | |
| 988 | QString path = location; |
| 989 | |
| 990 | int pipefd[2]; |
| 991 | |
| 992 | if (-1 == (pipe(pipefd))) { |
| 993 | qWarning("pipe2 failed: %s", ::strerror(errno)); |
| 994 | #ifndef Q_OS_MAC |
| 995 | ::inotify_rm_watch(fd, wd); |
| 996 | ::close(fd); |
| 997 | #endif |
| 998 | delete w; |
| 999 | delete w_inotify; |
| 1000 | return socketfd; |
| 1001 | } |
| 1002 | |
| 1003 | fcntl(pipefd[0], F_SETFD, FD_CLOEXEC); |
| 1004 | fcntl(pipefd[1], F_SETFD, FD_CLOEXEC); |
| 1005 | |
| 1006 | qint64 pid = 0; |
| 1007 | |
| 1008 | // Start the service as a detached process |
| 1009 | if (!(pid = fork())) { |
| 1010 | char buffer[] = "FAIL"; |
| 1011 | close(pipefd[0]); |
| 1012 | |
| 1013 | qServiceLog() << "class" << "doStart" |
| 1014 | << "event" << "client starting" |
| 1015 | << "location" << path; |
| 1016 | |
| 1017 | // child, move it away from the parent |
| 1018 | if (-1 == (setsid())) { |
| 1019 | qWarning("setsit failed: %s", ::strerror(errno)); |
| 1020 | write(pipefd[1], buffer, 5); |
| 1021 | exit(-1); |
| 1022 | } |
| 1023 | |
| 1024 | setpgid(getpid(), getpid()); |
| 1025 | |
| 1026 | execlp(path.toLatin1(), path.toLatin1(), NULL); |
| 1027 | |
| 1028 | qWarning() << "exec of process" << path.toLatin1() << ::strerror(errno); |
| 1029 | |
| 1030 | qServiceLog() << "class" << "doStart" |
| 1031 | << "event" << "failed start" |
| 1032 | << "error" << ::strerror(errno); |
| 1033 | |
| 1034 | write(pipefd[1], buffer, 5); |
| 1035 | exit(-1); |
| 1036 | } |
| 1037 | |
| 1038 | if (pid == -1) { |
| 1039 | close(pipefd[0]); |
| 1040 | close(pipefd[1]); |
| 1041 | qWarning("process start failed %s", ::strerror(errno)); |
| 1042 | #ifndef Q_OS_MAC |
| 1043 | ::inotify_rm_watch(fd, wd); |
| 1044 | ::close(fd); |
| 1045 | #endif |
| 1046 | delete w; |
| 1047 | delete w_inotify; |
| 1048 | return socketfd; |
| 1049 | } |
| 1050 | |
| 1051 | close(pipefd[1]); |
| 1052 | |
| 1053 | char buffer[5]; |
| 1054 | if (read(pipefd[0], buffer, 5) > 0) { |
| 1055 | qServiceLog() << "class" << "doStart" |
| 1056 | << "event" << "ddeamon error reported"; |
| 1057 | qWarning("process start failed"); |
| 1058 | #ifndef Q_OS_MAC |
| 1059 | ::inotify_rm_watch(fd, wd); |
| 1060 | ::close(fd); |
| 1061 | #endif |
| 1062 | delete w; |
| 1063 | delete w_inotify; |
| 1064 | return socketfd; |
| 1065 | } |
| 1066 | close(pipefd[0]); |
| 1067 | |
| 1068 | qServiceLog() << "class" << "doStart" |
| 1069 | << "event" << "deamon started"; |
| 1070 | |
| 1071 | QFileInfo file(QStringLiteral("/tmp/") + location); |
| 1072 | qWarning() << QTime::currentTime().toString(fmt) |
| 1073 | << "SFW checking in" << file.path() << "for the socket to come into existance" << file.filePath(); |
| 1074 | |
| 1075 | ret = ::connect(socketfd, (struct sockaddr *)&name, sizeof(name)); |
| 1076 | if (ret == -1 && errno == EINPROGRESS) { |
| 1077 | qWarning() << "SFW got conect in progress"; |
| 1078 | w->setEnabled(true); |
| 1079 | } else { |
| 1080 | qWarning() << QTime::currentTime().toString(fmt) |
| 1081 | << "SFW Failed to connect" << qt_error_string(errno) << total_time.elapsed(); |
| 1082 | w->setEnabled(false); |
| 1083 | } |
| 1084 | |
| 1085 | bool success = false; |
| 1086 | |
| 1087 | while (total_time.elapsed() < SFW_PROCESS_TIMEOUT) { |
| 1088 | UnixEndPoint::runLocalEventLoop(SFW_PROCESS_TIMEOUT-total_time.elapsed()); |
| 1089 | |
| 1090 | #ifndef Q_OS_MAC |
| 1091 | #define INOTIFY_SIZE (sizeof(struct inotify_event)+1024) |
| 1092 | char buffer[INOTIFY_SIZE]; |
| 1093 | int n; |
| 1094 | while ((n = ::read(fd, buffer, INOTIFY_SIZE)) == INOTIFY_SIZE) { |
| 1095 | } |
| 1096 | if (n == -1 && errno != EAGAIN) { |
| 1097 | qWarning() << QTime::currentTime().toString(fmt) |
| 1098 | << "Failed to read inotity, fall back to timeout" << qt_error_string(errno); |
| 1099 | } else { |
| 1100 | w_inotify->setEnabled(true); |
| 1101 | } |
| 1102 | #endif |
| 1103 | |
| 1104 | ret = ::connect(socketfd, (struct sockaddr *)&name, sizeof(name)); |
| 1105 | qServiceLog() << "class" << "qrsrup" |
| 1106 | << "event" << "connect woke up" |
| 1107 | << "return" << ret |
| 1108 | << "errno" << qt_error_string(errno); |
| 1109 | |
| 1110 | if (ret == 0 || (ret == -1 && errno == EISCONN)) { |
| 1111 | success = true; |
| 1112 | break; |
| 1113 | } else if (ret == -1 && errno == EINPROGRESS) { |
| 1114 | w->setEnabled(true); |
| 1115 | } else { |
| 1116 | w->setEnabled(false); |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | qServiceLog() << "func" << __FUNCTION__ |
| 1121 | << "event" << "connect done" |
| 1122 | << "time" << total_time.elapsed() |
| 1123 | << "fd" << socketfd; |
| 1124 | |
| 1125 | qWarning() << QTime::currentTime().toString(fmt) << |
| 1126 | "SFW doStart done. Total time" << total_time.elapsed() << |
| 1127 | "Socket exists:" << file.exists() << |
| 1128 | "isSocketValid" << socketfd; |
| 1129 | |
| 1130 | if (success == false) { |
| 1131 | ::close(socketfd); |
| 1132 | socketfd = -1; |
| 1133 | } |
| 1134 | |
| 1135 | delete w; |
| 1136 | delete w_inotify; |
| 1137 | #ifndef Q_OS_MAC |
| 1138 | ::inotify_rm_watch(fd, wd); |
| 1139 | ::close(fd); |
| 1140 | #endif |
| 1141 | return socketfd; |
| 1142 | } |
| 1143 | |
| 1144 | /* |
| 1145 | Creates endpoint on client side. |
| 1146 | */ |
| 1147 | QObject* QRemoteServiceRegisterPrivate::proxyForService(const QRemoteServiceRegister::Entry& entry, const QString& location) |
| 1148 | { |
| 1149 | qWarning() << "(all ok) starting SFW proxyForService" << location; |
| 1150 | |
| 1151 | #ifdef QT_SFW_IPC_DEBUG |
| 1152 | qServiceLog() << "class" << "qrsrp" |
| 1153 | << "event" << "proxy start" |
| 1154 | << "iface" << entry.d->iface |
| 1155 | << "name" << location; |
| 1156 | #endif |
| 1157 | |
| 1158 | int socketfd = doStart(location); |
| 1159 | |
| 1160 | if (socketfd >= 0){ |
| 1161 | UnixEndPoint* ipcEndPoint = new UnixEndPoint(socketfd); |
| 1162 | ObjectEndPoint* endPoint = new ObjectEndPoint(ObjectEndPoint::Client, ipcEndPoint); |
| 1163 | |
| 1164 | ipcEndPoint->setObjectName(entry.interfaceName() + QString(QStringLiteral(" client end %1")).arg(socketfd)); |
| 1165 | endPoint->setObjectName(entry.interfaceName() + QString(QStringLiteral(" client end %1")).arg(socketfd)); |
| 1166 | |
| 1167 | QObject *proxy = endPoint->constructProxy(entry); |
| 1168 | if (proxy){ |
| 1169 | QObject::connect(proxy, SIGNAL(destroyed()), endPoint, SLOT(deleteLater())); |
| 1170 | QObject::connect(ipcEndPoint, SIGNAL(errorUnrecoverableIPCFault(QService::UnrecoverableIPCError)), |
| 1171 | proxy, SIGNAL(errorUnrecoverableIPCFault(QService::UnrecoverableIPCError))); |
| 1172 | qServiceLog() << "class" << "qrsrp" |
| 1173 | << "event" << "create object" |
| 1174 | << "iface" << entry.interfaceName() |
| 1175 | << "name" << proxy->objectName(); |
| 1176 | ipcEndPoint->setParent(proxy); |
| 1177 | endPoint->setParent(proxy); |
| 1178 | qWarning() << "SFW created object for" << entry.interfaceName(); |
| 1179 | } |
| 1180 | else { |
| 1181 | qWarning() << "SFW failed to create object for" << entry.interfaceName(); |
| 1182 | qServiceLog() << "class" << "qrsrp" |
| 1183 | << "event" << "create object FAIL" |
| 1184 | << "iface" << entry.interfaceName(); |
| 1185 | delete endPoint; |
| 1186 | } |
| 1187 | return proxy; |
| 1188 | } |
| 1189 | return 0; |
| 1190 | } |
| 1191 | |
| 1192 | bool QRemoteServiceRegisterPrivate::isServiceRunning(const QRemoteServiceRegister::Entry &, const QString &location) |
| 1193 | { |
| 1194 | QString path = QDir::cleanPath(QDir::tempPath()); |
| 1195 | path += QLatin1Char('/') + location + QStringLiteral(".pid"); |
| 1196 | |
| 1197 | FILE *f = fopen(path.toLatin1(), "r"); |
| 1198 | if (f) { |
| 1199 | int pid; |
| 1200 | if (fscanf(f, "%d", &pid) == 1 && pid > 0) { |
| 1201 | int ret = ::kill(pid, 0); |
| 1202 | if (ret == 0) { |
| 1203 | return true; // signal was sent |
| 1204 | } |
| 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | return false; |
| 1209 | } |
| 1210 | |
| 1211 | #include "moc_qremoteserviceregister_unix_p.cpp" |
| 1212 | #include "qremoteserviceregister_unix_p.moc" |
| 1213 | QT_END_NAMESPACE |