b8c242
From 19d91eef7f15b654cd96ad5350385e535fab9e2a Mon Sep 17 00:00:00 2001
b8c242
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
b8c242
Date: Tue, 16 Oct 2018 13:28:39 +0200
b8c242
Subject: [PATCH] core: define "exit" and "exit-force" actions for user units
b8c242
 and only accept that
b8c242
b8c242
We would accept e.g. FailureAction=reboot-force in user units and then do an
b8c242
exit in the user manager. Let's be stricter, and define "exit"/"exit-force" as
b8c242
the only supported actions in user units.
b8c242
b8c242
v2:
b8c242
- rename 'exit' to 'exit-force' and add new 'exit'
b8c242
- add test for the parsing function
b8c242
b8c242
(cherry picked from commit 54fcb6192c618726d11404b24b1a1e9ec3169ee1)
b8c242
b8c242
Related: #1860899
b8c242
---
b8c242
 TODO                             |  4 +++
b8c242
 man/systemd.unit.xml             | 26 +++++++++-------
b8c242
 src/core/dbus-unit.c             | 37 ++++++++++++++++++++++-
b8c242
 src/core/emergency-action.c      | 47 ++++++++++++++++++++++-------
b8c242
 src/core/emergency-action.h      |  5 ++++
b8c242
 src/core/load-fragment.c         | 42 +++++++++++++++++++++++++-
b8c242
 src/test/meson.build             |  5 ++++
b8c242
 src/test/test-emergency-action.c | 51 ++++++++++++++++++++++++++++++++
b8c242
 8 files changed, 195 insertions(+), 22 deletions(-)
b8c242
 create mode 100644 src/test/test-emergency-action.c
b8c242
b8c242
diff --git a/TODO b/TODO
b8c242
index 3100e067d6..0705b6b08e 100644
b8c242
--- a/TODO
b8c242
+++ b/TODO
b8c242
@@ -4,6 +4,10 @@ Bugfixes:
b8c242
 
b8c242
 * copy.c: set the right chattrs before copying files and others after
b8c242
 
b8c242
+* Many manager configuration settings that are only applicable to user
b8c242
+  manager or system manager can be always set. It would be better to reject
b8c242
+  them when parsing config.
b8c242
+
b8c242
 External:
b8c242
 
b8c242
 * Fedora: add an rpmlint check that verifies that all unit files in the RPM are listed in %systemd_post macros.
b8c242
diff --git a/man/systemd.unit.xml b/man/systemd.unit.xml
b8c242
index 802db453a4..5772a6684e 100644
b8c242
--- a/man/systemd.unit.xml
b8c242
+++ b/man/systemd.unit.xml
b8c242
@@ -877,18 +877,24 @@
b8c242
         <term><varname>FailureAction=</varname></term>
b8c242
         <term><varname>SuccessAction=</varname></term>
b8c242
 
b8c242
-        <listitem><para>Configure the action to take when the unit stops and enters a failed state or inactive
b8c242
-        state. Takes one of <option>none</option>, <option>reboot</option>, <option>reboot-force</option>,
b8c242
-        <option>reboot-immediate</option>, <option>poweroff</option>, <option>poweroff-force</option> or
b8c242
-        <option>poweroff-immediate</option>. If <option>none</option> is set, no action will be triggered.
b8c242
-        <option>reboot</option> causes a reboot following the normal shutdown procedure (i.e. equivalent to
b8c242
-        <command>systemctl reboot</command>). <option>reboot-force</option> causes a forced reboot which will
b8c242
-        terminate all processes forcibly but should cause no dirty file systems on reboot (i.e. equivalent to
b8c242
-        <command>systemctl reboot -f</command>) and <option>reboot-immediate</option> causes immediate execution of the
b8c242
+        <listitem><para>Configure the action to take when the unit stops and enters a failed state or inactive state.
b8c242
+        Takes one of <option>none</option>, <option>reboot</option>, <option>reboot-force</option>,
b8c242
+        <option>reboot-immediate</option>, <option>poweroff</option>, <option>poweroff-force</option>,
b8c242
+        <option>poweroff-immediate</option>, <option>exit</option>, and <option>exit-force</option>. In system mode,
b8c242
+        all options except <option>exit</option> and <option>exit-force</option> are allowed. In user mode, only
b8c242
+        <option>none</option>, <option>exit</option>, and <option>exit-force</option> are allowed. Both options default
b8c242
+        to <option>none</option>.</para>
b8c242
+
b8c242
+        <para>If <option>none</option> is set, no action will be triggered. <option>reboot</option> causes a reboot
b8c242
+        following the normal shutdown procedure (i.e. equivalent to <command>systemctl reboot</command>).
b8c242
+        <option>reboot-force</option> causes a forced reboot which will terminate all processes forcibly but should
b8c242
+        cause no dirty file systems on reboot (i.e. equivalent to <command>systemctl reboot -f</command>) and
b8c242
+        <option>reboot-immediate</option> causes immediate execution of the
b8c242
         <citerefentry><refentrytitle>reboot</refentrytitle><manvolnum>2</manvolnum></citerefentry> system call, which
