803fb7
From be9fad86ae9ab721cd295210962da85706b839e9 Mon Sep 17 00:00:00 2001
803fb7
From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Nykr=C3=BDn?= <lnykryn@redhat.com>
803fb7
Date: Fri, 7 Oct 2016 03:08:21 +0200
803fb7
Subject: [PATCH] core: add possibility to set action for ctrl-alt-del burst
803fb7
 (#4105)
803fb7
803fb7
For some certification, it should not be possible to reboot the machine through ctrl-alt-delete. Currently we suggest our customers to mask the ctrl-alt-delete target, but that is obviously not enough.
803fb7
803fb7
Patching the keymaps to disable that is really not a way to go for them, because the settings need to be easily checked by some SCAP tools.
803fb7
803fb7
Cherry-picked from: 24dd31c19ede505143833346ff850af942694aa6
803fb7
Resolves: #1353028
803fb7
---
803fb7
 man/systemd-system.conf.xml | 11 ++++++++++
803fb7
 src/core/main.c             |  5 +++++
803fb7
 src/core/manager.c          | 51 +++++++++++++++++++++++++++++++++------------
803fb7
 src/core/manager.h          | 14 ++++++++++++-
803fb7
 src/core/system.conf        |  1 +
803fb7
 5 files changed, 68 insertions(+), 14 deletions(-)
803fb7
803fb7
diff --git a/man/systemd-system.conf.xml b/man/systemd-system.conf.xml
803fb7
index 39d19bc71..236c20d5f 100644
803fb7
--- a/man/systemd-system.conf.xml
803fb7
+++ b/man/systemd-system.conf.xml
803fb7
@@ -101,6 +101,17 @@
803fb7
         arguments.</para></listitem>
803fb7
       </varlistentry>
803fb7
 
803fb7
+      <varlistentry>
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
+        <literal>reboot-force</literal>.
803fb7
+        </para></listitem>
803fb7
+      </varlistentry>
803fb7
+
803fb7
       <varlistentry>
803fb7
         <term><varname>CPUAffinity=</varname></term>
803fb7
 
803fb7
diff --git a/src/core/main.c b/src/core/main.c
803fb7
index c9d8ce4a4..6ac9c9d44 100644
803fb7
--- a/src/core/main.c
803fb7
+++ b/src/core/main.c
803fb7
@@ -115,6 +115,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
 
803fb7
 static void nop_handler(int sig) {}
803fb7
 
803fb7
@@ -625,6 +626,8 @@ 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
@@ -673,6 +676,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
                 {}
803fb7
         };
803fb7
 
803fb7
@@ -1690,6 +1694,7 @@ int main(int argc, char *argv[]) {
803fb7
         m->initrd_timestamp = initrd_timestamp;
803fb7
         m->security_start_timestamp = security_start_timestamp;
803fb7
         m->security_finish_timestamp = security_finish_timestamp;
803fb7
+        m->cad_burst_action = arg_cad_burst_action;
803fb7
 
803fb7
         manager_set_default_rlimits(m, arg_default_rlimit);
803fb7
         manager_environment_add(m, NULL, arg_default_environment);
803fb7
diff --git a/src/core/manager.c b/src/core/manager.c
803fb7
index 6d045fdf3..9048dde96 100644
803fb7
--- a/src/core/manager.c
803fb7
+++ b/src/core/manager.c
803fb7
@@ -1859,6 +1859,35 @@ static int manager_start_target(Manager *m, const char *name, JobMode mode) {
803fb7
         return r;
803fb7
 }
803fb7
 
803fb7
+static void manager_handle_ctrl_alt_del(Manager *m) {
803fb7
+        /* If the user presses C-A-D more than
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
+                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
+}
803fb7
+
803fb7
 static int manager_dispatch_signal_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
803fb7
         Manager *m = userdata;
803fb7
         ssize_t n;
803fb7
@@ -1909,19 +1938,7 @@ static int manager_dispatch_signal_fd(sd_event_source *source, int fd, uint32_t
803fb7
 
803fb7
                 case SIGINT:
803fb7
                         if (m->running_as == SYSTEMD_SYSTEM) {
803fb7
-
803fb7
-                                /* If the user presses C-A-D more than
803fb7
-                                 * 7 times within 2s, we reboot
803fb7
-                                 * immediately. */
803fb7
-
803fb7
-                                if (ratelimit_test(&m->ctrl_alt_del_ratelimit))
803fb7
-                                        manager_start_target(m, SPECIAL_CTRL_ALT_DEL_TARGET, JOB_REPLACE_IRREVERSIBLY);
803fb7
-                                else {
803fb7
-                                        log_notice("Ctrl-Alt-Del was pressed more than 7 times within 2s, rebooting immediately.");
803fb7
-                                        status_printf(NULL, true, false, "Ctrl-Alt-Del was pressed more than 7 times within 2s, rebooting immediately.");
803fb7
-                                        m->exit_code = MANAGER_REBOOT;
803fb7
-                                }
803fb7
-
803fb7
+                                manager_handle_ctrl_alt_del(m);
803fb7
                                 break;
803fb7
                         }
803fb7
 
803fb7
@@ -3319,3 +3336,11 @@ 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 3e855db46..42be1fc43 100644
803fb7
--- a/src/core/manager.h
803fb7
+++ b/src/core/manager.h
803fb7
@@ -64,6 +64,14 @@ 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
@@ -300,8 +308,9 @@ struct Manager {
803fb7
         /* Used for processing polkit authorization responses */
803fb7
         Hashmap *polkit_registry;
803fb7
 
803fb7
-        /* When the user hits C-A-D more than 7 times per 2s, reboot immediately... */
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
 };
803fb7
 
803fb7
 int manager_new(SystemdRunningAs running_as, bool test_run, Manager **m);
803fb7
@@ -372,3 +381,6 @@ 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_;
803fb7
diff --git a/src/core/system.conf b/src/core/system.conf
803fb7
index 231609033..a11f59903 100644
803fb7
--- a/src/core/system.conf
803fb7
+++ b/src/core/system.conf
803fb7
@@ -20,6 +20,7 @@
803fb7
 #CrashShell=no
803fb7
 #ShowStatus=yes
803fb7
 #CrashChVT=1
803fb7
+#CtrlAltDelBurstAction=reboot-force
803fb7
 #CPUAffinity=1 2
803fb7
 #JoinControllers=cpu,cpuacct net_cls,net_prio
803fb7
 #RuntimeWatchdogSec=0