chiark / gitweb /
dbus: log received message only once, in filter function
[elogind.git] / src / dbus-unit.c
index ba428a8..85125c9 100644 (file)
@@ -25,6 +25,8 @@
 #include "log.h"
 #include "dbus-unit.h"
 
+const char bus_unit_interface[] = BUS_UNIT_INTERFACE;
+
 int bus_unit_append_names(Manager *m, DBusMessageIter *i, const char *property, void *data) {
         char *t;
         Iterator j;
@@ -253,11 +255,12 @@ int bus_unit_append_cgroups(Manager *m, DBusMessageIter *i, const char *property
 
 DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_unit_append_kill_mode, kill_mode, KillMode);
 
-static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusMessage *message) {
+static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusConnection *connection, DBusMessage *message) {
         DBusMessage *reply = NULL;
         Manager *m = u->meta.manager;
         DBusError error;
         JobType job_type = _JOB_TYPE_INVALID;
+        char *path = NULL;
 
         dbus_error_init(&error);
 
@@ -269,8 +272,10 @@ static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusMessage *message
                 job_type = JOB_RELOAD;
         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Restart"))
                 job_type = JOB_RESTART;
+        else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "TryRestart"))
+                job_type = JOB_TRY_RESTART;
         else if (UNIT_VTABLE(u)->bus_message_handler)
-                return UNIT_VTABLE(u)->bus_message_handler(u, message);
+                return UNIT_VTABLE(u)->bus_message_handler(u, connection, message);
         else
                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
 
@@ -279,23 +284,22 @@ static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusMessage *message
                 JobMode mode;
                 Job *j;
                 int r;
-                char *path;
 
                 if (job_type == JOB_START && u->meta.only_by_dependency)
-                        return bus_send_error_reply(m, message, NULL, -EPERM);
+                        return bus_send_error_reply(m, connection, message, NULL, -EPERM);
 
                 if (!dbus_message_get_args(
                                     message,
                                     &error,
                                     DBUS_TYPE_STRING, &smode,
                                     DBUS_TYPE_INVALID))
-                        return bus_send_error_reply(m, message, &error, -EINVAL);
+                        return bus_send_error_reply(m, connection, message, &error, -EINVAL);
 
                 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID)
-                        return bus_send_error_reply(m, message, NULL, -EINVAL);
+                        return bus_send_error_reply(m, connection, message, NULL, -EINVAL);
 
                 if ((r = manager_add_job(m, job_type, u, mode, true, &j)) < 0)
-                        return bus_send_error_reply(m, message, NULL, r);
+                        return bus_send_error_reply(m, connection, message, NULL, r);
 
                 if (!(reply = dbus_message_new_method_return(message)))
                         goto oom;
@@ -310,8 +314,10 @@ static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusMessage *message
                         goto oom;
         }
 
+        free(path);
+
         if (reply) {
-                if (!dbus_connection_send(m->api_bus, reply, NULL))
+                if (!dbus_connection_send(connection, reply, NULL))
                         goto oom;
 
                 dbus_message_unref(reply);
@@ -320,6 +326,8 @@ static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusMessage *message
         return DBUS_HANDLER_RESULT_HANDLED;
 
 oom:
+        free(path);
+
         if (reply)
                 dbus_message_unref(reply);
 
@@ -328,7 +336,7 @@ oom:
         return DBUS_HANDLER_RESULT_NEED_MEMORY;
 }
 
-static DBusHandlerResult bus_unit_message_handler(DBusConnection  *connection, DBusMessage  *message, void *data) {
+static DBusHandlerResult bus_unit_message_handler(DBusConnection *connection, DBusMessage  *message, void *data) {
         Manager *m = data;
         Unit *u;
         int r;
@@ -337,11 +345,6 @@ static DBusHandlerResult bus_unit_message_handler(DBusConnection  *connection, D
         assert(message);
         assert(m);
 
-        log_debug("Got D-Bus request: %s.%s() on %s",
-                  dbus_message_get_interface(message),
-                  dbus_message_get_member(message),
-                  dbus_message_get_path(message));
-
         if ((r = manager_get_unit_from_dbus_path(m, dbus_message_get_path(message), &u)) < 0) {
 
                 if (r == -ENOMEM)
@@ -350,10 +353,10 @@ static DBusHandlerResult bus_unit_message_handler(DBusConnection  *connection, D
                 if (r == -ENOENT)
                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
 
-                return bus_send_error_reply(m, message, NULL, r);
+                return bus_send_error_reply(m, connection, message, NULL, r);
         }
 
-        return bus_unit_message_dispatch(u, message);
+        return bus_unit_message_dispatch(u, connection, message);
 }
 
 const DBusObjectPathVTable bus_unit_vtable = {
@@ -370,7 +373,7 @@ void bus_unit_send_change_signal(Unit *u) {
         LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
         u->meta.in_dbus_queue = false;
 
-        if (set_isempty(u->meta.manager->subscribed)) {
+        if (!bus_has_subscriber(u->meta.manager)) {
                 u->meta.sent_dbus_new_signal = true;
                 return;
         }
@@ -396,7 +399,7 @@ void bus_unit_send_change_signal(Unit *u) {
                         goto oom;
         }
 
-        if (!dbus_connection_send(u->meta.manager->api_bus, m, NULL))
+        if (bus_broadcast(u->meta.manager, m) < 0)
                 goto oom;
 
         free(p);
@@ -421,7 +424,7 @@ void bus_unit_send_removed_signal(Unit *u) {
 
         assert(u);
 
-        if (set_isempty(u->meta.manager->subscribed))
+        if (!bus_has_subscriber(u->meta.manager))
                 return;
 
         if (!u->meta.sent_dbus_new_signal)
@@ -439,7 +442,7 @@ void bus_unit_send_removed_signal(Unit *u) {
                                       DBUS_TYPE_INVALID))
                 goto oom;
 
-        if (!dbus_connection_send(u->meta.manager->api_bus, m, NULL))
+        if (bus_broadcast(u->meta.manager, m) < 0)
                 goto oom;
 
         free(p);