1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Copyright (C) 2016 Intel Corporation.
5** Contact: https://www.qt.io/licensing/
6**
7** This file is part of the QtDBus module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial License Usage
11** Licensees holding valid commercial Qt licenses may use this file in
12** accordance with the commercial license agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and The Qt Company. For licensing terms
15** and conditions see https://www.qt.io/terms-conditions. For further
16** information use the contact form at https://www.qt.io/contact-us.
17**
18** GNU Lesser General Public License Usage
19** Alternatively, this file may be used under the terms of the GNU Lesser
20** General Public License version 3 as published by the Free Software
21** Foundation and appearing in the file LICENSE.LGPL3 included in the
22** packaging of this file. Please review the following information to
23** ensure the GNU Lesser General Public License version 3 requirements
24** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
25**
26** GNU General Public License Usage
27** Alternatively, this file may be used under the terms of the GNU
28** General Public License version 2.0 or (at your option) the GNU General
29** Public license version 3 or any later version approved by the KDE Free
30** Qt Foundation. The licenses are as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
32** included in the packaging of this file. Please review the following
33** information to ensure the GNU General Public License requirements will
34** be met: https://www.gnu.org/licenses/gpl-2.0.html and
35** https://www.gnu.org/licenses/gpl-3.0.html.
36**
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#include <QtCore/qglobal.h>
42#if QT_CONFIG(library)
43#include <QtCore/qlibrary.h>
44#endif
45#include <QtCore/qmutex.h>
46
47#ifndef QT_NO_DBUS
48
49extern "C" void dbus_shutdown();
50
51QT_BEGIN_NAMESPACE
52
53void (*qdbus_resolve_me(const char *name))();
54
55#if !defined QT_LINKED_LIBDBUS
56
57#if QT_CONFIG(library)
58static QLibrary *qdbus_libdbus = 0;
59
60void qdbus_unloadLibDBus()
61{
62 if (qdbus_libdbus) {
63 if (qEnvironmentVariableIsSet("QDBUS_FORCE_SHUTDOWN"))
64 qdbus_libdbus->resolve("dbus_shutdown")();
65 qdbus_libdbus->unload();
66 }
67 delete qdbus_libdbus;
68 qdbus_libdbus = 0;
69}
70#endif
71
72bool qdbus_loadLibDBus()
73{
74#if QT_CONFIG(library)
75#ifdef QT_BUILD_INTERNAL
76 // this is to simulate a library load failure for our autotest suite.
77 if (!qEnvironmentVariableIsEmpty("QT_SIMULATE_DBUS_LIBFAIL"))
78 return false;
79#endif
80
81 static bool triedToLoadLibrary = false;
82 static QBasicMutex mutex;
83 QMutexLocker locker(&mutex);
84
85 QLibrary *&lib = qdbus_libdbus;
86 if (triedToLoadLibrary)
87 return lib && lib->isLoaded();
88
89 lib = new QLibrary;
90 lib->setLoadHints(QLibrary::ExportExternalSymbolsHint); // make libdbus symbols available for apps that need more advanced control over the dbus
91 triedToLoadLibrary = true;
92
93 static int majorversions[] = { 3, 2, -1 };
94 const QString baseNames[] = {
95#ifdef Q_OS_WIN
96 QLatin1String("dbus-1"),
97#endif
98 QLatin1String("libdbus-1")
99 };
100
101 lib->unload();
102 for (uint i = 0; i < sizeof(majorversions) / sizeof(majorversions[0]); ++i) {
103 for (uint j = 0; j < sizeof(baseNames) / sizeof(baseNames[0]); ++j) {
104#ifdef Q_OS_WIN
105 QString suffix;
106 if (majorversions[i] != -1)
107 suffix = QString::number(- majorversions[i]); // negative so it prepends the dash
108 lib->setFileName(baseNames[j] + suffix);
109#else
110 lib->setFileNameAndVersion(baseNames[j], majorversions[i]);
111#endif
112 if (lib->load() && lib->resolve("dbus_connection_open_private"))
113 return true;
114
115 lib->unload();
116 }
117 }
118
119 delete lib;
120 lib = 0;
121 return false;
122#else
123 return true;
124#endif
125}
126
127void (*qdbus_resolve_conditionally(const char *name))()
128{
129#if QT_CONFIG(library)
130 if (qdbus_loadLibDBus())
131 return qdbus_libdbus->resolve(name);
132#else
133 Q_UNUSED(name);
134#endif
135 return 0;
136}
137
138void (*qdbus_resolve_me(const char *name))()
139{
140#if QT_CONFIG(library)
141 if (Q_UNLIKELY(!qdbus_loadLibDBus()))
142 qFatal("Cannot find libdbus-1 in your system to resolve symbol '%s'.", name);
143
144 QFunctionPointer ptr = qdbus_libdbus->resolve(name);
145 if (Q_UNLIKELY(!ptr))
146 qFatal("Cannot resolve '%s' in your libdbus-1.", name);
147
148 return ptr;
149#else
150 Q_UNUSED(name);
151 return 0;
152#endif
153}
154
155#else
156static void qdbus_unloadLibDBus()
157{
158 if (qEnvironmentVariableIsSet("QDBUS_FORCE_SHUTDOWN"))
159 dbus_shutdown();
160}
161
162#endif // !QT_LINKED_LIBDBUS
163
164#if defined(QT_LINKED_LIBDBUS) || QT_CONFIG(library)
165Q_DESTRUCTOR_FUNCTION(qdbus_unloadLibDBus)
166#endif
167
168QT_END_NAMESPACE
169
170#endif // QT_NO_DBUS
171