17aa40
From c0aa64901aa4d5d7c917fccf0993819fb1a1262f 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 16:34:45 +0200
17aa40
Subject: [PATCH] units: allow and use SuccessAction=exit-force in system
17aa40
 systemd-exit.service
17aa40
17aa40
C.f. 287419c119ef961db487a281162ab037eba70c61: 'systemctl exit 42' can be
17aa40
used to set an exit value and pulls in exit.target, which pulls in systemd-exit.service,
17aa40
which calls org.fdo.Manager.Exit, which calls method_exit(), which sets the objective
17aa40
to MANAGER_EXIT. Allow the same to happen through SuccessAction=exit.
17aa40
17aa40
v2: update for 'exit' and 'exit-force'
17aa40
(cherry picked from commit a400bd8c2a6285576edf8e2147e1d17aab129501)
17aa40
17aa40
Related: #1860899
17aa40
---
17aa40
 man/systemd.unit.xml                          |  7 +++--
17aa40
 src/core/emergency-action.c                   | 27 +++++++++++--------
17aa40
 src/test/test-emergency-action.c              |  6 ++---
17aa40
 units/meson.build                             |  2 +-
17aa40
 ...d-exit.service.in => systemd-exit.service} |  5 +---
17aa40
 5 files changed, 24 insertions(+), 23 deletions(-)
17aa40
 rename units/{systemd-exit.service.in => systemd-exit.service} (88%)
17aa40
17aa40
diff --git a/man/systemd.unit.xml b/man/systemd.unit.xml
17aa40
index 5772a6684e..e80c760dd6 100644
17aa40
--- a/man/systemd.unit.xml
17aa40
+++ b/man/systemd.unit.xml
17aa40
@@ -881,9 +881,8 @@
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
+        all options are allowed. In user mode, only <option>none</option>, <option>exit</option>, and
17aa40
+        <option>exit-force</option> are allowed. Both options default 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
@@ -893,7 +892,7 @@
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
17aa40
-        semantics. <option>exit</option> causes the user manager to exit following the normal shutdown procedure, and
17aa40
+        semantics. <option>exit</option> causes the 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
diff --git a/src/core/emergency-action.c b/src/core/emergency-action.c
17aa40
index e9e757dfa3..44b92ae6f8 100644
17aa40
--- a/src/core/emergency-action.c
17aa40
+++ b/src/core/emergency-action.c
17aa40
@@ -13,6 +13,7 @@
17aa40
 #include "special.h"
17aa40
 #include "string-table.h"
17aa40
 #include "terminal-util.h"
17aa40
+#include "virt.h"
17aa40
 
