a19bc6
From 2170d1e510a9c30e71bc642b54d8b71fb01b47bc Mon Sep 17 00:00:00 2001
a19bc6
From: Lukas Nykryn <lnykryn@redhat.com>
a19bc6
Date: Tue, 18 Oct 2016 12:16:32 +0200
a19bc6
Subject: [PATCH] core: use emergency_action for ctr+alt+del burst
a19bc6
a19bc6
Fixes #4306
a19bc6
a19bc6
Cherry-picked from: ae8c7939df962cbf660b2b9517fe46be272f58b9
a19bc6
Resolves: #1353028
a19bc6
---
a19bc6
 man/systemd-system.conf.xml |  7 ++++---
a19bc6
 src/core/main.c             |  7 +++----
a19bc6
 src/core/manager.c          | 33 ++++-----------------------------
a19bc6
 src/core/manager.h          | 13 +------------
a19bc6
 4 files changed, 12 insertions(+), 48 deletions(-)
a19bc6
a19bc6
diff --git a/man/systemd-system.conf.xml b/man/systemd-system.conf.xml
c62b8e
index 236c20d5f9..57b3b90be1 100644
a19bc6
--- a/man/systemd-system.conf.xml
a19bc6
+++ b/man/systemd-system.conf.xml
a19bc6
@@ -105,9 +105,10 @@
a19bc6
         <term><varname>CtrlAltDelBurstAction=</varname></term>
a19bc6
 
a19bc6
         <listitem><para>Defines what action will be performed
a19bc6
-        if user presses Ctr-Alt-Delete more than 7 times in 2s.
a19bc6
-        Can be set to <literal>reboot-force</literal>, <literal>poweroff-force</literal>
a19bc6
-        or disabled with <literal>ignore</literal>. Defaults to
a19bc6
+        if user presses Ctrl-Alt-Delete more than 7 times in 2s.
a19bc6
+        Can be set to <literal>reboot-force</literal>, <literal>poweroff-force</literal>,
a19bc6
+        <literal>reboot-immediate</literal>, <literal>poweroff-immediate</literal>
a19bc6
+        or disabled with <literal>none</literal>. Defaults to
a19bc6
         <literal>reboot-force</literal>.
a19bc6
         </para></listitem>
a19bc6
       </varlistentry>
a19bc6
diff --git a/src/core/main.c b/src/core/main.c
c62b8e
index 6ac9c9d44f..6f8367632c 100644
a19bc6
--- a/src/core/main.c
a19bc6
+++ b/src/core/main.c
a19bc6
@@ -77,6 +77,7 @@
a19bc6
 #include "ima-setup.h"
a19bc6
 #include "smack-setup.h"
a19bc6
 #include "kmod-setup.h"
a19bc6
+#include "emergency-action.h"
a19bc6
 
