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