diff options
author | Lennart Poettering <lennart@poettering.net> | 2013-11-21 19:34:37 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2013-11-21 21:12:36 +0100 |
commit | ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602 (patch) | |
tree | def5185990acebac842ed8fca253531d88897a4a /src/libsystemd-bus/sd-bus.c | |
parent | 0ccad099d4c08dc5a16c87cdd6eefc05e9d4b670 (diff) |
bus: rework message handlers to always take an error argument
Message handler callbacks can be simplified drastically if the
dispatcher automatically replies to method calls if errors are returned.
Thus: add an sd_bus_error argument to all message handlers. When we
dispatch a message handler and it returns negative or a set sd_bus_error
we send this as message error back to the client. This means errors
returned by handlers by default are given back to clients instead of
rippling all the way up to the event loop, which is desirable to make
things robust.
As a side-effect we can now easily turn the SELinux checks into normal
function calls, since the method call dispatcher will generate the right
error replies automatically now.
Also, make sure we always pass the error structure to all property and
method handlers as last argument to follow the usual style of passing
variables for return values as last argument.
Diffstat (limited to 'src/libsystemd-bus/sd-bus.c')
-rw-r--r-- | src/libsystemd-bus/sd-bus.c | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/src/libsystemd-bus/sd-bus.c b/src/libsystemd-bus/sd-bus.c index 7f00eee6e1..2604434467 100644 --- a/src/libsystemd-bus/sd-bus.c +++ b/src/libsystemd-bus/sd-bus.c @@ -365,7 +365,7 @@ _public_ int sd_bus_set_anonymous(sd_bus *bus, int b) { return 0; } -static int hello_callback(sd_bus *bus, sd_bus_message *reply, void *userdata) { +static int hello_callback(sd_bus *bus, sd_bus_message *reply, void *userdata, sd_bus_error *error) { const char *s; int r; @@ -1824,6 +1824,7 @@ _public_ int sd_bus_get_timeout(sd_bus *bus, uint64_t *timeout_usec) { } static int process_timeout(sd_bus *bus) { + _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL; _cleanup_bus_message_unref_ sd_bus_message* m = NULL; struct reply_callback *c; usec_t n; @@ -1857,12 +1858,13 @@ static int process_timeout(sd_bus *bus) { bus->current = m; bus->iteration_counter ++; - r = c->callback(bus, m, c->userdata); + r = c->callback(bus, m, c->userdata, &error_buffer); + r = bus_maybe_reply_error(m, r, &error_buffer); free(c); bus->current = NULL; - return r < 0 ? r : 1; + return r; } static int process_hello(sd_bus *bus, sd_bus_message *m) { @@ -1888,6 +1890,7 @@ static int process_hello(sd_bus *bus, sd_bus_message *m) { } static int process_reply(sd_bus *bus, sd_bus_message *m) { + _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL; struct reply_callback *c; int r; @@ -1909,13 +1912,15 @@ static int process_reply(sd_bus *bus, sd_bus_message *m) { if (r < 0) return r; - r = c->callback(bus, m, c->userdata); + r = c->callback(bus, m, c->userdata, &error_buffer); + r = bus_maybe_reply_error(m, r, &error_buffer); free(c); - return r < 0 ? r : 1; + return r; } static int process_filter(sd_bus *bus, sd_bus_message *m) { + _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL; struct filter_callback *l; int r; @@ -1940,7 +1945,8 @@ static int process_filter(sd_bus *bus, sd_bus_message *m) { if (r < 0) return r; - r = l->callback(bus, m, l->userdata); + r = l->callback(bus, m, &error_buffer, l->userdata); + r = bus_maybe_reply_error(m, r, &error_buffer); if (r != 0) return r; @@ -2123,6 +2129,8 @@ static int process_closing(sd_bus *bus, sd_bus_message **ret) { c = hashmap_first(bus->reply_callbacks); if (c) { + _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL; + /* First, fail all outstanding method calls */ r = bus_message_new_synthetic_error( bus, @@ -2144,12 +2152,10 @@ static int process_closing(sd_bus *bus, sd_bus_message **ret) { bus->current = m; bus->iteration_counter++; - r = c->callback(bus, m, c->userdata); + r = c->callback(bus, m, c->userdata, &error_buffer); + r = bus_maybe_reply_error(m, r, &error_buffer); free(c); - if (r >= 0) - r = 1; - goto finish; } |