1
votes

I have one problem. In c++ app, I am using sd-bus and signal does not call my callback function.

I hooked to org.freedesktop.login1, interface is org.freedesktop.DBus.Properties, member is PropertiesChanged and path is /org/freedesktop/login1/seat/seat0

In my connect method I have this:

sd_bus_add_match(m_bus, NULL, "interface='org.freedesktop.DBus.Properties',member='PropertiesChanged',path='/org/freedesktop/login1/seat/seat0',type='signal'", on_properties_changed, NULL)

On properties changed method is this:

static int on_properties_changed(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
    printf("got some signal");
}

So, when I ran this program, I also ran following command in cmd:ΒΈ

gdbus monitor --system --dest org.freedesktop.login1 --object-path /org/freedesktop/login1/seat/seat0

However, when I switch from userA to userB, I got following line in cmd window:

/org/freedesktop/login1/seat/seat0: org.freedesktop.DBus.Properties.PropertiesChanged ('org.freedesktop.login1.Seat', {'ActiveSession': <('c7', objectpath '/org/freedesktop/login1/session/c7')>}, @as [])

Also when I tried this

busctl --system --match "interface='org.freedesktop.DBus.Properties',member='PropertiesChanged',path='/org/freedesktop/login1/seat/seat0',type='signal' monitor

then I also get proper response

Type=signal  Endian=l  Flags=1  Version=1  Priority=0 Cookie=2281
  Sender=:1.0  Path=/org/freedesktop/login1/seat/seat0  Interface=org.freedesktop.DBus.Properties  Member=PropertiesChanged
  UniqueName=:1.0
  MESSAGE "sa{sv}as" {
          STRING "org.freedesktop.login1.Seat";
          ARRAY "{sv}" {
                  DICT_ENTRY "sv" {
                          STRING "ActiveSession";
                          VARIANT "(so)" {
                                  STRUCT "so" {
                                          STRING "c2";
                                          OBJECT_PATH "/org/freedesktop/login1/session/c2";
                                  };
                          };
                  };
          };
          ARRAY "s" {
          };
  };

But in c++ that callback function is not called. Any idea why is it not called?

I am using Ubuntu 16.04 and my systemd version is 229.

1

1 Answers

2
votes

I found a solution to this problem. Problem was that I wasn't hooked to any event loop.

So I created new function run() and in this function I say this:

while(m_running) {
    sd_bus_message *m = NULL;
    r = sd_bus_process(m_bus, &m);
    if (r < 0) {
        //error handling
    }

    r = sd_bus_wait(m_bus, (uint64_t)-1);
    if (r < 0) {
        //error handling
    }
}

and now I call this function after I connect to signal, and callback function of sd_bus_add_match is normally called