尝试获取具有自定义类型的DBus属性时出现致命错误

时间:2015-02-05 13:37:13

标签: c++ qt dbus qtdbus

我试图将UDisks2与Qt应用程序连接,并且读取自定义属性会使程序崩溃。

对于SMART属性,一切正常(包括custom properties),但当我尝试读取RAID设备属性ActiveDevices时,我收到Qt致命错误并且应用程序崩溃

以下是一个测试程序,用于说明行为:

#include <QApplication>

#include <QDBusMetaType>
#include <QDBusConnection>
#include <QDBusInterface>

#include <QDebug>


//create the structure as defined in 
// http://udisks.freedesktop.org/docs/latest/gdbus-org.freedesktop.UDisks2.MDRaid.html#gdbus-property-org-freedesktop-UDisks2-MDRaid.ActiveDevices 
struct MDRaidMember {
   QDBusObjectPath block;
   qint32 slot;
   QStringList state;
   qint64 numReadErrors;
   QVariantMap expansion;
};
Q_DECLARE_METATYPE(MDRaidMember)


//marshalling operator
QDBusArgument &operator<<(QDBusArgument &argument, const MDRaidMember& raidMember)
{
    argument.beginStructure();
    argument << raidMember.block;
    argument << raidMember.slot;
    argument << raidMember.state;
    argument << raidMember.numReadErrors;
    argument << raidMember.expansion;
    argument.endStructure();
    return argument;
}


//unmarshall operator
const QDBusArgument &operator>>(const QDBusArgument &argument, MDRaidMember& raidMember)
{
    argument.beginStructure();
    argument >> raidMember.block;
    argument >> raidMember.slot;
    argument >> raidMember.state;
    argument >> raidMember.numReadErrors;
    argument >> raidMember.expansion;
    argument.endStructure();
    return argument;
}



int main(int argc, char *argv[]) {
  QApplication app(argc, argv);

  //register the type, everything looks fine
  qDebug() << qRegisterMetaType<MDRaidMember>("MDRaidMember");
  qDebug() << qDBusRegisterMetaType<MDRaidMember>();


  QDBusInterface iface("org.freedesktop.UDisks2",
                       "/org/freedesktop/UDisks2/mdraid/9aec6784_86ec37d4_8c1c6add_4dc9fe81",
                       "org.freedesktop.UDisks2.MDRaid",
                       QDBusConnection::systemBus());

  //read some properties
  qDebug() << iface.property("UUID");
  qDebug() << iface.property("ActiveDevices"); //crash the test program!

  return app.exec();
}

执行此代码会产生以下输出

1057
1057
QVariant(QString, "9aec6784:86ec37d4:8c1c6add:4dc9fe81")
Cannot construct placeholder type QDBusRawType
zsh: abort (core dumped)  ./app/test_members

有人遇到过这种情况吗?我根本不认为它与我的代码有关,因为如果没有声明自定义类型(仅保留 main 函数的内容),结果是相同的。

1 个答案:

答案 0 :(得分:2)

我没有为此找到正确答案,但这是一种解决方法:在接口Get上调用org.freedesktop.DBus.Properties方法并手动解组结果。棘手的部分是确定结果的内容(它是 QVariant 包含 QDBusVariant 包含 QDBusArgument

更新的main功能:

//...

int main(int argc, char *argv[]) {
  QApplication app(argc, argv);

  //register the type, everything looks fine
  qDebug() << qRegisterMetaType<MDRaidMember>("MDRaidMember");
  qDebug() << qDBusRegisterMetaType<MDRaidMember>();


  QDBusInterface iface("org.freedesktop.UDisks2",
                   "/org/freedesktop/UDisks2/mdraid/9aec6784_86ec37d4_8c1c6add_4dc9fe81",
                   "org.freedesktop.DBus.Properties",
                   QDBusConnection::systemBus());

  QDBusMessage reply = iface.call("Get", "org.freedesktop.UDisks2.MDRaid", "ActiveDevices");
  QVariant v = reply.arguments().first();
  QDBusArgument arg = v.value<QDBusVariant>().variant().value<QDBusArgument>();

  QList<MDRaidMember> members;
  arg.beginArray();
  while(!arg.atEnd()) {
    MDRaidMember m;
    arg >> m;
    members << m;
  }

  qDebug() << members;

  return app.exec();
}