Search code examples
c++qtdbus

Get sender PID from DBUS


I have spent four hours in deep searching :), but didn't find: How to get PID from DBUS sender under Qt.

From other dbus libs it's quite simple:

Call dbus_message_get_sender to get the caller's D-Bus id, and then call GetConnectionUnixProcessID to get the pid of the process that sent the message.

How to do it from Qt ? I have try to use this code, but it's returns my PID not a client application.

class ServerAdaptor: public QDBusAbstractAdaptor
{
public Q_SLOTS:

 // this method shared through the D-BUS and called from different application (client)
 bool Show(const QString &in0)
 { 
    QDBusConnection connection = QDBusConnection::connectToBus(QDBusConnection::SessionBus, "org.freedesktop.DBus");

    if( connection.isConnected() )
    {
       qDebug() << "Sender PID " << connection.interface()->servicePid(  QDBusConnection::sender().baseService() ).value();
    }

 }
}

Edit:

How to get PID from remote application through D-BUS (Qt)

// this class receive D-BUS methods
class Server: public QObject, protected QDBusContext {

public slots:

    bool SomeMethod( const QString &name ) 
    {
     qDebug() << "PID is: " << connection().interface()->servicePid( message().service() ); 
    }

}


Solution

  • 1) const QDBusMessage & QDBusContext::message () const Returns the message that generated this call.

    2) QString QDBusMessage::service () const Returns the name of the service or the bus address of the remote method call.

    3) QDBusReply QDBusConnectionInterface::servicePid ( const QString & serviceName ) const Returns the Unix Process ID (PID) for the process currently holding the bus service serviceName.