chiark / gitweb /
bus: implicitly handle peer commands Ping() and GetMachineId()
authorLennart Poettering <lennart@poettering.net>
Thu, 21 Mar 2013 23:08:58 +0000 (00:08 +0100)
committerLennart Poettering <lennart@poettering.net>
Thu, 21 Mar 2013 23:12:37 +0000 (00:12 +0100)
src/libsystemd-bus/bus-internal.h
src/libsystemd-bus/sd-bus.c
src/libsystemd-bus/sd-bus.h
src/libsystemd-bus/test-bus-chat.c

index b0e9791..636e998 100644 (file)
@@ -108,6 +108,7 @@ static inline void bus_unrefp(sd_bus **b) {
 }
 
 #define _cleanup_bus_unref_ __attribute__((cleanup(bus_unrefp)))
+#define _cleanup_bus_error_free_ __attribute__((cleanup(sd_bus_error_free)))
 
 #define BUS_DEFAULT_TIMEOUT ((usec_t) (25 * USEC_PER_SEC))
 
index 1dd234d..049bd35 100644 (file)
@@ -1475,6 +1475,57 @@ static int process_timeout(sd_bus *bus) {
         return r < 0 ? r : 1;
 }
 
+static int process_builtin(sd_bus *bus, sd_bus_message *m) {
+        _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
+        int r;
+
+        assert(bus);
+        assert(m);
+
+        if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
+                return 0;
+
+        if (!streq_ptr(m->interface, "org.freedesktop.DBus.Peer"))
+                return 0;
+
+        if (m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
+                return 1;
+
+        if (streq_ptr(m->member, "Ping"))
+                r = sd_bus_message_new_method_return(bus, m, &reply);
+        else if (streq_ptr(m->member, "GetMachineId")) {
+                sd_id128_t id;
+                char sid[33];
+
+                r = sd_id128_get_machine(&id);
+                if (r < 0)
+                        return r;
+
+                r = sd_bus_message_new_method_return(bus, m, &reply);
+                if (r < 0)
+                        return r;
+
+                r = sd_bus_message_append(reply, "s", sd_id128_to_string(id, sid));
+        } else {
+                _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_INIT;
+
+                sd_bus_error_set(&error,
+                                 "org.freedesktop.DBus.Error.UnknownMethod",
+                                 "Unknown method '%s' on interface '%s'.", m->member, m->interface);
+
+                r = sd_bus_message_new_method_error(bus, m, &error, &reply);
+        }
+
+        if (r < 0)
+                return r;
+
+        r = sd_bus_send(bus, reply, NULL);
+        if (r < 0)
+                return r;
+
+        return 1;
+}
+
 static int process_message(sd_bus *bus, sd_bus_message *m) {
         struct filter_callback *l;
         int r;
@@ -1482,7 +1533,7 @@ static int process_message(sd_bus *bus, sd_bus_message *m) {
         assert(bus);
         assert(m);
 
-        if (m->header->type == SD_BUS_MESSAGE_TYPE_METHOD_CALL || m->header->type == SD_BUS_MESSAGE_TYPE_METHOD_RETURN) {
+        if (m->header->type == SD_BUS_MESSAGE_TYPE_METHOD_RETURN || m->header->type == SD_BUS_MESSAGE_TYPE_METHOD_ERROR) {
                 struct reply_callback *c;
 
                 c = hashmap_remove(bus->reply_callbacks, &m->reply_serial);
@@ -1504,7 +1555,7 @@ static int process_message(sd_bus *bus, sd_bus_message *m) {
                         return r;
         }
 
-        return 0;
+        return process_builtin(bus, m);
 }
 
 int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
@@ -1599,11 +1650,13 @@ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
                         return 1;
                 }
 
-                if (sd_bus_message_is_method_call(m, NULL, NULL)) {
-                        const sd_bus_error e = SD_BUS_ERROR_INIT_CONST("org.freedesktop.DBus.Error.UnknownObject", "Unknown object.");
+                if (m->header->type == SD_BUS_MESSAGE_TYPE_METHOD_CALL) {
                         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
+                        _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_INIT;
+
+                        sd_bus_error_set(&error, "org.freedesktop.DBus.Error.UnknownObject", "Unknown object '%s'.", m->path);
 
-                        r = sd_bus_message_new_method_error(bus, m, &e, &reply);
+                        r = sd_bus_message_new_method_error(bus, m, &error, &reply);
                         if (r < 0)
                                 return r;
 
index 1d7bfa8..d5101c2 100644 (file)
@@ -36,8 +36,8 @@
  * - handle NULL strings nicer when appending
  * - merge busctl into systemctl or so?
  * - add object handlers
- * - add peer message handlers
  * - verify object paths
+ * - implicitly add stub introspection calls
  */
 
 typedef struct sd_bus sd_bus;
index 9e14824..d8677cc 100644 (file)
@@ -264,8 +264,9 @@ static void* client2(void*p) {
         _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
         sd_bus *bus = NULL;
         sd_bus_error error = SD_BUS_ERROR_INIT;
-        int r;
         bool quit = false;
+        const char *mid;
+        int r;
 
         r = sd_bus_open_user(&bus);
         if (r < 0) {
@@ -277,6 +278,35 @@ static void* client2(void*p) {
                         bus,
                         "org.freedesktop.systemd.test",
                         "/",
+                        "org.freedesktop.DBus.Peer",
+                        "GetMachineId",
+                        &m);
+        if (r < 0) {
+                log_error("Failed to allocate method call: %s", strerror(-r));
+                goto finish;
+        }
+
+        r = sd_bus_send_with_reply_and_block(bus, m, 0, &error, &reply);
+        if (r < 0) {
+                log_error("Failed to issue method call: %s", bus_error_message(&error, -r));
+                goto finish;
+        }
+
+        r = sd_bus_message_read(reply, "s", &mid);
+        if (r < 0) {
+                log_error("Failed to parse machine ID: %s", strerror(-r));
+                goto finish;
+        }
+
+        log_info("Machine ID is %s.", mid);
+
+        sd_bus_message_unref(m);
+        m = NULL;
+
+        r = sd_bus_message_new_method_call(
+                        bus,
+                        "org.freedesktop.systemd.test",
+                        "/",
                         "org.freedesktop.systemd.test",
                         "Slow",
                         &m);
@@ -285,6 +315,9 @@ static void* client2(void*p) {
                 goto finish;
         }
 
+        sd_bus_message_unref(reply);
+        reply = NULL;
+
         r = sd_bus_send_with_reply_and_block(bus, m, 200 * USEC_PER_MSEC, &error, &reply);
         if (r < 0)
                 log_info("Failed to issue method call: %s", bus_error_message(&error, -r));