ff2b41
From 94355e0146245c14012ac177461110d0a0c65f10 Mon Sep 17 00:00:00 2001
ff2b41
From: Lennart Poettering <lennart@poettering.net>
ff2b41
Date: Wed, 24 Jan 2018 19:54:26 +0100
ff2b41
Subject: [PATCH] core: add a new unit_needs_console() call
ff2b41
ff2b41
This call determines whether a specific unit currently needs access to
ff2b41
the console. It's a fancy wrapper around
ff2b41
exec_context_may_touch_console() ultimately, however for service units
ff2b41
we'll explicitly exclude the SERVICE_EXITED state from when we report
ff2b41
true.
ff2b41
ff2b41
(cherry picked from commit bb2c7685454842549bc1fe47adc35cbca2a84190)
ff2b41
ff2b41
Related: #1524359
ff2b41
---
ff2b41
 src/core/service.c | 27 +++++++++++++++++++++++++++
ff2b41
 src/core/unit.c    | 22 ++++++++++++++++++++++
ff2b41
 src/core/unit.h    |  5 +++++
ff2b41
 3 files changed, 54 insertions(+)
ff2b41
ff2b41
diff --git a/src/core/service.c b/src/core/service.c
ff2b41
index 1ad154e41f..8a8f4be149 100644
ff2b41
--- a/src/core/service.c
ff2b41
+++ b/src/core/service.c
ff2b41
@@ -3368,6 +3368,32 @@ static int service_control_pid(Unit *u) {
ff2b41
         return s->control_pid;
ff2b41
 }
ff2b41
 
ff2b41
+static bool service_needs_console(Unit *u) {
ff2b41
+        Service *s = SERVICE(u);
ff2b41
+
ff2b41
+        assert(s);
ff2b41
+
ff2b41
+        /* We provide our own implementation of this here, instead of relying of the generic implementation
ff2b41
+         * unit_needs_console() provides, since we want to return false if we are in SERVICE_EXITED state. */
ff2b41
+
ff2b41
+        if (!exec_context_may_touch_console(&s->exec_context))
ff2b41
+                return false;
ff2b41
+
ff2b41
+        return IN_SET(s->state,
ff2b41
+                      SERVICE_START_PRE,
ff2b41
+                      SERVICE_START,
ff2b41
+                      SERVICE_START_POST,
ff2b41
+                      SERVICE_RUNNING,
ff2b41
+                      SERVICE_RELOAD,
ff2b41
+                      SERVICE_STOP,
ff2b41
+                      SERVICE_STOP_SIGABRT,
ff2b41
+                      SERVICE_STOP_SIGTERM,
ff2b41
+                      SERVICE_STOP_SIGKILL,
ff2b41
+                      SERVICE_STOP_POST,
ff2b41
+                      SERVICE_FINAL_SIGTERM,
ff2b41
+                      SERVICE_FINAL_SIGKILL);
ff2b41
+}
ff2b41
+
ff2b41
 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
ff2b41
         [SERVICE_RESTART_NO] = "no",
ff2b41
         [SERVICE_RESTART_ON_SUCCESS] = "on-success",
ff2b41
@@ -3489,6 +3515,7 @@ const UnitVTable service_vtable = {
ff2b41
         .bus_commit_properties = bus_service_commit_properties,
ff2b41
 
ff2b41
         .get_timeout = service_get_timeout,
ff2b41
+        .needs_console = service_needs_console,
ff2b41
         .can_transient = true,
ff2b41
 
ff2b41
         .status_message_formats = {
ff2b41
diff --git a/src/core/unit.c b/src/core/unit.c
ff2b41
index 4069a6f4c4..48358bc026 100644
ff2b41
--- a/src/core/unit.c
ff2b41
+++ b/src/core/unit.c
ff2b41
@@ -3699,6 +3699,28 @@ pid_t unit_main_pid(Unit *u) {
ff2b41
         return 0;
ff2b41
 }
ff2b41
 
ff2b41
+bool unit_needs_console(Unit *u) {
ff2b41
+        ExecContext *ec;
ff2b41
+        UnitActiveState state;
ff2b41
+
ff2b41
+        assert(u);
ff2b41
+
ff2b41
+        state = unit_active_state(u);
ff2b41
+
ff2b41
+        if (UNIT_IS_INACTIVE_OR_FAILED(state))
ff2b41
+                return false;
ff2b41
+
ff2b41
+        if (UNIT_VTABLE(u)->needs_console)
ff2b41
+                return UNIT_VTABLE(u)->needs_console(u);
ff2b41
+
ff2b41
+        /* If this unit type doesn't implement this call, let's use a generic fallback implementation: */
ff2b41
+        ec = unit_get_exec_context(u);
ff2b41
+        if (!ec)
ff2b41
+                return false;
ff2b41
+
ff2b41
+        return exec_context_may_touch_console(ec);
ff2b41
+}
ff2b41
+
ff2b41
 static const char* const unit_active_state_table[_UNIT_ACTIVE_STATE_MAX] = {
ff2b41
         [UNIT_ACTIVE] = "active",
ff2b41
         [UNIT_RELOADING] = "reloading",
ff2b41
diff --git a/src/core/unit.h b/src/core/unit.h
ff2b41
index 4a8bd79052..fa7de11645 100644
ff2b41
--- a/src/core/unit.h
ff2b41
+++ b/src/core/unit.h
ff2b41
@@ -408,6 +408,9 @@ struct UnitVTable {
ff2b41
         /* Returns the main PID if there is any defined, or 0. */
ff2b41
         pid_t (*control_pid)(Unit *u);
ff2b41
 
ff2b41
+        /* Returns true if the unit currently needs access to the console */
ff2b41
+        bool (*needs_console)(Unit *u);
ff2b41
+
ff2b41
         /* This is called for each unit type and should be used to
ff2b41
          * enumerate existing devices and load them. However,
ff2b41
          * everything that is loaded here should still stay in
ff2b41
@@ -627,6 +630,8 @@ pid_t unit_main_pid(Unit *u);
ff2b41
 const char *unit_active_state_to_string(UnitActiveState i) _const_;
ff2b41
 UnitActiveState unit_active_state_from_string(const char *s) _pure_;
ff2b41
 
ff2b41
+bool unit_needs_console(Unit *u);
ff2b41
+
ff2b41
 /* Macros which append UNIT= or USER_UNIT= to the message */
ff2b41
 
ff2b41
 #define log_unit_full_errno(unit, level, error, ...) log_object_internal(level, error, __FILE__, __LINE__, __func__, getpid() == 1 ? "UNIT=" : "USER_UNIT=", unit, __VA_ARGS__)