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