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 "qdbusserver.h"
42 #include "qdbusconnection_p.h"
43 #include "qdbusconnectionmanager_p.h"
44 #include "qdbusutil_p.h"
45
46 #include <QtCore/private/qlocking_p.h>
47
48 #ifndef QT_NO_DBUS
49
50 QT_BEGIN_NAMESPACE
51
52 /*!
53 \class QDBusServer
54 \inmodule QtDBus
55
56 \brief The QDBusServer class provides peer-to-peer communication
57 between processes on the same computer.
58 */
59
60 /*!
61 Constructs a QDBusServer with the given \a address, and the given
62 \a parent.
63 */
QDBusServer(const QString & address,QObject * parent)64 QDBusServer::QDBusServer(const QString &address, QObject *parent)
65 : QObject(parent), d(nullptr)
66 {
67 if (address.isEmpty())
68 return;
69
70 if (!qdbus_loadLibDBus())
71 return;
72
73 QDBusConnectionManager *instance = QDBusConnectionManager::instance();
74 if (!instance)
75 return;
76
77 emit instance->serverRequested(address, this);
78 QObject::connect(d, SIGNAL(newServerConnection(QDBusConnectionPrivate*)),
79 this, SLOT(_q_newConnection(QDBusConnectionPrivate*)), Qt::QueuedConnection);
80 }
81
82 /*!
83 Constructs a QDBusServer with the given \a parent. The server will listen
84 for connections in \c {/tmp} (on Unix systems) or on a TCP port bound to
85 localhost (elsewhere).
86 */
QDBusServer(QObject * parent)87 QDBusServer::QDBusServer(QObject *parent)
88 : QObject(parent), d(nullptr)
89 {
90 #ifdef Q_OS_UNIX
91 // Use Unix sockets on Unix systems only
92 const QString address = QStringLiteral("unix:tmpdir=/tmp");
93 #else
94 const QString address = QStringLiteral("tcp:");
95 #endif
96
97 if (!qdbus_loadLibDBus())
98 return;
99
100 QDBusConnectionManager *instance = QDBusConnectionManager::instance();
101 if (!instance)
102 return;
103
104 emit instance->serverRequested(address, this);
105 QObject::connect(d, SIGNAL(newServerConnection(QDBusConnectionPrivate*)),
106 this, SLOT(_q_newConnection(QDBusConnectionPrivate*)), Qt::QueuedConnection);
107 }
108
109 /*!
110 Destructs a QDBusServer
111 */
~QDBusServer()112 QDBusServer::~QDBusServer()
113 {
114 QMutex *managerMutex = nullptr;
115 if (QDBusConnectionManager::instance())
116 managerMutex = &QDBusConnectionManager::instance()->mutex;
117 QMutexLocker locker(managerMutex);
118 QWriteLocker writeLocker(&d->lock);
119 if (QDBusConnectionManager::instance()) {
120 for (const QString &name : qAsConst(d->serverConnectionNames))
121 QDBusConnectionManager::instance()->removeConnection(name);
122 d->serverConnectionNames.clear();
123 locker.unlock();
124 }
125 d->serverObject = nullptr;
126 d->ref.storeRelaxed(0);
127 d->deleteLater();
128 }
129
130 /*!
131 Returns \c true if this QDBusServer object is connected.
132
133 If it isn't connected, you need to call the constructor again.
134 */
isConnected() const135 bool QDBusServer::isConnected() const
136 {
137 return d && d->server && q_dbus_server_get_is_connected(d->server);
138 }
139
140 /*!
141 Returns the last error that happened in this server.
142
143 This function is provided for low-level code.
144 */
lastError() const145 QDBusError QDBusServer::lastError() const
146 {
147 return d ? d->lastError : QDBusError(QDBusError::Disconnected, QDBusUtil::disconnectedErrorMessage());
148 }
149
150 /*!
151 Returns the address this server is associated with.
152 */
address() const153 QString QDBusServer::address() const
154 {
155 QString addr;
156 if (d && d->server) {
157 char *c = q_dbus_server_get_address(d->server);
158 addr = QString::fromUtf8(c);
159 q_dbus_free(c);
160 }
161
162 return addr;
163 }
164
165 /*!
166 \since 5.3
167
168 If \a value is set to true, an incoming connection can proceed even if the
169 connecting client is not authenticated as a user.
170
171 By default, this value is false.
172
173 \sa isAnonymousAuthenticationAllowed()
174 */
setAnonymousAuthenticationAllowed(bool value)175 void QDBusServer::setAnonymousAuthenticationAllowed(bool value)
176 {
177 d->anonymousAuthenticationAllowed = value;
178 }
179
180 /*!
181 \since 5.3
182
183 Returns true if anonymous authentication is allowed.
184
185 \sa setAnonymousAuthenticationAllowed()
186 */
isAnonymousAuthenticationAllowed() const187 bool QDBusServer::isAnonymousAuthenticationAllowed() const
188 {
189 return d->anonymousAuthenticationAllowed;
190 }
191
192 /*!
193 \fn void QDBusServer::newConnection(const QDBusConnection &connection)
194
195 This signal is emitted when a new client connection \a connection is
196 established to the server.
197 */
198
199 QT_END_NAMESPACE
200
201 #include "moc_qdbusserver.cpp"
202
203 #endif // QT_NO_DBUS
204