| 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.h" |
| 41 | #include "qremoteserviceregisterentry_p.h" |
| 42 | #include "ipc/instancemanager_p.h" |
| 43 | #include "qremoteserviceregister_p.h" |
| 44 | #include "qserviceclientcredentials_p.h" |
| 45 | |
| 46 | #include <QtCore/QDataStream> |
| 47 | #include <QtCore/QEvent> |
| 48 | |
| 49 | QT_BEGIN_NAMESPACE |
| 50 | |
| 51 | /*! |
| 52 | \class QRemoteServiceRegister::Entry |
| 53 | \ingroup servicefw |
| 54 | \inmodule QtServiceFramework |
| 55 | \brief The Entry class represents a remote service entry to be published on QRemoteServiceRegister. |
| 56 | |
| 57 | This class is created using QRemoteServiceRegister::createEntry to supply remote service |
| 58 | details matching a valid QServiceInterfaceDescriptor. |
| 59 | |
| 60 | A registration entry can then be published for discovery by remote clients. |
| 61 | |
| 62 | */ |
| 63 | |
| 64 | /*! |
| 65 | Constructs a null registration entry. |
| 66 | */ |
| 67 | QRemoteServiceRegister::Entry::Entry() |
| 68 | { |
| 69 | d = new QRemoteServiceRegisterEntryPrivate; |
| 70 | } |
| 71 | |
| 72 | /*! |
| 73 | Constructs the registration entry that is a copy of \a other. |
| 74 | */ |
| 75 | QRemoteServiceRegister::Entry::Entry(const Entry& other) |
| 76 | : d(other.d) |
| 77 | { |
| 78 | } |
| 79 | |
| 80 | /*! |
| 81 | Destroys the registration entry. |
| 82 | */ |
| 83 | QRemoteServiceRegister::Entry::~Entry() |
| 84 | { |
| 85 | } |
| 86 | |
| 87 | /*! |
| 88 | Checks if the registration entry is currently a valid remote service entry |
| 89 | |
| 90 | Returns true if the serviceName(), interfaceName() and version() point to |
| 91 | a valid QServiceInterfaceDescriptor, otherwise false. |
| 92 | */ |
| 93 | bool QRemoteServiceRegister::Entry::isValid() const |
| 94 | { |
| 95 | if (!d->iface.isEmpty() && !d->service.isEmpty() |
| 96 | && !d->ifaceVersion.isEmpty() && d->cptr!=0 && d->meta!=0) |
| 97 | return true; |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | /*! |
| 102 | Returns true if this font is equal to \a other; otherwise false. |
| 103 | */ |
| 104 | bool QRemoteServiceRegister::Entry::operator==(const Entry& other) const |
| 105 | { |
| 106 | return d->service == other.d->service && |
| 107 | d->iface == other.d->iface && |
| 108 | d->ifaceVersion == other.d->ifaceVersion; |
| 109 | } |
| 110 | |
| 111 | /*! |
| 112 | Returns true if this font is different from \a other; otherwise false. |
| 113 | */ |
| 114 | bool QRemoteServiceRegister::Entry::operator!=(const Entry& other) const |
| 115 | { |
| 116 | return !(other == *this); |
| 117 | } |
| 118 | |
| 119 | /*! |
| 120 | Assigns \a other to this registration entry and returns a reference to it. |
| 121 | */ |
| 122 | QRemoteServiceRegister::Entry &QRemoteServiceRegister::Entry::operator=(const Entry& other) |
| 123 | { |
| 124 | d = other.d; |
| 125 | return *this; |
| 126 | } |
| 127 | |
| 128 | /*! |
| 129 | Returns the interface name of the registration entry. |
| 130 | |
| 131 | This should correspond to the interface name from the service XML description. |
| 132 | |
| 133 | \sa serviceName(), version() |
| 134 | */ |
| 135 | QString QRemoteServiceRegister::Entry::interfaceName() const |
| 136 | { |
| 137 | return d->iface; |
| 138 | } |
| 139 | |
| 140 | /*! |
| 141 | Returns the service name of the registration entry. |
| 142 | |
| 143 | This should correspond to the service name from the service XML description. |
| 144 | |
| 145 | \sa interfaceName(), version() |
| 146 | */ |
| 147 | QString QRemoteServiceRegister::Entry::serviceName() const |
| 148 | { |
| 149 | return d->service; |
| 150 | } |
| 151 | |
| 152 | /*! |
| 153 | Returns the version of the registration entry in format x.y. |
| 154 | |
| 155 | This should correspond to the interface version from the service XML description. |
| 156 | |
| 157 | \sa interfaceName(), serviceName() |
| 158 | */ |
| 159 | QString QRemoteServiceRegister::Entry::version() const |
| 160 | { |
| 161 | return d->ifaceVersion; |
| 162 | } |
| 163 | |
| 164 | const QMetaObject * QRemoteServiceRegister::Entry::metaObject() const |
| 165 | { |
| 166 | return d->meta; |
| 167 | } |
| 168 | |
| 169 | /*! |
| 170 | Sets the QRemoteServiceRegister::InstanceType of the registration entry. |
| 171 | |
| 172 | If this is not explicitly called, the default instance \a type for the registration entry |
| 173 | is QRemoteServiceRegister::PrivateInstance. |
| 174 | */ |
| 175 | void QRemoteServiceRegister::Entry::setInstantiationType(QRemoteServiceRegister::InstanceType type) |
| 176 | { |
| 177 | d->instanceType = type; |
| 178 | } |
| 179 | |
| 180 | /*! |
| 181 | Returns the QRemoteServiceRegister::InstanceType of the registration entry. |
| 182 | */ |
| 183 | QRemoteServiceRegister::InstanceType QRemoteServiceRegister::Entry::instantiationType() const |
| 184 | { |
| 185 | return d->instanceType; |
| 186 | } |
| 187 | |
| 188 | /*! |
| 189 | \class QRemoteServiceRegister |
| 190 | \inmodule QtServiceFramework |
| 191 | \ingroup servicefw |
| 192 | \brief The QRemoteServiceRegister class manages instances of remote service objects. |
| 193 | |
| 194 | This class registers and publishes IPC based service objects. It owns the service's |
| 195 | objects and uses the platform specific IPC mechanism to publish the service. |
| 196 | |
| 197 | In order for the remote services to be discoverable by QServiceManager each |
| 198 | QRemoteServiceRegister::Entry must be registered with the same information in |
| 199 | the XML description, otherwise no corresponding QServiceInterfaceDescriptor can be |
| 200 | found. |
| 201 | |
| 202 | The following XML descriptor is used for subsequent examples. |
| 203 | |
| 204 | \code |
| 205 | <SFW version="1.1"> |
| 206 | <service> |
| 207 | <name>MyService</name> |
| 208 | <ipcaddress>my_executable</ipcaddress> |
| 209 | <description>My service example</description> |
| 210 | <interface> |
| 211 | <name>com.nokia.qt.example.myService</name> |
| 212 | <version>1.0</version> |
| 213 | <description>My private service</description> |
| 214 | <capabilities></capabilities> |
| 215 | </interface> |
| 216 | </service> |
| 217 | </SFW> |
| 218 | \endcode |
| 219 | |
| 220 | The snippet belows demonstrates how an application can register the class \a MyClass |
| 221 | as a remote service, which is published and accessible to clients who wish to load |
| 222 | service object instances. |
| 223 | |
| 224 | \code |
| 225 | int main(int argc, char** argv) |
| 226 | { |
| 227 | QCoreApplication app(argc, argv); |
| 228 | |
| 229 | QRemoteServiceRegister *serviceRegister = new QRemoteServiceRegister(); |
| 230 | |
| 231 | QRemoteServiceRegister::Entry myService; |
| 232 | myService = serviceRegister->createEntry<MyClass>( |
| 233 | "MyService", "com.nokia.qt.example.myservice", "1.0"); |
| 234 | |
| 235 | serviceRegister->publishEntries("my_service"); |
| 236 | |
| 237 | return app.exec(); |
| 238 | delete serviceRegister; |
| 239 | } |
| 240 | \endcode |
| 241 | |
| 242 | By default all entries are created as \l QRemoteServiceRegister::PrivateInstance |
| 243 | types. This can be changed by calling QRemoteServiceRegister::Entry::setInstantiationType() |
| 244 | on the entry. Once the service register has been published the associated service entries |
| 245 | can no longer be changed. |
| 246 | |
| 247 | \sa QRemoteServiceRegister::Entry |
| 248 | */ |
| 249 | |
| 250 | /*! |
| 251 | \enum QRemoteServiceRegister::InstanceType |
| 252 | Defines the two types of instances for a registration entry |
| 253 | \value GlobalInstance New requests for a service gets the same service instance |
| 254 | \value PrivateInstance New requests for a service gets a new service instance |
| 255 | */ |
| 256 | |
| 257 | /*! |
| 258 | \fn void QRemoteServiceRegister::instanceClosed(const QRemoteServiceRegister::Entry& entry) |
| 259 | |
| 260 | This signal is emitted whenever a created instance has been closed. This indicates |
| 261 | that a connected client has either shutdown or released the loaded service object. |
| 262 | |
| 263 | \a entry is supplied to identify which registered service |
| 264 | entry the closed instance belonged to. |
| 265 | |
| 266 | \sa allInstancesClosed() |
| 267 | */ |
| 268 | |
| 269 | /*! |
| 270 | \fn void QRemoteServiceRegister::allInstancesClosed() |
| 271 | |
| 272 | This signal is emitted whenever all service instances have been closed. This indicates |
| 273 | that the last connected client has either shutdown or released the loaded service object. |
| 274 | |
| 275 | \sa instanceClosed() |
| 276 | */ |
| 277 | |
| 278 | /*! |
| 279 | \typedef QRemoteServiceRegister::CreateServiceFunc |
| 280 | \internal |
| 281 | Denotes a function pointer returning a service instance |
| 282 | */ |
| 283 | |
| 284 | /*! |
| 285 | \typedef QRemoteServiceRegister::SecurityFilter |
| 286 | \internal |
| 287 | Denotes a function pointer used for the security filter feature |
| 288 | */ |
| 289 | |
| 290 | /*! |
| 291 | Creates a service register instance with the given \a parent. |
| 292 | */ |
| 293 | QRemoteServiceRegister::QRemoteServiceRegister(QObject* parent) |
| 294 | : QObject(parent) |
| 295 | , d(0) |
| 296 | { |
| 297 | connect(InstanceManager::instance(), SIGNAL(allInstancesClosed()), |
| 298 | this, SIGNAL(allInstancesClosed())); |
| 299 | connect(InstanceManager::instance(), SIGNAL(instanceClosed(QRemoteServiceRegister::Entry)), |
| 300 | this, SIGNAL(instanceClosed(QRemoteServiceRegister::Entry))); |
| 301 | } |
| 302 | |
| 303 | /*! |
| 304 | Destroys the service register instance |
| 305 | */ |
| 306 | QRemoteServiceRegister::~QRemoteServiceRegister() |
| 307 | { |
| 308 | } |
| 309 | |
| 310 | /*! |
| 311 | Publishes every service QRemoteServiceRegister::Entry that has been created using |
| 312 | \l createEntry(). The \a ident is the service specific IPC address under which |
| 313 | the service can be reached. |
| 314 | |
| 315 | This address must match the address provided in the services XML descriptor, otherwise |
| 316 | the service will not be discoverable. In some cases this may also cause the IPC |
| 317 | rendezvous feature to fail. |
| 318 | |
| 319 | \sa createEntry() |
| 320 | */ |
| 321 | void QRemoteServiceRegister::publishEntries(const QString& ident) |
| 322 | { |
| 323 | if (!d) init(); |
| 324 | d->publishServices(ident); |
| 325 | } |
| 326 | |
| 327 | /*! |
| 328 | \property QRemoteServiceRegister::quitOnLastInstanceClosed |
| 329 | |
| 330 | \brief Terminate the service when all clients have closed all objects. Default value is true. |
| 331 | */ |
| 332 | bool QRemoteServiceRegister::quitOnLastInstanceClosed() const |
| 333 | { |
| 334 | if (!d) const_cast<QRemoteServiceRegister*>(this)->init(); |
| 335 | return d->quitOnLastInstanceClosed(); |
| 336 | } |
| 337 | |
| 338 | void QRemoteServiceRegister::setQuitOnLastInstanceClosed(bool quit) |
| 339 | { |
| 340 | if (!d) init(); |
| 341 | d->setQuitOnLastInstanceClosed(quit); |
| 342 | } |
| 343 | |
| 344 | /*! |
| 345 | \since 5.0 |
| 346 | |
| 347 | Set the user id for the socket or pipe. For backends that use sockets or |
| 348 | pipes and provide filesystem based access control. |
| 349 | |
| 350 | */ |
| 351 | |
| 352 | void QRemoteServiceRegister::setBaseUserIdentifier(qintptr uid) |
| 353 | { |
| 354 | if (!d) init(); |
| 355 | d->setBaseUserIdentifier(uid); |
| 356 | } |
| 357 | |
| 358 | /*! |
| 359 | \since 5.0 |
| 360 | |
| 361 | Get the user id set on the socket or pipe. |
| 362 | */ |
| 363 | |
| 364 | qintptr QRemoteServiceRegister::getBaseUserIdentifier() const |
| 365 | { |
| 366 | if (!d) const_cast<QRemoteServiceRegister*>(this)->init(); |
| 367 | return d->getBaseUserIdentifier(); |
| 368 | } |
| 369 | |
| 370 | /*! |
| 371 | \since 5.0 |
| 372 | |
| 373 | Set the group id for the socket or pipe. For backends that use sockets or |
| 374 | pipes and provide filesystem based access control. |
| 375 | */ |
| 376 | |
| 377 | void QRemoteServiceRegister::setBaseGroupIdentifier(qintptr gid) |
| 378 | { |
| 379 | if (!d) init(); |
| 380 | d->setBaseGroupIdentifier(gid); |
| 381 | } |
| 382 | |
| 383 | /*! |
| 384 | \since 5.0 |
| 385 | |
| 386 | Get the group id set on the socket or pipe. |
| 387 | */ |
| 388 | |
| 389 | qintptr QRemoteServiceRegister::getBaseGroupIdentifier() const |
| 390 | { |
| 391 | if (!d) const_cast<QRemoteServiceRegister*>(this)->init(); |
| 392 | return d->getBaseGroupIdentifier(); |
| 393 | } |
| 394 | |
| 395 | /*! |
| 396 | \since 5.0 |
| 397 | |
| 398 | Set the socket access control. This sets the file |
| 399 | system permissions on that socket. |
| 400 | |
| 401 | */ |
| 402 | |
| 403 | void QRemoteServiceRegister::setSecurityAccessOptions(SecurityAccessOptions options) |
| 404 | { |
| 405 | if (!d) init(); |
| 406 | d->setSecurityOptions(options); |
| 407 | } |
| 408 | |
| 409 | /*! |
| 410 | Allows a security filter to be set which can access |
| 411 | QRemoteServiceRegister::QRemoteServiceRegisterCredentials. |
| 412 | |
| 413 | The \a filter is a function pointer where the function code implements possible |
| 414 | permission checks and returns true or false. If a connecting client fails the security |
| 415 | filter it will be denied access and unable to obtain a valid service instance. |
| 416 | |
| 417 | The following snippet is an example of how to use the security filter feature. |
| 418 | |
| 419 | \code |
| 420 | bool myFunction(const void *p) |
| 421 | { |
| 422 | const QRemoteServiceRegisterCredentials *cred = |
| 423 | (const struct QRemoteServiceRegisterCredentials *)p; |
| 424 | |
| 425 | // allow the superuser |
| 426 | if (cred->uid == 0) |
| 427 | return true; |
| 428 | |
| 429 | return false; |
| 430 | } |
| 431 | |
| 432 | int main(int argc, char** argv) |
| 433 | { |
| 434 | ... |
| 435 | |
| 436 | QRemoteServiceRegister* serviceRegister = new QRemoteServiceRegister(); |
| 437 | service->setSecurityFilter(myFunction); |
| 438 | |
| 439 | ... |
| 440 | } |
| 441 | \endcode |
| 442 | |
| 443 | */ |
| 444 | QRemoteServiceRegister::SecurityFilter QRemoteServiceRegister::setSecurityFilter(QRemoteServiceRegister::SecurityFilter filter) |
| 445 | { |
| 446 | if (!d) init(); |
| 447 | return d->setSecurityFilter(filter); |
| 448 | } |
| 449 | |
| 450 | /*! |
| 451 | \fn QRemoteServiceRegister::createEntry(const QString& serviceName, const QString& interfaceName, const QString& version) |
| 452 | |
| 453 | Creates an entry on our remote instance manager. The \a serviceName, \a interfaceName and |
| 454 | \a version must match the service XML descriptor in order for the remote service to be |
| 455 | discoverable. |
| 456 | |
| 457 | \sa publishEntries() |
| 458 | */ |
| 459 | QRemoteServiceRegister::Entry QRemoteServiceRegister::createEntry(const QString& serviceName, const QString& interfaceName, const QString& version, QRemoteServiceRegister::CreateServiceFunc cptr, const QMetaObject* meta) |
| 460 | { |
| 461 | if (serviceName.isEmpty() |
| 462 | || interfaceName.isEmpty() |
| 463 | || version.isEmpty() ) { |
| 464 | qWarning() << "QRemoteServiceRegister::registerService: service name, interface name and version must be specified"; |
| 465 | return Entry(); |
| 466 | } |
| 467 | |
| 468 | Entry e; |
| 469 | e.d->service = serviceName; |
| 470 | e.d->iface = interfaceName; |
| 471 | e.d->ifaceVersion = version; |
| 472 | e.d->cptr = cptr; |
| 473 | e.d->meta = meta; |
| 474 | |
| 475 | Q_ASSERT(InstanceManager::instance()); |
| 476 | InstanceManager::instance()->addType(e); |
| 477 | |
| 478 | return e; |
| 479 | } |
| 480 | |
| 481 | bool QRemoteServiceRegister::event(QEvent *e) |
| 482 | { |
| 483 | if (!d && e->type() == QEvent::DynamicPropertyChange) { |
| 484 | QDynamicPropertyChangeEvent *event = static_cast<QDynamicPropertyChangeEvent*>(e); |
| 485 | if (event->propertyName() == QByteArray("serviceType")) { |
| 486 | QService::Type serviceType = static_cast<QService::Type>(property("serviceType").toInt()); |
| 487 | d = QRemoteServiceRegisterPrivate::constructPrivateObject(serviceType, this); |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | return QObject::event(e); |
| 492 | } |
| 493 | |
| 494 | void QRemoteServiceRegister::init() |
| 495 | { |
| 496 | d = QRemoteServiceRegisterPrivate::constructPrivateObject(this); |
| 497 | } |
| 498 | |
| 499 | #ifndef QT_NO_DATASTREAM |
| 500 | Q_SERVICEFW_EXPORT QDataStream& operator>>(QDataStream& s, QRemoteServiceRegister::Entry& entry) { |
| 501 | //for now we only serialize version, iface and service name |
| 502 | //needs to sync with qHash and operator== |
| 503 | s >> entry.d->service >> entry.d->iface >> entry.d->ifaceVersion; |
| 504 | return s; |
| 505 | } |
| 506 | |
| 507 | Q_SERVICEFW_EXPORT QDataStream& operator<<(QDataStream& s, const QRemoteServiceRegister::Entry& entry) { |
| 508 | //for now we only serialize version, iface and service name |
| 509 | //needs to sync with qHash and operator== |
| 510 | s << entry.d->service << entry.d->iface << entry.d->ifaceVersion; |
| 511 | return s; |
| 512 | } |
| 513 | #endif |
| 514 | |
| 515 | #ifndef QT_NO_DEBUG_STREAM |
| 516 | QDebug operator<<(QDebug dbg, const QRemoteServiceRegister::Entry& entry) { |
| 517 | dbg.nospace() << "QRemoteServiceRegister::Entry(" |
| 518 | << entry.serviceName() << ", " |
| 519 | << entry.interfaceName() << ", " |
| 520 | << entry.version() << ")"; |
| 521 | return dbg.space(); |
| 522 | } |
| 523 | #endif |
| 524 | |
| 525 | #include "moc_qremoteserviceregister.cpp" |
| 526 | |
| 527 | QT_END_NAMESPACE |