man: improve VtableExample

The methods published by the example have a reply in the signature, but
the code was not sending any, so the client gets stuck waiting for a
response that doesn't arrive. Echo back the input string.

Update the object path to follow what would be the canonical format.

Request a service name on the bus, so that the code can be dropped in a
service and it can be dbus-activatable. It also makes it easier to see
on busctl list.
This commit is contained in:
Luca Boccassi 2022-05-11 15:19:58 +01:00 committed by Luca Boccassi
parent 646cba5c42
commit 0cfb00d9da

View file

@ -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;