[systemd-devel] Callback isn't called when using sd_bus_add_match

Lennart Poettering lennart at poettering.net
Wed May 4 13:00:51 UTC 2016


On Wed, 04.05.16 12:56, Ben-melech, Shiran (shiran.ben-melech at intel.com) wrote:

> Hi,
> 
> I'm currently developing a little library using sd-bus to work with bluez.
> I'm trying to catch signals sent over the bus using sd_bus_add_match (see code below)
> But the callback does not seem to be triggered.
> 
> I'm opening the bus using:
>         /* Connect to the system bus */
>         r = sd_bus_open_system(&new_context->bus);
>         if(r < 0) {
>                 syslog(LOG_ERR, "Failed to connect to system bus: %s\n", strerror(-r));
>         }
> 
> Could anyone please help?
> 
> static int test_callback(sd_bus_message *message, void *userdata, sd_bus_error *error)
> {
>         printf("callback called\n");
>         return EXIT_SUCCESS;
> }
> 
> int
> lb_register_for_device_data(lb_context *lb_ctx, sd_bus_message_handler_t callback, void *userdata)
> {
>         if (DEBUG > 0) printf("Method Called: %s\n", __FUNCTION__);
>         int r;
>         r = sd_bus_add_match(lb_ctx->bus, NULL, "type=signal", test_callback, userdata);
>         if (r < 0) {
>                 syslog(LOG_ERR, "Failed on sd_bus_add_object\n");
>                 return EXIT_FAILURE;
>         }
>         return EXIT_SUCCESS;
> }
> 
> I've tried various option in the "type" parameter but none succeeded.

Note that you have to run an event loop for the library to do
anything.

You can either use sd-event for that (which has a full set of man
pages), and then connect sd-bus to sd-event with
sd_bus_attach_event(), or you can connect sd-bus to any other event
loop of your choice, by using sd_bus_get_fd(), sd_bus_get_events() and
sd_bus_get_timeout(). A third option is to do without a proper event
loop, and just call sd_bus_wait() and sd_bus_process() in a loop. The
latter two options are a not particularly well documented.

Lennart

-- 
Lennart Poettering, Red Hat


More information about the systemd-devel mailing list