daandemeyer / rpms / systemd

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