summaryrefslogtreecommitdiffstats
path: root/src/dbus/qdbusconnectionmanager.cpp
blob: ce52c9fa633d7b1d646c6bb56c09c8a57f8e681d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Copyright (C) 2016 The Qt Company Ltd.
// Copyright (C) 2016 Intel Corporation.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qdbusconnectionmanager_p.h"

#include <qcoreapplication.h>
#include <qthread.h>
#include <qstringlist.h>
#include <QtCore/private/qlocking_p.h>

#include "qdbuserror.h"
#include "qdbuspendingcall_p.h"
#include "qdbusmetatype_p.h"

#ifndef QT_NO_DBUS

QT_BEGIN_NAMESPACE

#ifdef Q_OS_WIN
static void preventDllUnload();
#endif

Q_GLOBAL_STATIC(QDBusConnectionManager, _q_manager)

QDBusConnectionPrivate *QDBusConnectionManager::busConnection(QDBusConnection::BusType type)
{
    static_assert(int(QDBusConnection::SessionBus) + int(QDBusConnection::SystemBus) == 1);
    Q_ASSERT(type == QDBusConnection::SessionBus || type == QDBusConnection::SystemBus);

    if (!qdbus_loadLibDBus())
        return nullptr;

    // we'll start in suspended delivery mode if we're in the main thread
    // (the event loop will resume delivery)
    bool suspendedDelivery = qApp && qApp->thread() == QThread::currentThread();

    const auto locker = qt_scoped_lock(defaultBusMutex);
    if (defaultBuses[type])
        return defaultBuses[type];

    QString name = QStringLiteral("qt_default_session_bus");
    if (type == QDBusConnection::SystemBus)
        name = QStringLiteral("qt_default_system_bus");
    return defaultBuses[type] = connectToBus(type, name, suspendedDelivery);
}

QDBusConnectionPrivate *QDBusConnectionManager::connection(const QString &name) const
{
    return connectionHash.value(name, nullptr);
}

QDBusConnectionPrivate *QDBusConnectionManager::existingConnection(const QString &name) const
{
    const auto locker = qt_scoped_lock(mutex);
    auto *conn = connection(name);
    if (conn)
        conn->ref.ref();
    return conn;
}

void QDBusConnectionManager::removeConnection(const QString &name)
{
    QDBusConnectionPrivate *d = nullptr;
    d = connectionHash.take(name);
    if (d && !d->ref.deref())
        d->deleteLater();

    // Static objects may be keeping the connection open.
    // However, it is harmless to have outstanding references to a connection that is
    // closing as long as those references will be soon dropped without being used.

    // ### Output a warning if connections are being used after they have been removed.
}

void QDBusConnectionManager::removeConnections(const QStringList &names)
{
    const auto locker = qt_scoped_lock(mutex);

    for (const auto &name : names)
        removeConnection(name);
}

void QDBusConnectionManager::disconnectFrom(const QString &name,
                                            QDBusConnectionPrivate::ConnectionMode mode)
{
    const auto locker = qt_scoped_lock(mutex);

    QDBusConnectionPrivate *d = connection(name);
    if (d && d->mode != mode)
        return;
    removeConnection(name);
}

QDBusConnectionManager::QDBusConnectionManager()
{
    // Ensure that the custom metatype registry is created before the instance
    // of this class. This will ensure that the registry is not destroyed before
    // the connection manager at application exit (see also QTBUG-58732). This
    // works with compilers that use mechanism similar to atexit() to call
    // destructurs for global statics.
    QDBusMetaTypeId::init();

    moveToThread(this);         // ugly, don't do this in other projects

#ifdef Q_OS_WIN
    // prevent the library from being unloaded on Windows. See comments in the function.
    preventDllUnload();
#endif
    defaultBuses[0] = defaultBuses[1] = nullptr;
    start();
}

QDBusConnectionManager::~QDBusConnectionManager()
{
    quit();
    wait();
}

QDBusConnectionManager* QDBusConnectionManager::instance()
{
    return _q_manager();
}

Q_DBUS_EXPORT void qDBusBindToApplication();
void qDBusBindToApplication()
{
}

void QDBusConnectionManager::setConnection(const QString &name, QDBusConnectionPrivate *c)
{
    connectionHash[name] = c;
    c->name = name;
}

void QDBusConnectionManager::addConnection(const QString &name, QDBusConnectionPrivate *c)
{
    const auto locker = qt_scoped_lock(mutex);
    setConnection(name, c);
}

void QDBusConnectionManager::run()
{
    exec();

    // cleanup:
    const auto locker = qt_scoped_lock(mutex);
    for (QDBusConnectionPrivate *d : std::as_const(connectionHash)) {
        if (!d->ref.deref()) {
            delete d;
        } else {
            d->closeConnection();
            d->moveToThread(nullptr);     // allow it to be deleted in another thread
        }
    }
    connectionHash.clear();

    // allow deletion from any thread without warning
    moveToThread(nullptr);
}

