]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core: introduce new stop protocol for unit scopes
authorLennart Poettering <lennart@poettering.net>
Fri, 31 Jan 2014 16:45:13 +0000 (17:45 +0100)
committerLennart Poettering <lennart@poettering.net>
Fri, 31 Jan 2014 16:48:36 +0000 (17:48 +0100)
By specifiy a Controller property when creating the scope a client can
specify a bus name that will be notified with a RequestStop bus signal
when the scope has been asked to shut down, instead of sending SIGTERM
to the scope processes themselves.

https://bugzilla.redhat.com/show_bug.cgi?id=1032695

src/core/dbus-scope.c
src/core/dbus-scope.h
src/core/scope.c
src/core/scope.h
src/run/run.c

index bb251edc76ca11573da3ee5e10b9c9e806da59a6..d5a20485167319ef13ac78c6bd7a41458b9e703a 100644 (file)
 #include "dbus-kill.h"
 #include "dbus-scope.h"
 #include "bus-util.h"
+#include "bus-internal.h"
 
 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_result, scope_result, ScopeResult);
 
 const sd_bus_vtable bus_scope_vtable[] = {
         SD_BUS_VTABLE_START(0),
+        SD_BUS_PROPERTY("Controller", "s", NULL, offsetof(Scope, controller), SD_BUS_VTABLE_PROPERTY_CONST),
         SD_BUS_PROPERTY("TimeoutStopUSec", "t", bus_property_get_usec, offsetof(Scope, timeout_stop_usec), SD_BUS_VTABLE_PROPERTY_CONST),
         SD_BUS_PROPERTY("Result", "s", property_get_result, offsetof(Scope, result), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
+        SD_BUS_SIGNAL("RequestStop", NULL, 0),
         SD_BUS_VTABLE_END
 };
 
@@ -86,6 +89,32 @@ static int bus_scope_set_transient_property(
 
                 return 1;
 
+        } else if (streq(name, "Controller")) {
+                const char *controller;
+                char *c;
+
+                r = sd_bus_message_read(message, "s", &controller);
+                if (r < 0)
+                        return r;
+
+                if (!isempty(controller) && !service_name_is_valid(controller))
+                        return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Controller '%s' is not a valid bus name.", controller);
+
+                if (mode != UNIT_CHECK) {
+                        if (isempty(controller))
+                                c = NULL;
+                        else {
+                                c = strdup(controller);
+                                if (!c)
+                                        return -ENOMEM;
+                        }
+
+                        free(s->controller);
+                        s->controller = c;
+                }
+
+                return 1;
+
         } else if (streq(name, "TimeoutStopUSec")) {
 
                 if (mode != UNIT_CHECK) {
@@ -145,3 +174,29 @@ int bus_scope_commit_properties(Unit *u) {
         unit_realize_cgroup(u);
         return 0;
 }
+
+int bus_scope_send_request_stop(Scope *s) {
+        _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
+        _cleanup_free_ char *p = NULL;
+        int r;
+
+        assert(s);
+
+        if (!s->controller)
+                return 0;
+
+        p = unit_dbus_path(UNIT(s));
+        if (!p)
+                return -ENOMEM;
+
+        r = sd_bus_message_new_signal(
+                        UNIT(s)->manager->api_bus,
+                        p,
+                        "org.freedesktop.systemd1.Scope",
+                        "RequestStop",
+                        &m);
+        if (r < 0)
+                return r;
+
+        return sd_bus_send_to(UNIT(s)->manager->api_bus, m, /* s->controller */ NULL, NULL);
+}
index 7e8f0053da0b1f3961054b70797948aa4b7cb7ad..33beda47b77d46f7043230ecafa3325890b27063 100644 (file)
@@ -28,3 +28,5 @@ extern const sd_bus_vtable bus_scope_vtable[];
 
 int bus_scope_set_property(Unit *u, const char *name, sd_bus_message *i, UnitSetPropertiesMode mode, sd_bus_error *error);
 int bus_scope_commit_properties(Unit *u);
+
+int bus_scope_send_request_stop(Scope *s);
index 5beb4f80c427c65ed8d0588379ef92e34cceb5ed..0c1d17e68c569a2fd5bcfaa1261db29a756875c3 100644 (file)
@@ -64,6 +64,8 @@ static void scope_done(Unit *u) {
 
         cgroup_context_done(&s->cgroup_context);
 
+        free(s->controller);
+
         set_free(s->pids);
         s->pids = NULL;
 
@@ -217,6 +219,7 @@ static void scope_enter_dead(Scope *s, ScopeResult f) {
 }
 
 static void scope_enter_signal(Scope *s, ScopeState state, ScopeResult f) {
+        bool skip_signal = false;
         int r;
 
         assert(s);
@@ -224,13 +227,22 @@ static void scope_enter_signal(Scope *s, ScopeState state, ScopeResult f) {
         if (f != SCOPE_SUCCESS)
                 s->result = f;
 
-        r = unit_kill_context(
-                        UNIT(s),
-                        &s->kill_context,
-                        state != SCOPE_STOP_SIGTERM,
-                        -1, -1, false);
-        if (r < 0)
-                goto fail;
+        /* If we have a controller set let's ask the controller nicely
+         * to terminate the scope, instead of us going directly into
+         * SIGTERM beserk mode */
+        if (state == SCOPE_STOP_SIGTERM)
+                skip_signal = bus_scope_send_request_stop(s) > 0;
+
+        if (!skip_signal) {
+                r = unit_kill_context(
+                                UNIT(s),
+                                &s->kill_context,
+                                state != SCOPE_STOP_SIGTERM,
+                                -1, -1, false);
+                if (r < 0)
+                        goto fail;
+        } else
+                r = 1;
 
         if (r > 0) {
                 r = scope_arm_timer(s);
index 4d8a1714bcc1a9385787bec346469fe07b52598f..014b50c765199f60d74a1dea6000f2fff2c935ad 100644 (file)
@@ -55,6 +55,8 @@ struct Scope {
 
         usec_t timeout_stop_usec;
 
+        char *controller;
+
         Set *pids;
 
         sd_event_source *timer_event_source;
index 1b14e40e4d2e7ed6dd38f545ed19042150e34df6..b9e1700d33da67d75ede3279b71ffcb66fc1841e 100644 (file)
@@ -356,6 +356,14 @@ static int start_transient_scope(
         if (r < 0)
                 return r;
 
+        {
+                const char *unique_id;
+                sd_bus_get_unique_name(bus, &unique_id);
+                r = sd_bus_message_append(m, "(sv)", "Controller", "s", unique_id);
+                if (r < 0)
+                        return r;
+        }
+
         r = message_start_transient_unit_send(bus, m, error, NULL);
         if (r < 0)
                 return r;