Debian Codegraph @

src/serviceframework/ipc/qremoteserviceregister_dbus_p.cpp

raw symbols SCIP
1 /****************************************************************************
2 **
3 ** Copyright (C) 2018 The Qt Company Ltd and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtSystems module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #include "qremoteserviceregister_p.h"
41 #include "qremoteserviceregister_dbus_p.h"
42 #include "qserviceclientcredentials_p.h"
43
44 #include <QDataStream>
45 #include <QTimer>
46
47
48 QT_BEGIN_NAMESPACE
49
50 struct dbus_creds
51 {
52 int pid;
53 int uid;
54 };
55
56 class DBusEndPoint : public QServiceIpcEndPoint
57 {
58 Q_OBJECT
59
60 public:
61 DBusEndPoint(QDBusInterface* iface, int type, QObject* parent = 0)
62 : QServiceIpcEndPoint(parent), interface(iface), endType(type)
63 {
64 Q_ASSERT(interface);
65 interface->setParent(this);
66
67 connect(interface, SIGNAL(packageReceived(QByteArray,int,QString,int,int)),
68 this, SLOT(readPackage(QByteArray,int,QString,int,int)));
69
70 if (endType == CLIENT) {
71 QDBusServiceWatcher *watcher = new QDBusServiceWatcher(interface->service(),
72 interface->connection(),
73 QDBusServiceWatcher::WatchForUnregistration);
74
75 QObject::connect(watcher, SIGNAL(serviceUnregistered(QString)),
76 this, SLOT(serviceRemoved(QString)));
77 }
78 }
79
80 ~DBusEndPoint()
81 {
82 }
83
84 public Q_SLOTS:
85 void closeIncoming()
86 {
87 QDBusMessage msg = interface->callWithArgumentList(QDBus::AutoDetect, QLatin1String("closeIncoming"),
88 QList<QVariant>() << instanceId);
89 }
90
91 void setInstanceId(const QString& id)
92 {
93 instanceId = id;
94 }
95
96 Q_SIGNALS:
97 void ipcFault(QService::UnrecoverableIPCError);
98
99 protected:
100 void flushPackage(const QServicePackage& package)
101 {
102 if (!QDBusConnection::sessionBus().isConnected()) {
103 qWarning() << "Cannot connect to DBus";
104 }
105
106 QByteArray block;
107 QDataStream out(&block, QIODevice::WriteOnly);
108 out.setVersion(QDataStream::Qt_4_6);
109 out << package;
110
111 packageId = package.d->messageId.toString();
112 interface->asyncCall(QLatin1String("writePackage"), block, endType, packageId);
113 }
114
115 protected Q_SLOTS:
116 void readPackage(const QByteArray &package, int type, const QString &id, int pid, int uid) {
117 // Check that its of a client-server nature
118 if (endType != type) {
119 // Client to Server
120 if (type != SERVER) {
121 readIncoming(package, pid, uid);
122 } else {
123 // Server to Client
124 if (id == packageId) {
125 readIncoming(package, pid, uid);
126 }
127 }
128 }
129 }
130
131 void readIncoming(const QByteArray &package, int pid, int uid)
132 {
133 QDataStream data(package);
134 QServicePackage pack;
135 data >> pack;
136
137 dbus_creds dcreds;
138
139 dcreds.pid = pid;
140 dcreds.uid = uid;
141
142 creds_list.enqueue(dcreds);
143 incoming.enqueue(pack);
144 emit readyRead();
145 }
146
147 void serviceRemoved(const QString& name)
148 {
149 Q_UNUSED(name);
150 QString serviceName = interface->service();
151 QDBusReply<bool> reply = interface->connection().interface()->isServiceRegistered(serviceName);
152 if (!reply.value()) {
153 emit ipcFault(QService::ErrorServiceNoLongerAvailable);
154 }
155 }
156
157 void getSecurityCredentials(QServiceClientCredentials &creds)
158 {
159 if (!creds_list.isEmpty()) {
160 dbus_creds dcreds = creds_list.dequeue();
161 creds.d->pid = dcreds.pid;
162 creds.d->uid = dcreds.uid;
163 creds.d->gid = -1;
164 }
165 }
166
167 private:
168 QDBusInterface* interface;
169 QString packageId;
170 int endType;
171 QString instanceId;
172 QQueue<dbus_creds> creds_list;
173 };
174
175 class DBusSessionAdaptor: public QDBusAbstractAdaptor
176 {
177 Q_OBJECT
178 Q_CLASSINFO("D-Bus Interface", "com.nokia.qtmobility.sfw.DBusSession")
179
180 public:
181 DBusSessionAdaptor(QObject *parent);
182 ~DBusSessionAdaptor() {}
183
184 public Q_SLOTS:
185 QByteArray writePackage(const QByteArray &package, int type, const QString &id) {
186 QByteArray ret;
187 QMetaObject::invokeMethod(parent(), "writePackage",
188 Q_RETURN_ARG(QByteArray, ret),
189 Q_ARG(QByteArray, package),
190 Q_ARG(int, type),
191 Q_ARG(QString, id));
192 return ret;
193 }
194
195 bool processIncoming() {
196 bool ret;
197 QMetaObject::invokeMethod(parent(), "processIncoming",
198 Q_RETURN_ARG(bool, ret));
199 return ret;
200 }
201
202 void acceptIncoming(bool accept) {
203 QMetaObject::invokeMethod(parent(), "acceptIncoming",
204 Q_ARG(bool, accept));
205 }
206
207 void closeIncoming(const QString& instanceId) {
208 QMetaObject::invokeMethod(parent(), "closeIncoming",
209 Q_ARG(QString, instanceId));
210 }
211
212 void q_autostart() {
213 }
214
215 Q_SIGNALS:
216 void packageReceived(const QByteArray &package, int type, const QString &id, int uid, int pid);
217 void newConnection(int pid, int uid);
218 };
219
220 DBusSessionAdaptor::DBusSessionAdaptor(QObject *parent)
221 : QDBusAbstractAdaptor(parent)
222 {
223 setAutoRelaySignals(true);
224 }
225
226 QRemoteServiceRegisterDBusPrivate::QRemoteServiceRegisterDBusPrivate(QObject* parent)
227 : QRemoteServiceRegisterPrivate(parent)
228 {
229 }
230
231 QRemoteServiceRegisterDBusPrivate::~QRemoteServiceRegisterDBusPrivate()
232 {
233 }
234
235 void QRemoteServiceRegisterDBusPrivate::publishServices(const QString& ident)
236 {
237 if (!createServiceEndPoint(ident))
238 QTimer::singleShot(0, QCoreApplication::instance(), SLOT(quit()));
239 }
240
241 /*!
242 Creates endpoint on service side.
243 */
244 bool QRemoteServiceRegisterDBusPrivate::createServiceEndPoint(const QString& ident)
245 {
246 int endPoints = 0;
247
248 InstanceManager *iManager = InstanceManager::instance();
249 QList<QRemoteServiceRegister::Entry> list = iManager->allEntries();
250
251 if (list.size() < 1)
252 return false;
253
254 QDBusConnection connection = QDBusConnection::sessionBus();
255 if (!connection.isConnected()) {
256 qWarning() << "Cannot connect to DBus";
257 return 0;
258 }
259
260 // Registers the service and session object on DBus if needed
261 for (int i=0; i<list.size(); i++) {
262 QString serviceName = QStringLiteral("com.nokia.qtmobility.sfw.") + list.at(i).serviceName();
263 QDBusReply<bool> reply = connection.interface()->isServiceRegistered(serviceName);
264 if (reply.value())
265 continue;
266
267 if (!connection.registerService(serviceName)) {
268 qWarning() << "Cannot register service to DBus:" << serviceName;
269 continue;
270 }
271
272 // Create and register our DBusSession server/client
273 session = new DBusSession(this);
274 new DBusSessionAdaptor(session);
275 QObject::connect(session, SIGNAL(newConnection(int,int)),
276 this, SLOT(processIncoming(int,int)));
277
278 QString path = QLatin1Char('/') + list.at(i).interfaceName() + QLatin1Char('/') + ident;
279 path.replace(QLatin1Char('.'), QLatin1Char('/'));
280 if (!connection.objectRegisteredAt(path)) {
281 if (!connection.registerObject(path, session)) {
282 qWarning() << "Cannot register service session to DBus:" << path;
283 continue;
284 }
285
286 iface = new QDBusInterface(serviceName, path, QString(), QDBusConnection::sessionBus());
287 if (!iface->isValid()) {
288 qWarning() << "createServiceEndPoint: Cannot connect to remote service" << serviceName << path;;
289 qWarning() << QString::fromLatin1("%1 %2 %3").arg(iface->lastError().name()).
290 arg(iface->lastError().message()).
291 arg(iface->lastError().type());
292 continue;
293 }
294
295 DBusEndPoint* ipcEndPoint = new DBusEndPoint(iface, SERVER);
296 ObjectEndPoint* endPoint = new ObjectEndPoint(ObjectEndPoint::Service, ipcEndPoint, this);
297
298 // Connect session process disconnections
299 QObject::connect(session, SIGNAL(closeConnection(QString,QString)),
300 endPoint, SLOT(disconnected(QString,QString)));
301
302 endPoints++;
303 }
304 }
305
306 if (endPoints > 0)
307 return true;
308
309 return false;
310 }
311
312 void QRemoteServiceRegisterDBusPrivate::processIncoming(int pid, int uid)
313 {
314 if (getSecurityFilter()) {
315 QServiceClientCredentials cred;
316 cred.d->pid = pid;
317 cred.d->uid = uid;
318 cred.d->gid = -1;
319
320 getSecurityFilter()(&cred);
321
322 if (!cred.isClientAccepted()) {
323 session->acceptIncoming(false);
324
325 // Close service if no instances
326 if (quitOnLastInstanceClosed() &&
327 InstanceManager::instance()->totalInstances() < 1)
328 QCoreApplication::exit();
329
330 return;
331 }
332 }
333
334 session->acceptIncoming(true);
335 }
336
337 QRemoteServiceRegisterPrivate* QRemoteServiceRegisterPrivate::constructPrivateObject(QObject *parent)
338 {
339 return new QRemoteServiceRegisterDBusPrivate(parent);
340 }
341
342 /*!
343 Creates endpoint on client side.
344 */
345 QObject* QRemoteServiceRegisterPrivate::proxyForService(const QRemoteServiceRegister::Entry& entry, const QString& location)
346 {
347 const QString serviceName = QStringLiteral("com.nokia.qtmobility.sfw.") + entry.serviceName();
348 QString path = QLatin1Char('/') + entry.interfaceName() + QLatin1Char('/') + location;
349 path.replace(QLatin1Char('.'), QLatin1Char('/'));
350
351 QDBusConnection connection = QDBusConnection::sessionBus();
352 if (!connection.isConnected()) {
353 qWarning() << "Cannot connect to DBus";
354 return 0;
355 }
356
357 // Dummy call to autostart the service if not running
358 connection.call(QDBusMessage::createMethodCall(serviceName, path, QString(), QStringLiteral("q_autostart")));
359
360 QDBusInterface *iface = new QDBusInterface(serviceName, path, QString(), QDBusConnection::sessionBus());
361 if (!iface->isValid()) {
362 qWarning() << "ProxyForService: Cannot connect to remote service" << serviceName << path;
363 qWarning() << QString::fromLatin1("%1 %2 %3").arg(iface->lastError().name()).
364 arg(iface->lastError().message()).
365 arg(iface->lastError().type());
366 return 0;
367 }
368
369 QDBusReply<bool> reply = iface->call(QDBus::Block, QLatin1String("processIncoming"));
370 if (reply.value()) {
371 DBusEndPoint* ipcEndPoint = new DBusEndPoint(iface, CLIENT);
372 ObjectEndPoint* endPoint = new ObjectEndPoint(ObjectEndPoint::Client, ipcEndPoint);
373
374 QObject *proxy = endPoint->constructProxy(entry);
375 ipcEndPoint->setInstanceId(endPoint->getInstanceId());
376
377 if (proxy) {
378 QObject::connect(proxy, SIGNAL(destroyed()), endPoint, SLOT(deleteLater()));
379 QObject::connect(proxy, SIGNAL(destroyed()), ipcEndPoint, SLOT(closeIncoming()));
380 QObject::connect(ipcEndPoint, SIGNAL(ipcFault(QService::UnrecoverableIPCError)),
381 proxy, SIGNAL(errorUnrecoverableIPCFault(QService::UnrecoverableIPCError)));
382 }
383 return proxy;
384 }
385
386 qDebug() << "Insufficient credentials to load a service instance";
387 return 0;
388 }
389
390 /*!
391 Returns true is the service is running
392 */
393
394 bool QRemoteServiceRegisterPrivate::isServiceRunning(const QRemoteServiceRegister::Entry &entry, const QString & /*location*/)
395 {
396 QDBusConnection connection = QDBusConnection::sessionBus();
397 if (!connection.isConnected()) {
398 qWarning() << "Cannot connect to DBus";
399 return false;
400 }
401
402 // Not exacly right, just because it's registered doesn't mean it's running
403 QString serviceName = QStringLiteral("com.nokia.qtmobility.sfw.") + entry.serviceName();
404 QDBusReply<bool> reply = connection.interface()->isServiceRegistered(serviceName);
405 return reply.value();
406 }
407
408 #include "moc_qremoteserviceregister_dbus_p.cpp"
409 #include "qremoteserviceregister_dbus_p.moc"
410 QT_END_NAMESPACE