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