b8c242
         might result in data loss. Similarly, <option>poweroff</option>, <option>poweroff-force</option>,
b8c242
-        <option>poweroff-immediate</option> have the effect of powering down the system with similar semantics. Both
b8c242
-        options default to <option>none</option>.</para></listitem>
b8c242
+        <option>poweroff-immediate</option> have the effect of powering down the system with similar
b8c242
+        semantics. <option>exit</option> causes the user manager to exit following the normal shutdown procedure, and
b8c242
+        <option>exit-force</option> causes it terminate without shutting down services.</para></listitem>
b8c242
       </varlistentry>
b8c242
 
b8c242
       <varlistentry>
b8c242
diff --git a/src/core/dbus-unit.c b/src/core/dbus-unit.c
b8c242
index 549a166abc..e7ea9db3ac 100644
b8c242
--- a/src/core/dbus-unit.c
b8c242
+++ b/src/core/dbus-unit.c
b8c242
@@ -1564,8 +1564,43 @@ static int bus_unit_set_live_property(
b8c242
         return 0;
b8c242
 }
b8c242
 
b8c242
+static int bus_set_transient_emergency_action(
b8c242
+                Unit *u,
b8c242
+                const char *name,
b8c242
+                EmergencyAction *p,
b8c242
+                sd_bus_message *message,
b8c242
+                UnitWriteFlags flags,
b8c242
+                sd_bus_error *error) {
b8c242
+
b8c242
+        const char *s;
b8c242
+        EmergencyAction v;
b8c242
+        int r;
b8c242
+        bool system;
b8c242
+
b8c242
+        assert(p);
b8c242
+
b8c242
+        r = sd_bus_message_read(message, "s", &s);
b8c242
+        if (r < 0)
b8c242
+                return r;
b8c242
+
b8c242
+        system = MANAGER_IS_SYSTEM(u->manager);
b8c242
+        r = parse_emergency_action(s, system, &v);
b8c242
+        if (v < 0)
b8c242
+                return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
b8c242
+                                         v == -EOPNOTSUPP ? "EmergencyAction setting invalid for manager type: %s"
b8c242
+                                                          : "Invalid %s setting: %s",
b8c242
+                                         name, s);
b8c242
+
b8c242
+        if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
b8c242
+                *p = v;
b8c242
+                unit_write_settingf(u, flags, name,
b8c242
+                                    "%s=%s", name, s);
b8c242
+        }
b8c242
+
b8c242
+        return 1;
b8c242
+}
b8c242
+
b8c242
 static BUS_DEFINE_SET_TRANSIENT_PARSE(collect_mode, CollectMode, collect_mode_from_string);
b8c242
-static BUS_DEFINE_SET_TRANSIENT_PARSE(emergency_action, EmergencyAction, emergency_action_from_string);
b8c242
 static BUS_DEFINE_SET_TRANSIENT_PARSE(job_mode, JobMode, job_mode_from_string);
b8c242
 
