diff --git a/man/vtable-example.c b/man/vtable-example.c index c6e73ae7c90..d82bb18a390 100644 --- a/man/vtable-example.c +++ b/man/vtable-example.c @@ -9,6 +9,14 @@ #define _cleanup_(f) __attribute__((cleanup(f))) +#define check(x) ({ \ + int r = (x); \ + errno = r < 0 ? -r : 0; \ + printf(#x ": %m\n"); \ + if (r < 0) \ + return EXIT_FAILURE; \ + }) + typedef struct object { char *name; uint32_t number; @@ -16,6 +24,16 @@ typedef struct object { static int method(sd_bus_message *m, void *userdata, sd_bus_error *error) { printf("Got called with userdata=%p\n", userdata); + + if (sd_bus_message_is_method_call(m, + "org.freedesktop.systemd.VtableExample", + "Method4")) + return 1; + + const char *string; + check(sd_bus_message_read(m, "s", &string)); + check(sd_bus_reply_method_return(m, "s", string)); + return 1; } @@ -64,14 +82,6 @@ static const sd_bus_vtable vtable[] = { SD_BUS_VTABLE_END }; -#define check(x) ({ \ - int r = x; \ - errno = r < 0 ? -r : 0; \ - printf(#x ": %m\n"); \ - if (r < 0) \ - return EXIT_FAILURE; \ - }) - int main(int argc, char **argv) { _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; @@ -80,16 +90,22 @@ int main(int argc, char **argv) { object object = { .number = 666 }; check((object.name = strdup("name")) != NULL); - check(sd_bus_add_object_vtable(bus, NULL, "/object", + check(sd_bus_add_object_vtable(bus, NULL, + "/org/freedesktop/systemd/VtableExample", "org.freedesktop.systemd.VtableExample", vtable, &object)); + check(sd_bus_request_name(bus, + "org.freedesktop.systemd.VtableExample", + 0)); + for (;;) { check(sd_bus_wait(bus, UINT64_MAX)); check(sd_bus_process(bus, NULL)); } + check(sd_bus_release_name(bus, "org.freedesktop.systemd.VtableExample")); free(object.name); return 0;