From: Lennart Poettering Date: Fri, 15 Apr 2011 23:54:49 +0000 (+0200) Subject: dbus: split out object management code into dbus-common, and simplify it X-Git-Tag: v25~35 X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bfebab7f69cb23f3a488f02bc2be9c8309808221;p=systemd dbus: split out object management code into dbus-common, and simplify it --- diff --git a/Makefile.am b/Makefile.am index 6a5c491b..db28572d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -481,7 +481,8 @@ libsystemd_core_la_SOURCES = \ src/namespace.c \ src/tcpwrap.c \ src/cgroup-util.c \ - src/condition.c + src/condition.c \ + src/dbus-common.c libsystemd_core_la_CFLAGS = \ $(AM_CFLAGS) \ diff --git a/src/dbus-automount.c b/src/dbus-automount.c index eccad37e..82684256 100644 --- a/src/dbus-automount.c +++ b/src/dbus-automount.c @@ -21,6 +21,7 @@ #include "dbus-unit.h" #include "dbus-automount.h" +#include "dbus-common.h" #define BUS_AUTOMOUNT_INTERFACE \ " \n" \ @@ -52,5 +53,5 @@ DBusHandlerResult bus_automount_message_handler(Unit *u, DBusConnection *c, DBus { NULL, NULL, NULL, NULL, NULL } }; - return bus_default_message_handler(u->meta.manager, c, message, INTROSPECTION, INTERFACES_LIST, properties); + return bus_default_message_handler(c, message, INTROSPECTION, INTERFACES_LIST, properties); } diff --git a/src/dbus-common.c b/src/dbus-common.c index dcce10e5..b23373c5 100644 --- a/src/dbus-common.c +++ b/src/dbus-common.c @@ -26,11 +26,13 @@ #include #include #include +#include #include "log.h" #include "dbus-common.h" #include "util.h" #include "def.h" +#include "strv.h" int bus_check_peercred(DBusConnection *c) { int fd; @@ -232,3 +234,448 @@ const char *bus_error_message(const DBusError *error) { return error->message; } + +DBusHandlerResult bus_default_message_handler( + DBusConnection *c, + DBusMessage *message, + const char *introspection, + const char *interfaces, + const BusProperty *properties) { + + DBusError error; + DBusMessage *reply = NULL; + int r; + + assert(c); + assert(message); + + dbus_error_init(&error); + + if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect") && introspection) { + + if (!(reply = dbus_message_new_method_return(message))) + goto oom; + + if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) + goto oom; + + } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Get") && properties) { + const char *interface, *property; + const BusProperty *p; + + if (!dbus_message_get_args( + message, + &error, + DBUS_TYPE_STRING, &interface, + DBUS_TYPE_STRING, &property, + DBUS_TYPE_INVALID)) + return bus_send_error_reply(c, message, &error, -EINVAL); + + for (p = properties; p->property; p++) + if (streq(p->interface, interface) && streq(p->property, property)) + break; + + if (p->property) { + DBusMessageIter iter, sub; + + if (!(reply = dbus_message_new_method_return(message))) + goto oom; + + dbus_message_iter_init_append(reply, &iter); + + if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, p->signature, &sub)) + goto oom; + + if ((r = p->append(&sub, property, (void*) p->data)) < 0) { + + if (r == -ENOMEM) + goto oom; + + dbus_message_unref(reply); + return bus_send_error_reply(c, message, NULL, r); + } + + if (!dbus_message_iter_close_container(&iter, &sub)) + goto oom; + } else { + if (!nulstr_contains(interfaces, interface)) + dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface"); + else + dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_PROPERTY, "Unknown property"); + + return bus_send_error_reply(c, message, &error, -EINVAL); + } + + } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "GetAll") && properties) { + const char *interface; + const BusProperty *p; + DBusMessageIter iter, sub, sub2, sub3; + + if (!dbus_message_get_args( + message, + &error, + DBUS_TYPE_STRING, &interface, + DBUS_TYPE_INVALID)) + return bus_send_error_reply(c, message, &error, -EINVAL); + + if (interface[0] && !nulstr_contains(interfaces, interface)) { + dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface"); + return bus_send_error_reply(c, message, &error, -EINVAL); + } + + if (!(reply = dbus_message_new_method_return(message))) + goto oom; + + dbus_message_iter_init_append(reply, &iter); + + if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub)) + goto oom; + + for (p = properties; p->property; p++) { + if (interface[0] && !streq(p->interface, interface)) + continue; + + if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_DICT_ENTRY, NULL, &sub2) || + !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &p->property) || + !dbus_message_iter_open_container(&sub2, DBUS_TYPE_VARIANT, p->signature, &sub3)) + goto oom; + + if ((r = p->append(&sub3, p->property, (void*) p->data)) < 0) { + + if (r == -ENOMEM) + goto oom; + + dbus_message_unref(reply); + return bus_send_error_reply(c, message, NULL, r); + } + + if (!dbus_message_iter_close_container(&sub2, &sub3) || + !dbus_message_iter_close_container(&sub, &sub2)) + goto oom; + } + + if (!dbus_message_iter_close_container(&iter, &sub)) + goto oom; + + } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Set") && properties) { + const char *interface, *property; + DBusMessageIter iter; + const BusProperty *p; + + if (!dbus_message_iter_init(message, &iter) || + dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING) + return bus_send_error_reply(c, message, NULL, -EINVAL); + + dbus_message_iter_get_basic(&iter, &interface); + + if (!dbus_message_iter_next(&iter) || + dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING) + return bus_send_error_reply(c, message, NULL, -EINVAL); + + dbus_message_iter_get_basic(&iter, &property); + + if (!dbus_message_iter_next(&iter) || + dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT || + dbus_message_iter_has_next(&iter)) + return bus_send_error_reply(c, message, NULL, -EINVAL); + + for (p = properties; p->property; p++) + if (streq(p->interface, interface) && streq(p->property, property)) + break; + + if (p->set) { + DBusMessageIter sub; + char *sig; + + dbus_message_iter_recurse(&iter, &sub); + + if (!(sig = dbus_message_iter_get_signature(&sub))) + goto oom; + + if (!streq(sig, p->signature)) { + dbus_free(sig); + return bus_send_error_reply(c, message, NULL, -EINVAL); + } + + dbus_free(sig); + + if ((r = p->set(&sub, property)) < 0) { + if (r == -ENOMEM) + goto oom; + return bus_send_error_reply(c, message, NULL, r); + } + + if (!(reply = dbus_message_new_method_return(message))) + goto oom; + } else { + if (p->property) + dbus_set_error_const(&error, DBUS_ERROR_PROPERTY_READ_ONLY, "Property read-only"); + else if (!nulstr_contains(interfaces, interface)) + dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface"); + else + dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_PROPERTY, "Unknown property"); + + return bus_send_error_reply(c, message, &error, -EINVAL); + } + + } else if (!nulstr_contains(interfaces, dbus_message_get_interface(message))) { + dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface"); + return bus_send_error_reply(c, message, &error, -EINVAL); + } + + if (reply) { + if (!dbus_connection_send(c, reply, NULL)) + goto oom; + + dbus_message_unref(reply); + return DBUS_HANDLER_RESULT_HANDLED; + } + + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + +oom: + if (reply) + dbus_message_unref(reply); + + dbus_error_free(&error); + + return DBUS_HANDLER_RESULT_NEED_MEMORY; +} + +int bus_property_append_string(DBusMessageIter *i, const char *property, void *data) { + const char *t = data; + + assert(i); + assert(property); + + if (!t) + t = ""; + + if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t)) + return -ENOMEM; + + return 0; +} + +int bus_property_append_strv(DBusMessageIter *i, const char *property, void *data) { + DBusMessageIter sub; + char **t = data; + + assert(i); + assert(property); + + if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub)) + return -ENOMEM; + + STRV_FOREACH(t, t) + if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t)) + return -ENOMEM; + + if (!dbus_message_iter_close_container(i, &sub)) + return -ENOMEM; + + return 0; +} + +int bus_property_append_bool(DBusMessageIter *i, const char *property, void *data) { + bool *b = data; + dbus_bool_t db; + + assert(i); + assert(property); + assert(b); + + db = *b; + + if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &db)) + return -ENOMEM; + + return 0; +} + +int bus_property_append_uint64(DBusMessageIter *i, const char *property, void *data) { + assert(i); + assert(property); + assert(data); + + /* Let's ensure that pid_t is actually 64bit, and hence this + * function can be used for usec_t */ + assert_cc(sizeof(uint64_t) == sizeof(usec_t)); + + if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, data)) + return -ENOMEM; + + return 0; +} + +int bus_property_append_uint32(DBusMessageIter *i, const char *property, void *data) { + assert(i); + assert(property); + assert(data); + + /* Let's ensure that pid_t and mode_t is actually 32bit, and + * hence this function can be used for pid_t/mode_t */ + assert_cc(sizeof(uint32_t) == sizeof(pid_t)); + assert_cc(sizeof(uint32_t) == sizeof(mode_t)); + assert_cc(sizeof(uint32_t) == sizeof(unsigned)); + + if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, data)) + return -ENOMEM; + + return 0; +} + +int bus_property_append_int32(DBusMessageIter *i, const char *property, void *data) { + assert(i); + assert(property); + assert(data); + + assert_cc(sizeof(int32_t) == sizeof(int)); + + if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, data)) + return -ENOMEM; + + return 0; +} + +int bus_property_append_size(DBusMessageIter *i, const char *property, void *data) { + uint64_t u; + + assert(i); + assert(property); + assert(data); + + u = (uint64_t) *(size_t*) data; + + if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u)) + return -ENOMEM; + + return 0; +} + +int bus_property_append_ul(DBusMessageIter *i, const char *property, void *data) { + uint64_t u; + + assert(i); + assert(property); + assert(data); + + u = (uint64_t) *(unsigned long*) data; + + if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u)) + return -ENOMEM; + + return 0; +} + +const char *bus_errno_to_dbus(int error) { + + switch(error) { + + case -EINVAL: + return DBUS_ERROR_INVALID_ARGS; + + case -ENOMEM: + return DBUS_ERROR_NO_MEMORY; + + case -EPERM: + case -EACCES: + return DBUS_ERROR_ACCESS_DENIED; + + case -ESRCH: + return DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN; + + case -ENOENT: + return DBUS_ERROR_FILE_NOT_FOUND; + + case -EEXIST: + return DBUS_ERROR_FILE_EXISTS; + + case -ETIMEDOUT: + case -ETIME: + return DBUS_ERROR_TIMEOUT; + + case -EIO: + return DBUS_ERROR_IO_ERROR; + + case -ENETRESET: + case -ECONNABORTED: + case -ECONNRESET: + return DBUS_ERROR_DISCONNECTED; + } + + return DBUS_ERROR_FAILED; +} + +DBusHandlerResult bus_send_error_reply(DBusConnection *c, DBusMessage *message, DBusError *berror, int error) { + DBusMessage *reply = NULL; + const char *name, *text; + + if (berror && dbus_error_is_set(berror)) { + name = berror->name; + text = berror->message; + } else { + name = bus_errno_to_dbus(error); + text = strerror(-error); + } + + if (!(reply = dbus_message_new_error(message, name, text))) + goto oom; + + if (!dbus_connection_send(c, reply, NULL)) + goto oom; + + dbus_message_unref(reply); + + if (berror) + dbus_error_free(berror); + + return DBUS_HANDLER_RESULT_HANDLED; + +oom: + if (reply) + dbus_message_unref(reply); + + if (berror) + dbus_error_free(berror); + + return DBUS_HANDLER_RESULT_NEED_MEMORY; +} + +DBusMessage* bus_properties_changed_new(const char *path, const char *interface, const char *properties) { + DBusMessage *m; + DBusMessageIter iter, sub; + const char *i; + + assert(interface); + assert(properties); + + if (!(m = dbus_message_new_signal(path, "org.freedesktop.DBus.Properties", "PropertiesChanged"))) + goto oom; + + dbus_message_iter_init_append(m, &iter); + + /* We won't send any property values, since they might be + * large and sometimes not cheap to generated */ + + if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &interface) || + !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub) || + !dbus_message_iter_close_container(&iter, &sub) || + !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) + goto oom; + + NULSTR_FOREACH(i, properties) + if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &i)) + goto oom; + + if (!dbus_message_iter_close_container(&iter, &sub)) + goto oom; + + return m; + +oom: + if (m) + dbus_message_unref(m); + + return NULL; +} diff --git a/src/dbus-common.h b/src/dbus-common.h index 76333cd4..729519c5 100644 --- a/src/dbus-common.h +++ b/src/dbus-common.h @@ -24,6 +24,65 @@ #include +#ifndef DBUS_ERROR_UNKNOWN_OBJECT +#define DBUS_ERROR_UNKNOWN_OBJECT "org.freedesktop.DBus.Error.UnknownObject" +#endif + +#ifndef DBUS_ERROR_UNKNOWN_INTERFACE +#define DBUS_ERROR_UNKNOWN_INTERFACE "org.freedesktop.DBus.Error.UnknownInterface" +#endif + +#ifndef DBUS_ERROR_UNKNOWN_PROPERTY +#define DBUS_ERROR_UNKNOWN_PROPERTY "org.freedesktop.DBus.Error.UnknownProperty" +#endif + +#ifndef DBUS_ERROR_PROPERTY_READ_ONLY +#define DBUS_ERROR_PROPERTY_READ_ONLY "org.freedesktop.DBus.Error.PropertyReadOnly" +#endif + +#define BUS_PROPERTIES_INTERFACE \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" + +#define BUS_INTROSPECTABLE_INTERFACE \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" + +#define BUS_PEER_INTERFACE \ + "\n" \ + " \n" \ + " \n" \ + " \n" \ + " \n" \ + "\n" + +#define BUS_GENERIC_INTERFACES_LIST \ + "org.freedesktop.DBus.Properties\0" \ + "org.freedesktop.DBus.Introspectable\0" \ + "org.freedesktop.DBus.Peer\0" + int bus_check_peercred(DBusConnection *c); int bus_connect(DBusBusType t, DBusConnection **_bus, bool *private_bus, DBusError *error); @@ -33,4 +92,64 @@ int bus_connect_system_polkit(DBusConnection **_bus, DBusError *error); const char *bus_error_message(const DBusError *error); +typedef int (*BusPropertyCallback)(DBusMessageIter *iter, const char *property, void *data); +typedef int (*BusPropertySetCallback)(DBusMessageIter *iter, const char *property); + +typedef struct BusProperty { + const char *interface; /* interface of the property */ + const char *property; /* name of the property */ + BusPropertyCallback append; /* Function that is called to serialize this property */ + const char *signature; + const void *data; /* The data of this property */ + BusPropertySetCallback set; /* Optional: Function that is called to set this property */ +} BusProperty; + +DBusHandlerResult bus_send_error_reply( + DBusConnection *c, + DBusMessage *message, + DBusError *bus_error, + int error); + +DBusHandlerResult bus_default_message_handler( + DBusConnection *c, + DBusMessage *message, + const char *introspection, + const char *interfaces, + const BusProperty *properties); + +int bus_property_append_string(DBusMessageIter *i, const char *property, void *data); +int bus_property_append_strv(DBusMessageIter *i, const char *property, void *data); +int bus_property_append_bool(DBusMessageIter *i, const char *property, void *data); +int bus_property_append_int32(DBusMessageIter *i, const char *property, void *data); +int bus_property_append_uint32(DBusMessageIter *i, const char *property, void *data); +int bus_property_append_uint64(DBusMessageIter *i, const char *property, void *data); +int bus_property_append_size(DBusMessageIter *i, const char *property, void *data); +int bus_property_append_ul(DBusMessageIter *i, const char *property, void *data); + +#define bus_property_append_int bus_property_append_int32 +#define bus_property_append_pid bus_property_append_uint32 +#define bus_property_append_mode bus_property_append_uint32 +#define bus_property_append_unsigned bus_property_append_uint32 +#define bus_property_append_usec bus_property_append_uint64 + +#define DEFINE_BUS_PROPERTY_APPEND_ENUM(function,name,type) \ + int function(DBusMessageIter *i, const char *property, void *data) { \ + const char *value; \ + type *field = data; \ + \ + assert(i); \ + assert(property); \ + \ + value = name##_to_string(*field); \ + \ + if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &value)) \ + return -ENOMEM; \ + \ + return 0; \ + } + +const char *bus_errno_to_dbus(int error); + +DBusMessage* bus_properties_changed_new(const char *path, const char *interface, const char *properties); + #endif diff --git a/src/dbus-device.c b/src/dbus-device.c index b046eae9..f85ad2da 100644 --- a/src/dbus-device.c +++ b/src/dbus-device.c @@ -21,6 +21,7 @@ #include "dbus-unit.h" #include "dbus-device.h" +#include "dbus-common.h" #define BUS_DEVICE_INTERFACE \ " \n" \ @@ -53,5 +54,5 @@ DBusHandlerResult bus_device_message_handler(Unit *u, DBusConnection *c, DBusMes { NULL, NULL, NULL, NULL, NULL } }; - return bus_default_message_handler(u->meta.manager, c, message, INTROSPECTION, INTERFACES_LIST, properties); + return bus_default_message_handler(c, message, INTROSPECTION, INTERFACES_LIST, properties); } diff --git a/src/dbus-execute.c b/src/dbus-execute.c index 3cb09c2d..3f8fafe6 100644 --- a/src/dbus-execute.c +++ b/src/dbus-execute.c @@ -27,17 +27,17 @@ #include "missing.h" #include "ioprio.h" #include "strv.h" +#include "dbus-common.h" DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_execute_append_kill_mode, kill_mode, KillMode); DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_execute_append_input, exec_input, ExecInput); DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_execute_append_output, exec_output, ExecOutput); -int bus_execute_append_env_files(Manager *m, DBusMessageIter *i, const char *property, void *data) { +int bus_execute_append_env_files(DBusMessageIter *i, const char *property, void *data) { char **env_files = data, **j; DBusMessageIter sub, sub2; - assert(m); assert(i); assert(property); @@ -66,11 +66,10 @@ int bus_execute_append_env_files(Manager *m, DBusMessageIter *i, const char *pro return 0; } -int bus_execute_append_oom_score_adjust(Manager *m, DBusMessageIter *i, const char *property, void *data) { +int bus_execute_append_oom_score_adjust(DBusMessageIter *i, const char *property, void *data) { ExecContext *c = data; int32_t n; - assert(m); assert(i); assert(property); assert(c); @@ -101,11 +100,10 @@ int bus_execute_append_oom_score_adjust(Manager *m, DBusMessageIter *i, const ch return 0; } -int bus_execute_append_nice(Manager *m, DBusMessageIter *i, const char *property, void *data) { +int bus_execute_append_nice(DBusMessageIter *i, const char *property, void *data) { ExecContext *c = data; int32_t n; - assert(m); assert(i); assert(property); assert(c); @@ -121,11 +119,10 @@ int bus_execute_append_nice(Manager *m, DBusMessageIter *i, const char *property return 0; } -int bus_execute_append_ioprio(Manager *m, DBusMessageIter *i, const char *property, void *data) { +int bus_execute_append_ioprio(DBusMessageIter *i, const char *property, void *data) { ExecContext *c = data; int32_t n; - assert(m); assert(i); assert(property); assert(c); @@ -141,11 +138,10 @@ int bus_execute_append_ioprio(Manager *m, DBusMessageIter *i, const char *proper return 0; } -int bus_execute_append_cpu_sched_policy(Manager *m, DBusMessageIter *i, const char *property, void *data) { +int bus_execute_append_cpu_sched_policy(DBusMessageIter *i, const char *property, void *data) { ExecContext *c = data; int32_t n; - assert(m); assert(i); assert(property); assert(c); @@ -161,11 +157,10 @@ int bus_execute_append_cpu_sched_policy(Manager *m, DBusMessageIter *i, const ch return 0; } -int bus_execute_append_cpu_sched_priority(Manager *m, DBusMessageIter *i, const char *property, void *data) { +int bus_execute_append_cpu_sched_priority(DBusMessageIter *i, const char *property, void *data) { ExecContext *c = data; int32_t n; - assert(m); assert(i); assert(property); assert(c); @@ -187,12 +182,11 @@ int bus_execute_append_cpu_sched_priority(Manager *m, DBusMessageIter *i, const return 0; } -int bus_execute_append_affinity(Manager *m, DBusMessageIter *i, const char *property, void *data) { +int bus_execute_append_affinity(DBusMessageIter *i, const char *property, void *data) { ExecContext *c = data; dbus_bool_t b; DBusMessageIter sub; - assert(m); assert(i); assert(property); assert(c); @@ -214,11 +208,10 @@ int bus_execute_append_affinity(Manager *m, DBusMessageIter *i, const char *prop return 0; } -int bus_execute_append_timer_slack_nsec(Manager *m, DBusMessageIter *i, const char *property, void *data) { +int bus_execute_append_timer_slack_nsec(DBusMessageIter *i, const char *property, void *data) { ExecContext *c = data; uint64_t u; - assert(m); assert(i); assert(property); assert(c); @@ -234,11 +227,10 @@ int bus_execute_append_timer_slack_nsec(Manager *m, DBusMessageIter *i, const ch return 0; } -int bus_execute_append_capability_bs(Manager *m, DBusMessageIter *i, const char *property, void *data) { +int bus_execute_append_capability_bs(DBusMessageIter *i, const char *property, void *data) { ExecContext *c = data; uint64_t normal, inverted; - assert(m); assert(i); assert(property); assert(c); @@ -249,16 +241,15 @@ int bus_execute_append_capability_bs(Manager *m, DBusMessageIter *i, const char normal = *(uint64_t*) data; inverted = ~normal; - return bus_property_append_uint64(m, i, property, &inverted); + return bus_property_append_uint64(i, property, &inverted); } -int bus_execute_append_capabilities(Manager *m, DBusMessageIter *i, const char *property, void *data) { +int bus_execute_append_capabilities(DBusMessageIter *i, const char *property, void *data) { ExecContext *c = data; char *t = NULL; const char *s; dbus_bool_t b; - assert(m); assert(i); assert(property); assert(c); @@ -282,12 +273,11 @@ int bus_execute_append_capabilities(Manager *m, DBusMessageIter *i, const char * return 0; } -int bus_execute_append_rlimits(Manager *m, DBusMessageIter *i, const char *property, void *data) { +int bus_execute_append_rlimits(DBusMessageIter *i, const char *property, void *data) { ExecContext *c = data; int r; uint64_t u; - assert(m); assert(i); assert(property); assert(c); @@ -311,11 +301,10 @@ int bus_execute_append_rlimits(Manager *m, DBusMessageIter *i, const char *prope return 0; } -int bus_execute_append_command(Manager *m, DBusMessageIter *i, const char *property, void *data) { +int bus_execute_append_command(DBusMessageIter *i, const char *property, void *data) { ExecCommand *c = data; DBusMessageIter sub, sub2, sub3; - assert(m); assert(i); assert(property); diff --git a/src/dbus-execute.h b/src/dbus-execute.h index 548738a7..ed66390d 100644 --- a/src/dbus-execute.h +++ b/src/dbus-execute.h @@ -161,20 +161,20 @@ #define BUS_EXEC_COMMAND_PROPERTY(interface, command, name) \ { interface, name, bus_execute_append_command, "a(sasbttttuii)", (command) } -int bus_execute_append_output(Manager *m, DBusMessageIter *i, const char *property, void *data); -int bus_execute_append_input(Manager *m, DBusMessageIter *i, const char *property, void *data); -int bus_execute_append_oom_score_adjust(Manager *m, DBusMessageIter *i, const char *property, void *data); -int bus_execute_append_nice(Manager *m, DBusMessageIter *i, const char *property, void *data); -int bus_execute_append_ioprio(Manager *m, DBusMessageIter *i, const char *property, void *data); -int bus_execute_append_cpu_sched_policy(Manager *m, DBusMessageIter *i, const char *property, void *data); -int bus_execute_append_cpu_sched_priority(Manager *m, DBusMessageIter *i, const char *property, void *data); -int bus_execute_append_affinity(Manager *m, DBusMessageIter *i, const char *property, void *data); -int bus_execute_append_timer_slack_nsec(Manager *m, DBusMessageIter *i, const char *property, void *data); -int bus_execute_append_capabilities(Manager *m, DBusMessageIter *i, const char *property, void *data); -int bus_execute_append_capability_bs(Manager *m, DBusMessageIter *i, const char *property, void *data); -int bus_execute_append_rlimits(Manager *m, DBusMessageIter *i, const char *property, void *data); -int bus_execute_append_command(Manager *m, DBusMessageIter *u, const char *property, void *data); -int bus_execute_append_kill_mode(Manager *m, DBusMessageIter *i, const char *property, void *data); -int bus_execute_append_env_files(Manager *m, DBusMessageIter *i, const char *property, void *data); +int bus_execute_append_output(DBusMessageIter *i, const char *property, void *data); +int bus_execute_append_input(DBusMessageIter *i, const char *property, void *data); +int bus_execute_append_oom_score_adjust(DBusMessageIter *i, const char *property, void *data); +int bus_execute_append_nice(DBusMessageIter *i, const char *property, void *data); +int bus_execute_append_ioprio(DBusMessageIter *i, const char *property, void *data); +int bus_execute_append_cpu_sched_policy(DBusMessageIter *i, const char *property, void *data); +int bus_execute_append_cpu_sched_priority(DBusMessageIter *i, const char *property, void *data); +int bus_execute_append_affinity(DBusMessageIter *i, const char *property, void *data); +int bus_execute_append_timer_slack_nsec(DBusMessageIter *i, const char *property, void *data); +int bus_execute_append_capabilities(DBusMessageIter *i, const char *property, void *data); +int bus_execute_append_capability_bs(DBusMessageIter *i, const char *property, void *data); +int bus_execute_append_rlimits(DBusMessageIter *i, const char *property, void *data); +int bus_execute_append_command(DBusMessageIter *u, const char *property, void *data); +int bus_execute_append_kill_mode(DBusMessageIter *i, const char *property, void *data); +int bus_execute_append_env_files(DBusMessageIter *i, const char *property, void *data); #endif diff --git a/src/dbus-job.c b/src/dbus-job.c index 908ddba5..2308be34 100644 --- a/src/dbus-job.c +++ b/src/dbus-job.c @@ -24,6 +24,7 @@ #include "dbus.h" #include "log.h" #include "dbus-job.h" +#include "dbus-common.h" #define BUS_JOB_INTERFACE \ " \n" \ @@ -55,12 +56,11 @@ const char bus_job_interface[] _introspect_("Job") = BUS_JOB_INTERFACE; static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_job_append_state, job_state, JobState); static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_job_append_type, job_type, JobType); -static int bus_job_append_unit(Manager *m, DBusMessageIter *i, const char *property, void *data) { +static int bus_job_append_unit(DBusMessageIter *i, const char *property, void *data) { Job *j = data; DBusMessageIter sub; char *p; - assert(m); assert(i); assert(property); assert(j); @@ -103,7 +103,7 @@ static DBusHandlerResult bus_job_message_dispatch(Job *j, DBusConnection *connec job_finish_and_invalidate(j, JOB_CANCELED); } else - return bus_default_message_handler(j->manager, connection, message, INTROSPECTION, INTERFACES_LIST, properties); + return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, properties); if (reply) { if (!dbus_connection_send(connection, reply, NULL)) @@ -201,10 +201,10 @@ static DBusHandlerResult bus_job_message_handler(DBusConnection *connection, DBu dbus_error_init(&e); dbus_set_error_const(&e, DBUS_ERROR_UNKNOWN_OBJECT, "Unknown job"); - return bus_send_error_reply(m, connection, message, &e, r); + return bus_send_error_reply(connection, message, &e, r); } - return bus_send_error_reply(m, connection, message, NULL, r); + return bus_send_error_reply(connection, message, NULL, r); } return bus_job_message_dispatch(j, connection, message); diff --git a/src/dbus-manager.c b/src/dbus-manager.c index e1bf9fd2..4224afac 100644 --- a/src/dbus-manager.c +++ b/src/dbus-manager.c @@ -28,6 +28,7 @@ #include "strv.h" #include "bus-errors.h" #include "build.h" +#include "dbus-common.h" #define BUS_MANAGER_INTERFACE_BEGIN \ " \n" @@ -219,13 +220,14 @@ const char bus_manager_interface[] _introspect_("Manager") = BUS_MANAGER_INTERFA static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_running_as, manager_running_as, ManagerRunningAs); static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_exec_output, exec_output, ExecOutput); -static int bus_manager_append_tainted(Manager *m, DBusMessageIter *i, const char *property, void *data) { +static int bus_manager_append_tainted(DBusMessageIter *i, const char *property, void *data) { const char *t; + Manager *m = data; char buf[LINE_MAX] = "", *e = buf, *p = NULL; - assert(m); assert(i); assert(property); + assert(m); if (m->taint_usr) e = stpcpy(e, "usr-separate-fs "); @@ -246,10 +248,9 @@ static int bus_manager_append_tainted(Manager *m, DBusMessageIter *i, const char return 0; } -static int bus_manager_append_log_target(Manager *m, DBusMessageIter *i, const char *property, void *data) { +static int bus_manager_append_log_target(DBusMessageIter *i, const char *property, void *data) { const char *t; - assert(m); assert(i); assert(property); @@ -261,10 +262,9 @@ static int bus_manager_append_log_target(Manager *m, DBusMessageIter *i, const c return 0; } -static int bus_manager_set_log_target(Manager *m, DBusMessageIter *i, const char *property) { +static int bus_manager_set_log_target(DBusMessageIter *i, const char *property) { const char *t; - assert(m); assert(i); assert(property); @@ -273,10 +273,9 @@ static int bus_manager_set_log_target(Manager *m, DBusMessageIter *i, const char return log_set_target_from_string(t); } -static int bus_manager_append_log_level(Manager *m, DBusMessageIter *i, const char *property, void *data) { +static int bus_manager_append_log_level(DBusMessageIter *i, const char *property, void *data) { const char *t; - assert(m); assert(i); assert(property); @@ -288,10 +287,9 @@ static int bus_manager_append_log_level(Manager *m, DBusMessageIter *i, const ch return 0; } -static int bus_manager_set_log_level(Manager *m, DBusMessageIter *i, const char *property) { +static int bus_manager_set_log_level(DBusMessageIter *i, const char *property) { const char *t; - assert(m); assert(i); assert(property); @@ -300,12 +298,13 @@ static int bus_manager_set_log_level(Manager *m, DBusMessageIter *i, const char return log_set_max_level_from_string(t); } -static int bus_manager_append_n_names(Manager *m, DBusMessageIter *i, const char *property, void *data) { +static int bus_manager_append_n_names(DBusMessageIter *i, const char *property, void *data) { + Manager *m = data; uint32_t u; - assert(m); assert(i); assert(property); + assert(m); u = hashmap_size(m->units); @@ -315,12 +314,13 @@ static int bus_manager_append_n_names(Manager *m, DBusMessageIter *i, const char return 0; } -static int bus_manager_append_n_jobs(Manager *m, DBusMessageIter *i, const char *property, void *data) { +static int bus_manager_append_n_jobs(DBusMessageIter *i, const char *property, void *data) { + Manager *m = data; uint32_t u; - assert(m); assert(i); assert(property); + assert(m); u = hashmap_size(m->jobs); @@ -330,12 +330,13 @@ static int bus_manager_append_n_jobs(Manager *m, DBusMessageIter *i, const char return 0; } -static int bus_manager_append_progress(Manager *m, DBusMessageIter *i, const char *property, void *data) { +static int bus_manager_append_progress(DBusMessageIter *i, const char *property, void *data) { double d; + Manager *m = data; - assert(m); assert(i); assert(property); + assert(m); if (dual_timestamp_is_set(&m->finish_timestamp)) d = 1.0; @@ -377,13 +378,13 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, { "org.freedesktop.systemd1.Manager", "StartupTimestampMonotonic", bus_property_append_uint64, "t", &m->startup_timestamp.monotonic }, { "org.freedesktop.systemd1.Manager", "FinishTimestamp", bus_property_append_uint64, "t", &m->finish_timestamp.realtime }, { "org.freedesktop.systemd1.Manager", "FinishTimestampMonotonic", bus_property_append_uint64, "t",&m->finish_timestamp.monotonic }, - { "org.freedesktop.systemd1.Manager", "LogLevel", bus_manager_append_log_level, "s", NULL, bus_manager_set_log_level}, - { "org.freedesktop.systemd1.Manager", "LogTarget", bus_manager_append_log_target, "s", NULL, bus_manager_set_log_target}, - { "org.freedesktop.systemd1.Manager", "NNames", bus_manager_append_n_names, "u", NULL }, + { "org.freedesktop.systemd1.Manager", "LogLevel", bus_manager_append_log_level, "s", m, bus_manager_set_log_level }, + { "org.freedesktop.systemd1.Manager", "LogTarget", bus_manager_append_log_target, "s", m, bus_manager_set_log_target }, + { "org.freedesktop.systemd1.Manager", "NNames", bus_manager_append_n_names, "u", m }, { "org.freedesktop.systemd1.Manager", "NJobs", bus_manager_append_n_jobs, "u", NULL }, { "org.freedesktop.systemd1.Manager", "NInstalledJobs",bus_property_append_uint32, "u", &m->n_installed_jobs }, { "org.freedesktop.systemd1.Manager", "NFailedJobs", bus_property_append_uint32, "u", &m->n_failed_jobs }, - { "org.freedesktop.systemd1.Manager", "Progress", bus_manager_append_progress, "d", NULL }, + { "org.freedesktop.systemd1.Manager", "Progress", bus_manager_append_progress, "d", m }, { "org.freedesktop.systemd1.Manager", "Environment", bus_property_append_strv, "as", m->environment }, { "org.freedesktop.systemd1.Manager", "ConfirmSpawn", bus_property_append_bool, "b", &m->confirm_spawn }, { "org.freedesktop.systemd1.Manager", "ShowStatus", bus_property_append_bool, "b", &m->show_status }, @@ -425,11 +426,11 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, &error, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID)) - return bus_send_error_reply(m, connection, message, &error, -EINVAL); + return bus_send_error_reply(connection, message, &error, -EINVAL); if (!(u = manager_get_unit(m, name))) { dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name); - return bus_send_error_reply(m, connection, message, &error, -ENOENT); + return bus_send_error_reply(connection, message, &error, -ENOENT); } if (!(reply = dbus_message_new_method_return(message))) @@ -452,11 +453,11 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, &error, DBUS_TYPE_UINT32, &pid, DBUS_TYPE_INVALID)) - return bus_send_error_reply(m, connection, message, &error, -EINVAL); + return bus_send_error_reply(connection, message, &error, -EINVAL); if (!(u = cgroup_unit_by_pid(m, (pid_t) pid))) { dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "No unit for PID %lu is loaded.", (unsigned long) pid); - return bus_send_error_reply(m, connection, message, &error, -ENOENT); + return bus_send_error_reply(connection, message, &error, -ENOENT); } if (!(reply = dbus_message_new_method_return(message))) @@ -479,10 +480,10 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, &error, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID)) - return bus_send_error_reply(m, connection, message, &error, -EINVAL); + return bus_send_error_reply(connection, message, &error, -EINVAL); if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0) - return bus_send_error_reply(m, connection, message, &error, r); + return bus_send_error_reply(connection, message, &error, r); if (!(reply = dbus_message_new_method_return(message))) goto oom; @@ -529,21 +530,21 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, DBUS_TYPE_STRING, &smode, DBUS_TYPE_INT32, &signo, DBUS_TYPE_INVALID)) - return bus_send_error_reply(m, connection, message, &error, -EINVAL); + return bus_send_error_reply(connection, message, &error, -EINVAL); if ((mode = kill_mode_from_string(smode)) < 0 || (who = kill_who_from_string(swho)) < 0 || signo <= 0 || signo >= _NSIG) - return bus_send_error_reply(m, connection, message, &error, -EINVAL); + return bus_send_error_reply(connection, message, &error, -EINVAL); if (!(u = manager_get_unit(m, name))) { dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name); - return bus_send_error_reply(m, connection, message, &error, -ENOENT); + return bus_send_error_reply(connection, message, &error, -ENOENT); } if ((r = unit_kill(u, who, mode, signo, &error)) < 0) - return bus_send_error_reply(m, connection, message, &error, r); + return bus_send_error_reply(connection, message, &error, r); if (!(reply = dbus_message_new_method_return(message))) goto oom; @@ -557,11 +558,11 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, &error, DBUS_TYPE_UINT32, &id, DBUS_TYPE_INVALID)) - return bus_send_error_reply(m, connection, message, &error, -EINVAL); + return bus_send_error_reply(connection, message, &error, -EINVAL); if (!(j = manager_get_job(m, id))) { dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "Job %u does not exist.", (unsigned) id); - return bus_send_error_reply(m, connection, message, &error, -ENOENT); + return bus_send_error_reply(connection, message, &error, -ENOENT); } if (!(reply = dbus_message_new_method_return(message))) @@ -599,11 +600,11 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, &error, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID)) - return bus_send_error_reply(m, connection, message, &error, -EINVAL); + return bus_send_error_reply(connection, message, &error, -EINVAL); if (!(u = manager_get_unit(m, name))) { dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name); - return bus_send_error_reply(m, connection, message, &error, -ENOENT); + return bus_send_error_reply(connection, message, &error, -ENOENT); } unit_reset_failed(u); @@ -765,7 +766,7 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, if ((r = set_put(s, client)) < 0) { free(client); - return bus_send_error_reply(m, connection, message, NULL, r); + return bus_send_error_reply(connection, message, NULL, r); } if (!(reply = dbus_message_new_method_return(message))) @@ -776,7 +777,7 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, if (!(client = set_remove(BUS_CONNECTION_SUBSCRIBED(m, connection), (char*) message_get_sender_with_fallback(message)))) { dbus_set_error(&error, BUS_ERROR_NOT_SUBSCRIBED, "Client is not subscribed."); - return bus_send_error_reply(m, connection, message, &error, -ENOENT); + return bus_send_error_reply(connection, message, &error, -ENOENT); } free(client); @@ -823,13 +824,13 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, DBUS_TYPE_STRING, &name, DBUS_TYPE_BOOLEAN, &cleanup, DBUS_TYPE_INVALID)) - return bus_send_error_reply(m, connection, message, &error, -EINVAL); + return bus_send_error_reply(connection, message, &error, -EINVAL); if (name && name[0] == 0) name = NULL; if ((r = snapshot_create(m, name, cleanup, &error, &s)) < 0) - return bus_send_error_reply(m, connection, message, &error, r); + return bus_send_error_reply(connection, message, &error, r); if (!(reply = dbus_message_new_method_return(message))) goto oom; @@ -930,7 +931,7 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, if (m->running_as == MANAGER_SYSTEM) { dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Exit is only supported for user service managers."); - return bus_send_error_reply(m, connection, message, &error, -ENOTSUP); + return bus_send_error_reply(connection, message, &error, -ENOTSUP); } if (!(reply = dbus_message_new_method_return(message))) @@ -942,7 +943,7 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, if (m->running_as != MANAGER_SYSTEM) { dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Reboot is only supported for system managers."); - return bus_send_error_reply(m, connection, message, &error, -ENOTSUP); + return bus_send_error_reply(connection, message, &error, -ENOTSUP); } if (!(reply = dbus_message_new_method_return(message))) @@ -954,7 +955,7 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, if (m->running_as != MANAGER_SYSTEM) { dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Powering off is only supported for system managers."); - return bus_send_error_reply(m, connection, message, &error, -ENOTSUP); + return bus_send_error_reply(connection, message, &error, -ENOTSUP); } if (!(reply = dbus_message_new_method_return(message))) @@ -966,7 +967,7 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, if (m->running_as != MANAGER_SYSTEM) { dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Halting is only supported for system managers."); - return bus_send_error_reply(m, connection, message, &error, -ENOTSUP); + return bus_send_error_reply(connection, message, &error, -ENOTSUP); } if (!(reply = dbus_message_new_method_return(message))) @@ -978,7 +979,7 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, if (m->running_as != MANAGER_SYSTEM) { dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "kexec is only supported for system managers."); - return bus_send_error_reply(m, connection, message, &error, -ENOTSUP); + return bus_send_error_reply(connection, message, &error, -ENOTSUP); } if (!(reply = dbus_message_new_method_return(message))) @@ -993,7 +994,7 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, if (r == -ENOMEM) goto oom; - return bus_send_error_reply(m, connection, message, NULL, r); + return bus_send_error_reply(connection, message, NULL, r); } e = strv_env_merge(2, m->environment, l); @@ -1017,7 +1018,7 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, if (r == -ENOMEM) goto oom; - return bus_send_error_reply(m, connection, message, NULL, r); + return bus_send_error_reply(connection, message, NULL, r); } e = strv_env_delete(m->environment, 1, l); @@ -1035,7 +1036,7 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, m->environment = e; } else - return bus_default_message_handler(m, connection, message, NULL, INTERFACES_LIST, properties); + return bus_default_message_handler(connection, message, NULL, INTERFACES_LIST, properties); if (job_type != _JOB_TYPE_INVALID) { const char *name, *smode, *old_name = NULL; @@ -1061,24 +1062,24 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, DBUS_TYPE_INVALID); if (!b) - return bus_send_error_reply(m, connection, message, &error, -EINVAL); + return bus_send_error_reply(connection, message, &error, -EINVAL); if (old_name) if (!(u = manager_get_unit(m, old_name)) || !u->meta.job || u->meta.job->type != JOB_START) { dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "No job queued for unit %s", old_name); - return bus_send_error_reply(m, connection, message, &error, -ENOENT); + return bus_send_error_reply(connection, message, &error, -ENOENT); } if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) { dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode); - return bus_send_error_reply(m, connection, message, &error, -EINVAL); + return bus_send_error_reply(connection, message, &error, -EINVAL); } if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0) - return bus_send_error_reply(m, connection, message, &error, r); + return bus_send_error_reply(connection, message, &error, r); if (reload_if_possible && unit_can_reload(u)) { if (job_type == JOB_RESTART) @@ -1092,11 +1093,11 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, ((job_type == JOB_RESTART || job_type == JOB_TRY_RESTART) && (u->meta.refuse_manual_start || u->meta.refuse_manual_stop))) { dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, may be requested by dependency only."); - return bus_send_error_reply(m, connection, message, &error, -EPERM); + return bus_send_error_reply(connection, message, &error, -EPERM); } if ((r = manager_add_job(m, job_type, u, mode, true, &error, &j)) < 0) - return bus_send_error_reply(m, connection, message, &error, r); + return bus_send_error_reply(connection, message, &error, r); if (!(j->bus_client = strdup(message_get_sender_with_fallback(message)))) goto oom; diff --git a/src/dbus-mount.c b/src/dbus-mount.c index 5fb9a3f0..cfbfd453 100644 --- a/src/dbus-mount.c +++ b/src/dbus-mount.c @@ -24,6 +24,7 @@ #include "dbus-unit.h" #include "dbus-mount.h" #include "dbus-execute.h" +#include "dbus-common.h" #define BUS_MOUNT_INTERFACE \ " \n" \ @@ -65,11 +66,10 @@ const char bus_mount_invalidating_properties[] = "ExecRemount\0" "ControlPID\0"; -static int bus_mount_append_what(Manager *n, DBusMessageIter *i, const char *property, void *data) { +static int bus_mount_append_what(DBusMessageIter *i, const char *property, void *data) { Mount *m = data; const char *d; - assert(n); assert(i); assert(property); assert(m); @@ -89,11 +89,10 @@ static int bus_mount_append_what(Manager *n, DBusMessageIter *i, const char *pro return 0; } -static int bus_mount_append_options(Manager *n, DBusMessageIter *i, const char *property, void *data) { +static int bus_mount_append_options(DBusMessageIter *i, const char *property, void *data) { Mount *m = data; const char *d; - assert(n); assert(i); assert(property); assert(m); @@ -113,11 +112,10 @@ static int bus_mount_append_options(Manager *n, DBusMessageIter *i, const char * return 0; } -static int bus_mount_append_type(Manager *n, DBusMessageIter *i, const char *property, void *data) { +static int bus_mount_append_type(DBusMessageIter *i, const char *property, void *data) { Mount *m = data; const char *d; - assert(n); assert(i); assert(property); assert(m); @@ -138,6 +136,7 @@ static int bus_mount_append_type(Manager *n, DBusMessageIter *i, const char *pro } DBusHandlerResult bus_mount_message_handler(Unit *u, DBusConnection *c, DBusMessage *message) { + const BusProperty properties[] = { BUS_UNIT_PROPERTIES, { "org.freedesktop.systemd1.Mount", "Where", bus_property_append_string, "s", u->mount.where }, @@ -154,5 +153,5 @@ DBusHandlerResult bus_mount_message_handler(Unit *u, DBusConnection *c, DBusMess { NULL, NULL, NULL, NULL, NULL } }; - return bus_default_message_handler(u->meta.manager, c, message, INTROSPECTION, INTERFACES_LIST, properties); + return bus_default_message_handler(c, message, INTROSPECTION, INTERFACES_LIST, properties); } diff --git a/src/dbus-path.c b/src/dbus-path.c index 1e757a30..15238798 100644 --- a/src/dbus-path.c +++ b/src/dbus-path.c @@ -24,6 +24,7 @@ #include "dbus-unit.h" #include "dbus-path.h" #include "dbus-execute.h" +#include "dbus-common.h" #define BUS_PATH_INTERFACE \ " \n" \ @@ -49,12 +50,11 @@ const char bus_path_interface[] _introspect_("Path") = BUS_PATH_INTERFACE; -static int bus_path_append_paths(Manager *m, DBusMessageIter *i, const char *property, void *data) { +static int bus_path_append_paths(DBusMessageIter *i, const char *property, void *data) { Path *p = data; DBusMessageIter sub, sub2; PathSpec *k; - assert(m); assert(i); assert(property); assert(p); @@ -78,11 +78,10 @@ static int bus_path_append_paths(Manager *m, DBusMessageIter *i, const char *pro return 0; } -static int bus_path_append_unit(Manager *m, DBusMessageIter *i, const char *property, void *data) { +static int bus_path_append_unit(DBusMessageIter *i, const char *property, void *data) { Unit *u = data; const char *t; - assert(m); assert(i); assert(property); assert(u); @@ -102,5 +101,5 @@ DBusHandlerResult bus_path_message_handler(Unit *u, DBusConnection *c, DBusMessa { NULL, NULL, NULL, NULL, NULL } }; - return bus_default_message_handler(u->meta.manager, c, message, INTROSPECTION, INTERFACES_LIST, properties); + return bus_default_message_handler(c, message, INTROSPECTION, INTERFACES_LIST, properties); } diff --git a/src/dbus-service.c b/src/dbus-service.c index 9c3d73cb..3486623e 100644 --- a/src/dbus-service.c +++ b/src/dbus-service.c @@ -24,6 +24,7 @@ #include "dbus-unit.h" #include "dbus-execute.h" #include "dbus-service.h" +#include "dbus-common.h" #ifdef HAVE_SYSV_COMPAT #define BUS_SERVICE_SYSV_INTERFACE_FRAGMENT \ @@ -129,5 +130,5 @@ DBusHandlerResult bus_service_message_handler(Unit *u, DBusConnection *connectio { NULL, NULL, NULL, NULL, NULL } }; - return bus_default_message_handler(u->meta.manager, connection, message, INTROSPECTION, INTERFACES_LIST, properties); + return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, properties); } diff --git a/src/dbus-snapshot.c b/src/dbus-snapshot.c index cc12b1bd..0c2f3495 100644 --- a/src/dbus-snapshot.c +++ b/src/dbus-snapshot.c @@ -21,6 +21,7 @@ #include "dbus-unit.h" #include "dbus-snapshot.h" +#include "dbus-common.h" #define BUS_SNAPSHOT_INTERFACE \ " \n" \ @@ -45,6 +46,7 @@ const char bus_snapshot_interface[] _introspect_("Snapshot") = BUS_SNAPSHOT_INTERFACE; DBusHandlerResult bus_snapshot_message_handler(Unit *u, DBusConnection *c, DBusMessage *message) { + const BusProperty properties[] = { BUS_UNIT_PROPERTIES, { "org.freedesktop.systemd1.Snapshot", "Cleanup", bus_property_append_bool, "b", &u->snapshot.cleanup }, @@ -64,7 +66,7 @@ DBusHandlerResult bus_snapshot_message_handler(Unit *u, DBusConnection *c, DBusM goto oom; } else - return bus_default_message_handler(u->meta.manager, c, message, INTROSPECTION, INTERFACES_LIST, properties); + return bus_default_message_handler(c, message, INTROSPECTION, INTERFACES_LIST, properties); if (reply) { if (!dbus_connection_send(c, reply, NULL)) diff --git a/src/dbus-socket.c b/src/dbus-socket.c index 5b068b45..88727bbb 100644 --- a/src/dbus-socket.c +++ b/src/dbus-socket.c @@ -24,6 +24,7 @@ #include "dbus-unit.h" #include "dbus-socket.h" #include "dbus-execute.h" +#include "dbus-common.h" #define BUS_SOCKET_INTERFACE \ " \n" \ @@ -113,5 +114,5 @@ DBusHandlerResult bus_socket_message_handler(Unit *u, DBusConnection *c, DBusMes { NULL, NULL, NULL, NULL, NULL } }; - return bus_default_message_handler(u->meta.manager, c, message, INTROSPECTION, INTERFACES_LIST, properties); + return bus_default_message_handler(c, message, INTROSPECTION, INTERFACES_LIST, properties); } diff --git a/src/dbus-swap.c b/src/dbus-swap.c index 079912a5..988ca58d 100644 --- a/src/dbus-swap.c +++ b/src/dbus-swap.c @@ -25,6 +25,7 @@ #include "dbus-unit.h" #include "dbus-swap.h" #include "dbus-execute.h" +#include "dbus-common.h" #define BUS_SWAP_INTERFACE \ " \n" \ @@ -60,11 +61,10 @@ const char bus_swap_invalidating_properties[] = "ExecDeactivate\0" "ControlPID\0"; -static int bus_swap_append_priority(Manager *m, DBusMessageIter *i, const char *property, void *data) { +static int bus_swap_append_priority(DBusMessageIter *i, const char *property, void *data) { Swap *s = data; dbus_int32_t j; - assert(m); assert(i); assert(property); assert(s); @@ -96,5 +96,5 @@ DBusHandlerResult bus_swap_message_handler(Unit *u, DBusConnection *c, DBusMessa { NULL, NULL, NULL, NULL, NULL } }; - return bus_default_message_handler(u->meta.manager, c, message, INTROSPECTION, INTERFACES_LIST, properties); + return bus_default_message_handler(c, message, INTROSPECTION, INTERFACES_LIST, properties); } diff --git a/src/dbus-target.c b/src/dbus-target.c index 1cbeccb5..1e00f2db 100644 --- a/src/dbus-target.c +++ b/src/dbus-target.c @@ -23,6 +23,7 @@ #include "dbus-unit.h" #include "dbus-target.h" +#include "dbus-common.h" #define BUS_TARGET_INTERFACE \ " \n" \ @@ -50,5 +51,5 @@ DBusHandlerResult bus_target_message_handler(Unit *u, DBusConnection *c, DBusMes { NULL, NULL, NULL, NULL, NULL } }; - return bus_default_message_handler(u->meta.manager, c, message, INTROSPECTION, INTERFACES_LIST, properties); + return bus_default_message_handler(c, message, INTROSPECTION, INTERFACES_LIST, properties); } diff --git a/src/dbus-timer.c b/src/dbus-timer.c index e44f4e2f..abcbe6f9 100644 --- a/src/dbus-timer.c +++ b/src/dbus-timer.c @@ -24,6 +24,7 @@ #include "dbus-unit.h" #include "dbus-timer.h" #include "dbus-execute.h" +#include "dbus-common.h" #define BUS_TIMER_INTERFACE \ " \n" \ @@ -52,12 +53,11 @@ const char bus_timer_invalidating_properties[] = "Timers\0" "NextElapseUSec\0"; -static int bus_timer_append_timers(Manager *m, DBusMessageIter *i, const char *property, void *data) { +static int bus_timer_append_timers(DBusMessageIter *i, const char *property, void *data) { Timer *p = data; DBusMessageIter sub, sub2; TimerValue *k; - assert(m); assert(i); assert(property); assert(p); @@ -99,11 +99,10 @@ static int bus_timer_append_timers(Manager *m, DBusMessageIter *i, const char *p return 0; } -static int bus_timer_append_unit(Manager *m, DBusMessageIter *i, const char *property, void *data) { +static int bus_timer_append_unit(DBusMessageIter *i, const char *property, void *data) { Unit *u = data; const char *t; - assert(m); assert(i); assert(property); assert(u); @@ -122,5 +121,5 @@ DBusHandlerResult bus_timer_message_handler(Unit *u, DBusConnection *c, DBusMess { NULL, NULL, NULL, NULL, NULL } }; - return bus_default_message_handler(u->meta.manager, c, message, INTROSPECTION, INTERFACES_LIST, properties); + return bus_default_message_handler(c, message, INTROSPECTION, INTERFACES_LIST, properties); } diff --git a/src/dbus-unit.c b/src/dbus-unit.c index b5daa66b..ad8e9a1a 100644 --- a/src/dbus-unit.c +++ b/src/dbus-unit.c @@ -25,6 +25,7 @@ #include "log.h" #include "dbus-unit.h" #include "bus-errors.h" +#include "dbus-common.h" const char bus_unit_interface[] _introspect_("Unit") = BUS_UNIT_INTERFACE; @@ -39,7 +40,7 @@ const char bus_unit_interface[] _introspect_("Unit") = BUS_UNIT_INTERFACE; "Job\0" \ "NeedDaemonReload\0" -int bus_unit_append_names(Manager *m, DBusMessageIter *i, const char *property, void *data) { +int bus_unit_append_names(DBusMessageIter *i, const char *property, void *data) { char *t; Iterator j; DBusMessageIter sub; @@ -58,11 +59,10 @@ int bus_unit_append_names(Manager *m, DBusMessageIter *i, const char *property, return 0; } -int bus_unit_append_following(Manager *m, DBusMessageIter *i, const char *property, void *data) { +int bus_unit_append_following(DBusMessageIter *i, const char *property, void *data) { Unit *u = data, *f; const char *d; - assert(m); assert(i); assert(property); assert(u); @@ -76,7 +76,7 @@ int bus_unit_append_following(Manager *m, DBusMessageIter *i, const char *proper return 0; } -int bus_unit_append_dependencies(Manager *m, DBusMessageIter *i, const char *property, void *data) { +int bus_unit_append_dependencies(DBusMessageIter *i, const char *property, void *data) { Unit *u; Iterator j; DBusMessageIter sub; @@ -95,11 +95,10 @@ int bus_unit_append_dependencies(Manager *m, DBusMessageIter *i, const char *pro return 0; } -int bus_unit_append_description(Manager *m, DBusMessageIter *i, const char *property, void *data) { +int bus_unit_append_description(DBusMessageIter *i, const char *property, void *data) { Unit *u = data; const char *d; - assert(m); assert(i); assert(property); assert(u); @@ -114,11 +113,10 @@ int bus_unit_append_description(Manager *m, DBusMessageIter *i, const char *prop DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_unit_append_load_state, unit_load_state, UnitLoadState); -int bus_unit_append_active_state(Manager *m, DBusMessageIter *i, const char *property, void *data) { +int bus_unit_append_active_state(DBusMessageIter *i, const char *property, void *data) { Unit *u = data; const char *state; - assert(m); assert(i); assert(property); assert(u); @@ -131,11 +129,10 @@ int bus_unit_append_active_state(Manager *m, DBusMessageIter *i, const char *pro return 0; } -int bus_unit_append_sub_state(Manager *m, DBusMessageIter *i, const char *property, void *data) { +int bus_unit_append_sub_state(DBusMessageIter *i, const char *property, void *data) { Unit *u = data; const char *state; - assert(m); assert(i); assert(property); assert(u); @@ -148,11 +145,10 @@ int bus_unit_append_sub_state(Manager *m, DBusMessageIter *i, const char *proper return 0; } -int bus_unit_append_can_start(Manager *m, DBusMessageIter *i, const char *property, void *data) { +int bus_unit_append_can_start(DBusMessageIter *i, const char *property, void *data) { Unit *u = data; dbus_bool_t b; - assert(m); assert(i); assert(property); assert(u); @@ -166,11 +162,10 @@ int bus_unit_append_can_start(Manager *m, DBusMessageIter *i, const char *proper return 0; } -int bus_unit_append_can_stop(Manager *m, DBusMessageIter *i, const char *property, void *data) { +int bus_unit_append_can_stop(DBusMessageIter *i, const char *property, void *data) { Unit *u = data; dbus_bool_t b; - assert(m); assert(i); assert(property); assert(u); @@ -187,11 +182,10 @@ int bus_unit_append_can_stop(Manager *m, DBusMessageIter *i, const char *propert return 0; } -int bus_unit_append_can_reload(Manager *m, DBusMessageIter *i, const char *property, void *data) { +int bus_unit_append_can_reload(DBusMessageIter *i, const char *property, void *data) { Unit *u = data; dbus_bool_t b; - assert(m); assert(i); assert(property); assert(u); @@ -204,11 +198,10 @@ int bus_unit_append_can_reload(Manager *m, DBusMessageIter *i, const char *prope return 0; } -int bus_unit_append_can_isolate(Manager *m, DBusMessageIter *i, const char *property, void *data) { +int bus_unit_append_can_isolate(DBusMessageIter *i, const char *property, void *data) { Unit *u = data; dbus_bool_t b; - assert(m); assert(i); assert(property); assert(u); @@ -222,12 +215,11 @@ int bus_unit_append_can_isolate(Manager *m, DBusMessageIter *i, const char *prop return 0; } -int bus_unit_append_job(Manager *m, DBusMessageIter *i, const char *property, void *data) { +int bus_unit_append_job(DBusMessageIter *i, const char *property, void *data) { Unit *u = data; DBusMessageIter sub; char *p; - assert(m); assert(i); assert(property); assert(u); @@ -270,13 +262,12 @@ int bus_unit_append_job(Manager *m, DBusMessageIter *i, const char *property, vo return 0; } -int bus_unit_append_default_cgroup(Manager *m, DBusMessageIter *i, const char *property, void *data) { +int bus_unit_append_default_cgroup(DBusMessageIter *i, const char *property, void *data) { Unit *u = data; char *t; CGroupBonding *cgb; bool success; - assert(m); assert(i); assert(property); assert(u); @@ -295,7 +286,7 @@ int bus_unit_append_default_cgroup(Manager *m, DBusMessageIter *i, const char *p return success ? 0 : -ENOMEM; } -int bus_unit_append_cgroups(Manager *m, DBusMessageIter *i, const char *property, void *data) { +int bus_unit_append_cgroups(DBusMessageIter *i, const char *property, void *data) { Unit *u = data; CGroupBonding *cgb; DBusMessageIter sub; @@ -323,11 +314,10 @@ int bus_unit_append_cgroups(Manager *m, DBusMessageIter *i, const char *property return 0; } -int bus_unit_append_need_daemon_reload(Manager *m, DBusMessageIter *i, const char *property, void *data) { +int bus_unit_append_need_daemon_reload(DBusMessageIter *i, const char *property, void *data) { Unit *u = data; dbus_bool_t b; - assert(m); assert(i); assert(property); assert(u); @@ -380,16 +370,16 @@ static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusConnection *conn DBUS_TYPE_STRING, &smode, DBUS_TYPE_INT32, &signo, DBUS_TYPE_INVALID)) - return bus_send_error_reply(m, connection, message, &error, -EINVAL); + return bus_send_error_reply(connection, message, &error, -EINVAL); if ((mode = kill_mode_from_string(smode)) < 0 || (who = kill_who_from_string(swho)) < 0 || signo <= 0 || signo >= _NSIG) - return bus_send_error_reply(m, connection, message, &error, -EINVAL); + return bus_send_error_reply(connection, message, &error, -EINVAL); if ((r = unit_kill(u, who, mode, signo, &error)) < 0) - return bus_send_error_reply(m, connection, message, &error, r); + return bus_send_error_reply(connection, message, &error, r); if (!(reply = dbus_message_new_method_return(message))) goto oom; @@ -417,7 +407,7 @@ static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusConnection *conn ((job_type == JOB_RESTART || job_type == JOB_TRY_RESTART) && (u->meta.refuse_manual_start || u->meta.refuse_manual_stop))) { dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, may be requested by dependency only."); - return bus_send_error_reply(m, connection, message, &error, -EPERM); + return bus_send_error_reply(connection, message, &error, -EPERM); } if (!dbus_message_get_args( @@ -425,7 +415,7 @@ static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusConnection *conn &error, DBUS_TYPE_STRING, &smode, DBUS_TYPE_INVALID)) - return bus_send_error_reply(m, connection, message, &error, -EINVAL); + return bus_send_error_reply(connection, message, &error, -EINVAL); if (reload_if_possible && unit_can_reload(u)) { if (job_type == JOB_RESTART) @@ -436,11 +426,11 @@ static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusConnection *conn if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) { dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode); - return bus_send_error_reply(m, connection, message, &error, -EINVAL); + return bus_send_error_reply(connection, message, &error, -EINVAL); } if ((r = manager_add_job(m, job_type, u, mode, true, &error, &j)) < 0) - return bus_send_error_reply(m, connection, message, &error, r); + return bus_send_error_reply(connection, message, &error, r); if (!(reply = dbus_message_new_method_return(message))) goto oom; @@ -571,10 +561,10 @@ static DBusHandlerResult bus_unit_message_handler(DBusConnection *connection, DB dbus_error_init(&e); dbus_set_error_const(&e, DBUS_ERROR_UNKNOWN_OBJECT, "Unknown unit"); - return bus_send_error_reply(m, connection, message, &e, r); + return bus_send_error_reply(connection, message, &e, r); } - return bus_send_error_reply(m, connection, message, NULL, r); + return bus_send_error_reply(connection, message, NULL, r); } return bus_unit_message_dispatch(u, connection, message); diff --git a/src/dbus-unit.h b/src/dbus-unit.h index b578ff98..a2a93235 100644 --- a/src/dbus-unit.h +++ b/src/dbus-unit.h @@ -170,21 +170,21 @@ { "org.freedesktop.systemd1.Unit", "ConditionTimestampMonotonic", bus_property_append_usec,"t", &u->meta.condition_timestamp.monotonic }, \ { "org.freedesktop.systemd1.Unit", "ConditionResult", bus_property_append_bool, "b", &u->meta.condition_result } -int bus_unit_append_names(Manager *m, DBusMessageIter *i, const char *property, void *data); -int bus_unit_append_following(Manager *m, DBusMessageIter *i, const char *property, void *data); -int bus_unit_append_dependencies(Manager *m, DBusMessageIter *i, const char *property, void *data); -int bus_unit_append_description(Manager *m, DBusMessageIter *i, const char *property, void *data); -int bus_unit_append_load_state(Manager *m, DBusMessageIter *i, const char *property, void *data); -int bus_unit_append_active_state(Manager *m, DBusMessageIter *i, const char *property, void *data); -int bus_unit_append_sub_state(Manager *m, DBusMessageIter *i, const char *property, void *data); -int bus_unit_append_can_start(Manager *m, DBusMessageIter *i, const char *property, void *data); -int bus_unit_append_can_stop(Manager *m, DBusMessageIter *i, const char *property, void *data); -int bus_unit_append_can_reload(Manager *m, DBusMessageIter *i, const char *property, void *data); -int bus_unit_append_can_isolate(Manager *m, DBusMessageIter *i, const char *property, void *data); -int bus_unit_append_job(Manager *m, DBusMessageIter *i, const char *property, void *data); -int bus_unit_append_default_cgroup(Manager *m, DBusMessageIter *i, const char *property, void *data); -int bus_unit_append_cgroups(Manager *m, DBusMessageIter *i, const char *property, void *data); -int bus_unit_append_need_daemon_reload(Manager *m, DBusMessageIter *i, const char *property, void *data); +int bus_unit_append_names(DBusMessageIter *i, const char *property, void *data); +int bus_unit_append_following(DBusMessageIter *i, const char *property, void *data); +int bus_unit_append_dependencies(DBusMessageIter *i, const char *property, void *data); +int bus_unit_append_description(DBusMessageIter *i, const char *property, void *data); +int bus_unit_append_load_state(DBusMessageIter *i, const char *property, void *data); +int bus_unit_append_active_state(DBusMessageIter *i, const char *property, void *data); +int bus_unit_append_sub_state(DBusMessageIter *i, const char *property, void *data); +int bus_unit_append_can_start(DBusMessageIter *i, const char *property, void *data); +int bus_unit_append_can_stop(DBusMessageIter *i, const char *property, void *data); +int bus_unit_append_can_reload(DBusMessageIter *i, const char *property, void *data); +int bus_unit_append_can_isolate(DBusMessageIter *i, const char *property, void *data); +int bus_unit_append_job(DBusMessageIter *i, const char *property, void *data); +int bus_unit_append_default_cgroup(DBusMessageIter *i, const char *property, void *data); +int bus_unit_append_cgroups(DBusMessageIter *i, const char *property, void *data); +int bus_unit_append_need_daemon_reload(DBusMessageIter *i, const char *property, void *data); void bus_unit_send_change_signal(Unit *u); void bus_unit_send_removed_signal(Unit *u); diff --git a/src/dbus.c b/src/dbus.c index 6f43c410..1907560b 100644 --- a/src/dbus.c +++ b/src/dbus.c @@ -44,6 +44,7 @@ #include "dbus-path.h" #include "bus-errors.h" #include "special.h" +#include "dbus-common.h" #define CONNECTIONS_MAX 52 @@ -69,7 +70,6 @@ const char *const bus_interface_table[] = { NULL }; -static const char *error_to_dbus(int error); static void bus_done_api(Manager *m); static void bus_done_system(Manager *m); static void bus_done_private(Manager *m); @@ -406,7 +406,7 @@ static DBusHandlerResult api_bus_message_filter(DBusConnection *connection, DBus if (!(reply = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Activator", "ActivationFailure"))) goto oom; - id = error.name ? error.name : error_to_dbus(r); + id = error.name ? error.name : bus_errno_to_dbus(r); text = bus_error(&error, r); if (!dbus_message_set_destination(reply, DBUS_SERVICE_DBUS) || @@ -1214,289 +1214,6 @@ oom: return -ENOMEM; } -DBusHandlerResult bus_default_message_handler( - Manager *m, - DBusConnection *c, - DBusMessage *message, - const char *introspection, - const char *interfaces, - const BusProperty *properties) { - - DBusError error; - DBusMessage *reply = NULL; - int r; - - assert(m); - assert(c); - assert(message); - - dbus_error_init(&error); - - if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect") && introspection) { - - if (!(reply = dbus_message_new_method_return(message))) - goto oom; - - if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) - goto oom; - - } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Get") && properties) { - const char *interface, *property; - const BusProperty *p; - - if (!dbus_message_get_args( - message, - &error, - DBUS_TYPE_STRING, &interface, - DBUS_TYPE_STRING, &property, - DBUS_TYPE_INVALID)) - return bus_send_error_reply(m, c, message, &error, -EINVAL); - - for (p = properties; p->property; p++) - if (streq(p->interface, interface) && streq(p->property, property)) - break; - - if (p->property) { - DBusMessageIter iter, sub; - - if (!(reply = dbus_message_new_method_return(message))) - goto oom; - - dbus_message_iter_init_append(reply, &iter); - - if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, p->signature, &sub)) - goto oom; - - if ((r = p->append(m, &sub, property, (void*) p->data)) < 0) { - - if (r == -ENOMEM) - goto oom; - - dbus_message_unref(reply); - return bus_send_error_reply(m, c, message, NULL, r); - } - - if (!dbus_message_iter_close_container(&iter, &sub)) - goto oom; - } else { - if (!nulstr_contains(interfaces, interface)) - dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface"); - else - dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_PROPERTY, "Unknown property"); - - return bus_send_error_reply(m, c, message, &error, -EINVAL); - } - - } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "GetAll") && properties) { - const char *interface; - const BusProperty *p; - DBusMessageIter iter, sub, sub2, sub3; - - if (!dbus_message_get_args( - message, - &error, - DBUS_TYPE_STRING, &interface, - DBUS_TYPE_INVALID)) - return bus_send_error_reply(m, c, message, &error, -EINVAL); - - if (interface[0] && !nulstr_contains(interfaces, interface)) { - dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface"); - return bus_send_error_reply(m, c, message, &error, -EINVAL); - } - - if (!(reply = dbus_message_new_method_return(message))) - goto oom; - - dbus_message_iter_init_append(reply, &iter); - - if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub)) - goto oom; - - for (p = properties; p->property; p++) { - if (interface[0] && !streq(p->interface, interface)) - continue; - - if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_DICT_ENTRY, NULL, &sub2) || - !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &p->property) || - !dbus_message_iter_open_container(&sub2, DBUS_TYPE_VARIANT, p->signature, &sub3)) - goto oom; - - if ((r = p->append(m, &sub3, p->property, (void*) p->data)) < 0) { - - if (r == -ENOMEM) - goto oom; - - dbus_message_unref(reply); - return bus_send_error_reply(m, c, message, NULL, r); - } - - if (!dbus_message_iter_close_container(&sub2, &sub3) || - !dbus_message_iter_close_container(&sub, &sub2)) - goto oom; - } - - if (!dbus_message_iter_close_container(&iter, &sub)) - goto oom; - - } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Set") && properties) { - const char *interface, *property; - DBusMessageIter iter; - const BusProperty *p; - - if (!dbus_message_iter_init(message, &iter) || - dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING) - return bus_send_error_reply(m, c, message, NULL, -EINVAL); - - dbus_message_iter_get_basic(&iter, &interface); - - if (!dbus_message_iter_next(&iter) || - dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING) - return bus_send_error_reply(m, c, message, NULL, -EINVAL); - - dbus_message_iter_get_basic(&iter, &property); - - if (!dbus_message_iter_next(&iter) || - dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT || - dbus_message_iter_has_next(&iter)) - return bus_send_error_reply(m, c, message, NULL, -EINVAL); - - for (p = properties; p->property; p++) - if (streq(p->interface, interface) && streq(p->property, property)) - break; - - if (p->set) { - DBusMessageIter sub; - char *sig; - - dbus_message_iter_recurse(&iter, &sub); - - if (!(sig = dbus_message_iter_get_signature(&sub))) - goto oom; - - if (!streq(sig, p->signature)) { - dbus_free(sig); - return bus_send_error_reply(m, c, message, NULL, -EINVAL); - } - - dbus_free(sig); - - if ((r = p->set(m, &sub, property)) < 0) { - if (r == -ENOMEM) - goto oom; - return bus_send_error_reply(m, c, message, NULL, r); - } - - if (!(reply = dbus_message_new_method_return(message))) - goto oom; - } else { - if (p->property) - dbus_set_error_const(&error, DBUS_ERROR_PROPERTY_READ_ONLY, "Property read-only"); - else if (!nulstr_contains(interfaces, interface)) - dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface"); - else - dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_PROPERTY, "Unknown property"); - - return bus_send_error_reply(m, c, message, &error, -EINVAL); - } - - } else if (!nulstr_contains(interfaces, dbus_message_get_interface(message))) { - dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface"); - return bus_send_error_reply(m, c, message, &error, -EINVAL); - } - - if (reply) { - if (!dbus_connection_send(c, reply, NULL)) - goto oom; - - dbus_message_unref(reply); - return DBUS_HANDLER_RESULT_HANDLED; - } - - return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; - -oom: - if (reply) - dbus_message_unref(reply); - - dbus_error_free(&error); - - return DBUS_HANDLER_RESULT_NEED_MEMORY; -} - -static const char *error_to_dbus(int error) { - - switch(error) { - - case -EINVAL: - return DBUS_ERROR_INVALID_ARGS; - - case -ENOMEM: - return DBUS_ERROR_NO_MEMORY; - - case -EPERM: - case -EACCES: - return DBUS_ERROR_ACCESS_DENIED; - - case -ESRCH: - return DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN; - - case -ENOENT: - return DBUS_ERROR_FILE_NOT_FOUND; - - case -EEXIST: - return DBUS_ERROR_FILE_EXISTS; - - case -ETIMEDOUT: - case -ETIME: - return DBUS_ERROR_TIMEOUT; - - case -EIO: - return DBUS_ERROR_IO_ERROR; - - case -ENETRESET: - case -ECONNABORTED: - case -ECONNRESET: - return DBUS_ERROR_DISCONNECTED; - } - - return DBUS_ERROR_FAILED; -} - -DBusHandlerResult bus_send_error_reply(Manager *m, DBusConnection *c, DBusMessage *message, DBusError *berror, int error) { - DBusMessage *reply = NULL; - const char *name, *text; - - if (berror && dbus_error_is_set(berror)) { - name = berror->name; - text = berror->message; - } else { - name = error_to_dbus(error); - text = strerror(-error); - } - - if (!(reply = dbus_message_new_error(message, name, text))) - goto oom; - - if (!dbus_connection_send(c, reply, NULL)) - goto oom; - - dbus_message_unref(reply); - - if (berror) - dbus_error_free(berror); - - return DBUS_HANDLER_RESULT_HANDLED; - -oom: - if (reply) - dbus_message_unref(reply); - - if (berror) - dbus_error_free(berror); - - return DBUS_HANDLER_RESULT_NEED_MEMORY; -} - int bus_broadcast(Manager *m, DBusMessage *message) { bool oom = false; Iterator i; @@ -1516,140 +1233,6 @@ int bus_broadcast(Manager *m, DBusMessage *message) { return oom ? -ENOMEM : 0; } -int bus_property_append_string(Manager *m, DBusMessageIter *i, const char *property, void *data) { - const char *t = data; - - assert(m); - assert(i); - assert(property); - - if (!t) - t = ""; - - if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t)) - return -ENOMEM; - - return 0; -} - -int bus_property_append_strv(Manager *m, DBusMessageIter *i, const char *property, void *data) { - DBusMessageIter sub; - char **t = data; - - assert(m); - assert(i); - assert(property); - - if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub)) - return -ENOMEM; - - STRV_FOREACH(t, t) - if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t)) - return -ENOMEM; - - if (!dbus_message_iter_close_container(i, &sub)) - return -ENOMEM; - - return 0; -} - -int bus_property_append_bool(Manager *m, DBusMessageIter *i, const char *property, void *data) { - bool *b = data; - dbus_bool_t db; - - assert(m); - assert(i); - assert(property); - assert(b); - - db = *b; - - if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &db)) - return -ENOMEM; - - return 0; -} - -int bus_property_append_uint64(Manager *m, DBusMessageIter *i, const char *property, void *data) { - assert(m); - assert(i); - assert(property); - assert(data); - - /* Let's ensure that pid_t is actually 64bit, and hence this - * function can be used for usec_t */ - assert_cc(sizeof(uint64_t) == sizeof(usec_t)); - - if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, data)) - return -ENOMEM; - - return 0; -} - -int bus_property_append_uint32(Manager *m, DBusMessageIter *i, const char *property, void *data) { - assert(m); - assert(i); - assert(property); - assert(data); - - /* Let's ensure that pid_t and mode_t is actually 32bit, and - * hence this function can be used for pid_t/mode_t */ - assert_cc(sizeof(uint32_t) == sizeof(pid_t)); - assert_cc(sizeof(uint32_t) == sizeof(mode_t)); - assert_cc(sizeof(uint32_t) == sizeof(unsigned)); - - if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, data)) - return -ENOMEM; - - return 0; -} - -int bus_property_append_int32(Manager *m, DBusMessageIter *i, const char *property, void *data) { - assert(m); - assert(i); - assert(property); - assert(data); - - assert_cc(sizeof(int32_t) == sizeof(int)); - - if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, data)) - return -ENOMEM; - - return 0; -} - -int bus_property_append_size(Manager *m, DBusMessageIter *i, const char *property, void *data) { - uint64_t u; - - assert(m); - assert(i); - assert(property); - assert(data); - - u = (uint64_t) *(size_t*) data; - - if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u)) - return -ENOMEM; - - return 0; -} - -int bus_property_append_ul(Manager *m, DBusMessageIter *i, const char *property, void *data) { - uint64_t u; - - assert(m); - assert(i); - assert(property); - assert(data); - - u = (uint64_t) *(unsigned long*) data; - - if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u)) - return -ENOMEM; - - return 0; -} - int bus_parse_strv(DBusMessage *m, char ***_l) { DBusMessageIter iter, sub; unsigned n = 0, i = 0; @@ -1722,41 +1305,3 @@ bool bus_connection_has_subscriber(Manager *m, DBusConnection *c) { return !set_isempty(BUS_CONNECTION_SUBSCRIBED(m, c)); } - -DBusMessage* bus_properties_changed_new(const char *path, const char *interface, const char *properties) { - DBusMessage *m; - DBusMessageIter iter, sub; - const char *i; - - assert(interface); - assert(properties); - - if (!(m = dbus_message_new_signal(path, "org.freedesktop.DBus.Properties", "PropertiesChanged"))) - goto oom; - - dbus_message_iter_init_append(m, &iter); - - /* We won't send any property values, since they might be - * large and sometimes not cheap to generated */ - - if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &interface) || - !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub) || - !dbus_message_iter_close_container(&iter, &sub) || - !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) - goto oom; - - NULSTR_FOREACH(i, properties) - if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &i)) - goto oom; - - if (!dbus_message_iter_close_container(&iter, &sub)) - goto oom; - - return m; - -oom: - if (m) - dbus_message_unref(m); - - return NULL; -} diff --git a/src/dbus.h b/src/dbus.h index f93ad620..57a2b388 100644 --- a/src/dbus.h +++ b/src/dbus.h @@ -24,79 +24,8 @@ #include -#ifndef DBUS_ERROR_UNKNOWN_OBJECT -#define DBUS_ERROR_UNKNOWN_OBJECT "org.freedesktop.DBus.Error.UnknownObject" -#endif - -#ifndef DBUS_ERROR_UNKNOWN_INTERFACE -#define DBUS_ERROR_UNKNOWN_INTERFACE "org.freedesktop.DBus.Error.UnknownInterface" -#endif - -#ifndef DBUS_ERROR_UNKNOWN_PROPERTY -#define DBUS_ERROR_UNKNOWN_PROPERTY "org.freedesktop.DBus.Error.UnknownProperty" -#endif - -#ifndef DBUS_ERROR_PROPERTY_READ_ONLY -#define DBUS_ERROR_PROPERTY_READ_ONLY "org.freedesktop.DBus.Error.PropertyReadOnly" -#endif - #include "manager.h" -typedef int (*BusPropertyCallback)(Manager *m, DBusMessageIter *iter, const char *property, void *data); -typedef int (*BusPropertySetCallback)(Manager *m, DBusMessageIter *iter, const char *property); - -typedef struct BusProperty { - const char *interface; /* interface of the property */ - const char *property; /* name of the property */ - BusPropertyCallback append; /* Function that is called to serialize this property */ - const char *signature; - const void *data; /* The data of this property */ - BusPropertySetCallback set; /* Function that is called to set this property */ -} BusProperty; - -#define BUS_PROPERTIES_INTERFACE \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" - -#define BUS_INTROSPECTABLE_INTERFACE \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" - -#define BUS_PEER_INTERFACE \ - "\n" \ - " \n" \ - " \n" \ - " \n" \ - " \n" \ - "\n" - -#define BUS_GENERIC_INTERFACES_LIST \ - "org.freedesktop.DBus.Properties\0" \ - "org.freedesktop.DBus.Introspectable\0" \ - "org.freedesktop.DBus.Peer\0" - int bus_init(Manager *m, bool try_bus_connect); void bus_done(Manager *m); @@ -107,50 +36,13 @@ void bus_timeout_event(Manager *m, Watch *w, int events); int bus_query_pid(Manager *m, const char *name); -DBusHandlerResult bus_default_message_handler(Manager *m, DBusConnection *c, DBusMessage *message, const char* introspection, const char *interfaces, const BusProperty *properties); -DBusHandlerResult bus_send_error_reply(Manager *m, DBusConnection *c, DBusMessage *message, DBusError *bus_error, int error); - int bus_broadcast(Manager *m, DBusMessage *message); -int bus_property_append_string(Manager *m, DBusMessageIter *i, const char *property, void *data); -int bus_property_append_strv(Manager *m, DBusMessageIter *i, const char *property, void *data); -int bus_property_append_bool(Manager *m, DBusMessageIter *i, const char *property, void *data); -int bus_property_append_int32(Manager *m, DBusMessageIter *i, const char *property, void *data); -int bus_property_append_uint32(Manager *m, DBusMessageIter *i, const char *property, void *data); -int bus_property_append_uint64(Manager *m, DBusMessageIter *i, const char *property, void *data); -int bus_property_append_size(Manager *m, DBusMessageIter *i, const char *property, void *data); -int bus_property_append_ul(Manager *m, DBusMessageIter *i, const char *property, void *data); - -#define bus_property_append_int bus_property_append_int32 -#define bus_property_append_pid bus_property_append_uint32 -#define bus_property_append_mode bus_property_append_uint32 -#define bus_property_append_unsigned bus_property_append_uint32 -#define bus_property_append_usec bus_property_append_uint64 - -#define DEFINE_BUS_PROPERTY_APPEND_ENUM(function,name,type) \ - int function(Manager *m, DBusMessageIter *i, const char *property, void *data) { \ - const char *value; \ - type *field = data; \ - \ - assert(m); \ - assert(i); \ - assert(property); \ - \ - value = name##_to_string(*field); \ - \ - if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &value)) \ - return -ENOMEM; \ - \ - return 0; \ - } - int bus_parse_strv(DBusMessage *m, char ***_l); bool bus_has_subscriber(Manager *m); bool bus_connection_has_subscriber(Manager *m, DBusConnection *c); -DBusMessage* bus_properties_changed_new(const char *path, const char *interface, const char *properties); - #define BUS_CONNECTION_SUBSCRIBED(m, c) dbus_connection_get_data((c), (m)->subscribed_data_slot) #define BUS_PENDING_CALL_NAME(m, p) dbus_pending_call_get_data((p), (m)->name_data_slot)