9fc0f6
From 1c961f8bf67a798d51d88195f89d035012a681d4 Mon Sep 17 00:00:00 2001
9fc0f6
From: Michal Sekletar <msekleta@redhat.com>
9fc0f6
Date: Thu, 27 Feb 2014 17:56:16 +0100
9fc0f6
Subject: [PATCH] core: introduce new stop protocol for unit scopes
9fc0f6
9fc0f6
By specifiy a Controller property when creating the scope a client can
9fc0f6
specify a bus name that will be notified with a RequestStop bus signal
9fc0f6
when the scope has been asked to shut down, instead of sending SIGTERM
9fc0f6
to the scope processes themselves.
9fc0f6
9fc0f6
https://bugzilla.redhat.com/show_bug.cgi?id=1032695
9fc0f6
9fc0f6
Based-on: 2d4a39e759c4ab846ad8a546abeddd40bc8d736e
9fc0f6
---
9fc0f6
 src/core/dbus-scope.c    | 55 ++++++++++++++++++++++++++++++++++++++++++++++++
9fc0f6
 src/core/dbus-scope.h    |  2 ++
9fc0f6
 src/core/scope.c         | 20 +++++++++++++++---
9fc0f6
 src/core/scope.h         |  2 ++
9fc0f6
 src/run/run.c            |  8 +++++++
9fc0f6
 src/shared/dbus-common.c | 42 ++++++++++++++++++++++++++++++++++++
9fc0f6
 src/shared/dbus-common.h |  2 ++
9fc0f6
 7 files changed, 128 insertions(+), 3 deletions(-)
9fc0f6
9fc0f6
diff --git a/src/core/dbus-scope.c b/src/core/dbus-scope.c
9fc0f6
index 783a969..b576f76 100644
9fc0f6
--- a/src/core/dbus-scope.c
9fc0f6
+++ b/src/core/dbus-scope.c
9fc0f6
@@ -31,10 +31,12 @@
9fc0f6
 #define BUS_SCOPE_INTERFACE                                             \
9fc0f6
         " <interface name=\"org.freedesktop.systemd1.Scope\">\n"        \
9fc0f6
         BUS_UNIT_CGROUP_INTERFACE                                       \
9fc0f6
+        "  <property name=\"Controller\" type=\"s\" access=\"read\"/>\n"\
9fc0f6
         "  <property name=\"TimeoutStopUSec\" type=\"t\" access=\"read\"/>\n" \
9fc0f6
         BUS_KILL_CONTEXT_INTERFACE                                      \
9fc0f6
         BUS_CGROUP_CONTEXT_INTERFACE                                    \
9fc0f6
         "  <property name=\"Result\" type=\"s\" access=\"read\"/>\n"    \
9fc0f6
+        "  <signal name=\"RequestStop\"/>\n"                            \
9fc0f6
         " </interface>\n"
9fc0f6
 
9fc0f6
 #define INTROSPECTION                                                   \
9fc0f6
@@ -56,6 +58,7 @@ const char bus_scope_interface[] _introspect_("Scope") = BUS_SCOPE_INTERFACE;
9fc0f6
 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_scope_append_scope_result, scope_result, ScopeResult);
9fc0f6
 
9fc0f6
 static const BusProperty bus_scope_properties[] = {
9fc0f6
+        { "Controller",             bus_property_append_string,    "s", offsetof(Scope, controller)        },
9fc0f6
         { "TimeoutStopUSec",        bus_property_append_usec,      "t", offsetof(Scope, timeout_stop_usec) },
9fc0f6
         { "Result",                 bus_scope_append_scope_result, "s", offsetof(Scope, result)            },
9fc0f6
         {}
9fc0f6
@@ -127,6 +130,31 @@ static int bus_scope_set_transient_property(
9fc0f6
 
9fc0f6
                 return 1;
9fc0f6
 
9fc0f6
+        } else if (streq(name, "Controller")) {
9fc0f6
+                const char *controller;
9fc0f6
+
9fc0f6
+                if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_STRING)
9fc0f6
+                        return -EINVAL;
9fc0f6
+
9fc0f6
+                dbus_message_iter_get_basic(i, &controller);
9fc0f6
+
9fc0f6
+                if (!isempty(controller) && !bus_service_name_is_valid(controller))
9fc0f6
+                        return -EINVAL;
9fc0f6
+
9fc0f6
+                if (mode != UNIT_CHECK) {
9fc0f6
+                        char *c = NULL;
9fc0f6
+
9fc0f6
+                        if (!isempty(controller)) {
9fc0f6
+                                c = strdup(controller);
9fc0f6
+                                if (!c)
9fc0f6
+                                        return -ENOMEM;
9fc0f6
+                        }
9fc0f6
+
9fc0f6
+                        free(s->controller);
9fc0f6
+                        s->controller = c;
9fc0f6
+                }
9fc0f6
+
9fc0f6
+                return 1;
9fc0f6
         } else if (streq(name, "TimeoutStopUSec")) {
9fc0f6
 
9fc0f6
                 if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_UINT64)
