diff options
-rw-r--r-- | src/libsystemd-bus/bus-internal.h | 1 | ||||
-rw-r--r-- | src/libsystemd-bus/sd-bus.c | 63 | ||||
-rw-r--r-- | src/libsystemd-bus/sd-bus.h | 2 | ||||
-rw-r--r-- | src/libsystemd-bus/test-bus-chat.c | 35 |
4 files changed, 94 insertions, 7 deletions
diff --git a/src/libsystemd-bus/bus-internal.h b/src/libsystemd-bus/bus-internal.h index b0e97915cc..636e998c30 100644 --- a/src/libsystemd-bus/bus-internal.h +++ b/src/libsystemd-bus/bus-internal.h @@ -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)) diff --git a/src/libsystemd-bus/sd-bus.c b/src/libsystemd-bus/sd-bus.c index 1dd234d61b..049bd356a0 100644 --- a/src/libsystemd-bus/sd-bus.c +++ b/src/libsystemd-bus/sd-bus.c @@ -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; diff --git a/src/libsystemd-bus/sd-bus.h b/src/libsystemd-bus/sd-bus.h index 1d7bfa86b8..d5101c20d7 100644 --- a/src/libsystemd-bus/sd-bus.h +++ b/src/libsystemd-bus/sd-bus.h @@ -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; diff --git a/src/libsystemd-bus/test-bus-chat.c b/src/libsystemd-bus/test-bus-chat.c index 9e14824186..d8677cc9e8 100644 --- a/src/libsystemd-bus/test-bus-chat.c +++ b/src/libsystemd-bus/test-bus-chat.c @@ -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)); |