| 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 "qservicedebuglog_p.h" |
| 41 | #include <QDebug> |
| 42 | #include <QTime> |
| 43 | |
| 44 | #include <QMutex> |
| 45 | #include <QMutexLocker> |
| 46 | #include <QCoreApplication> |
| 47 | #include <QFileInfo> |
| 48 | |
| 49 | #ifdef QT_SFW_IPC_DEBUG |
| 50 | #ifdef Q_OS_UNIX |
| 51 | #include <signal.h> |
| 52 | #include <errno.h> |
| 53 | #endif |
| 54 | #include <QNetworkInterface> |
| 55 | #include <QUdpSocket> |
| 56 | #endif |
| 57 | |
| 58 | QT_BEGIN_NAMESPACE |
| 59 | |
| 60 | QServiceDebugMessage::QServiceDebugMessage() |
| 61 | { |
| 62 | #ifdef QT_SFW_IPC_DEBUG |
| 63 | buffer = new QBuffer(); |
| 64 | buffer->open(QIODevice::WriteOnly); |
| 65 | ds.setDevice(buffer); |
| 66 | QTime t = QTime::currentTime(); |
| 67 | ds << (quint8)t.hour(); |
| 68 | ds << (quint8)t.minute(); |
| 69 | ds << (quint8)t.second(); |
| 70 | ds << (quint16)t.msec(); |
| 71 | ds << (quint32)(QCoreApplication::applicationPid()); |
| 72 | QFileInfo fi(QCoreApplication::applicationFilePath()); |
| 73 | QByteArray ba = fi.fileName().toLatin1(); |
| 74 | ds.writeBytes(ba.constData(), ba.size()); |
| 75 | #endif |
| 76 | } |
| 77 | |
| 78 | QServiceDebugMessage::~QServiceDebugMessage() |
| 79 | { |
| 80 | #ifdef QT_SFW_IPC_DEBUG |
| 81 | /* when we're destructed, we're ready to send! */ |
| 82 | buffer->close(); |
| 83 | QServiceDebugLog::instance()->logMessage(this); |
| 84 | #endif |
| 85 | } |
| 86 | |
| 87 | #ifdef QT_SFW_IPC_DEBUG |
| 88 | #include <QHostAddress> |
| 89 | const static QHostAddress _group_addr(QLatin1String("224.0.105.201")); |
| 90 | #endif |
| 91 | |
| 92 | QServiceDebugLog::QServiceDebugLog() |
| 93 | { |
| 94 | makeSockets(); |
| 95 | } |
| 96 | |
| 97 | #ifdef QT_SFW_IPC_DEBUG |
| 98 | #include <fcntl.h> |
| 99 | #endif |
| 100 | |
| 101 | void QServiceDebugLog::makeSockets() |
| 102 | { |
| 103 | #ifdef QT_SFW_IPC_DEBUG |
| 104 | if (sockets.size() > 0) |
| 105 | return; |
| 106 | |
| 107 | QList<QNetworkInterface> ifs = QNetworkInterface::allInterfaces(); |
| 108 | foreach (const QNetworkInterface &inf, ifs) { |
| 109 | /* avoid the loopback or any wireless interfaces |
| 110 | * (we probably don't want debug over those) */ |
| 111 | if (inf.name().startsWith("wifi") |
| 112 | || inf.name().startsWith("wl") || inf.name().startsWith("ppp") |
| 113 | || inf.name().startsWith("tun") || inf.name().startsWith("tap")) |
| 114 | continue; |
| 115 | |
| 116 | QUdpSocket *socket = new QUdpSocket(); |
| 117 | if (!socket->bind(QHostAddress::AnyIPv4, 10520, QAbstractSocket::ShareAddress)) { |
| 118 | delete socket; |
| 119 | continue; |
| 120 | } |
| 121 | socket->setMulticastInterface(inf); |
| 122 | if (!socket->joinMulticastGroup(_group_addr, inf)) { |
| 123 | delete socket; |
| 124 | continue; |
| 125 | } |
| 126 | |
| 127 | int fd = socket->socketDescriptor(); |
| 128 | int flags = ::fcntl(fd, F_GETFL, 0); |
| 129 | if (flags < 0) { |
| 130 | delete socket; |
| 131 | continue; |
| 132 | } |
| 133 | flags |= O_NONBLOCK; |
| 134 | if (::fcntl(fd, F_SETFL, flags)) { |
| 135 | delete socket; |
| 136 | continue; |
| 137 | } |
| 138 | |
| 139 | qDebug("SFW udp debug on interface %s", qPrintable(inf.name())); |
| 140 | sockets << socket; |
| 141 | } |
| 142 | #endif |
| 143 | } |
| 144 | |
| 145 | void QServiceDebugLog::logMessage(QServiceDebugMessage *msg) |
| 146 | { |
| 147 | #ifdef QT_SFW_IPC_DEBUG |
| 148 | QMutexLocker m(&socketLock); |
| 149 | |
| 150 | if (sockets.size() == 0) { |
| 151 | makeSockets(); |
| 152 | |
| 153 | if (sockets.size() == 0) { |
| 154 | queue << msg->buffer; |
| 155 | return; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | if (sockets.size() > 0) { |
| 160 | while (!queue.isEmpty()) { |
| 161 | foreach (QUdpSocket *socket, sockets) { |
| 162 | int ret = socket->writeDatagram(queue.front()->data(), _group_addr, 10520); |
| 163 | if (ret == -1 && (errno == EWOULDBLOCK || errno == EAGAIN)) { |
| 164 | queue << msg->buffer; |
| 165 | return; |
| 166 | } |
| 167 | } |
| 168 | delete queue.front(); |
| 169 | queue.pop_front(); |
| 170 | } |
| 171 | foreach (QUdpSocket *socket, sockets) { |
| 172 | int ret = socket->writeDatagram(msg->buffer->data(), _group_addr, 10520); |
| 173 | if (ret == -1 && (errno == EWOULDBLOCK || errno == EAGAIN)) { |
| 174 | queue << msg->buffer; |
| 175 | return; |
| 176 | } |
| 177 | } |
| 178 | delete msg->buffer; |
| 179 | } |
| 180 | #else |
| 181 | Q_UNUSED(msg) |
| 182 | #endif |
| 183 | } |
| 184 | |
| 185 | QServiceDebugLog *QServiceDebugLog::instance() |
| 186 | { |
| 187 | static QServiceDebugLog *dbg = 0; |
| 188 | static QMutex m; |
| 189 | QMutexLocker l(&m); |
| 190 | |
| 191 | if (!dbg) |
| 192 | dbg = new QServiceDebugLog(); |
| 193 | return dbg; |
| 194 | } |
| 195 | |
| 196 | QT_END_NAMESPACE |