Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qdbus_symbols.cpp
Go to the documentation of this file.
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
16
17using namespace Qt::StringLiterals;
18
19#if !defined QT_LINKED_LIBDBUS
20
21#if QT_CONFIG(library)
22Q_CONSTINIT static QLibrary *qdbus_libdbus = nullptr;
23
24void 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
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
91QFunctionPointer qdbus_resolve_conditionally(const char *name)
92{
93#if QT_CONFIG(library)
95 return qdbus_libdbus->resolve(name);
96#else
98#endif
99 return nullptr;
100}
101
102QFunctionPointer qdbus_resolve_me(const char *name)
103{
104#if QT_CONFIG(library)
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
120static void qdbus_unloadLibDBus()
121{
122 if (qEnvironmentVariableIsSet("QDBUS_FORCE_SHUTDOWN"))
123 dbus_shutdown();
124}
125
126#endif // !QT_LINKED_LIBDBUS
127
128#if defined(QT_LINKED_LIBDBUS) || QT_CONFIG(library)
129Q_DESTRUCTOR_FUNCTION(qdbus_unloadLibDBus)
130#endif
131
133
134#endif // QT_NO_DBUS
\inmodule QtCore \reentrant
Definition qlibrary.h:17
bool load()
Loads the library and returns true if the library was loaded successfully; otherwise returns false.
Definition qlibrary.cpp:813
@ ExportExternalSymbolsHint
Definition qlibrary.h:24
void setFileNameAndVersion(const QString &fileName, int verNum)
Sets the fileName property and major version number to fileName and versionNumber respectively.
Definition qlibrary.cpp:978
void setFileName(const QString &fileName)
Definition qlibrary.cpp:955
bool isLoaded() const
Returns true if load() succeeded; otherwise returns false.
Definition qlibrary.cpp:861
bool unload()
Unloads the library and returns true if the library could be unloaded; otherwise returns false.
Definition qlibrary.cpp:843
void setLoadHints(LoadHints hints)
QFunctionPointer resolve(const char *symbol)
Returns the address of the exported symbol symbol.
\inmodule QtCore
Definition qmutex.h:281
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:8084
Combined button and popup list for selecting options.
#define Q_UNLIKELY(x)
bool qdbus_loadLibDBus()
QFunctionPointer qdbus_resolve_conditionally(const char *name)
QFunctionPointer qdbus_resolve_me(const char *name)
#define qFatal
Definition qlogging.h:168
static ControlElement< T > * ptr(QWidget *widget)
GLuint name
Q_CORE_EXPORT bool qEnvironmentVariableIsEmpty(const char *varName) noexcept
Q_CORE_EXPORT bool qEnvironmentVariableIsSet(const char *varName) noexcept
#define Q_UNUSED(x)
QMutex mutex
[2]