chiark / gitweb /
add org.freedesktop.DBus.Properies.Set method
[elogind.git] / src / dbus.c
index 952806bb2ac9feab08a5161d4687534abf784c09..5a4750d6b9dd6750a2c91b8d002e63de48f62c85 100644 (file)
@@ -43,6 +43,7 @@
 #include "dbus-timer.h"
 #include "dbus-path.h"
 #include "bus-errors.h"
+#include "special.h"
 
 #define CONNECTIONS_MAX 52
 
@@ -383,18 +384,24 @@ static DBusHandlerResult api_bus_message_filter(DBusConnection *connection, DBus
 
                         log_debug("Got D-Bus activation request for %s", name);
 
-                        r = manager_load_unit(m, name, NULL, &error, &u);
+                        if (manager_unit_pending_inactive(m, SPECIAL_DBUS_SERVICE) ||
+                            manager_unit_pending_inactive(m, SPECIAL_DBUS_SOCKET)) {
+                                r = -EADDRNOTAVAIL;
+                                dbus_set_error(&error, BUS_ERROR_SHUTTING_DOWN, "Refusing activation, D-Bus is shutting down.");
+                        } else {
+                                r = manager_load_unit(m, name, NULL, &error, &u);
 
-                        if (r >= 0 && u->meta.refuse_manual_start)
-                                r = -EPERM;
+                                if (r >= 0 && u->meta.refuse_manual_start)
+                                        r = -EPERM;
 
-                        if (r >= 0)
-                                r = manager_add_job(m, JOB_START, u, JOB_REPLACE, true, &error, NULL);
+                                if (r >= 0)
+                                        r = manager_add_job(m, JOB_START, u, JOB_REPLACE, true, &error, NULL);
+                        }
 
                         if (r < 0) {
                                 const char *id, *text;
 
-                                log_warning("D-Bus activation failed for %s: %s", name, strerror(-r));
+                                log_debug("D-Bus activation failed for %s: %s", name, strerror(-r));
 
                                 if (!(reply = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Activator", "ActivationFailure")))
                                         goto oom;
@@ -411,7 +418,7 @@ static DBusHandlerResult api_bus_message_filter(DBusConnection *connection, DBus
                                         goto oom;
                         }
 
-                        /* On success we don't do anything, the service will be spwaned now */
+                        /* On success we don't do anything, the service will be spawned now */
                 }
         }
 
@@ -457,7 +464,9 @@ static DBusHandlerResult system_bus_message_filter(DBusConnection *connection, D
                 log_debug("System D-Bus connection terminated.");
                 bus_done_system(m);
 
-        } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) {
+        } else if (m->running_as != MANAGER_SYSTEM &&
+                   dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) {
+
                 const char *cgroup;
 
                 if (!dbus_message_get_args(message, &error,
@@ -491,7 +500,9 @@ static DBusHandlerResult private_bus_message_filter(DBusConnection *connection,
 
         if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected"))
                 shutdown_connection(m, connection);
-        else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) {
+        else if (m->running_as == MANAGER_SYSTEM &&
+                 dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) {
+
                 const char *cgroup;
 
                 if (!dbus_message_get_args(message, &error,
@@ -500,6 +511,12 @@ static DBusHandlerResult private_bus_message_filter(DBusConnection *connection,
                         log_error("Failed to parse Released message: %s", error.message);
                 else
                         cgroup_notify_empty(m, cgroup);
+
+                /* Forward the message to the system bus, so that user
+                 * instances are notified as well */
+
+                if (m->system_bus)
+                        dbus_connection_send(m->system_bus, message, NULL);
         }
 
         dbus_error_free(&error);
@@ -801,17 +818,19 @@ static int bus_init_system(Manager *m) {
                 goto fail;
         }
 
-        dbus_bus_add_match(m->system_bus,
-                           "type='signal',"
-                           "interface='org.freedesktop.systemd1.Agent',"
-                           "member='Released',"
-                           "path='/org/freedesktop/systemd1/agent'",
-                           &error);
-
-        if (dbus_error_is_set(&error)) {
-                log_error("Failed to register match: %s", error.message);
-                r = -EIO;
-                goto fail;
+        if (m->running_as != MANAGER_SYSTEM) {
+                dbus_bus_add_match(m->system_bus,
+                                   "type='signal',"
+                                   "interface='org.freedesktop.systemd1.Agent',"
+                                   "member='Released',"
+                                   "path='/org/freedesktop/systemd1/agent'",
+                                   &error);
+
+                if (dbus_error_is_set(&error)) {
+                        log_error("Failed to register match: %s", error.message);
+                        r = -EIO;
+                        goto fail;
+                }
         }
 
         if (m->api_bus != m->system_bus) {
@@ -845,7 +864,7 @@ static int bus_init_api(Manager *m) {
         if (m->running_as == MANAGER_SYSTEM && m->system_bus)
                 m->api_bus = m->system_bus;
         else {
-                if (!(m->api_bus = dbus_bus_get_private(m->running_as == MANAGER_SESSION ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &error))) {
+                if (!(m->api_bus = dbus_bus_get_private(m->running_as == MANAGER_USER ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &error))) {
                         log_debug("Failed to get API D-Bus connection, retrying later: %s", error.message);
                         r = 0;
                         goto fail;
@@ -963,7 +982,7 @@ fail:
         return r;
 }
 
-int bus_init(Manager *m) {
+int bus_init(Manager *m, bool try_bus_connect) {
         int r;
 
         if (set_ensure_allocated(&m->bus_connections, trivial_hash_func, trivial_compare_func) < 0 ||
@@ -979,14 +998,18 @@ int bus_init(Manager *m) {
                 }
 
         if (m->subscribed_data_slot < 0)
-                if (!dbus_pending_call_allocate_data_slot(&m->subscribed_data_slot)) {
+                if (!dbus_connection_allocate_data_slot(&m->subscribed_data_slot)) {
                         log_error("Not enough memory");
                         return -ENOMEM;
                 }
 
-        if ((r = bus_init_system(m)) < 0 ||
-            (r = bus_init_api(m)) < 0 ||
-            (r = bus_init_private(m)) < 0)
+        if (try_bus_connect) {
+                if ((r = bus_init_system(m)) < 0 ||
+                    (r = bus_init_api(m)) < 0)
+                        return r;
+        }
+
+        if ((r = bus_init_private(m)) < 0)
                 return r;
 
         return 0;
@@ -1091,7 +1114,7 @@ void bus_done(Manager *m) {
                dbus_pending_call_free_data_slot(&m->name_data_slot);
 
         if (m->subscribed_data_slot >= 0)
-                dbus_pending_call_free_data_slot(&m->subscribed_data_slot);
+                dbus_connection_free_data_slot(&m->subscribed_data_slot);
 }
 
 static void query_pid_pending_cb(DBusPendingCall *pending, void *userdata) {
@@ -1293,6 +1316,58 @@ DBusHandlerResult bus_default_message_handler(Manager *m, DBusConnection *c, DBu
 
                 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
+                        return bus_send_error_reply(m, c, message, NULL, -EINVAL);
         }
 
         if (reply) {
@@ -1338,6 +1413,7 @@ static const char *error_to_dbus(int error) {
                 return DBUS_ERROR_FILE_EXISTS;
 
         case -ETIMEDOUT:
+        case -ETIME:
                 return DBUS_ERROR_TIMEOUT;
 
         case -EIO: