anitazha / rpms / systemd

Forked from rpms/systemd 3 years ago
Clone
923a60
From be9fad86ae9ab721cd295210962da85706b839e9 Mon Sep 17 00:00:00 2001
923a60
From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Nykr=C3=BDn?= <lnykryn@redhat.com>
923a60
Date: Fri, 7 Oct 2016 03:08:21 +0200
923a60
Subject: [PATCH] core: add possibility to set action for ctrl-alt-del burst
923a60
 (#4105)
923a60
923a60
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.
923a60
923a60
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.
923a60
923a60
Cherry-picked from: 24dd31c19ede505143833346ff850af942694aa6
923a60
Resolves: #1353028
923a60
---
923a60
 man/systemd-system.conf.xml | 11 ++++++++
923a60
 src/core/main.c             |  5 ++++
923a60
 src/core/manager.c          | 51 +++++++++++++++++++++++++++----------
923a60
 src/core/manager.h          | 14 +++++++++-
923a60
 src/core/system.conf        |  1 +
923a60
 5 files changed, 68 insertions(+), 14 deletions(-)
923a60
923a60
diff --git a/man/systemd-system.conf.xml b/man/systemd-system.conf.xml
923a60
index 39d19bc71a..236c20d5f9 100644
923a60
--- a/man/systemd-system.conf.xml
923a60
+++ b/man/systemd-system.conf.xml
923a60
@@ -101,6 +101,17 @@
923a60
         arguments.</para></listitem>
923a60
       </varlistentry>
923a60
 
923a60
+      <varlistentry>
923a60
+        <term><varname>CtrlAltDelBurstAction=</varname></term>
923a60
+
923a60
+        <listitem><para>Defines what action will be performed
923a60
+        if user presses Ctr-Alt-Delete more than 7 times in 2s.
923a60
+        Can be set to <literal>reboot-force</literal>, <literal>poweroff-force</literal>
923a60
+        or disabled with <literal>ignore</literal>. Defaults to
923a60
+        <literal>reboot-force</literal>.
923a60
+        </para></listitem>
923a60
+      </varlistentry>
923a60
+
923a60
       <varlistentry>
923a60
         <term><varname>CPUAffinity=</varname></term>
923a60
 
923a60
diff --git a/src/core/main.c b/src/core/main.c
923a60
index c9d8ce4a40..6ac9c9d44f 100644
923a60
--- a/src/core/main.c
923a60
+++ b/src/core/main.c
923a60
@@ -115,6 +115,7 @@ static FILE* arg_serialization = NULL;
923a60
 static bool arg_default_cpu_accounting = false;
923a60
 static bool arg_default_blockio_accounting = false;
923a60
 static bool arg_default_memory_accounting = false;
923a60
+static CADBurstAction arg_cad_burst_action = CAD_BURST_ACTION_REBOOT;
923a60
 
923a60
 static void nop_handler(int sig) {}
923a60
 
923a60
@@ -625,6 +626,8 @@ static int config_parse_join_controllers(const char *unit,
923a60
         return 0;
923a60
 }
923a60
 
923a60
+static DEFINE_CONFIG_PARSE_ENUM(config_parse_cad_burst_action, cad_burst_action, CADBurstAction, "Failed to parse service restart specifier");
923a60
+
923a60
 static int parse_config_file(void) {
923a60
 
923a60
         const ConfigTableItem items[] = {
923a60
@@ -673,6 +676,7 @@ static int parse_config_file(void) {
923a60
                 { "Manager", "DefaultCPUAccounting",      config_parse_bool,             0, &arg_default_cpu_accounting            },
923a60
                 { "Manager", "DefaultBlockIOAccounting",  config_parse_bool,             0, &arg_default_blockio_accounting        },
923a60
                 { "Manager", "DefaultMemoryAccounting",   config_parse_bool,             0, &arg_default_memory_accounting         },
923a60
+                { "Manager", "CtrlAltDelBurstAction",     config_parse_cad_burst_action, 0, &arg_cad_burst_action},
923a60
                 {}
923a60
         };
923a60
 
923a60
@@ -1690,6 +1694,7 @@ int main(int argc, char *argv[]) {
923a60
         m->initrd_timestamp = initrd_timestamp;
923a60
         m->security_start_timestamp = security_start_timestamp;
923a60
         m->security_finish_timestamp = security_finish_timestamp;
923a60
+        m->cad_burst_action = arg_cad_burst_action;
923a60
 
923a60
         manager_set_default_rlimits(m, arg_default_rlimit);
923a60
         manager_environment_add(m, NULL, arg_default_environment);
923a60
diff --git a/src/core/manager.c b/src/core/manager.c
923a60
index 6d045fdf35..9048dde96e 100644
923a60
--- a/src/core/manager.c
923a60
+++ b/src/core/manager.c
923a60
@@ -1859,6 +1859,35 @@ static int manager_start_target(Manager *m, const char *name, JobMode mode) {
923a60
         return r;
923a60
 }
923a60
 
923a60
+static void manager_handle_ctrl_alt_del(Manager *m) {
923a60
+        /* If the user presses C-A-D more than
923a60
+         * 7 times within 2s, we reboot/shutdown immediately,
923a60
+         * unless it was disabled in system.conf */
923a60
+
923a60
+        if (ratelimit_test(&m->ctrl_alt_del_ratelimit) || m->cad_burst_action == CAD_BURST_ACTION_IGNORE)
923a60
+                manager_start_target(m, SPECIAL_CTRL_ALT_DEL_TARGET, JOB_REPLACE_IRREVERSIBLY);
923a60
+        else {
923a60
+                switch (m->cad_burst_action) {
923a60
+
923a60
+                case CAD_BURST_ACTION_REBOOT:
923a60
+                        m->exit_code = MANAGER_REBOOT;
923a60
+                        break;
923a60
+
923a60
+                case CAD_BURST_ACTION_POWEROFF:
923a60
+                        m->exit_code = MANAGER_POWEROFF;
923a60
+                        break;
923a60
+
923a60
+                default:
923a60
+                        assert_not_reached("Unknown action.");
923a60
+                }
923a60
+
923a60
+                log_notice("Ctrl-Alt-Del was pressed more than 7 times within 2s, performing immediate %s.",
923a60
+                                cad_burst_action_to_string(m->cad_burst_action));
923a60
+                status_printf(NULL, true, false, "Ctrl-Alt-Del was pressed more than 7 times within 2s, performing immediate %s.",
923a60
+                                cad_burst_action_to_string(m->cad_burst_action));
923a60
+        }
923a60
+}
923a60
+
923a60
 static int manager_dispatch_signal_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
923a60
         Manager *m = userdata;
923a60
         ssize_t n;
923a60
@@ -1909,19 +1938,7 @@ static int manager_dispatch_signal_fd(sd_event_source *source, int fd, uint32_t
923a60
 
923a60
                 case SIGINT:
923a60
                         if (m->running_as == SYSTEMD_SYSTEM) {
923a60
-
923a60
-                                /* If the user presses C-A-D more than
923a60
-                                 * 7 times within 2s, we reboot
923a60
-                                 * immediately. */
923a60
-
923a60
-                                if (ratelimit_test(&m->ctrl_alt_del_ratelimit))
923a60
-                                        manager_start_target(m, SPECIAL_CTRL_ALT_DEL_TARGET, JOB_REPLACE_IRREVERSIBLY);
923a60
-                                else {
923a60
-                                        log_notice("Ctrl-Alt-Del was pressed more than 7 times within 2s, rebooting immediately.");
923a60
-                                        status_printf(NULL, true, false, "Ctrl-Alt-Del was pressed more than 7 times within 2s, rebooting immediately.");
923a60
-                                        m->exit_code = MANAGER_REBOOT;
923a60
-                                }
923a60
-
923a60
+                                manager_handle_ctrl_alt_del(m);
923a60
                                 break;
923a60
                         }
923a60
 
923a60
@@ -3319,3 +3336,11 @@ static const char *const manager_state_table[_MANAGER_STATE_MAX] = {
923a60
 };
923a60
 
923a60
 DEFINE_STRING_TABLE_LOOKUP(manager_state, ManagerState);
923a60
+
923a60
+static const char *const cad_burst_action_table[_CAD_BURST_ACTION_MAX] = {
923a60
+        [CAD_BURST_ACTION_IGNORE] = "ignore",
923a60
+        [CAD_BURST_ACTION_REBOOT] = "reboot-force",
923a60
+        [CAD_BURST_ACTION_POWEROFF] = "poweroff-force",
923a60
+};
923a60
+
923a60
+DEFINE_STRING_TABLE_LOOKUP(cad_burst_action, CADBurstAction);
923a60
diff --git a/src/core/manager.h b/src/core/manager.h
923a60
index 3e855db466..42be1fc437 100644
923a60
--- a/src/core/manager.h
923a60
+++ b/src/core/manager.h
923a60
@@ -64,6 +64,14 @@ typedef enum ManagerExitCode {
923a60
         _MANAGER_EXIT_CODE_INVALID = -1
923a60
 } ManagerExitCode;
923a60
 
923a60
+typedef enum CADBurstAction {
923a60
+        CAD_BURST_ACTION_IGNORE,
923a60
+        CAD_BURST_ACTION_REBOOT,
923a60
+        CAD_BURST_ACTION_POWEROFF,
923a60
+        _CAD_BURST_ACTION_MAX,
923a60
+        _CAD_BURST_ACTION_INVALID = -1
923a60
+} CADBurstAction;
923a60
+
923a60
 typedef enum StatusType {
923a60
         STATUS_TYPE_EPHEMERAL,
923a60
         STATUS_TYPE_NORMAL,
923a60
@@ -300,8 +308,9 @@ struct Manager {
923a60
         /* Used for processing polkit authorization responses */
923a60
         Hashmap *polkit_registry;
923a60
 
923a60
-        /* When the user hits C-A-D more than 7 times per 2s, reboot immediately... */
923a60
+        /* When the user hits C-A-D more than 7 times per 2s, do something immediately... */
923a60
         RateLimit ctrl_alt_del_ratelimit;
923a60
+        CADBurstAction cad_burst_action;
923a60
 };
923a60
 
923a60
 int manager_new(SystemdRunningAs running_as, bool test_run, Manager **m);
923a60
@@ -372,3 +381,6 @@ ManagerState manager_state(Manager *m);
923a60
 
923a60
 const char *manager_state_to_string(ManagerState m) _const_;
923a60
 ManagerState manager_state_from_string(const char *s) _pure_;
923a60
+
923a60
+const char *cad_burst_action_to_string(CADBurstAction a) _const_;
923a60
+CADBurstAction cad_burst_action_from_string(const char *s) _pure_;
923a60
diff --git a/src/core/system.conf b/src/core/system.conf
923a60
index 231609033b..a11f599038 100644
923a60
--- a/src/core/system.conf
923a60
+++ b/src/core/system.conf
923a60
@@ -20,6 +20,7 @@
923a60
 #CrashShell=no
923a60
 #ShowStatus=yes
923a60
 #CrashChVT=1
923a60
+#CtrlAltDelBurstAction=reboot-force
923a60
 #CPUAffinity=1 2
923a60
 #JoinControllers=cpu,cpuacct net_cls,net_prio
923a60
 #RuntimeWatchdogSec=0