9fc0f6
@@ -187,3 +215,30 @@ int bus_scope_commit_properties(Unit *u) {
9fc0f6
         unit_realize_cgroup(u);
9fc0f6
         return 0;
9fc0f6
 }
9fc0f6
+
9fc0f6
+int bus_scope_send_request_stop(Scope *s) {
9fc0f6
+        _cleanup_dbus_message_unref_ DBusMessage *m = NULL;
9fc0f6
+        _cleanup_free_ char *p = NULL;
9fc0f6
+        int r;
9fc0f6
+
9fc0f6
+        assert(s);
9fc0f6
+
9fc0f6
+        if (!s->controller)
9fc0f6
+                return 0;
9fc0f6
+
9fc0f6
+        p = unit_dbus_path(UNIT(s));
9fc0f6
+        if (!p)
9fc0f6
+                return -ENOMEM;
9fc0f6
+
9fc0f6
+        m = dbus_message_new_signal(p,
9fc0f6
+                                    "org.freedesktop.systemd1.Scope",
9fc0f6
+                                    "RequestStop");
9fc0f6
+        if (!m)
9fc0f6
+                return 0;
9fc0f6
+
9fc0f6
+        r = dbus_message_set_destination(m, s->controller);
9fc0f6
+        if (!r)
9fc0f6
+                return 0;
9fc0f6
+
9fc0f6
+        return dbus_connection_send(UNIT(s)->manager->api_bus, m, NULL);
9fc0f6
+}
9fc0f6
diff --git a/src/core/dbus-scope.h b/src/core/dbus-scope.h
9fc0f6
index e6836f1..34720f2 100644
9fc0f6
--- a/src/core/dbus-scope.h
9fc0f6
+++ b/src/core/dbus-scope.h
9fc0f6
@@ -30,4 +30,6 @@ DBusHandlerResult bus_scope_message_handler(Unit *u, DBusConnection *c, DBusMess
9fc0f6
 int bus_scope_set_property(Unit *u, const char *name, DBusMessageIter *i, UnitSetPropertiesMode mode, DBusError *error);
9fc0f6
 int bus_scope_commit_properties(Unit *u);
9fc0f6
 
9fc0f6
+int bus_scope_send_request_stop(Scope *s);
9fc0f6
+
9fc0f6
 extern const char bus_scope_interface[];
9fc0f6
diff --git a/src/core/scope.c b/src/core/scope.c
9fc0f6
index 41da3b9..e75fc2b 100644
9fc0f6
--- a/src/core/scope.c
9fc0f6
+++ b/src/core/scope.c
9fc0f6
@@ -64,6 +64,9 @@ static void scope_done(Unit *u) {
9fc0f6
 
9fc0f6
         cgroup_context_done(&s->cgroup_context);
9fc0f6
 
9fc0f6
+        free(s->controller);
9fc0f6
+        s->controller = NULL;
9fc0f6
+
9fc0f6
         set_free(s->pids);
9fc0f6
         s->pids = NULL;
9fc0f6
 
9fc0f6
@@ -198,6 +201,7 @@ static void scope_enter_dead(Scope *s, ScopeResult f) {
9fc0f6
 }
9fc0f6
 
9fc0f6
 static void scope_enter_signal(Scope *s, ScopeState state, ScopeResult f) {
9fc0f6
+        bool skip_signal = false;
9fc0f6
         int r;
9fc0f6
 
9fc0f6
         assert(s);
9fc0f6
@@ -205,13 +209,23 @@ static void scope_enter_signal(Scope *s, ScopeState state, ScopeResult f) {
9fc0f6
         if (f != SCOPE_SUCCESS)
9fc0f6
                 s->result = f;
9fc0f6
 
9fc0f6
-        r = unit_kill_context(
9fc0f6
+        /* If we have a controller set let's ask the controller nicely
9fc0f6
+         * to terminate the scope, instead of us going directly into
9fc0f6
+         * SIGTERM beserk mode */
9fc0f6
+        if (state == SCOPE_STOP_SIGTERM)
9fc0f6
+                skip_signal = bus_scope_send_request_stop(s) > 0;
9fc0f6
+
9fc0f6
+        if (!skip_signal) {
9fc0f6
+                r = unit_kill_context(
9fc0f6
                         UNIT(s),
9fc0f6
                         &s->kill_context,
9fc0f6
                         state != SCOPE_STOP_SIGTERM,
9fc0f6
                         -1, -1, false);
9fc0f6
-        if (r < 0)
9fc0f6
-                goto fail;
9fc0f6
+
9fc0f6
+                if (r < 0)
9fc0f6
+                        goto fail;
9fc0f6
+        } else
9fc0f6
+                r = 1;
9fc0f6
 
9fc0f6
         if (r > 0) {
9fc0f6
                 if (s->timeout_stop_usec > 0) {
9fc0f6
diff --git a/src/core/scope.h b/src/core/scope.h
9fc0f6
index 2a3dcb7..b4bafa7 100644
9fc0f6
--- a/src/core/scope.h
9fc0f6
+++ b/src/core/scope.h
9fc0f6
@@ -55,6 +55,8 @@ struct Scope {
9fc0f6
 
9fc0f6
         usec_t timeout_stop_usec;
9fc0f6
 
9fc0f6
+        char *controller;
9fc0f6
+
9fc0f6
         Set *pids;
9fc0f6
 
9fc0f6
         Watch timer_watch;
9fc0f6
diff --git a/src/run/run.c b/src/run/run.c
9fc0f6
index a6abead..93e3f88 100644
9fc0f6
--- a/src/run/run.c
9fc0f6
+++ b/src/run/run.c
9fc0f6
@@ -315,6 +315,14 @@ static int start_transient_scope(
9fc0f6
         if (r < 0)
9fc0f6
                 return r;
9fc0f6
 
9fc0f6
+        {
9fc0f6
+                const char *unique_id;
9fc0f6
+                sd_bus_get_unique_name(bus, &unique_id);
9fc0f6
+                r = sd_bus_message_append(m, "(sv)", "Controller", "s", unique_id);
9fc0f6
+                if (r < 0)
9fc0f6
+                        return r;
9fc0f6
+        }
9fc0f6
+
9fc0f6
         r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, (uint32_t) getpid());
9fc0f6
         if (r < 0)
9fc0f6
                 return r;
9fc0f6
diff --git a/src/shared/dbus-common.c b/src/shared/dbus-common.c
9fc0f6
index 3ba2d87..8a68708 100644
9fc0f6
--- a/src/shared/dbus-common.c
9fc0f6
+++ b/src/shared/dbus-common.c
9fc0f6
@@ -1428,3 +1428,45 @@ const char *bus_message_get_sender_with_fallback(DBusMessage *m) {
9fc0f6
 
9fc0f6
         return ":no-sender";
9fc0f6
 }
9fc0f6
+
9fc0f6
+bool bus_service_name_is_valid(const char *p) {
9fc0f6
+        const char *q;
9fc0f6
+        bool dot, found_dot = false, unique;
9fc0f6
+
9fc0f6
+        if (isempty(p))
9fc0f6
+                return false;
9fc0f6
+
9fc0f6
+        unique = p[0] == ':';
9fc0f6
+
9fc0f6
+        for (dot = true, q = unique ? p+1 : p; *q; q++)
9fc0f6
+                if (*q == '.') {
9fc0f6
+                        if (dot)
9fc0f6
+                                return false;
9fc0f6
+
9fc0f6
+                        found_dot = dot = true;
9fc0f6
+                } else {
9fc0f6
+                        bool good;
9fc0f6
+
9fc0f6
+                        good =
9fc0f6
+                                (*q >= 'a' && *q <= 'z') ||
9fc0f6
+                                (*q >= 'A' && *q <= 'Z') ||
9fc0f6
+                                ((!dot || unique) && *q >= '0' && *q <= '9') ||
9fc0f6
+                                *q == '_' || *q == '-';
9fc0f6
+
9fc0f6
+                        if (!good)
9fc0f6
+                                return false;
9fc0f6
+
9fc0f6
+                        dot = false;
9fc0f6
+                }
9fc0f6
+
9fc0f6
+        if (q - p > 255)
9fc0f6
+                return false;
9fc0f6
+
9fc0f6
+        if (dot)
9fc0f6
+                return false;
9fc0f6
+
9fc0f6
+        if (!found_dot)
9fc0f6
+                return false;
9fc0f6
+
9fc0f6
+        return true;
9fc0f6
+}
9fc0f6
diff --git a/src/shared/dbus-common.h b/src/shared/dbus-common.h
9fc0f6
index 9752f08..8d01d14 100644
9fc0f6
--- a/src/shared/dbus-common.h
9fc0f6
+++ b/src/shared/dbus-common.h
9fc0f6
@@ -242,5 +242,7 @@ const char *bus_message_get_sender_with_fallback(DBusMessage *m);
9fc0f6
 
9fc0f6
 void bus_message_unrefp(DBusMessage **reply);
9fc0f6
 
9fc0f6
+bool bus_service_name_is_valid(const char *p);
9fc0f6
+
9fc0f6
 #define _cleanup_dbus_message_unref_ __attribute__((cleanup(bus_message_unrefp)))
9fc0f6
 #define _cleanup_dbus_error_free_ __attribute__((cleanup(dbus_error_free)))