【问题标题】:Get sender PID from DBUS从 DBUS 获取发送方 PID
【发布时间】:2012-03-20 11:09:34
【问题描述】:

我花了四个小时深入搜索:),但没有找到:如何在 Qt 下从 DBUS 发送方获取 PID。

从其他 dbus 库来看,这很简单:

调用 dbus_message_get_sender 获取调用者的 D-Bus id,然后调用 GetConnectionUnixProcessID 获取发送消息的进程的 pid。

如何从 Qt 做到这一点? 我尝试使用此代码,但它返回的是我的 PID 而不是客户端应用程序。

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();
    }

 }
}

编辑:

如何通过 D-BUS (Qt) 从远程应用程序获取 PID

// 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() ); 
    }

}

【问题讨论】:

    标签: c++ qt dbus


    【解决方案1】:

    1) const QDBusMessage & QDBusContext::message () const 返回生成此调用的消息。

    2) QString QDBusMessage::service () const 返回服务名称或远程方法调用的总线地址。

    3) QDBusReply QDBusConnectionInterface::servicePid ( const QString & serviceName ) const 返回当前持有总线服务 serviceName 的进程的 Unix 进程 ID (PID)。

    【讨论】:

      【解决方案2】:

      您是否考虑过从QDBusContext 派生您的注册课程?这应该会在您的广告位中为您提供更多信息。

      【讨论】:

      • connection().baseService() 与 QDBusConnection::sender().baseService() 相同。我需要获取有关来电者的信息,而不是关于我的信息。如何获得有关调用者进程的信息?我需要获取连接的总线名称以从 org.freedesktop.DBus.GetConnectionUnixProcessID 检索 PID
      猜你喜欢
      • 1970-01-01
      • 2019-02-14
      • 2016-05-16
      • 2015-06-23
      • 2021-11-18
      • 2012-01-05
      • 1970-01-01
      • 1970-01-01
      • 2012-08-29
      相关资源
      最近更新 更多