1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // Copyright (C) 2016 Intel Corporation. |
3 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
4 | |
5 | #include "qdbus_symbols_p.h" |
6 | #include <QtCore/qlatin1stringview.h> |
7 | #if QT_CONFIG(library) |
8 | #include <QtCore/qlibrary.h> |
9 | #include <QtCore/private/qlocking_p.h> |
10 | #endif |
11 | #include <QtCore/qmutex.h> |
12 | |
13 | #ifndef QT_NO_DBUS |
14 | |
15 | QT_BEGIN_NAMESPACE |
16 | |
17 | using namespace Qt::StringLiterals; |
18 | |
19 | #if !defined QT_LINKED_LIBDBUS |
20 | |
21 | #if QT_CONFIG(library) |
22 | Q_CONSTINIT static QLibrary *qdbus_libdbus = nullptr; |
23 | |
24 | void qdbus_unloadLibDBus() |
25 | { |
26 | if (qdbus_libdbus) { |
27 | if (qEnvironmentVariableIsSet("QDBUS_FORCE_SHUTDOWN" )) |
28 | qdbus_libdbus->resolve("dbus_shutdown" )(); |
29 | qdbus_libdbus->unload(); |
30 | } |
31 | delete qdbus_libdbus; |
32 | qdbus_libdbus = nullptr; |
33 | } |
34 | #endif |
35 | |
36 | bool qdbus_loadLibDBus() |
37 | { |
38 | #if QT_CONFIG(library) |
39 | #ifdef QT_BUILD_INTERNAL |
40 | // this is to simulate a library load failure for our autotest suite. |
41 | if (!qEnvironmentVariableIsEmpty("QT_SIMULATE_DBUS_LIBFAIL" )) |
42 | return false; |
43 | #endif |
44 | |
45 | Q_CONSTINIT static bool triedToLoadLibrary = false; |
46 | Q_CONSTINIT static QBasicMutex mutex; |
47 | const auto locker = qt_scoped_lock(mutex); |
48 | |
49 | QLibrary *&lib = qdbus_libdbus; |
50 | if (triedToLoadLibrary) |
51 | return lib && lib->isLoaded(); |
52 | |
53 | lib = new QLibrary; |
54 | lib->setLoadHints(QLibrary::ExportExternalSymbolsHint); // make libdbus symbols available for apps that need more advanced control over the dbus |
55 | triedToLoadLibrary = true; |
56 | |
57 | static constexpr int majorversions[] = { 3, 2, -1 }; |
58 | const QString baseNames[] = { |
59 | #ifdef Q_OS_WIN |
60 | "dbus-1"_L1 , |
61 | #endif |
62 | "libdbus-1"_L1 |
63 | }; |
64 | |
65 | lib->unload(); |
66 | for (const int majorversion : majorversions) { |
67 | for (const QString &baseName : baseNames) { |
68 | #ifdef Q_OS_WIN |
69 | QString suffix; |
70 | if (majorversion != -1) |
71 | suffix = QString::number(- majorversion); // negative so it prepends the dash |
72 | lib->setFileName(baseName + suffix); |
73 | #else |
74 | lib->setFileNameAndVersion(baseName, majorversion); |
75 | #endif |
76 | if (lib->load() && lib->resolve("dbus_connection_open_private" )) |
77 | return true; |
78 | |
79 | lib->unload(); |
80 | } |
81 | } |
82 | |
83 | delete lib; |
84 | lib = nullptr; |
85 | return false; |
86 | #else |
87 | return true; |
88 | #endif |
89 | } |
90 | |
91 | QFunctionPointer qdbus_resolve_conditionally(const char *name) |
92 | { |
93 | #if QT_CONFIG(library) |
94 | if (qdbus_loadLibDBus()) |
95 | return qdbus_libdbus->resolve(name); |
96 | #else |
97 | Q_UNUSED(name); |
98 | #endif |
99 | return nullptr; |
100 | } |
101 | |
102 | QFunctionPointer qdbus_resolve_me(const char *name) |
103 | { |
104 | #if QT_CONFIG(library) |
105 | if (Q_UNLIKELY(!qdbus_loadLibDBus())) |
106 | qFatal("Cannot find libdbus-1 in your system to resolve symbol '%s'." , name); |
107 | |
108 | QFunctionPointer ptr = qdbus_libdbus->resolve(name); |
109 | if (Q_UNLIKELY(!ptr)) |
110 | qFatal("Cannot resolve '%s' in your libdbus-1." , name); |
111 | |
112 | return ptr; |
113 | #else |
114 | Q_UNUSED(name); |
115 | return nullptr; |
116 | #endif |
117 | } |
118 | |
119 | #else |
120 | static void qdbus_unloadLibDBus() |
121 | { |
122 | if (qEnvironmentVariableIsSet(varName: "QDBUS_FORCE_SHUTDOWN" )) |
123 | dbus_shutdown(); |
124 | } |
125 | |
126 | #endif // !QT_LINKED_LIBDBUS |
127 | |
128 | #if defined(QT_LINKED_LIBDBUS) || QT_CONFIG(library) |
129 | Q_DESTRUCTOR_FUNCTION(qdbus_unloadLibDBus) |
130 | #endif |
131 | |
132 | QT_END_NAMESPACE |
133 | |
134 | #endif // QT_NO_DBUS |
135 | |