-1

I'm writing a program for a Raspberry Pi that needs to be notified when the user inserts a USB drive. I'm using Qt's D-Bus support to listen to InterfacesAdded from org.freedesktop.UDisks2, which works fine on my x86 Linux desktop.

Here's my code that sets up the D-Bus signal to my class's slot:

auto bus = QDBusConnection::systemBus();
auto monitor = new UsbMonitor(&app);   // my class

if (!bus.connect("org.freedesktop.UDisks2", "/org/freedesktop/UDisks2", "org.freedesktop.DBus.ObjectManager", "InterfacesAdded",
            monitor, SLOT(handleit(QDBusObjectPath, QMap<QString,QVariant>)))) {
    qFatal("FATAL Couldn't connect USB monitor to D-Bus signal");
}

It does not work on the Pi. I tracked it down to the fact that udisksd isn't running, even though it should automatically start whenever it's needed.

I'm guessing that because I'm booting to console (not GUI) and only adding a signal handler, that that's not enough to have D-Bus automatically start udisksd.

If I add

auto msg = QDBusMessage::createMethodCall("org.freedesktop.UDisks2", "/org/freedesktop/UDisks2", "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
bus.call(msg);

before my call to bus.connect then everything works.

Is there a better way to make sure udisksd is running? I'd rather change a configuration file on the Pi than have to have extra code just to "wake up" a service.

1 Answers1

2

Depends what distribution you are running on your Pi. If you’re using a distribution which uses systemd, you can run sudo systemctl enable --now udisks2.service to configure the service to always start.

You might want to extend your code to watch the org.freedesktop.UDisks2 name on D-Bus, since you may want to re-connect the signal when it disappears and reappears. (I don’t know exactly how Qt D-Bus handles this; whether it resolves well-known names to unique names at signal handler registration time, or at signal emission time.)

Philip Withnall
  • 5,293
  • 14
  • 28
  • That's such a simple solution thanks! I didn't realize that udisks2 was written as a regular systemd service file. However, since it's a ["static"](https://bbs.archlinux.org/viewtopic.php?id=147964) service, the `systemctl enable` command doesn't work like it would for a normal service. So, since my own app is started as a systemd service, I can make it depend on udisks2. – A Humble Supplicant Apr 27 '17 at 15:42