a19bc6
 static enum {
a19bc6
         ACTION_RUN,
a19bc6
@@ -115,7 +116,7 @@ static FILE* arg_serialization = NULL;
a19bc6
 static bool arg_default_cpu_accounting = false;
a19bc6
 static bool arg_default_blockio_accounting = false;
a19bc6
 static bool arg_default_memory_accounting = false;
a19bc6
-static CADBurstAction arg_cad_burst_action = CAD_BURST_ACTION_REBOOT;
a19bc6
+static EmergencyAction arg_cad_burst_action = EMERGENCY_ACTION_REBOOT_FORCE;
a19bc6
 
a19bc6
 static void nop_handler(int sig) {}
a19bc6
 
a19bc6
@@ -626,8 +627,6 @@ static int config_parse_join_controllers(const char *unit,
a19bc6
         return 0;
a19bc6
 }
a19bc6
 
a19bc6
-static DEFINE_CONFIG_PARSE_ENUM(config_parse_cad_burst_action, cad_burst_action, CADBurstAction, "Failed to parse service restart specifier");
a19bc6
-
a19bc6
 static int parse_config_file(void) {
a19bc6
 
a19bc6
         const ConfigTableItem items[] = {
a19bc6
@@ -676,7 +675,7 @@ static int parse_config_file(void) {
a19bc6
                 { "Manager", "DefaultCPUAccounting",      config_parse_bool,             0, &arg_default_cpu_accounting            },
a19bc6
                 { "Manager", "DefaultBlockIOAccounting",  config_parse_bool,             0, &arg_default_blockio_accounting        },
a19bc6
                 { "Manager", "DefaultMemoryAccounting",   config_parse_bool,             0, &arg_default_memory_accounting         },
a19bc6
-                { "Manager", "CtrlAltDelBurstAction",     config_parse_cad_burst_action, 0, &arg_cad_burst_action},
a19bc6
+                { "Manager", "CtrlAltDelBurstAction",     config_parse_emergency_action, 0, &arg_cad_burst_action                  },
a19bc6
                 {}
a19bc6
         };
a19bc6
 
a19bc6
diff --git a/src/core/manager.c b/src/core/manager.c
c62b8e
index 9048dde96e..8bd80e6875 100644
a19bc6
--- a/src/core/manager.c
a19bc6
+++ b/src/core/manager.c
a19bc6
@@ -1864,28 +1864,11 @@ static void manager_handle_ctrl_alt_del(Manager *m) {
a19bc6
          * 7 times within 2s, we reboot/shutdown immediately,
a19bc6
          * unless it was disabled in system.conf */
a19bc6
 
a19bc6
-        if (ratelimit_test(&m->ctrl_alt_del_ratelimit) || m->cad_burst_action == CAD_BURST_ACTION_IGNORE)
a19bc6
+        if (ratelimit_test(&m->ctrl_alt_del_ratelimit) || m->cad_burst_action == EMERGENCY_ACTION_NONE)
a19bc6
                 manager_start_target(m, SPECIAL_CTRL_ALT_DEL_TARGET, JOB_REPLACE_IRREVERSIBLY);
a19bc6
-        else {
a19bc6
-                switch (m->cad_burst_action) {
a19bc6
-
a19bc6
-                case CAD_BURST_ACTION_REBOOT:
a19bc6
-                        m->exit_code = MANAGER_REBOOT;
a19bc6
-                        break;
a19bc6
-
a19bc6
-                case CAD_BURST_ACTION_POWEROFF:
a19bc6
-                        m->exit_code = MANAGER_POWEROFF;
a19bc6
-                        break;
a19bc6
-
a19bc6
-                default:
a19bc6
-                        assert_not_reached("Unknown action.");
a19bc6
-                }
a19bc6
-
a19bc6
-                log_notice("Ctrl-Alt-Del was pressed more than 7 times within 2s, performing immediate %s.",
a19bc6
-                                cad_burst_action_to_string(m->cad_burst_action));
a19bc6
-                status_printf(NULL, true, false, "Ctrl-Alt-Del was pressed more than 7 times within 2s, performing immediate %s.",
a19bc6
-                                cad_burst_action_to_string(m->cad_burst_action));
a19bc6
-        }
a19bc6
+        else
a19bc6
+                emergency_action(m, m->cad_burst_action, NULL,
a19bc6
+                                "Ctrl-Alt-Del was pressed more than 7 times within 2s");
a19bc6
 }
a19bc6
 
a19bc6
 static int manager_dispatch_signal_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
a19bc6
@@ -3336,11 +3319,3 @@ static const char *const manager_state_table[_MANAGER_STATE_MAX] = {
a19bc6
 };
a19bc6
 
a19bc6
 DEFINE_STRING_TABLE_LOOKUP(manager_state, ManagerState);
a19bc6
-
a19bc6
-static const char *const cad_burst_action_table[_CAD_BURST_ACTION_MAX] = {
a19bc6
-        [CAD_BURST_ACTION_IGNORE] = "ignore",
a19bc6
-        [CAD_BURST_ACTION_REBOOT] = "reboot-force",
a19bc6
-        [CAD_BURST_ACTION_POWEROFF] = "poweroff-force",
a19bc6
-};
a19bc6
-
a19bc6
-DEFINE_STRING_TABLE_LOOKUP(cad_burst_action, CADBurstAction);
a19bc6
diff --git a/src/core/manager.h b/src/core/manager.h
c62b8e
index 59913f4896..231c076b10 100644
a19bc6
--- a/src/core/manager.h
a19bc6
+++ b/src/core/manager.h
a19bc6
@@ -64,14 +64,6 @@ typedef enum ManagerExitCode {
a19bc6
         _MANAGER_EXIT_CODE_INVALID = -1
a19bc6
 } ManagerExitCode;
a19bc6
 
a19bc6
-typedef enum CADBurstAction {
a19bc6
-        CAD_BURST_ACTION_IGNORE,
a19bc6
-        CAD_BURST_ACTION_REBOOT,
a19bc6
-        CAD_BURST_ACTION_POWEROFF,
a19bc6
-        _CAD_BURST_ACTION_MAX,
a19bc6
-        _CAD_BURST_ACTION_INVALID = -1
a19bc6
-} CADBurstAction;
a19bc6
-
a19bc6
 typedef enum StatusType {
a19bc6
         STATUS_TYPE_EPHEMERAL,
a19bc6
         STATUS_TYPE_NORMAL,
a19bc6
@@ -310,7 +302,7 @@ struct Manager {
a19bc6
 
a19bc6
         /* When the user hits C-A-D more than 7 times per 2s, do something immediately... */
a19bc6
         RateLimit ctrl_alt_del_ratelimit;
a19bc6
-        CADBurstAction cad_burst_action;
a19bc6
+        EmergencyAction cad_burst_action;
a19bc6
 };
a19bc6
 
a19bc6
 int manager_new(SystemdRunningAs running_as, bool test_run, Manager **m);
a19bc6
@@ -381,6 +373,3 @@ ManagerState manager_state(Manager *m);
a19bc6
 
a19bc6
 const char *manager_state_to_string(ManagerState m) _const_;
a19bc6
 ManagerState manager_state_from_string(const char *s) _pure_;
a19bc6
-
a19bc6
-const char *cad_burst_action_to_string(CADBurstAction a) _const_;
a19bc6
-CADBurstAction cad_burst_action_from_string(const char *s) _pure_;