diff options
author | Lennart Poettering <lennart@poettering.net> | 2013-10-16 06:10:04 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2013-10-16 06:15:02 +0200 |
commit | 40ca29a1370379d43e44c0ed425eecc7218dcbca (patch) | |
tree | 39cde46c8166bd79471b0111251693edddfae3c4 /src/libsystemd-bus | |
parent | 7a37d62501c97eb3b48a418ef764d0308e0c3fb9 (diff) |
timedated: use libsystemd-bus instead of libdbus for bus communication
Among other things this also adds a few things necessary for the change:
- Considerably more powerful error returning APIs in libsystemd-bus
- Adapter for connecting an sd_bus to an sd_event
- As I reworked the PolicyKit logic to the new library I also made it
asynchronous, so that PolicyKit requests of one user cannot block out
another user anymore.
- We always use the macro names for common bus error. That way it is
harder to mistype them since the compiler will notice
Diffstat (limited to 'src/libsystemd-bus')
22 files changed, 1060 insertions, 210 deletions
diff --git a/src/libsystemd-bus/bus-control.c b/src/libsystemd-bus/bus-control.c index 0ba8585805..836570d41e 100644 --- a/src/libsystemd-bus/bus-control.c +++ b/src/libsystemd-bus/bus-control.c @@ -33,6 +33,7 @@ #include "bus-message.h" #include "bus-control.h" #include "bus-bloom.h" +#include "bus-util.h" int sd_bus_get_unique_name(sd_bus *bus, const char **unique) { int r; diff --git a/src/libsystemd-bus/bus-convenience.c b/src/libsystemd-bus/bus-convenience.c index ddde5da163..95a7577b29 100644 --- a/src/libsystemd-bus/bus-convenience.c +++ b/src/libsystemd-bus/bus-convenience.c @@ -22,6 +22,7 @@ #include "bus-internal.h" #include "bus-message.h" #include "bus-signature.h" +#include "bus-util.h" int sd_bus_emit_signal( sd_bus *bus, @@ -99,7 +100,7 @@ int sd_bus_reply_method_return( assert_return(bus, -EINVAL); assert_return(call, -EINVAL); assert_return(call->sealed, -EPERM); - assert_return(call->header->type == SD_BUS_MESSAGE_TYPE_METHOD_CALL, -EINVAL); + assert_return(call->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL); assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN); assert_return(!bus_pid_changed(bus), -ECHILD); @@ -134,7 +135,7 @@ int sd_bus_reply_method_error( assert_return(bus, -EINVAL); assert_return(call, -EINVAL); assert_return(call->sealed, -EPERM); - assert_return(call->header->type == SD_BUS_MESSAGE_TYPE_METHOD_CALL, -EINVAL); + assert_return(call->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL); assert_return(sd_bus_error_is_set(e), -EINVAL); assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN); assert_return(!bus_pid_changed(bus), -ECHILD); @@ -163,29 +164,74 @@ int sd_bus_reply_method_errorf( assert_return(bus, -EINVAL); assert_return(call, -EINVAL); assert_return(call->sealed, -EPERM); - assert_return(call->header->type == SD_BUS_MESSAGE_TYPE_METHOD_CALL, -EINVAL); + assert_return(call->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL); assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN); assert_return(!bus_pid_changed(bus), -ECHILD); if (call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED) return 0; - error.name = strdup(name); - if (!error.name) - return -ENOMEM; + va_start(ap, format); + r = bus_error_setfv(&error, name, format, ap); + va_end(ap); - error.need_free = true; + if (r < 0) + return r; - if (format) { - va_start(ap, format); - r = vasprintf((char**) &error.message, format, ap); - va_end(ap); + return sd_bus_reply_method_error(bus, call, &error); +} - if (r < 0) - return -ENOMEM; - } +int sd_bus_reply_method_errno( + sd_bus *bus, + sd_bus_message *call, + int error, + const sd_bus_error *p) { - return sd_bus_reply_method_error(bus, call, &error); + _cleanup_bus_error_free_ sd_bus_error berror = SD_BUS_ERROR_NULL; + + assert_return(bus, -EINVAL); + assert_return(call, -EINVAL); + assert_return(call->sealed, -EPERM); + assert_return(call->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL); + assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN); + assert_return(!bus_pid_changed(bus), -ECHILD); + + if (call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED) + return 0; + + if (sd_bus_error_is_set(p)) + return sd_bus_reply_method_error(bus, call, p); + + sd_bus_error_set_errno(&berror, error); + + return sd_bus_reply_method_error(bus, call, &berror); +} + +int sd_bus_reply_method_errnof( + sd_bus *bus, + sd_bus_message *call, + int error, + const char *format, + ...) { + + _cleanup_bus_error_free_ sd_bus_error berror = SD_BUS_ERROR_NULL; + va_list ap; + + assert_return(bus, -EINVAL); + assert_return(call, -EINVAL); + assert_return(call->sealed, -EPERM); + assert_return(call->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL); + assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN); + assert_return(!bus_pid_changed(bus), -ECHILD); + + if (call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED) + return 0; + + va_start(ap, format); + bus_error_set_errnofv(&berror, error, format, ap); + va_end(ap); + + return sd_bus_reply_method_error(bus, call, &berror); } int sd_bus_get_property( diff --git a/src/libsystemd-bus/bus-error.c b/src/libsystemd-bus/bus-error.c index 28fe15467f..c347b101bc 100644 --- a/src/libsystemd-bus/bus-error.c +++ b/src/libsystemd-bus/bus-error.c @@ -56,10 +56,9 @@ int sd_bus_error_set(sd_bus_error *e, const char *name, const char *message) { if (!e) return 0; - if (bus_error_is_dirty(e)) - return -EINVAL; - if (!name) - return -EINVAL; + + assert_return(!bus_error_is_dirty(e), -EINVAL); + assert_return(name, -EINVAL); n = strdup(name); if (!n) @@ -78,27 +77,22 @@ int sd_bus_error_set(sd_bus_error *e, const char *name, const char *message) { return 0; } -int sd_bus_error_setf(sd_bus_error *e, const char *name, const char *format, ...) { +int bus_error_setfv(sd_bus_error *e, const char *name, const char *format, va_list ap) { char *n, *m = NULL; - va_list ap; int r; if (!e) return 0; - if (bus_error_is_dirty(e)) - return -EINVAL; - if (!name) - return -EINVAL; + + assert_return(!bus_error_is_dirty(e), -EINVAL); + assert_return(name, -EINVAL); n = strdup(name); if (!n) return -ENOMEM; if (format) { - va_start(ap, format); r = vasprintf(&m, format, ap); - va_end(ap); - if (r < 0) { free(n); return -ENOMEM; @@ -112,16 +106,32 @@ int sd_bus_error_setf(sd_bus_error *e, const char *name, const char *format, ... return 0; } +int sd_bus_error_setf(sd_bus_error *e, const char *name, const char *format, ...) { + + if (format) { + int r; + va_list ap; + + va_start(ap, format); + r = bus_error_setfv(e, name, format, ap); + va_end(ap); + + return r; + } + + return sd_bus_error_set(e, name, NULL); +} + int sd_bus_error_copy(sd_bus_error *dest, const sd_bus_error *e) { char *x, *y = NULL; if (!dest) return 0; - if (bus_error_is_dirty(dest)) - return -EINVAL; if (!sd_bus_error_is_set(e)) return 0; + assert_return(!bus_error_is_dirty(dest), -EINVAL); + x = strdup(e->name); if (!x) return -ENOMEM; @@ -140,13 +150,15 @@ int sd_bus_error_copy(sd_bus_error *dest, const sd_bus_error *e) { return 0; } -void sd_bus_error_set_const(sd_bus_error *e, const char *name, const char *message) { +int sd_bus_error_set_const(sd_bus_error *e, const char *name, const char *message) { if (!e) - return; - if (bus_error_is_dirty(e)) - return; + return 0; + + assert_return(!bus_error_is_dirty(e), -EINVAL); + assert_return(name, -EINVAL); *e = SD_BUS_ERROR_MAKE(name, message); + return 0; } int sd_bus_error_is_set(const sd_bus_error *e) { @@ -163,106 +175,282 @@ int sd_bus_error_has_name(const sd_bus_error *e, const char *name) { return streq_ptr(e->name, name); } -int bus_error_to_errno(const sd_bus_error* e) { +int sd_bus_error_get_errno(const sd_bus_error* e) { /* Better replce this with a gperf table */ if (!e) - return -EIO; + return EIO; if (!e->name) - return -EIO; + return EIO; - if (streq(e->name, "org.freedesktop.DBus.Error.NoMemory")) - return -ENOMEM; + if (streq(e->name, SD_BUS_ERROR_NO_MEMORY)) + return ENOMEM; + + if (streq(e->name, SD_BUS_ERROR_SERVICE_UNKNOWN)) + return EHOSTUNREACH; + + if (streq(e->name, SD_BUS_ERROR_NAME_HAS_NO_OWNER)) + return ENXIO; + + if (streq(e->name, SD_BUS_ERROR_NO_REPLY) || + streq(e->name, SD_BUS_ERROR_TIMEOUT) || + streq(e->name, "org.freedesktop.DBus.Error.TimedOut")) + return ETIMEDOUT; + + if (streq(e->name, SD_BUS_ERROR_IO_ERROR)) + return EIO; + + if (streq(e->name, SD_BUS_ERROR_BAD_ADDRESS)) + return EADDRNOTAVAIL; - if (streq(e->name, "org.freedesktop.DBus.Error.AuthFailed") || - streq(e->name, "org.freedesktop.DBus.Error.AccessDenied")) - return -EPERM; + if (streq(e->name, SD_BUS_ERROR_NOT_SUPPORTED)) + return ENOTSUP; - if (streq(e->name, "org.freedesktop.DBus.Error.InvalidArgs")) - return -EINVAL; + if (streq(e->name, SD_BUS_ERROR_LIMITS_EXCEEDED)) + return ENOBUFS; - if (streq(e->name, "org.freedesktop.DBus.Error.UnixProcessIdUnknown")) - return -ESRCH; + if (streq(e->name, SD_BUS_ERROR_ACCESS_DENIED) || + streq(e->name, SD_BUS_ERROR_AUTH_FAILED)) + return EACCES; - if (streq(e->name, "org.freedesktop.DBus.Error.FileNotFound")) - return -ENOENT; + if (streq(e->name, SD_BUS_ERROR_NO_SERVER)) + return EHOSTDOWN; - if (streq(e->name, "org.freedesktop.DBus.Error.FileExists")) - return -EEXIST; + if (streq(e->name, SD_BUS_ERROR_NO_NETWORK)) + return ENONET; - if (streq(e->name, "org.freedesktop.DBus.Error.Timeout")) - return -ETIMEDOUT; + if (streq(e->name, SD_BUS_ERROR_ADDRESS_IN_USE)) + return EADDRINUSE; - if (streq(e->name, "org.freedesktop.DBus.Error.IOError")) - return -EIO; + if (streq(e->name, SD_BUS_ERROR_DISCONNECTED)) + return ECONNRESET; - if (streq(e->name, "org.freedesktop.DBus.Error.Disconnected")) - return -ECONNRESET; + if (streq(e->name, SD_BUS_ERROR_INVALID_ARGS) || + streq(e->name, SD_BUS_ERROR_INVALID_SIGNATURE) || + streq(e->name, "org.freedesktop.DBus.Error.MatchRuleInvalid") || + streq(e->name, "org.freedesktop.DBus.Error.InvalidFileContent")) + return EINVAL; - if (streq(e->name, "org.freedesktop.DBus.Error.NotSupported")) - return -ENOTSUP; + if (streq(e->name, SD_BUS_ERROR_FILE_NOT_FOUND) || + streq(e->name, "org.freedesktop.DBus.Error.MatchRuleNotFound")) + return ENOENT; - return -EIO; + if (streq(e->name, SD_BUS_ERROR_FILE_EXISTS)) + return EEXIST; + + if (streq(e->name, SD_BUS_ERROR_UNKNOWN_METHOD) || + streq(e->name, SD_BUS_ERROR_UNKNOWN_OBJECT) || + streq(e->name, SD_BUS_ERROR_UNKNOWN_INTERFACE) || + streq(e->name, SD_BUS_ERROR_UNKNOWN_PROPERTY)) + return EBADR; + + if (streq(e->name, SD_BUS_ERROR_PROPERTY_READ_ONLY)) + return EROFS; + + if (streq(e->name, SD_BUS_ERROR_UNIX_PROCESS_ID_UNKNOWN) || + streq(e->name, "org.freedesktop.DBus.Error.SELinuxSecurityContextUnknown")) + return ESRCH; + + if (streq(e->name, SD_BUS_ERROR_INCONSISTENT_MESSAGE)) + return EBADMSG; + + if (streq(e->name, "org.freedesktop.DBus.Error.ObjectPathInUse")) + return EBUSY; + + return EIO; } -int bus_error_from_errno(sd_bus_error *e, int error) { +static int bus_error_set_strerror_or_const(sd_bus_error *e, const char *name, int error, const char *fallback) { + size_t k = 64; + char *n = NULL, *m = NULL; + + if (error < 0) + error = -error; + if (!e) - return error; + return -error; - switch (error) { + assert_return(!bus_error_is_dirty(e), -EINVAL); + assert_return(name, -EINVAL); - case -ENOMEM: - sd_bus_error_set_const(e, "org.freedesktop.DBus.Error.NoMemory", "Out of memory"); - break; + for (;;) { + char *x; - case -EPERM: - case -EACCES: - sd_bus_error_set_const(e, "org.freedesktop.DBus.Error.AccessDenied", "Access denied"); - break; + m = new(char, k); + if (!m) + goto use_fallback; + + errno = 0; + x = strerror_r(error, m, k); + if (errno == ERANGE || strlen(x) >= k - 1) { + free(m); + k *= 2; + continue; + } - case -EINVAL: - sd_bus_error_set_const(e, "org.freedesktop.DBus.Error.InvalidArgs", "Invalid argument"); - break; + if (!x || errno) { + free(m); + goto use_fallback; + } - case -ESRCH: - sd_bus_error_set_const(e, "org.freedesktop.DBus.Error.UnixProcessIdUnknown", "No such process"); - break; - case -ENOENT: - sd_bus_error_set_const(e, "org.freedesktop.DBus.Error.FileNotFound", "File not found"); - break; + if (x != m) { + free(m); + sd_bus_error_set_const(e, name, x); + return -error; + } - case -EEXIST: - sd_bus_error_set_const(e, "org.freedesktop.DBus.Error.FileExists", "File exists"); break; + } - case -ETIMEDOUT: - case -ETIME: - sd_bus_error_set_const(e, "org.freedesktop.DBus.Error.Timeout", "Timed out"); - break; - case -EIO: - sd_bus_error_set_const(e, "org.freedesktop.DBus.Error.IOError", "Input/output error"); - break; + n = strdup(name); + if (!n) { + free(m); + goto use_fallback; + } - case -ENETRESET: - case -ECONNABORTED: - case -ECONNRESET: - sd_bus_error_set_const(e, "org.freedesktop.DBus.Error.Disconnected", "Disconnected"); - break; + e->name = n; + e->message = m; + e->need_free = true; - case -ENOTSUP: - sd_bus_error_set_const(e, "org.freedesktop.DBus.Error.NotSupported", "Not supported"); - break; + return -error; + +use_fallback: + sd_bus_error_set_const(e, name, fallback); + return -error; +} + +static sd_bus_error map_from_errno(int error) { + + if (error < 0) + error = -error; + + switch (error) { + + case ENOMEM: + return SD_BUS_ERROR_MAKE(SD_BUS_ERROR_NO_NETWORK, "Out of memory"); + + case EPERM: + case EACCES: + return SD_BUS_ERROR_MAKE(SD_BUS_ERROR_ACCESS_DENIED, "Access denied"); + + case EINVAL: + return SD_BUS_ERROR_MAKE(SD_BUS_ERROR_INVALID_ARGS, "Invalid argument"); + + case ESRCH: + return SD_BUS_ERROR_MAKE(SD_BUS_ERROR_UNIX_PROCESS_ID_UNKNOWN, "No such process"); + + case ENOENT: + return SD_BUS_ERROR_MAKE(SD_BUS_ERROR_FILE_NOT_FOUND, "File not found"); + + case EEXIST: + return SD_BUS_ERROR_MAKE(SD_BUS_ERROR_FILE_EXISTS, "File exists"); + + case ETIMEDOUT: + case ETIME: + return SD_BUS_ERROR_MAKE(SD_BUS_ERROR_TIMEOUT, "Timed out"); + + case EIO: + return SD_BUS_ERROR_MAKE(SD_BUS_ERROR_IO_ERROR, "Input/output error"); + + case ENETRESET: + case ECONNABORTED: + case ECONNRESET: + return SD_BUS_ERROR_MAKE(SD_BUS_ERROR_DISCONNECTED, "Disconnected"); + + case ENOTSUP: + return SD_BUS_ERROR_MAKE(SD_BUS_ERROR_NOT_SUPPORTED, "Not supported"); + + case EADDRNOTAVAIL: + return SD_BUS_ERROR_MAKE(SD_BUS_ERROR_BAD_ADDRESS, "Address not available"); + + case ENOBUFS: + return SD_BUS_ERROR_MAKE(SD_BUS_ERROR_LIMITS_EXCEEDED, "Limits exceeded"); + + case EADDRINUSE: + return SD_BUS_ERROR_MAKE(SD_BUS_ERROR_ADDRESS_IN_USE, "Address in use"); + + case EBADMSG: + return SD_BUS_ERROR_MAKE(SD_BUS_ERROR_INCONSISTENT_MESSAGE, "Inconsistent message"); + } + + return SD_BUS_ERROR_MAKE(SD_BUS_ERROR_FAILED, "Operation failed"); +} + +int sd_bus_error_set_errno(sd_bus_error *e, int error) { + sd_bus_error x; + + x = map_from_errno(error); + + return bus_error_set_strerror_or_const(e, x.name, error, x.message); +} + +int bus_error_set_errnofv(sd_bus_error *e, int error, const char *format, va_list ap) { + sd_bus_error x; + int r; + + if (error < 0) + error = -error; + + if (!e) + return 0; + + assert_return(!bus_error_is_dirty(e), -EINVAL); + + x = map_from_errno(error); + + if (format) { + char *n, *m; + + r = vasprintf(&m, format, ap); + if (r < 0) + goto fallback; + + n = strdup(x.name); + if (!n) { + free(m); + goto fallback; + } + + e->name = n; + e->message = m; + e->need_free = true; + return -error; } - sd_bus_error_set_const(e, "org.freedesktop.DBus.Error.Failed", "Operation failed"); - return error; +fallback: + return bus_error_set_strerror_or_const(e, x.name, error, x.message); +} + +int sd_bus_error_set_errnof(sd_bus_error *e, int error, const char *format, ...) { + int r; + + if (!e) + return 0; + + assert_return(!bus_error_is_dirty(e), -EINVAL); + + if (format) { + va_list ap; + + va_start(ap, format); + r = bus_error_set_errnofv(e, error, format, ap); + va_end(ap); + + return r; + } + + return sd_bus_error_set_errno(e, error); } const char *bus_error_message(const sd_bus_error *e, int error) { + + if (error < 0) + error = -error; + if (e && e->message) return e->message; diff --git a/src/libsystemd-bus/bus-error.h b/src/libsystemd-bus/bus-error.h index ceca7d678f..5474c8c5ec 100644 --- a/src/libsystemd-bus/bus-error.h +++ b/src/libsystemd-bus/bus-error.h @@ -23,9 +23,9 @@ #include "sd-bus.h" -int bus_error_to_errno(const sd_bus_error *e); -int bus_error_from_errno(sd_bus_error *e, int error); - bool bus_error_is_dirty(sd_bus_error *e); const char *bus_error_message(const sd_bus_error *e, int error); + +int bus_error_setfv(sd_bus_error *e, const char *name, const char *format, va_list ap); +int bus_error_set_errnofv(sd_bus_error *e, int error, const char *format, va_list ap); diff --git a/src/libsystemd-bus/bus-internal.c b/src/libsystemd-bus/bus-internal.c index afff7bd7c1..942ac2b953 100644 --- a/src/libsystemd-bus/bus-internal.c +++ b/src/libsystemd-bus/bus-internal.c @@ -252,13 +252,13 @@ bool path_simple_pattern(const char *pattern, const char *value) { int bus_message_type_from_string(const char *s, uint8_t *u) { if (streq(s, "signal")) - *u = SD_BUS_MESSAGE_TYPE_SIGNAL; + *u = SD_BUS_MESSAGE_SIGNAL; else if (streq(s, "method_call")) - *u = SD_BUS_MESSAGE_TYPE_METHOD_CALL; + *u = SD_BUS_MESSAGE_METHOD_CALL; else if (streq(s, "error")) - *u = SD_BUS_MESSAGE_TYPE_METHOD_ERROR; + *u = SD_BUS_MESSAGE_METHOD_ERROR; else if (streq(s, "method_return")) - *u = SD_BUS_MESSAGE_TYPE_METHOD_RETURN; + *u = SD_BUS_MESSAGE_METHOD_RETURN; else return -EINVAL; @@ -266,13 +266,13 @@ int bus_message_type_from_string(const char *s, uint8_t *u) { } const char *bus_message_type_to_string(uint8_t u) { - if (u == SD_BUS_MESSAGE_TYPE_SIGNAL) + if (u == SD_BUS_MESSAGE_SIGNAL) return "signal"; - else if (u == SD_BUS_MESSAGE_TYPE_METHOD_CALL) + else if (u == SD_BUS_MESSAGE_METHOD_CALL) return "method_call"; - else if (u == SD_BUS_MESSAGE_TYPE_METHOD_ERROR) + else if (u == SD_BUS_MESSAGE_METHOD_ERROR) return "error"; - else if (u == SD_BUS_MESSAGE_TYPE_METHOD_RETURN) + else if (u == SD_BUS_MESSAGE_METHOD_RETURN) return "method_return"; else return NULL; diff --git a/src/libsystemd-bus/bus-internal.h b/src/libsystemd-bus/bus-internal.h index 31e10b2c27..1726b61841 100644 --- a/src/libsystemd-bus/bus-internal.h +++ b/src/libsystemd-bus/bus-internal.h @@ -240,14 +240,12 @@ struct sd_bus { uint64_t hello_flags; uint64_t match_cookie; -}; - -static inline void bus_unrefp(sd_bus **b) { - sd_bus_unref(*b); -} -#define _cleanup_bus_unref_ __attribute__((cleanup(bus_unrefp))) -#define _cleanup_bus_error_free_ __attribute__((cleanup(sd_bus_error_free))) + sd_event_source *input_io_event_source; + sd_event_source *output_io_event_source; + sd_event_source *time_event_source; + sd_event *event; +}; #define BUS_DEFAULT_TIMEOUT ((usec_t) (25 * USEC_PER_SEC)) diff --git a/src/libsystemd-bus/bus-message.c b/src/libsystemd-bus/bus-message.c index 3f68435d21..bb339f5a82 100644 --- a/src/libsystemd-bus/bus-message.c +++ b/src/libsystemd-bus/bus-message.c @@ -461,7 +461,7 @@ int sd_bus_message_new_signal( if (bus && bus->state == BUS_UNSET) return -ENOTCONN; - t = message_new(bus, SD_BUS_MESSAGE_TYPE_SIGNAL); + t = message_new(bus, SD_BUS_MESSAGE_SIGNAL); if (!t) return -ENOMEM; @@ -509,7 +509,7 @@ int sd_bus_message_new_method_call( if (bus && bus->state == BUS_UNSET) return -ENOTCONN; - t = message_new(bus, SD_BUS_MESSAGE_TYPE_METHOD_CALL); + t = message_new(bus, SD_BUS_MESSAGE_METHOD_CALL); if (!t) return -ENOMEM; @@ -553,7 +553,7 @@ static int message_new_reply( return -EINVAL; if (!call->sealed) return -EPERM; - if (call->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL) + if (call->header->type != SD_BUS_MESSAGE_METHOD_CALL) return -EINVAL; if (!m) return -EINVAL; @@ -592,7 +592,7 @@ int sd_bus_message_new_method_return( sd_bus_message *call, sd_bus_message **m) { - return message_new_reply(bus, call, SD_BUS_MESSAGE_TYPE_METHOD_RETURN, m); + return message_new_reply(bus, call, SD_BUS_MESSAGE_METHOD_RETURN, m); } int sd_bus_message_new_method_error( @@ -609,7 +609,7 @@ int sd_bus_message_new_method_error( if (!m) return -EINVAL; - r = message_new_reply(bus, call, SD_BUS_MESSAGE_TYPE_METHOD_ERROR, &t); + r = message_new_reply(bus, call, SD_BUS_MESSAGE_METHOD_ERROR, &t); if (r < 0) return r; @@ -639,46 +639,60 @@ int sd_bus_message_new_method_errorf( const char *format, ...) { - sd_bus_message *t; + _cleanup_free_ sd_bus_error error = SD_BUS_ERROR_NULL; va_list ap; int r; - if (!name) - return -EINVAL; - if (!m) - return -EINVAL; + assert_return(name, -EINVAL); + assert_return(m, -EINVAL); + + va_start(ap, format); + r = bus_error_setfv(&error, name, format, ap); + va_end(ap); - r = message_new_reply(bus, call, SD_BUS_MESSAGE_TYPE_METHOD_ERROR, &t); if (r < 0) return r; - r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_ERROR_NAME, SD_BUS_TYPE_STRING, name, &t->error.name); - if (r < 0) - goto fail; + return sd_bus_message_new_method_error(bus, call, &error, m); +} - if (format) { - _cleanup_free_ char *message = NULL; +int sd_bus_message_new_method_errno( + sd_bus *bus, + sd_bus_message *call, + int error, + const sd_bus_error *p, + sd_bus_message **m) { - va_start(ap, format); - r = vasprintf(&message, format, ap); - va_end(ap); + _cleanup_free_ sd_bus_error berror = SD_BUS_ERROR_NULL; - if (r < 0) { - r = -ENOMEM; - goto fail; - } + if (sd_bus_error_is_set(p)) + return sd_bus_message_new_method_error(bus, call, p, m); - r = message_append_basic(t, SD_BUS_TYPE_STRING, message, (const void**) &t->error.message); - if (r < 0) - goto fail; - } + sd_bus_error_set_errno(&berror, error); - *m = t; - return 0; + return sd_bus_message_new_method_error(bus, call, &berror, m); +} -fail: - message_free(t); - return r; +int sd_bus_message_new_method_errnof( + sd_bus *bus, + sd_bus_message *call, + sd_bus_message **m, + int error, + const char *format, + ...) { + + _cleanup_free_ sd_bus_error berror = SD_BUS_ERROR_NULL; + va_list ap; + int r; + + va_start(ap, format); + r = bus_error_set_errnofv(&berror, error, format, ap); + va_end(ap); + + if (r < 0) + return r; + + return sd_bus_message_new_method_error(bus, call, &berror, m); } int bus_message_new_synthetic_error( @@ -693,7 +707,7 @@ int bus_message_new_synthetic_error( assert(sd_bus_error_is_set(e)); assert(m); - t = message_new(bus, SD_BUS_MESSAGE_TYPE_METHOD_ERROR); + t = message_new(bus, SD_BUS_MESSAGE_METHOD_ERROR); if (!t) return -ENOMEM; @@ -789,7 +803,7 @@ int sd_bus_message_get_no_reply(sd_bus_message *m) { if (!m) return -EINVAL; - return m->header->type == SD_BUS_MESSAGE_TYPE_METHOD_CALL ? !!(m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED) : 0; + return m->header->type == SD_BUS_MESSAGE_METHOD_CALL ? !!(m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED) : 0; } const char *sd_bus_message_get_path(sd_bus_message *m) { @@ -1126,7 +1140,7 @@ int sd_bus_message_is_signal(sd_bus_message *m, const char *interface, const cha if (!m) return -EINVAL; - if (m->header->type != SD_BUS_MESSAGE_TYPE_SIGNAL) + if (m->header->type != SD_BUS_MESSAGE_SIGNAL) return 0; if (interface && (!m->interface || !streq(m->interface, interface))) @@ -1142,7 +1156,7 @@ int sd_bus_message_is_method_call(sd_bus_message *m, const char *interface, cons if (!m) return -EINVAL; - if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL) + if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL) return 0; if (interface && (!m->interface || !streq(m->interface, interface))) @@ -1158,7 +1172,7 @@ int sd_bus_message_is_method_error(sd_bus_message *m, const char *name) { if (!m) return -EINVAL; - if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_ERROR) + if (m->header->type != SD_BUS_MESSAGE_METHOD_ERROR) return 0; if (name && (!m->error.name || !streq(m->error.name, name))) @@ -1172,7 +1186,7 @@ int sd_bus_message_set_no_reply(sd_bus_message *m, int b) { return -EINVAL; if (m->sealed) return -EPERM; - if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL) + if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL) return -EPERM; if (b) @@ -3876,25 +3890,25 @@ int bus_message_parse_fields(sd_bus_message *m) { switch (m->header->type) { - case SD_BUS_MESSAGE_TYPE_SIGNAL: + case SD_BUS_MESSAGE_SIGNAL: if (!m->path || !m->interface || !m->member) return -EBADMSG; break; - case SD_BUS_MESSAGE_TYPE_METHOD_CALL: + case SD_BUS_MESSAGE_METHOD_CALL: if (!m->path || !m->member) return -EBADMSG; break; - case SD_BUS_MESSAGE_TYPE_METHOD_RETURN: + case SD_BUS_MESSAGE_METHOD_RETURN: if (m->reply_serial == 0) return -EBADMSG; break; - case SD_BUS_MESSAGE_TYPE_METHOD_ERROR: + case SD_BUS_MESSAGE_METHOD_ERROR: if (m->reply_serial == 0 || !m->error.name) return -EBADMSG; @@ -3902,7 +3916,7 @@ int bus_message_parse_fields(sd_bus_message *m) { } /* Try to read the error message, but if we can't it's a non-issue */ - if (m->header->type == SD_BUS_MESSAGE_TYPE_METHOD_ERROR) + if (m->header->type == SD_BUS_MESSAGE_METHOD_ERROR) sd_bus_message_read(m, "s", &m->error.message); return 0; @@ -4363,24 +4377,21 @@ int bus_header_message_size(struct bus_header *h, size_t *sum) { return 0; } -int bus_message_to_errno(sd_bus_message *m) { - assert(m); +int sd_bus_message_get_errno(sd_bus_message *m) { + assert_return(m, -EINVAL); - if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_ERROR) + if (m->header->type != SD_BUS_MESSAGE_METHOD_ERROR) return 0; - return bus_error_to_errno(&m->error); + return sd_bus_error_get_errno(&m->error); } -int sd_bus_message_get_signature(sd_bus_message *m, int complete, const char **signature) { +const char* sd_bus_message_get_signature(sd_bus_message *m, int complete) { struct bus_container *c; if (!m) - return -EINVAL; - if (!signature) - return -EINVAL; + return NULL; c = complete ? &m->root_container : message_get_container(m); - *signature = c->signature ?: ""; - return 0; + return c->signature ?: ""; } diff --git a/src/libsystemd-bus/bus-message.h b/src/libsystemd-bus/bus-message.h index 2fb11ea3b1..c79ff1bc93 100644 --- a/src/libsystemd-bus/bus-message.h +++ b/src/libsystemd-bus/bus-message.h @@ -189,12 +189,6 @@ static inline void* BUS_MESSAGE_FIELDS(sd_bus_message *m) { return (uint8_t*) m->header + sizeof(struct bus_header); } -static inline void bus_message_unrefp(sd_bus_message **m) { - sd_bus_message_unref(*m); -} - -#define _cleanup_bus_message_unref_ __attribute__((cleanup(bus_message_unrefp))) - int bus_message_seal(sd_bus_message *m, uint64_t serial); int bus_message_dump(sd_bus_message *m); int bus_message_get_blob(sd_bus_message *m, void **buffer, size_t *sz); diff --git a/src/libsystemd-bus/bus-objects.c b/src/libsystemd-bus/bus-objects.c index 4091490c5e..0c935e947d 100644 --- a/src/libsystemd-bus/bus-objects.c +++ b/src/libsystemd-bus/bus-objects.c @@ -27,6 +27,7 @@ #include "bus-signature.h" #include "bus-introspect.h" #include "bus-objects.h" +#include "bus-util.h" static int node_vtable_get_userdata( sd_bus *bus, @@ -275,13 +276,13 @@ static int method_callbacks_run( if (r < 0) return r; - r = sd_bus_message_get_signature(m, true, &signature); - if (r < 0) - return r; + signature = sd_bus_message_get_signature(m, true); + if (!signature) + return -EINVAL; if (!streq(strempty(c->vtable->x.method.signature), signature)) { r = sd_bus_reply_method_errorf(bus, m, - "org.freedesktop.DBus.Error.InvalidArgs", + SD_BUS_ERROR_INVALID_ARGS, "Invalid arguments '%s' to call %s:%s, expecting '%s'.", signature, c->interface, c->member, strempty(c->vtable->x.method.signature)); if (r < 0) @@ -475,7 +476,7 @@ static int property_get_set_callbacks_run( } else { if (c->vtable->type != _SD_BUS_VTABLE_WRITABLE_PROPERTY) - sd_bus_error_setf(&error, "org.freedesktop.DBus.Error.PropertyReadOnly", "Property '%s' is not writable.", c->member); + sd_bus_error_setf(&error, SD_BUS_ERROR_PROPERTY_READ_ONLY, "Property '%s' is not writable.", c->member); else { /* Avoid that we call the set routine more * than once if the processing of this message @@ -634,7 +635,7 @@ static int property_get_all_callbacks_run( if (!found_interface) { r = sd_bus_reply_method_errorf( bus, m, - "org.freedesktop.DBus.Error.UnknownInterface", + SD_BUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface '%s'.", iface); if (r < 0) return r; @@ -1164,7 +1165,7 @@ int bus_process_object(sd_bus *bus, sd_bus_message *m) { assert(bus); assert(m); - if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL) + if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL) return 0; if (!m->path) @@ -1203,12 +1204,12 @@ int bus_process_object(sd_bus *bus, sd_bus_message *m) { sd_bus_message_is_method_call(m, "org.freedesktop.DBus.Properties", "Set")) r = sd_bus_reply_method_errorf( bus, m, - "org.freedesktop.DBus.Error.UnknownProperty", + SD_BUS_ERROR_UNKNOWN_PROPERTY, "Unknown property or interface."); else r = sd_bus_reply_method_errorf( bus, m, - "org.freedesktop.DBus.Error.UnknownMethod", + SD_BUS_ERROR_UNKNOWN_METHOD, "Unknown method '%s' or interface '%s'.", m->member, m->interface); if (r < 0) @@ -1875,8 +1876,6 @@ static int emit_properties_changed_on_interface( r = invoke_property_get(bus, v->vtable, m->path, interface, *property, m, &error, vtable_property_convert_userdata(v->vtable, u)); if (r < 0) return r; - if (sd_bus_error_is_set(&error)) - return bus_error_to_errno(&error); if (bus->nodes_modified) return 0; @@ -2047,8 +2046,6 @@ static int interfaces_added_append_one_prefix( r = vtable_append_all_properties(bus, m,path, c, u, &error); if (r < 0) return r; - if (sd_bus_error_is_set(&error)) - return bus_error_to_errno(&error); if (bus->nodes_modified) return 0; diff --git a/src/libsystemd-bus/bus-util.c b/src/libsystemd-bus/bus-util.c new file mode 100644 index 0000000000..5a70fae77f --- /dev/null +++ b/src/libsystemd-bus/bus-util.c @@ -0,0 +1,378 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +/*** + This file is part of systemd. + + Copyright 2013 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see <http://www.gnu.org/licenses/>. +***/ + +#include "sd-event.h" +#include "sd-bus.h" + +#include "util.h" +#include "macro.h" +#include "def.h" + +#include "bus-util.h" + +static int quit_callback(sd_bus *bus, sd_bus_message *m, void *userdata) { + sd_event *e = userdata; + + assert(bus); + assert(m); + assert(e); + + sd_event_request_quit(e); + return 1; +} + +int bus_async_unregister_and_quit(sd_event *e, sd_bus *bus, const char *name) { + _cleanup_free_ char *match = NULL; + int r; + + assert(e); + assert(bus); + assert(name); + + r = asprintf(&match, "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameLost',arg0='%s'", name); + if (r < 0) + return r; + + r = sd_bus_add_match(bus, match, quit_callback, e); + if (r < 0) + return r; + + r = sd_bus_release_name(bus, name); + if (r < 0) + return r; + + if (r != SD_BUS_NAME_RELEASED) + return -EIO; + + return 0; +} + +int bus_event_loop_with_idle(sd_event *e, sd_bus *bus, const char *name, usec_t timeout) { + bool exiting = false; + int r; + + assert(e); + assert(bus); + assert(name); + + for (;;) { + r = sd_event_get_state(e); + if (r < 0) + return r; + + if (r == SD_EVENT_FINISHED) + break; + + r = sd_event_run(e, exiting ? (uint64_t) -1 : 5 * USEC_PER_SEC /* DEFAULT_EXIT_USEC */); + if (r < 0) + return r; + + if (r == 0 && !exiting) { + r = bus_async_unregister_and_quit(e, bus, name); + if (r < 0) + return r; + + exiting = true; + } + } + + return 0; +} + +int bus_property_get_tristate( + sd_bus *bus, + const char *path, + const char *interface, + const char *property, + sd_bus_message *reply, + sd_bus_error *error, + void *userdata) { + + int *tristate = userdata; + int r; + + r = sd_bus_message_append(reply, "b", *tristate > 0); + if (r < 0) + return r; + + return 1; +} + +int bus_verify_polkit( + sd_bus *bus, + sd_bus_message *m, + const char *action, + bool interactive, + bool *_challenge, + sd_bus_error *e) { + + const char *sender; + uid_t uid; + int r; + + assert(bus); + assert(m); + assert(action); + + sender = sd_bus_message_get_sender(m); + if (!sender) + return -EBADMSG; + + r = sd_bus_get_owner_uid(bus, sender, &uid); + if (r < 0) + return r; + + if (uid == 0) + return 1; + +#ifdef ENABLE_POLKIT + else { + _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; + bool authorized = false, challenge = false; + + r = sd_bus_call_method( + bus, + "org.freedesktop.PolicyKit1", + "/org/freedesktop/PolicyKit1/Authority", + "org.freedesktop.PolicyKit1.Authority", + "CheckAuthorization", + e, + &reply, + "(sa{sv})sa{ss}us", + "system-bus-name", 1, "name", "s", sender, + action, + 0, + interactive ? 1 : 0, + ""); + + if (r < 0) { + /* Treat no PK available as access denied */ + if (sd_bus_error_has_name(e, SD_BUS_ERROR_SERVICE_UNKNOWN)) { + sd_bus_error_free(e); + return -EACCES; + } + + return r; + } + + r = sd_bus_message_read(reply, "(bb)", &authorized, &challenge); + if (r < 0) + return r; + + if (authorized) + return 1; + + if (_challenge) { + *_challenge = challenge; + return 0; + } + } +#endif + + return -EACCES; +} + +#ifdef ENABLE_POLKIT + +typedef struct AsyncPolkitQuery { + sd_bus_message *request, *reply; + sd_bus_message_handler_t callback; + void *userdata; + uint64_t serial; +} AsyncPolkitQuery; + +static int async_polkit_callback(sd_bus *bus, sd_bus_message *reply, void *userdata) { + AsyncPolkitQuery *q = userdata; + _cleanup_bus_message_unref_ sd_bus_message *m = NULL; + int r; + + assert(bus); + assert(reply); + assert(q); + + q->reply = sd_bus_message_ref(reply); + q->serial = 0; + + m = sd_bus_message_ref(q->request); + + r = sd_bus_message_rewind(m, true); + if (r < 0) + return r; + + r = q->callback(bus, m, q->userdata); + if (r < 0) + return r; + + return 1; +} + +static void async_polkit_query_free(sd_bus *b, AsyncPolkitQuery *q) { + + if (!q) + return; + + if (q->serial > 0 && b) + sd_bus_send_with_reply_cancel(b, q->serial); + + sd_bus_message_unref(q->request); + sd_bus_message_unref(q->reply); + free(q); +} + +#endif + +int bus_verify_polkit_async( + sd_bus *bus, + Hashmap **registry, + sd_bus_message *m, + const char *action, + bool interactive, + sd_bus_error *error, + sd_bus_message_handler_t callback, + void *userdata) { + +#ifdef ENABLE_POLKIT + _cleanup_bus_message_unref_ sd_bus_message *pk = NULL; + AsyncPolkitQuery *q; +#endif + const char *sender; + uid_t uid; + int r; + + assert(bus); + assert(registry); + assert(m); + assert(action); + +#ifdef ENABLE_POLKIT + q = hashmap_remove(*registry, m); + if (q) { + bool authorized, challenge; + + /* This is the second invocation of this function, and + * there's already a response from polkit, let's + * process it */ + assert(q->reply); + + if (sd_bus_message_is_method_error(q->reply, NULL)) { + const sd_bus_error *e; + + /* Treat no PK available as access denied */ + if (sd_bus_message_is_method_error(q->reply, SD_BUS_ERROR_SERVICE_UNKNOWN)) { + async_polkit_query_free(bus, q); + return -EACCES; + } + + e = sd_bus_message_get_error(q->reply); + sd_bus_error_copy(error, e); + r = sd_bus_error_get_errno(e); + + async_polkit_query_free(bus, q); + return r; + } + + r = sd_bus_message_enter_container(q->reply, 'r', "bba{ss}"); + if (r >= 0) + r = sd_bus_message_read(q->reply, "bb", &authorized, &challenge); + + async_polkit_query_free(bus, q); + + if (r < 0) + return r; + + if (authorized) + return 1; + + return -EACCES; + } +#endif + + sender = sd_bus_message_get_sender(m); + if (!sender) + return -EBADMSG; + + r = sd_bus_get_owner_uid(bus, sender, &uid); + if (r < 0) + return r; + + if (uid == 0) + return 1; +#ifdef ENABLE_POLKIT + + r = hashmap_ensure_allocated(registry, trivial_hash_func, trivial_compare_func); + if (r < 0) + return r; + + r = sd_bus_message_new_method_call( + bus, + "org.freedesktop.PolicyKit1", + "/org/freedesktop/PolicyKit1/Authority", + "org.freedesktop.PolicyKit1.Authority", + "CheckAuthorization", + &pk); + if (r < 0) + return r; + + r = sd_bus_message_append( + pk, + "(sa{sv})sa{ss}us", + "system-bus-name", 1, "name", "s", sender, + action, + 0, + interactive ? 1 : 0, + ""); + if (r < 0) + return r; + + q = new0(AsyncPolkitQuery, 1); + if (!q) + return -ENOMEM; + + q->request = sd_bus_message_ref(m); + q->callback = callback; + q->userdata = userdata; + + r = hashmap_put(*registry, m, q); + if (r < 0) { + async_polkit_query_free(bus, q); + return r; + } + + r = sd_bus_send_with_reply(bus, pk, async_polkit_callback, q, 0, &q->serial); + if (r < 0) + return r; + + return 0; +#endif + + return -EACCES; +} + +void bus_verify_polkit_async_registry_free(sd_bus *bus, Hashmap *registry) { +#ifdef ENABLE_POLKIT + AsyncPolkitQuery *q; + + while ((q = hashmap_steal_first(registry))) + async_polkit_query_free(bus, q); + + hashmap_free(registry); +#endif +} diff --git a/src/libsystemd-bus/bus-util.h b/src/libsystemd-bus/bus-util.h new file mode 100644 index 0000000000..354183f690 --- /dev/null +++ b/src/libsystemd-bus/bus-util.h @@ -0,0 +1,45 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +#pragma once + +/*** + This file is part of systemd. + + Copyright 2013 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see <http://www.gnu.org/licenses/>. +***/ + +#include "sd-event.h" +#include "sd-bus.h" +#include "hashmap.h" +#include "time-util.h" +#include "util.h" + +int bus_async_unregister_and_quit(sd_event *e, sd_bus *bus, const char *name); + +int bus_event_loop_with_idle(sd_event *e, sd_bus *bus, const char *name, usec_t timeout); +int bus_property_get_tristate(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, sd_bus_error *error, void *userdata); + +int bus_verify_polkit(sd_bus *bus, sd_bus_message *m, const char *action, bool interactive, bool *_challenge, sd_bus_error *e); + +int bus_verify_polkit_async(sd_bus *bus, Hashmap **registry, sd_bus_message *m, const char *action, bool interactive, sd_bus_error *error, sd_bus_message_handler_t callback, void *userdata); +void bus_verify_polkit_async_registry_free(sd_bus *bus, Hashmap *registry); + +DEFINE_TRIVIAL_CLEANUP_FUNC(sd_bus*, sd_bus_unref); +DEFINE_TRIVIAL_CLEANUP_FUNC(sd_bus_message*, sd_bus_message_unref); + +#define _cleanup_bus_unref_ _cleanup_(sd_bus_unrefp) +#define _cleanup_bus_error_free_ _cleanup_(sd_bus_error_free) +#define _cleanup_bus_message_unref_ _cleanup_(sd_bus_message_unrefp) diff --git a/src/libsystemd-bus/busctl.c b/src/libsystemd-bus/busctl.c index 220c1eb34e..b15b41ae77 100644 --- a/src/libsystemd-bus/busctl.c +++ b/src/libsystemd-bus/busctl.c @@ -30,6 +30,7 @@ #include "sd-bus.h" #include "bus-message.h" #include "bus-internal.h" +#include "bus-util.h" static bool arg_no_pager = false; static char *arg_address = NULL; diff --git a/src/libsystemd-bus/event-util.h b/src/libsystemd-bus/event-util.h new file mode 100644 index 0000000000..e58020dedf --- /dev/null +++ b/src/libsystemd-bus/event-util.h @@ -0,0 +1,30 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +#pragma once + +/*** + This file is part of systemd. + + Copyright 2013 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see <http://www.gnu.org/licenses/>. +***/ + +#include "util.h" + +DEFINE_TRIVIAL_CLEANUP_FUNC(sd_event*, sd_event_unref); +DEFINE_TRIVIAL_CLEANUP_FUNC(sd_event_source*, sd_event_source_unref); + +#define _cleanup_event_unref_ _cleanup_(sd_event_unrefp) +#define _cleanup_event_source_unref_ _cleanup_(sd_event_source_unrefp) diff --git a/src/libsystemd-bus/sd-bus.c b/src/libsystemd-bus/sd-bus.c index 5d1fcd90a0..665f1e6c8b 100644 --- a/src/libsystemd-bus/sd-bus.c +++ b/src/libsystemd-bus/sd-bus.c @@ -45,6 +45,7 @@ #include "bus-introspect.h" #include "bus-signature.h" #include "bus-objects.h" +#include "bus-util.h" static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec); @@ -104,6 +105,8 @@ static void bus_free(sd_bus *b) { assert(b); + sd_bus_detach_event(b); + bus_close_fds(b); if (b->kdbus_buffer) @@ -350,9 +353,11 @@ static int hello_callback(sd_bus *bus, sd_bus_message *reply, void *userdata) { assert(bus->state == BUS_HELLO); assert(reply); - r = bus_message_to_errno(reply); + r = sd_bus_message_get_errno(reply); if (r < 0) return r; + if (r > 0) + return -r; r = sd_bus_message_read(reply, "s", &s); if (r < 0) @@ -1038,6 +1043,8 @@ void sd_bus_close(sd_bus *bus) { bus->state = BUS_CLOSED; + sd_bus_detach_event(bus); + if (!bus->is_kernel) bus_close_fds(bus); @@ -1318,7 +1325,7 @@ int sd_bus_send_with_reply( assert_return(bus, -EINVAL); assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN); assert_return(m, -EINVAL); - assert_return(m->header->type == SD_BUS_MESSAGE_TYPE_METHOD_CALL, -EINVAL); + assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL); assert_return(!(m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED), -EINVAL); assert_return(callback, -EINVAL); assert_return(!bus_pid_changed(bus), -ECHILD); @@ -1428,7 +1435,7 @@ int sd_bus_send_with_reply_and_block( assert_return(bus, -EINVAL); assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN); assert_return(m, -EINVAL); - assert_return(m->header->type == SD_BUS_MESSAGE_TYPE_METHOD_CALL, -EINVAL); + assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL); assert_return(!(m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED), -EINVAL); assert_return(!bus_error_is_dirty(error), -EINVAL); assert_return(!bus_pid_changed(bus), -ECHILD); @@ -1475,7 +1482,7 @@ int sd_bus_send_with_reply_and_block( if (incoming->reply_serial == serial) { /* Found a match! */ - if (incoming->header->type == SD_BUS_MESSAGE_TYPE_METHOD_RETURN) { + if (incoming->header->type == SD_BUS_MESSAGE_METHOD_RETURN) { if (reply) *reply = incoming; @@ -1485,7 +1492,7 @@ int sd_bus_send_with_reply_and_block( return 1; } - if (incoming->header->type == SD_BUS_MESSAGE_TYPE_METHOD_ERROR) { + if (incoming->header->type == SD_BUS_MESSAGE_METHOD_ERROR) { int k; r = sd_bus_error_copy(error, &incoming->error); @@ -1494,9 +1501,9 @@ int sd_bus_send_with_reply_and_block( return r; } - k = bus_error_to_errno(&incoming->error); + k = sd_bus_error_get_errno(&incoming->error); sd_bus_message_unref(incoming); - return k; + return -k; } sd_bus_message_unref(incoming); @@ -1623,7 +1630,7 @@ static int process_timeout(sd_bus *bus) { r = bus_message_new_synthetic_error( bus, c->serial, - &SD_BUS_ERROR_MAKE("org.freedesktop.DBus.Error.Timeout", "Timed out"), + &SD_BUS_ERROR_MAKE(SD_BUS_ERROR_NO_REPLY, "Method call timed out"), &m); if (r < 0) return r; @@ -1649,8 +1656,8 @@ static int process_hello(sd_bus *bus, sd_bus_message *m) { * here (we leave that to the usual handling), we just verify * we don't let any earlier msg through. */ - if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_RETURN && - m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_ERROR) + if (m->header->type != SD_BUS_MESSAGE_METHOD_RETURN && + m->header->type != SD_BUS_MESSAGE_METHOD_ERROR) return -EIO; if (m->reply_serial != bus->hello_serial) @@ -1666,8 +1673,8 @@ static int process_reply(sd_bus *bus, sd_bus_message *m) { assert(bus); assert(m); - if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_RETURN && - m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_ERROR) + if (m->header->type != SD_BUS_MESSAGE_METHOD_RETURN && + m->header->type != SD_BUS_MESSAGE_METHOD_ERROR) return 0; c = hashmap_remove(bus->reply_callbacks, &m->reply_serial); @@ -1748,7 +1755,7 @@ static int process_builtin(sd_bus *bus, sd_bus_message *m) { assert(bus); assert(m); - if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL) + if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL) return 0; if (!streq_ptr(m->interface, "org.freedesktop.DBus.Peer")) @@ -1775,7 +1782,7 @@ static int process_builtin(sd_bus *bus, sd_bus_message *m) { } else { r = sd_bus_message_new_method_errorf( bus, m, &reply, - "org.freedesktop.DBus.Error.UnknownMethod", + SD_BUS_ERROR_UNKNOWN_METHOD, "Unknown method '%s' on interface '%s'.", m->member, m->interface); } @@ -1797,6 +1804,12 @@ static int process_message(sd_bus *bus, sd_bus_message *m) { bus->iteration_counter++; + log_debug("Got message sender=%s object=%s interface=%s member=%s", + strna(sd_bus_message_get_sender(m)), + strna(sd_bus_message_get_path(m)), + strna(sd_bus_message_get_interface(m)), + strna(sd_bus_message_get_member(m))); + r = process_hello(bus, m); if (r != 0) return r; @@ -1855,11 +1868,11 @@ static int process_running(sd_bus *bus, sd_bus_message **ret) { return 1; } - if (m->header->type == SD_BUS_MESSAGE_TYPE_METHOD_CALL) { + if (m->header->type == SD_BUS_MESSAGE_METHOD_CALL) { r = sd_bus_reply_method_errorf( bus, m, - "org.freedesktop.DBus.Error.UnknownObject", + SD_BUS_ERROR_UNKNOWN_OBJECT, "Unknown object '%s'.", m->path); if (r < 0) return r; @@ -2123,3 +2136,143 @@ bool bus_pid_changed(sd_bus *bus) { return bus->original_pid != getpid(); } + +static int io_callback(sd_event_source *s, int fd, uint32_t revents, void *userdata) { + void *bus = userdata; + int r; + + assert(bus); + + r = sd_bus_process(bus, NULL); + if (r < 0) + return r; + + return 1; +} + +static int time_callback(sd_event_source *s, uint64_t usec, void *userdata) { + void *bus = userdata; + int r; + + assert(bus); + + r = sd_bus_process(bus, NULL); + if (r < 0) + return r; + + return 1; +} + +static int prepare_callback(sd_event_source *s, void *userdata) { + sd_bus *bus = userdata; + int r, e; + usec_t until; + + assert(s); + assert(bus); + + e = sd_bus_get_events(bus); + if (e < 0) + return e; + + if (bus->output_fd != bus->input_fd) { + + r = sd_event_source_set_io_events(bus->input_io_event_source, e & POLLIN); + if (r < 0) + return r; + + r = sd_event_source_set_io_events(bus->output_io_event_source, e & POLLOUT); + if (r < 0) + return r; + } else { + r = sd_event_source_set_io_events(bus->input_io_event_source, e); + if (r < 0) + return r; + } + + r = sd_bus_get_timeout(bus, &until); + if (r < 0) + return r; + if (r > 0) { + int j; + + j = sd_event_source_set_time(bus->time_event_source, until); + if (j < 0) + return j; + } + + r = sd_event_source_set_enabled(bus->time_event_source, r > 0); + if (r < 0) + return r; + + return 1; +} + +int sd_bus_attach_event(sd_bus *bus, sd_event *event, int priority) { + int r; + + assert_return(bus, -EINVAL); + assert_return(event, -EINVAL); + assert_return(!bus->event, -EBUSY); + + assert(!bus->input_io_event_source); + assert(!bus->output_io_event_source); + assert(!bus->time_event_source); + + bus->event = sd_event_ref(event); + + r = sd_event_add_io(event, bus->input_fd, 0, io_callback, bus, &bus->input_io_event_source); + if (r < 0) + goto fail; + + r = sd_event_source_set_priority(bus->input_io_event_source, priority); + if (r < 0) + goto fail; + + if (bus->output_fd != bus->input_fd) { + r = sd_event_add_io(event, bus->output_fd, 0, io_callback, bus, &bus->output_io_event_source); + if (r < 0) + goto fail; + + r = sd_event_source_set_priority(bus->output_io_event_source, priority); + if (r < 0) + goto fail; + } + + r = sd_event_source_set_prepare(bus->input_io_event_source, prepare_callback); + if (r < 0) + goto fail; + + r = sd_event_add_monotonic(event, 0, 0, time_callback, bus, &bus->time_event_source); + if (r < 0) + goto fail; + + r = sd_event_source_set_priority(bus->time_event_source, priority); + if (r < 0) + goto fail; + + return 0; + +fail: + sd_bus_detach_event(bus); + return r; +} + +int sd_bus_detach_event(sd_bus *bus) { + assert_return(bus, -EINVAL); + assert_return(bus->event, -ENXIO); + + if (bus->input_io_event_source) + bus->input_io_event_source = sd_event_source_unref(bus->input_io_event_source); + + if (bus->output_io_event_source) + bus->output_io_event_source = sd_event_source_unref(bus->output_io_event_source); + + if (bus->time_event_source) + bus->time_event_source = sd_event_source_unref(bus->time_event_source); + + if (bus->event) + bus->event = sd_event_unref(bus->event); + + return 0; +} diff --git a/src/libsystemd-bus/test-bus-chat.c b/src/libsystemd-bus/test-bus-chat.c index aefe8f1b3b..efc19c68b3 100644 --- a/src/libsystemd-bus/test-bus-chat.c +++ b/src/libsystemd-bus/test-bus-chat.c @@ -34,6 +34,7 @@ #include "bus-error.h" #include "bus-match.h" #include "bus-internal.h" +#include "bus-util.h" static int match_callback(sd_bus *bus, sd_bus_message *m, void *userdata) { log_info("Match triggered! interface=%s member=%s", strna(sd_bus_message_get_interface(m)), strna(sd_bus_message_get_member(m))); @@ -243,7 +244,7 @@ static int server(sd_bus *bus) { r = sd_bus_reply_method_error( bus, m, - &SD_BUS_ERROR_MAKE("org.freedesktop.DBus.Error.UnknownMethod", "Unknown method.")); + &SD_BUS_ERROR_MAKE(SD_BUS_ERROR_UNKNOWN_METHOD, "Unknown method.")); if (r < 0) { log_error("Failed to send reply: %s", strerror(-r)); goto fail; @@ -359,7 +360,7 @@ finish: static int quit_callback(sd_bus *b, sd_bus_message *m, void *userdata) { bool *x = userdata; - log_error("Quit callback: %s", strerror(-bus_message_to_errno(m))); + log_error("Quit callback: %s", strerror(sd_bus_message_get_errno(m))); *x = 1; return 1; diff --git a/src/libsystemd-bus/test-bus-kernel-benchmark.c b/src/libsystemd-bus/test-bus-kernel-benchmark.c index 2e84cd9244..23e4d67980 100644 --- a/src/libsystemd-bus/test-bus-kernel-benchmark.c +++ b/src/libsystemd-bus/test-bus-kernel-benchmark.c @@ -31,6 +31,7 @@ #include "bus-error.h" #include "bus-kernel.h" #include "bus-internal.h" +#include "bus-util.h" #define MAX_SIZE (4*1024*1024) diff --git a/src/libsystemd-bus/test-bus-kernel-bloom.c b/src/libsystemd-bus/test-bus-kernel-bloom.c index 5445d3488f..dcf048338f 100644 --- a/src/libsystemd-bus/test-bus-kernel-bloom.c +++ b/src/libsystemd-bus/test-bus-kernel-bloom.c @@ -26,6 +26,7 @@ #include "bus-message.h" #include "bus-error.h" #include "bus-kernel.h" +#include "bus-util.h" static void test_one( const char *path, diff --git a/src/libsystemd-bus/test-bus-kernel.c b/src/libsystemd-bus/test-bus-kernel.c index 680dcde5b4..075903c2f4 100644 --- a/src/libsystemd-bus/test-bus-kernel.c +++ b/src/libsystemd-bus/test-bus-kernel.c @@ -28,6 +28,7 @@ #include "bus-message.h" #include "bus-error.h" #include "bus-kernel.h" +#include "bus-util.h" int main(int argc, char *argv[]) { _cleanup_close_ int bus_ref = -1; diff --git a/src/libsystemd-bus/test-bus-marshal.c b/src/libsystemd-bus/test-bus-marshal.c index ef1a77f5fc..1e2caa0620 100644 --- a/src/libsystemd-bus/test-bus-marshal.c +++ b/src/libsystemd-bus/test-bus-marshal.c @@ -34,6 +34,7 @@ #include "sd-bus.h" #include "bus-message.h" +#include "bus-util.h" int main(int argc, char *argv[]) { _cleanup_bus_message_unref_ sd_bus_message *m = NULL; diff --git a/src/libsystemd-bus/test-bus-match.c b/src/libsystemd-bus/test-bus-match.c index db977f726e..8df491a60a 100644 --- a/src/libsystemd-bus/test-bus-match.c +++ b/src/libsystemd-bus/test-bus-match.c @@ -27,6 +27,7 @@ #include "bus-match.h" #include "bus-message.h" +#include "bus-util.h" static bool mask[32]; diff --git a/src/libsystemd-bus/test-bus-objects.c b/src/libsystemd-bus/test-bus-objects.c index 543063f361..8758404189 100644 --- a/src/libsystemd-bus/test-bus-objects.c +++ b/src/libsystemd-bus/test-bus-objects.c @@ -33,6 +33,7 @@ #include "sd-bus.h" #include "bus-internal.h" #include "bus-message.h" +#include "bus-util.h" struct context { int fds[2]; @@ -269,13 +270,13 @@ static int client(struct context *c) { r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "Doesntexist", &error, &reply, ""); assert_se(r < 0); - assert_se(sd_bus_error_has_name(&error, "org.freedesktop.DBus.Error.UnknownMethod")); + assert_se(sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_METHOD)); sd_bus_error_free(&error); r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.systemd.test", "AlterSomething", &error, &reply, "as", 1, "hallo"); assert_se(r < 0); - assert_se(sd_bus_error_has_name(&error, "org.freedesktop.DBus.Error.InvalidArgs")); + assert_se(sd_bus_error_has_name(&error, SD_BUS_ERROR_INVALID_ARGS)); sd_bus_error_free(&error); @@ -372,12 +373,12 @@ static int client(struct context *c) { r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/value/a", "org.freedesktop.DBus.Properties", "GetAll", &error, &reply, "s", "org.freedesktop.systemd.ValueTest2"); assert_se(r < 0); - assert_se(sd_bus_error_has_name(&error, "org.freedesktop.DBus.Error.UnknownInterface")); + assert_se(sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_INTERFACE)); sd_bus_error_free(&error); r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/foo", "org.freedesktop.DBus.ObjectManager", "GetManagedObjects", &error, &reply, ""); assert_se(r < 0); - assert_se(sd_bus_error_has_name(&error, "org.freedesktop.DBus.Error.UnknownMethod")); + assert_se(sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_METHOD)); sd_bus_error_free(&error); r = sd_bus_call_method(bus, "org.freedesktop.systemd.test", "/value", "org.freedesktop.DBus.ObjectManager", "GetManagedObjects", &error, &reply, ""); diff --git a/src/libsystemd-bus/test-bus-server.c b/src/libsystemd-bus/test-bus-server.c index ef26a65d87..478a81e5cb 100644 --- a/src/libsystemd-bus/test-bus-server.c +++ b/src/libsystemd-bus/test-bus-server.c @@ -32,6 +32,7 @@ #include "sd-bus.h" #include "bus-internal.h" #include "bus-message.h" +#include "bus-util.h" struct context { int fds[2]; @@ -98,7 +99,7 @@ static void *server(void *p) { } else if (sd_bus_message_is_method_call(m, NULL, NULL)) { r = sd_bus_message_new_method_error( bus, m, - &SD_BUS_ERROR_MAKE("org.freedesktop.DBus.Error.UnknownMethod", "Unknown method."), + &SD_BUS_ERROR_MAKE(SD_BUS_ERROR_UNKNOWN_METHOD, "Unknown method."), &reply); if (r < 0) { log_error("Failed to allocate return: %s", strerror(-r)); |