From 917b5dc7075a15bded4948efba8e686dba4509f8 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 5 Apr 2013 04:15:39 +0200 Subject: [PATCH] bus: add convenience functions for constructing and sending method calls/signals in one call --- src/journal/journal-gatewayd.c | 18 ++++------ src/libsystemd-bus/bus-message.c | 12 +++---- src/libsystemd-bus/bus-message.h | 2 ++ src/libsystemd-bus/sd-bus.c | 57 ++++++++++++++++++++++++++++++++ src/systemd/sd-bus.h | 5 +++ 5 files changed, 77 insertions(+), 17 deletions(-) diff --git a/src/journal/journal-gatewayd.c b/src/journal/journal-gatewayd.c index 133d0ae478..a633638f80 100644 --- a/src/journal/journal-gatewayd.c +++ b/src/journal/journal-gatewayd.c @@ -743,7 +743,7 @@ static int request_handler_file( } static int get_virtualization(char **v) { - _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL; + _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; _cleanup_bus_unref_ sd_bus *bus = NULL; const char *t; char *b; @@ -753,21 +753,17 @@ static int get_virtualization(char **v) { if (r < 0) return r; - r = sd_bus_message_new_method_call( + r = sd_bus_call_method( bus, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.DBus.Properties", "Get", - &m); - if (r < 0) - return r; - - r = sd_bus_message_append(m, "ss", "org.freedesktop.systemd1.Manager", "Virtualization"); - if (r < 0) - return r; - - r = sd_bus_send_with_reply_and_block(bus, m, 0, NULL, &reply); + NULL, + &reply, + "ss", + "org.freedesktop.systemd1.Manager", + "Virtualization"); if (r < 0) return r; diff --git a/src/libsystemd-bus/bus-message.c b/src/libsystemd-bus/bus-message.c index 773a670119..7acab50979 100644 --- a/src/libsystemd-bus/bus-message.c +++ b/src/libsystemd-bus/bus-message.c @@ -1241,7 +1241,7 @@ int sd_bus_message_close_container(sd_bus_message *m) { return 0; } -static int message_append_ap( +int bus_message_append_ap( sd_bus_message *m, const char *types, va_list ap) { @@ -1327,7 +1327,7 @@ static int message_append_ap( n = va_arg(ap, unsigned); for (i = 0; i < n; i++) { - r = message_append_ap(m, s, ap); + r = bus_message_append_ap(m, s, ap); if (r < 0) return r; } @@ -1349,7 +1349,7 @@ static int message_append_ap( if (r < 0) return r; - r = message_append_ap(m, s, ap); + r = bus_message_append_ap(m, s, ap); if (r < 0) return r; @@ -1377,7 +1377,7 @@ static int message_append_ap( t += k - 1; - r = message_append_ap(m, s, ap); + r = bus_message_append_ap(m, s, ap); if (r < 0) return r; @@ -1407,10 +1407,10 @@ int sd_bus_message_append(sd_bus_message *m, const char *types, ...) { if (m->sealed) return -EPERM; if (!types) - return -EINVAL; + return 0; va_start(ap, types); - r = message_append_ap(m, types, ap); + r = bus_message_append_ap(m, types, ap); va_end(ap); return r; diff --git a/src/libsystemd-bus/bus-message.h b/src/libsystemd-bus/bus-message.h index 91b1668f19..cc1e10d843 100644 --- a/src/libsystemd-bus/bus-message.h +++ b/src/libsystemd-bus/bus-message.h @@ -144,3 +144,5 @@ int bus_message_from_malloc( sd_bus_message **ret); const char* bus_message_get_arg(sd_bus_message *m, unsigned i); + +int bus_message_append_ap(sd_bus_message *m, const char *types, va_list ap); diff --git a/src/libsystemd-bus/sd-bus.c b/src/libsystemd-bus/sd-bus.c index 943cd43cb1..8c5b9da91d 100644 --- a/src/libsystemd-bus/sd-bus.c +++ b/src/libsystemd-bus/sd-bus.c @@ -2208,3 +2208,60 @@ int sd_bus_remove_match(sd_bus *bus, const char *match, sd_bus_message_handler_t return r; return q; } + +int sd_bus_emit_signal( + sd_bus *bus, + const char *path, + const char *interface, + const char *member, + const char *types, ...) { + + _cleanup_bus_message_unref_ sd_bus_message *m = NULL; + va_list ap; + int r; + + if (!bus) + return -EINVAL; + + r = sd_bus_message_new_signal(bus, path, interface, member, &m); + if (r < 0) + return r; + + va_start(ap, types); + r = bus_message_append_ap(m, types, ap); + va_end(ap); + if (r < 0) + return r; + + return sd_bus_send(bus, m, NULL); +} + +int sd_bus_call_method( + sd_bus *bus, + const char *destination, + const char *path, + const char *interface, + const char *member, + sd_bus_error *error, + sd_bus_message **reply, + const char *types, ...) { + + _cleanup_bus_message_unref_ sd_bus_message *m = NULL; + va_list ap; + int r; + + if (!bus) + return -EINVAL; + + r = sd_bus_message_new_method_call(bus, destination, path, interface, member, &m); + if (r < 0) + return r; + + va_start(ap, types); + r = bus_message_append_ap(m, types, ap); + va_end(ap); + if (r < 0) + return r; + + return sd_bus_send_with_reply_and_block(bus, m, 0, error, reply); +} diff --git a/src/systemd/sd-bus.h b/src/systemd/sd-bus.h index 057931d0df..fd87de0813 100644 --- a/src/systemd/sd-bus.h +++ b/src/systemd/sd-bus.h @@ -145,6 +145,11 @@ int sd_bus_message_exit_container(sd_bus_message *m); int sd_bus_message_peek_type(sd_bus_message *m, char *type, const char **contents); int sd_bus_message_rewind(sd_bus_message *m, int complete); +/* Convenience calls */ + +int sd_bus_emit_signal(sd_bus *bus, const char *path, const char *interface, const char *member, const char *types, ...); +int sd_bus_call_method(sd_bus *bus, const char *destination, const char *path, const char *interface, const char *member, sd_bus_error *error, sd_bus_message **reply, const char *types, ...); + /* Bus management */ int sd_bus_get_unique_name(sd_bus *bus, const char **unique);