17aa40
 static void log_and_status(Manager *m, const char *message, const char *reason) {
17aa40
         log_warning("%s: %s", message, reason);
17aa40
@@ -73,12 +74,14 @@ int emergency_action(
17aa40
                 break;
17aa40
 
17aa40
         case EMERGENCY_ACTION_EXIT:
17aa40
-                assert(MANAGER_IS_USER(m));
17aa40
-
17aa40
-                log_and_status(m, "Exiting", reason);
17aa40
+                if (MANAGER_IS_USER(m) || detect_container() > 0) {
17aa40
+                        log_and_status(m, "Exiting", reason);
17aa40
+                        (void) manager_add_job_by_name_and_warn(m, JOB_START, SPECIAL_EXIT_TARGET, JOB_REPLACE_IRREVERSIBLY, NULL, NULL);
17aa40
+                        break;
17aa40
+                }
17aa40
 
17aa40
-                (void) manager_add_job_by_name_and_warn(m, JOB_START, SPECIAL_EXIT_TARGET, JOB_REPLACE_IRREVERSIBLY, NULL, NULL);
17aa40
-                break;
17aa40
+                log_notice("Doing \"poweroff\" action instead of an \"exit\" emergency action.");
17aa40
+                _fallthrough_;
17aa40
 
17aa40
         case EMERGENCY_ACTION_POWEROFF:
17aa40
                 log_and_status(m, "Powering off", reason);
17aa40
@@ -86,11 +89,14 @@ int emergency_action(
17aa40
                 break;
17aa40
 
17aa40
         case EMERGENCY_ACTION_EXIT_FORCE:
17aa40
-                assert(MANAGER_IS_USER(m));
17aa40
+                if (MANAGER_IS_USER(m) || detect_container() > 0) {
17aa40
+                        log_and_status(m, "Exiting immediately", reason);
17aa40
+                        m->exit_code = MANAGER_EXIT;
17aa40
+                        break;
17aa40
+                }
17aa40
 
17aa40
-                log_and_status(m, "Exiting immediately", reason);
17aa40
-                m->exit_code = MANAGER_EXIT;
17aa40
-                break;
17aa40
+                log_notice("Doing \"poweroff-force\" action instead of an \"exit-force\" emergency action.");
17aa40
+                _fallthrough_;
17aa40
 
17aa40
         case EMERGENCY_ACTION_POWEROFF_FORCE:
17aa40
                 log_and_status(m, "Forcibly powering off", reason);
17aa40
@@ -137,8 +143,7 @@ int parse_emergency_action(
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
+        if (!system && x != EMERGENCY_ACTION_NONE && x < _EMERGENCY_ACTION_FIRST_USER_ACTION)
17aa40
                 return -EOPNOTSUPP;
17aa40
 
17aa40
         *ret = x;
17aa40
diff --git a/src/test/test-emergency-action.c b/src/test/test-emergency-action.c
17aa40
index 493b23227e..8ce28ed9f5 100644
17aa40
--- a/src/test/test-emergency-action.c
17aa40
+++ b/src/test/test-emergency-action.c
17aa40
@@ -36,10 +36,10 @@ static void test_parse_emergency_action(void) {
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", true, &x) == 0);
17aa40
+        assert_se(parse_emergency_action("exit-force", true, &x) == 0);
17aa40
         assert_se(parse_emergency_action("exit-forcee", true, &x) == -EINVAL);
17aa40
-        assert_se(x == EMERGENCY_ACTION_POWEROFF_IMMEDIATE);
17aa40
+        assert_se(x == EMERGENCY_ACTION_EXIT_FORCE);
17aa40
 }
17aa40
 
17aa40
 int main(int argc, char **argv) {
17aa40
diff --git a/units/meson.build b/units/meson.build
17aa40
index 6fa804148b..a74fa95195 100644
17aa40
--- a/units/meson.build
17aa40
+++ b/units/meson.build
17aa40
@@ -86,6 +86,7 @@ units = [
17aa40
          'multi-user.target.wants/'],
17aa40
         ['systemd-coredump.socket',             'ENABLE_COREDUMP',
17aa40
          'sockets.target.wants/'],
17aa40
+        ['systemd-exit.service',                 ''],
17aa40
         ['systemd-initctl.socket',              '',
17aa40
          'sockets.target.wants/'],
17aa40
         ['systemd-journal-gatewayd.socket',     'ENABLE_REMOTE HAVE_MICROHTTPD'],
17aa40
@@ -135,7 +136,6 @@ in_units = [
17aa40
         ['systemd-binfmt.service',               'ENABLE_BINFMT',
17aa40
          'sysinit.target.wants/'],
17aa40
         ['systemd-coredump@.service',            'ENABLE_COREDUMP'],
17aa40
-        ['systemd-exit.service',                 ''],
17aa40
         ['systemd-firstboot.service',            'ENABLE_FIRSTBOOT',
17aa40
          'sysinit.target.wants/'],
17aa40
         ['systemd-fsck-root.service',            ''],
17aa40
diff --git a/units/systemd-exit.service.in b/units/systemd-exit.service
17aa40
similarity index 88%
17aa40
rename from units/systemd-exit.service.in
17aa40
rename to units/systemd-exit.service
17aa40
index 2fb6ebd767..6029b13a05 100644
17aa40
--- a/units/systemd-exit.service.in
17aa40
+++ b/units/systemd-exit.service
17aa40
@@ -13,7 +13,4 @@ Documentation=man:systemd.special(7)
17aa40
 DefaultDependencies=no
17aa40
 Requires=shutdown.target
17aa40
 After=shutdown.target
17aa40
-
17aa40
-[Service]
17aa40
-Type=oneshot
17aa40
-ExecStart=@SYSTEMCTL@ --force exit
17aa40
+SuccessAction=exit