b8c242
 static int bus_set_transient_conditions(
b8c242
diff --git a/src/core/emergency-action.c b/src/core/emergency-action.c
b8c242
index 766a3b4d2b..00f5996317 100644
b8c242
--- a/src/core/emergency-action.c
b8c242
+++ b/src/core/emergency-action.c
b8c242
@@ -39,15 +39,6 @@ int emergency_action(
b8c242
                 return -ECANCELED;
b8c242
         }
b8c242
 
b8c242
-        if (!MANAGER_IS_SYSTEM(m)) {
b8c242
-                /* Downgrade all options to simply exiting if we run
b8c242
-                 * in user mode */
b8c242
-
b8c242
-                log_warning("Exiting: %s", reason);
b8c242
-                m->exit_code = MANAGER_EXIT;
b8c242
-                return -ECANCELED;
b8c242
-        }
b8c242
-
b8c242
         switch (action) {
b8c242
 
b8c242
         case EMERGENCY_ACTION_REBOOT:
b8c242
@@ -80,11 +71,26 @@ int emergency_action(
b8c242
                 (void) reboot(RB_AUTOBOOT);
b8c242
                 break;
b8c242
 
b8c242
+        case EMERGENCY_ACTION_EXIT:
b8c242
+                assert(MANAGER_IS_USER(m));
b8c242
+
b8c242
+                log_and_status(m, "Exiting", reason);
b8c242
+
b8c242
+                (void) manager_add_job_by_name_and_warn(m, JOB_START, SPECIAL_EXIT_TARGET, JOB_REPLACE_IRREVERSIBLY, NULL, NULL);
b8c242
+                break;
b8c242
+
b8c242
         case EMERGENCY_ACTION_POWEROFF:
b8c242
                 log_and_status(m, "Powering off", reason);
b8c242
                 (void) manager_add_job_by_name_and_warn(m, JOB_START, SPECIAL_POWEROFF_TARGET, JOB_REPLACE_IRREVERSIBLY, NULL, NULL);
b8c242
                 break;
b8c242
 
b8c242
+        case EMERGENCY_ACTION_EXIT_FORCE:
b8c242
+                assert(MANAGER_IS_USER(m));
b8c242
+
b8c242
+                log_and_status(m, "Exiting immediately", reason);
b8c242
+                m->exit_code = MANAGER_EXIT;
b8c242
+                break;
b8c242
+
b8c242
         case EMERGENCY_ACTION_POWEROFF_FORCE:
b8c242
                 log_and_status(m, "Forcibly powering off", reason);
b8c242
                 m->exit_code = MANAGER_POWEROFF;
b8c242
@@ -113,6 +119,27 @@ static const char* const emergency_action_table[_EMERGENCY_ACTION_MAX] = {
b8c242
         [EMERGENCY_ACTION_REBOOT_IMMEDIATE] = "reboot-immediate",
b8c242
         [EMERGENCY_ACTION_POWEROFF] = "poweroff",
b8c242
         [EMERGENCY_ACTION_POWEROFF_FORCE] = "poweroff-force",
b8c242
-        [EMERGENCY_ACTION_POWEROFF_IMMEDIATE] = "poweroff-immediate"
b8c242
+        [EMERGENCY_ACTION_POWEROFF_IMMEDIATE] = "poweroff-immediate",
b8c242
+        [EMERGENCY_ACTION_EXIT] = "exit",
b8c242
+        [EMERGENCY_ACTION_EXIT_FORCE] = "exit-force",
b8c242
 };
b8c242
 DEFINE_STRING_TABLE_LOOKUP(emergency_action, EmergencyAction);
b8c242
+
b8c242
+int parse_emergency_action(
b8c242
+                const char *value,
b8c242
+                bool system,
b8c242
+                EmergencyAction *ret) {
b8c242
+
b8c242
+        EmergencyAction x;
b8c242
+
b8c242
+        x = emergency_action_from_string(value);
b8c242
+        if (x < 0)
b8c242
+                return -EINVAL;
b8c242
+
b8c242
+        if ((system && x >= _EMERGENCY_ACTION_FIRST_USER_ACTION) ||
b8c242
+            (!system && x != EMERGENCY_ACTION_NONE && x < _EMERGENCY_ACTION_FIRST_USER_ACTION))
b8c242
+                return -EOPNOTSUPP;
b8c242
+
b8c242
+        *ret = x;
b8c242
+        return 0;
b8c242
+}
b8c242
diff --git a/src/core/emergency-action.h b/src/core/emergency-action.h
b8c242
index 61791f176f..646ccc4e6b 100644
b8c242
--- a/src/core/emergency-action.h
b8c242
+++ b/src/core/emergency-action.h
b8c242
@@ -13,6 +13,9 @@ typedef enum EmergencyAction {
b8c242
         EMERGENCY_ACTION_POWEROFF,
b8c242
         EMERGENCY_ACTION_POWEROFF_FORCE,
b8c242
         EMERGENCY_ACTION_POWEROFF_IMMEDIATE,
b8c242
+        EMERGENCY_ACTION_EXIT,
b8c242
+        _EMERGENCY_ACTION_FIRST_USER_ACTION = EMERGENCY_ACTION_EXIT,
b8c242
+        EMERGENCY_ACTION_EXIT_FORCE,
b8c242
         _EMERGENCY_ACTION_MAX,
b8c242
         _EMERGENCY_ACTION_INVALID = -1
b8c242
 } EmergencyAction;
b8c242
@@ -24,3 +27,5 @@ int emergency_action(Manager *m, EmergencyAction action, const char *reboot_arg,
b8c242
 
b8c242
 const char* emergency_action_to_string(EmergencyAction i) _const_;
b8c242
 EmergencyAction emergency_action_from_string(const char *s) _pure_;
b8c242
+
b8c242
+int parse_emergency_action(const char *value, bool system, EmergencyAction *ret);
b8c242
diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c
b8c242
index e0d7b8f7f8..c102ffb9f0 100644
b8c242
--- a/src/core/load-fragment.c
b8c242
+++ b/src/core/load-fragment.c
b8c242
@@ -77,7 +77,6 @@ DEFINE_CONFIG_PARSE(config_parse_socket_protocol, supported_socket_protocol_from
b8c242
 DEFINE_CONFIG_PARSE(config_parse_exec_secure_bits, secure_bits_from_string, "Failed to parse secure bits");
b8c242
 DEFINE_CONFIG_PARSE_ENUM(config_parse_collect_mode, collect_mode, CollectMode, "Failed to parse garbage collection mode");
b8c242
 DEFINE_CONFIG_PARSE_ENUM(config_parse_device_policy, cgroup_device_policy, CGroupDevicePolicy, "Failed to parse device policy");
b8c242
-DEFINE_CONFIG_PARSE_ENUM(config_parse_emergency_action, emergency_action, EmergencyAction, "Failed to parse failure action specifier");
b8c242
 DEFINE_CONFIG_PARSE_ENUM(config_parse_exec_keyring_mode, exec_keyring_mode, ExecKeyringMode, "Failed to parse keyring mode");
b8c242
 DEFINE_CONFIG_PARSE_ENUM(config_parse_exec_utmp_mode, exec_utmp_mode, ExecUtmpMode, "Failed to parse utmp mode");
b8c242
 DEFINE_CONFIG_PARSE_ENUM(config_parse_job_mode, job_mode, JobMode, "Failed to parse job mode");
b8c242
@@ -4253,6 +4252,47 @@ int config_parse_job_running_timeout_sec(
b8c242
         return 0;
b8c242
 }
b8c242
 
b8c242
+int config_parse_emergency_action(
b8c242
+                const char* unit,
b8c242
+                const char *filename,
b8c242
+                unsigned line,
b8c242
+                const char *section,
b8c242
+                unsigned section_line,
b8c242
+                const char *lvalue,
b8c242
+                int ltype,
b8c242
+                const char *rvalue,
b8c242
+                void *data,
b8c242
+                void *userdata) {
b8c242
+
b8c242
+        Manager *m = NULL;
b8c242
+        EmergencyAction *x = data;
b8c242
+        int r;
b8c242
+
b8c242
+        assert(filename);
b8c242
+        assert(lvalue);
b8c242
+        assert(rvalue);
b8c242
+        assert(data);
b8c242
+
b8c242
+        if (unit)
b8c242
+                m = ((Unit*) userdata)->manager;
b8c242
+        else
b8c242
+                m = data;
b8c242
+
b8c242
+        r = parse_emergency_action(rvalue, MANAGER_IS_SYSTEM(m), x);
b8c242
+        if (r < 0) {
b8c242
+                if (r == -EOPNOTSUPP)
b8c242
+                        log_syntax(unit, LOG_ERR, filename, line, r,
b8c242
+                                   "%s= specified as %s mode action, ignoring: %s",
b8c242
+                                   lvalue, MANAGER_IS_SYSTEM(m) ? "user" : "system", rvalue);
b8c242
+                else
b8c242
+                        log_syntax(unit, LOG_ERR, filename, line, r,
b8c242
+                                   "Failed to parse %s=, ignoring: %s", lvalue, rvalue);
b8c242
+                return 0;
b8c242
+        }
b8c242
+
b8c242
+        return 0;
b8c242
+}
b8c242
+
b8c242
 #define FOLLOW_MAX 8
b8c242
 
b8c242
 static int open_follow(char **filename, FILE **_f, Set *names, char **_final) {
b8c242
diff --git a/src/test/meson.build b/src/test/meson.build
b8c242
index 7b310d4ec7..40cf56d73d 100644
b8c242
--- a/src/test/meson.build
b8c242
+++ b/src/test/meson.build
b8c242
@@ -65,6 +65,11 @@ tests += [
b8c242
           libshared],
b8c242
          []],
b8c242
 
b8c242
+        [['src/test/test-emergency-action.c'],
b8c242
+         [libcore,
b8c242
+          libshared],
b8c242
+         []],
b8c242
+
b8c242
         [['src/test/test-job-type.c'],
b8c242
          [libcore,
b8c242
           libshared],
b8c242
diff --git a/src/test/test-emergency-action.c b/src/test/test-emergency-action.c
b8c242
new file mode 100644
b8c242
index 0000000000..493b23227e
b8c242
--- /dev/null
b8c242
+++ b/src/test/test-emergency-action.c
b8c242
@@ -0,0 +1,51 @@
b8c242
+/* SPDX-License-Identifier: LGPL-2.1+ */
b8c242
+
b8c242
+#include "emergency-action.h"
b8c242
+#include "tests.h"
b8c242
+
b8c242
+static void test_parse_emergency_action(void) {
b8c242
+        EmergencyAction x;
b8c242
+
b8c242
+        log_info("/* %s */", __func__);
b8c242
+
b8c242
+        assert_se(parse_emergency_action("none", false, &x) == 0);
b8c242
+        assert_se(x == EMERGENCY_ACTION_NONE);
b8c242
+        assert_se(parse_emergency_action("reboot", false, &x) == -EOPNOTSUPP);
b8c242
+        assert_se(parse_emergency_action("reboot-force", false, &x) == -EOPNOTSUPP);
b8c242
+        assert_se(parse_emergency_action("reboot-immediate", false, &x) == -EOPNOTSUPP);
b8c242
+        assert_se(parse_emergency_action("poweroff", false, &x) == -EOPNOTSUPP);
b8c242
+        assert_se(parse_emergency_action("poweroff-force", false, &x) == -EOPNOTSUPP);
b8c242
+        assert_se(parse_emergency_action("poweroff-immediate", false, &x) == -EOPNOTSUPP);
b8c242
+        assert_se(x == EMERGENCY_ACTION_NONE);
b8c242
+        assert_se(parse_emergency_action("exit", false, &x) == 0);
b8c242
+        assert_se(x == EMERGENCY_ACTION_EXIT);
b8c242
+        assert_se(parse_emergency_action("exit-force", false, &x) == 0);
b8c242
+        assert_se(x == EMERGENCY_ACTION_EXIT_FORCE);
b8c242
+        assert_se(parse_emergency_action("exit-forcee", false, &x) == -EINVAL);
b8c242
+
b8c242
+        assert_se(parse_emergency_action("none", true, &x) == 0);
b8c242
+        assert_se(x == EMERGENCY_ACTION_NONE);
b8c242
+        assert_se(parse_emergency_action("reboot", true, &x) == 0);
b8c242
+        assert_se(x == EMERGENCY_ACTION_REBOOT);
b8c242
+        assert_se(parse_emergency_action("reboot-force", true, &x) == 0);
b8c242
+        assert_se(x == EMERGENCY_ACTION_REBOOT_FORCE);
b8c242
+        assert_se(parse_emergency_action("reboot-immediate", true, &x) == 0);
b8c242
+        assert_se(x == EMERGENCY_ACTION_REBOOT_IMMEDIATE);
b8c242
+        assert_se(parse_emergency_action("poweroff", true, &x) == 0);
b8c242
+        assert_se(x == EMERGENCY_ACTION_POWEROFF);
b8c242
+        assert_se(parse_emergency_action("poweroff-force", true, &x) == 0);
b8c242
+        assert_se(x == EMERGENCY_ACTION_POWEROFF_FORCE);
b8c242
+        assert_se(parse_emergency_action("poweroff-immediate", true, &x) == 0);
b8c242
+        assert_se(parse_emergency_action("exit", true, &x) == -EOPNOTSUPP);
b8c242
+        assert_se(parse_emergency_action("exit-force", true, &x) == -EOPNOTSUPP);
b8c242
+        assert_se(parse_emergency_action("exit-forcee", true, &x) == -EINVAL);
b8c242
+        assert_se(x == EMERGENCY_ACTION_POWEROFF_IMMEDIATE);
b8c242
+}
b8c242
+
b8c242
+int main(int argc, char **argv) {
b8c242
+        test_setup_logging(LOG_INFO);
b8c242
+
b8c242
+        test_parse_emergency_action();
b8c242
+
b8c242
+        return EXIT_SUCCESS;
b8c242
+}