QDBusConnectionPrivate *QDBusConnectionManager::connectToBus(QDBusConnection::BusType type, const QString &name,
                                                             bool suspendedDelivery)
{
    QDBusConnectionPrivate *result = nullptr;

    QMetaObject::invokeMethod(this, &QDBusConnectionManager::doConnectToStandardBus,
                              Qt::BlockingQueuedConnection, qReturnArg(result), type, name,
                              suspendedDelivery);

    if (suspendedDelivery && result && result->connection)
        result->enableDispatchDelayed(qApp); // qApp was checked in the caller

    return result;
}

QDBusConnectionPrivate *QDBusConnectionManager::connectToBus(const QString &address, const QString &name)
{
    QDBusConnectionPrivate *result = nullptr;

    QMetaObject::invokeMethod(this, &QDBusConnectionManager::doConnectToBus,
                              Qt::BlockingQueuedConnection, qReturnArg(result), address, name);

    return result;
}

QDBusConnectionPrivate *QDBusConnectionManager::connectToPeer(const QString &address, const QString &name)
{
    QDBusConnectionPrivate *result = nullptr;

    QMetaObject::invokeMethod(this, &QDBusConnectionManager::doConnectToPeer,
                              Qt::BlockingQueuedConnection, qReturnArg(result), address, name);

    return result;
}

QDBusConnectionPrivate *
QDBusConnectionManager::doConnectToStandardBus(QDBusConnection::BusType type, const QString &name,
                                               bool suspendedDelivery)
{
    const auto locker = qt_scoped_lock(mutex);

    // check if the connection exists by name
    QDBusConnectionPrivate *d = connection(name);
    if (d || name.isEmpty())
        return d;

    d = new QDBusConnectionPrivate;
    DBusConnection *c = nullptr;
    QDBusErrorInternal error;

    switch (type) {
    case QDBusConnection::SystemBus:
        c = q_dbus_bus_get_private(DBUS_BUS_SYSTEM, error);
        break;
    case QDBusConnection::SessionBus:
        c = q_dbus_bus_get_private(DBUS_BUS_SESSION, error);
        break;
    case QDBusConnection::ActivationBus:
        c = q_dbus_bus_get_private(DBUS_BUS_STARTER, error);
        break;
    }

    setConnection(name, d);

    // create the bus service
    // will lock in QDBusConnectionPrivate::connectRelay()
    d->setConnection(c, error);
    d->createBusService();
    if (c && suspendedDelivery)
        d->setDispatchEnabled(false);

    return d;
}

QDBusConnectionPrivate *QDBusConnectionManager::doConnectToBus(const QString &address,
                                                               const QString &name)
{
    const auto locker = qt_scoped_lock(mutex);

    // check if the connection exists by name
    QDBusConnectionPrivate *d = connection(name);
    if (d || name.isEmpty())
        return d;

    d = new QDBusConnectionPrivate;
    QDBusErrorInternal error;

    DBusConnection *c = q_dbus_connection_open_private(address.toUtf8().constData(), error);
    if (c) {
        // register on the bus
        if (!q_dbus_bus_register(c, error)) {
            q_dbus_connection_close(c);
            q_dbus_connection_unref(c);
            c = nullptr;
        }
    }

    setConnection(name, d);

    // create the bus service
    // will lock in QDBusConnectionPrivate::connectRelay()
    d->setConnection(c, error);
    d->createBusService();

    return d;
}

QDBusConnectionPrivate *QDBusConnectionManager::doConnectToPeer(const QString &address,
                                                                const QString &name)
{
    const auto locker = qt_scoped_lock(mutex);

    // check if the connection exists by name
    QDBusConnectionPrivate *d = connection(name);
    if (d || name.isEmpty())
        return d;

    d = new QDBusConnectionPrivate;
    QDBusErrorInternal error;

    DBusConnection *c = q_dbus_connection_open_private(address.toUtf8().constData(), error);

    setConnection(name, d);
    d->setPeer(c, error);

    return d;
}

void QDBusConnectionManager::createServer(const QString &address, QDBusServer *server)
{
    QMetaObject::invokeMethod(
            this,
            [&address, server] {
                QDBusErrorInternal error;
                QDBusConnectionPrivate *d = new QDBusConnectionPrivate;
                d->setServer(server, q_dbus_server_listen(address.toUtf8().constData(), error),
                             error);
            },
            Qt::BlockingQueuedConnection);
}

QT_END_NAMESPACE

#include "moc_qdbusconnectionmanager_p.cpp"

#ifdef Q_OS_WIN
#  include <qt_windows.h>

QT_BEGIN_NAMESPACE
static void preventDllUnload()
{
    // Thread termination is really wacky on Windows. For some reason we don't
    // understand, exiting from the thread may try to unload the DLL. Since the
    // QDBusConnectionManager thread runs until the DLL is unloaded, we've got
    // a deadlock: the main thread is waiting for the manager thread to exit,
    // but the manager thread is attempting to acquire a lock to unload the DLL.
    //
    // We work around the issue by preventing the unload from happening in the
    // first place.
    //
    // For this trick, see the blog post titled "What is the point of FreeLibraryAndExitThread?"
    // https://devblogs.microsoft.com/oldnewthing/20131105-00/?p=2733

    static HMODULE self;
    GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
                      GET_MODULE_HANDLE_EX_FLAG_PIN,
                      reinterpret_cast<const wchar_t *>(&self), // any address in this DLL
                      &self);
}
QT_END_NAMESPACE
#endif

#endif // QT_NO_DBUS