]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
man: improve VtableExample
authorLuca Boccassi <bluca@debian.org>
Wed, 11 May 2022 14:19:58 +0000 (15:19 +0100)
committerLuca Boccassi <luca.boccassi@gmail.com>
Wed, 11 May 2022 18:12:24 +0000 (19:12 +0100)
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.

man/vtable-example.c

index c6e73ae7c9047e1c73b62d4087f340c4b4b46c2a..d82bb18a3901dbe6b7cf569f3c7a935c74e23b43 100644 (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;