Debian Codegraph @

src/tools/servicefw/servicefw.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 <stdio.h>
41
42 #include <QtCore>
43 #include <QTextStream>
44 #include <qservicemanager.h>
45 #include <servicemetadata_p.h>
46 #include <QString>
47 #include <QDir>
48
49 static const char * const errorTable[] = {
50 "No error", //0
51 "Storage read error",
52 "Invalid service location",
53 "Invalid service xml",
54 "Invalid service interface descriptor",
55 "Service already exists",
56 "Interface implementation already exists",
57 "Loading of plug-in failed",
58 "Service or interface not found",
59 "Insufficient capabilities to access service",
60 "Unknown error",
61 };
62
63 class CommandProcessor : public QObject
64 {
65 Q_OBJECT
66
67 public:
68 CommandProcessor(QObject *parent = 0);
69 ~CommandProcessor();
70
71 void execute(const QStringList &options, const QString &cmd, const QStringList &args);
72 void showUsage();
73 static void showUsage(QTextStream *stream);
74 int errorCode();
75
76 public slots:
77 void browse(const QStringList &args);
78 void search(const QStringList &args);
79 void add(const QStringList &args);
80 void remove(const QStringList &args);
81 void dbusservice(const QStringList &args);
82 void setdefault(const QStringList &args);
83
84 private:
85 bool setOptions(const QStringList &options);
86 void setErrorCode(int error);
87 void showAllEntries();
88 void showInterfaceInfo(const QServiceFilter &filter);
89 void showInterfaceInfo(QList<QServiceInterfaceDescriptor> descriptors);
90 void showServiceInfo(const QString &service);
91
92 QServiceManager *serviceManager;
93 QTextStream *stdoutStream;
94 int m_error;
95 };
96
97 CommandProcessor::CommandProcessor(QObject *parent)
98 : QObject(parent),
99 serviceManager(0),
100 stdoutStream(new QTextStream(stdout)),
101 m_error(0)
102 {
103 }
104
105 CommandProcessor::~CommandProcessor()
106 {
107 delete stdoutStream;
108 }
109
110 void CommandProcessor::execute(const QStringList &options, const QString &cmd, const QStringList &args)
111 {
112 if (cmd.isEmpty()) {
113 *stdoutStream << "Error: no command given\n\n";
114 showUsage();
115 return;
116 }
117
118 if (!setOptions(options))
119 return;
120
121 int methodIndex = metaObject()->indexOfMethod(cmd.toLatin1() + "(QStringList)");
122 if (methodIndex < 0) {
123 *stdoutStream << "Bad command: " << cmd << "\n\n";
124 showUsage();
125 return;
126 }
127
128 if (!metaObject()->method(methodIndex).invoke(this, Q_ARG(QStringList, args)))
129 *stdoutStream << "Cannot invoke method for command:" << cmd << '\n';
130 }
131
132 void CommandProcessor::showUsage()
133 {
134 showUsage(stdoutStream);
135 }
136
137 void CommandProcessor::showUsage(QTextStream *stream)
138 {
139 *stream << "Usage: servicefw [options] <command> [command parameters]\n\n"
140 "Commands:\n"
141 "\tbrowse List all registered services\n"
142 "\tsearch Search for a service or interface\n"
143 "\tadd Register a service\n"
144 "\tremove Unregister a service\n"
145 "\tdbusservice Generates a .service file for D-Bus service autostart\n"
146 "\n"
147 "Options:\n"
148 "\t--system Use the system-wide services database instead of the\n"
149 "\t user-specific database\n"
150 "\t--user Use the user-specific services database for add/remove.\n"
151 "\t This is the default\n"
152 "\n";
153 }
154
155 void CommandProcessor::browse(const QStringList &args)
156 {
157 Q_UNUSED(args);
158 showAllEntries();
159 }
160
161 void CommandProcessor::setdefault(const QStringList &args)
162 {
163 if (args.isEmpty() || args.count() < 2) {
164 *stdoutStream << "Usage:\n\tsetdefault <interface> <service>\n\n"
165 "Examples:\n"
166 "\tsetdefault com.nokia.SomeInterface foo";
167 return;
168 }
169
170
171 const QString &serviceInterface = args[0];
172 const QString &service = args[1];
173 bool res = serviceManager->setInterfaceDefault(service, serviceInterface);
174 if(!res) {
175 *stdoutStream << "Failed to set interface default"
176 << errorTable[serviceManager->error()];
177 }
178 }
179
180 void CommandProcessor::search(const QStringList &args)
181 {
182 if (args.isEmpty()) {
183 *stdoutStream << "Usage:\n\tsearch <service|interface [version]>\n\n"
184 "Examples:\n"
185 "\tsearch SomeService\n"
186 "\tsearch com.nokia.SomeInterface\n"
187 "\tsearch com.nokia.SomeInterface 3.5\n"
188 "\tsearch com.nokia.SomeInterface 3.5+\n";
189 return;
190 }
191
192 const QString &name = args[0];
193 if (name.contains('.')) {
194 QServiceFilter filter;
195 if (args.count() > 1) {
196 const QString &version = args[1];
197 bool minimumMatch = version.endsWith('+');
198 filter.setInterface(name,
199 minimumMatch ? (version.mid(0, version.length()-1)) : version,
200 minimumMatch ? QServiceFilter::MinimumVersionMatch : QServiceFilter::ExactVersionMatch);
201 if (filter.interfaceName().isEmpty()) { // setInterface() failed
202 *stdoutStream << "Error: invalid interface version: " << version << '\n';
203 return;
204 }
205 } else {
206 filter.setInterface(name);
207 }
208 showInterfaceInfo(filter);
209 } else {
210 showServiceInfo(name);
211 }
212 }
213
214
215 void CommandProcessor::add(const QStringList &args)
216 {
217 if (args.isEmpty()) {
218 *stdoutStream << "Usage:\n\tadd <service-xml-file>\n";
219 return;
220 }
221
222 const QString &xmlPath = args[0];
223 if (!QFile::exists(xmlPath)) {
224 *stdoutStream << "Error: cannot find file " << xmlPath << '\n';
225 setErrorCode(11);
226 return;
227 }
228
229 if (serviceManager->addService(xmlPath)) {
230 *stdoutStream << "Registered service at " << xmlPath << '\n';
231 } else {
232 int error = serviceManager->error();
233 if (error > 10) //map anything larger than 10 to 10
234 error = 10;
235 *stdoutStream << "Error: cannot register service at " << xmlPath
236 << " (" << errorTable[error] << ")" << '\n';
237
238 setErrorCode(error);
239 }
240 }
241
242 void CommandProcessor::remove(const QStringList &args)
243 {
244 if (args.isEmpty()) {
245 *stdoutStream << "Usage:\n\tremove <service-name>\n";
246 return;
247 }
248
249 const QString &service = args[0];
250 if (serviceManager->removeService(service))
251 *stdoutStream << "Unregistered service " << service << '\n';
252 else
253 *stdoutStream << "Error: cannot unregister service " << service << '\n';
254 }
255
256 void CommandProcessor::dbusservice(const QStringList &args)
257 {
258 if (args.isEmpty() || args.size() == 1) {
259 *stdoutStream << "Usage:\n\tdbusservice <service-xml-file> <service-executable>\n";
260 return;
261 }
262
263 #if defined(QT_NO_DBUS)
264 *stdoutStream << "Error: no D-Bus module found in Qt\n";
265 return;
266 #endif
267
268 const QString &xmlPath = args[0];
269 if (!QFile::exists(xmlPath)) {
270 *stdoutStream << "Error: cannot find xml file at " << xmlPath << '\n';
271 return;
272 }
273
274 const QString &servicePath = args[1];
275 if (!QFile::exists(servicePath)) {
276 *stdoutStream << "Error: cannot find service file " << servicePath << '\n';
277 return;
278 }
279
280 QFile *f = new QFile(xmlPath);
281 ServiceMetaData parser(f);
282 if (!parser.extractMetadata()) {
283 *stdoutStream << "Error: invalid service xml at " << xmlPath << '\n';
284 return;
285 }
286
287 const ServiceMetaDataResults results = parser.parseResults();
288 if (results.type != QService::InterProcess) {
289 *stdoutStream << "Error: not an inter-process service xml at " << xmlPath << '\n';
290 return;
291 }
292
293 QString path;
294 if (serviceManager->scope() == QService::UserScope) {
295 // the d-bus xdg environment variable for the local service paths
296 const QByteArray xdgPath = qgetenv("XDG_DATA_HOME");
297 if (xdgPath.isEmpty()) {
298 // if not supplied generate in default
299 QDir dir(QDir::homePath());
300 dir.mkpath(".local/share/dbus-1/services/");
301 path = QDir::homePath() + "/.local/share/dbus-1/services/";
302 } else {
303 path = QString::fromLocal8Bit(xdgPath);
304 }
305 } else {
306 path = "/usr/share/dbus-1/services/";
307 }
308
309 const QString &name = "com.nokia.qtmobility.sfw." + results.name;
310 const QString &exec = QFileInfo(args[1]).absoluteFilePath();
311
312 QStringList names;
313 foreach (const QServiceInterfaceDescriptor &serviceInterface, results.interfaces) {
314 names << serviceInterface.interfaceName();
315 }
316 names.removeDuplicates();
317
318 for (int i = 0; i < names.size(); i++) {
319 const QString &file = path + names.at(i) + "." + results.name + ".service";
320 QFile data(file);
321 if (data.open(QFile::WriteOnly)) {
322 QTextStream out(&data);
323 out << "[D-BUS Service]\n"
324 << "Name=" << name << '\n'
325 << "Exec=" << exec;
326 data.close();
327 } else {
328 *stdoutStream << "Error: cannot write to " << file << " (insufficient permissions)" << '\n';
329 return;
330 }
331
332 *stdoutStream << "Generated D-Bus autostart file " << file << '\n';
333 }
334 }
335
336 bool CommandProcessor::setOptions(const QStringList &options)
337 {
338 if (serviceManager)
339 delete serviceManager;
340
341 QService::Scope scope = QService::UserScope;
342
343 QStringList opts = options;
344 QMutableListIterator<QString> i(opts);
345 while (i.hasNext()) {
346 QString option = i.next();
347 if (option == "--system") {
348 scope = QService::SystemScope;
349 i.remove();
350 } else if (option == "--user") {
351 scope = QService::UserScope;
352 i.remove();
353 }
354 }
355
356 if (!opts.isEmpty()) {
357 *stdoutStream << "Bad options: " << opts.join(QLatin1Char(' ')) << "\n\n";
358 showUsage();
359 return false;
360 }
361
362 serviceManager = new QServiceManager(scope, this);
363
364 return true;
365 }
366
367 void CommandProcessor::showAllEntries()
368 {
369 QStringList services = serviceManager->findServices();
370 if (services.isEmpty())
371 *stdoutStream << "No services found.\n";
372 else if (services.count() == 1)
373 *stdoutStream << "Found 1 service.\n\n";
374 else
375 *stdoutStream << "Found " << services.count() << " services.\n\n";
376
377 foreach (const QString &service, services)
378 showServiceInfo(service);
379 }
380
381 void CommandProcessor::showInterfaceInfo(const QServiceFilter &filter)
382 {
383 QString serviceInterface = filter.interfaceName();
384 if (filter.majorVersion() >= 0 && filter.minorVersion() >= 0) {
385 serviceInterface += QString(" %1.%2").arg(filter.majorVersion()).arg(filter.minorVersion());
386 if (filter.versionMatchRule() == QServiceFilter::MinimumVersionMatch)
387 serviceInterface += '+';
388 }
389
390 QList<QServiceInterfaceDescriptor> descriptors = serviceManager->findInterfaces(filter);
391 if (descriptors.isEmpty()) {
392 *stdoutStream << "Interface " << serviceInterface << " not found.\n";
393 return;
394 }
395
396 *stdoutStream << serviceInterface << " is implemented by:\n";
397 foreach (const QServiceInterfaceDescriptor &desc, descriptors)
398 *stdoutStream << '\t' << desc.serviceName() << '\n';
399 }
400
401 void CommandProcessor::showServiceInfo(const QString &service)
402 {
403 QList<QServiceInterfaceDescriptor> descriptors = serviceManager->findInterfaces(service);
404 if (descriptors.isEmpty()) {
405 *stdoutStream << "Service " << service << " not found.\n";
406 return;
407 }
408
409 QList<QServiceInterfaceDescriptor> pluginDescs;
410 QList<QServiceInterfaceDescriptor> ipcDescs;
411 foreach (const QServiceInterfaceDescriptor &desc, descriptors) {
412 int serviceType = desc.attribute(QServiceInterfaceDescriptor::ServiceType).toInt();
413 if (serviceType == QService::Plugin)
414 pluginDescs.append(desc);
415 else
416 ipcDescs.append(desc);
417 }
418
419 if (pluginDescs.size() > 0) {
420 *stdoutStream << service;
421 if (ipcDescs.size() > 0)
422 *stdoutStream << " (Plugin):\n";
423 else
424 *stdoutStream << ":\n";
425
426 QString description = pluginDescs[0].attribute(
427 QServiceInterfaceDescriptor::ServiceDescription).toString();
428 if (!description.isEmpty())
429 *stdoutStream << '\t' << description << '\n';
430
431 *stdoutStream << "\tPlugin Library: ";
432 showInterfaceInfo(pluginDescs);
433 }
434
435 if (ipcDescs.size() > 0) {
436 *stdoutStream << service;
437 if (pluginDescs.size() > 0)
438 *stdoutStream << " (IPC):\n";
439 else
440 *stdoutStream << ":\n";
441
442 QString description = ipcDescs[0].attribute(
443 QServiceInterfaceDescriptor::ServiceDescription).toString();
444 if (!description.isEmpty())
445 *stdoutStream << '\t' << description << '\n';
446
447 *stdoutStream << "\tIPC Address: ";
448 showInterfaceInfo(ipcDescs);
449 }
450 }
451
452 void CommandProcessor::showInterfaceInfo(QList<QServiceInterfaceDescriptor> descriptors)
453 {
454 QList<QServiceInterfaceDescriptor> systemList;
455 QList<QServiceInterfaceDescriptor> userList;
456
457 foreach (const QServiceInterfaceDescriptor &desc, descriptors) {
458 if (desc.scope() == QService::SystemScope)
459 systemList.append(desc);
460 else
461 userList.append(desc);
462 }
463
464 if (userList.size() > 0) {
465 *stdoutStream << userList[0].attribute(QServiceInterfaceDescriptor::Location).toString() << '\n'
466 << "\tUser Implements:\n";
467
468 foreach (const QServiceInterfaceDescriptor &desc, userList) {
469 QStringList capabilities = desc.attribute(
470 QServiceInterfaceDescriptor::Capabilities).toStringList();
471
472 *stdoutStream << "\t " << desc.interfaceName() << ' '
473 << desc.majorVersion() << '.' << desc.minorVersion()
474 << (capabilities.isEmpty() ? "" : "\t{" + capabilities.join(", ") + "}") << "\n";
475 }
476 }
477
478 if (systemList.size() > 0) {
479 if (userList.size() < 1)
480 *stdoutStream << systemList[0].attribute(QServiceInterfaceDescriptor::Location).toString() << '\n';
481
482 *stdoutStream << "\tSystem Implements:\n";
483
484 foreach (const QServiceInterfaceDescriptor &desc, systemList) {
485 QStringList capabilities = desc.attribute(
486 QServiceInterfaceDescriptor::Capabilities).toStringList();
487
488 *stdoutStream << "\t " << desc.interfaceName() << ' '
489 << desc.majorVersion() << '.' << desc.minorVersion()
490 << (capabilities.isEmpty() ? "" : "\t{" + capabilities.join(", ") + "}") << "\n";
491 }
492 }
493 }
494
495 int CommandProcessor::errorCode()
496 {
497 return m_error;
498 }
499
500 void CommandProcessor::setErrorCode(int error)
501 {
502 m_error = error;
503 }
504
505 int main(int argc, char *argv[])
506 {
507 QCoreApplication app(argc, argv);
508 QStringList args = QCoreApplication::arguments();
509
510 // check for at least one non-option argument
511 bool exec = false;
512
513 QStringList options;
514 for (int i=1; i<args.count(); i++) {
515 if (args[i].startsWith("--"))
516 options += args[i];
517 else
518 exec = true;
519 }
520
521 if (!exec || args.count() == 1 || args.value(1) == "--help" || args.value(1) == "-h") {
522 QTextStream stream(stdout);
523 CommandProcessor::showUsage(&stream);
524 return 0;
525 }
526
527 CommandProcessor processor;
528 processor.execute(options, args.value(options.count() + 1), args.mid(options.count() + 2));
529 return processor.errorCode();
530 }
531
532 #include "servicefw.moc"