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