be0c12
From c8e9877d14c8742cc3732d305af2422f8a16f47d Mon Sep 17 00:00:00 2001
be0c12
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
be0c12
Date: Wed, 17 Oct 2018 17:27:20 +0200
be0c12
Subject: [PATCH] core: do not "warn" about mundane emergency actions
be0c12
be0c12
For example in a container we'd log:
be0c12
Oct 17 17:01:10 rawhide systemd[1]: Started Power-Off.
be0c12
Oct 17 17:01:10 rawhide systemd[1]: Forcibly powering off: unit succeeded
be0c12
Oct 17 17:01:10 rawhide systemd[1]: Reached target Power-Off.
be0c12
Oct 17 17:01:10 rawhide systemd[1]: Shutting down.
be0c12
and on the console we'd write (in red)
be0c12
[  !!  ] Forcibly powering off: unit succeeded
be0c12
be0c12
This is not useful in any way, and the fact that we're calling an "emergency action"
be0c12
is an internal implementation detail. Let's log about c-a-d and the watchdog actions
be0c12
only.
be0c12
be0c12
(cherry picked from commit c7adcb1af9946d0672c16bb4bb7eedf39b3d1fcb)
be0c12
be0c12
Related: #1860899
be0c12
---
be0c12
 src/core/emergency-action.c | 29 ++++++++++++++++-------------
be0c12
 src/core/emergency-action.h |  1 +
be0c12
 src/core/job.c              |  3 ++-
be0c12
 src/core/manager.c          |  2 +-
be0c12
 src/core/unit.c             |  3 ++-
be0c12
 5 files changed, 22 insertions(+), 16 deletions(-)
be0c12
be0c12
diff --git a/src/core/emergency-action.c b/src/core/emergency-action.c
be0c12
index 44b92ae6f8..fea1cb83db 100644
be0c12
--- a/src/core/emergency-action.c
be0c12
+++ b/src/core/emergency-action.c
be0c12
@@ -15,11 +15,12 @@
be0c12
 #include "terminal-util.h"
be0c12
 #include "virt.h"
be0c12
 
be0c12
-static void log_and_status(Manager *m, const char *message, const char *reason) {
be0c12
-        log_warning("%s: %s", message, reason);
be0c12
-        manager_status_printf(m, STATUS_TYPE_EMERGENCY,
be0c12
-                              ANSI_HIGHLIGHT_RED "  !!  " ANSI_NORMAL,
be0c12
-                              "%s: %s", message, reason);
be0c12
+static void log_and_status(Manager *m, bool warn, const char *message, const char *reason) {
be0c12
+        log_full(warn ? LOG_WARNING : LOG_DEBUG, "%s: %s", message, reason);
be0c12
+        if (warn)
be0c12
+                manager_status_printf(m, STATUS_TYPE_EMERGENCY,
be0c12
+                                      ANSI_HIGHLIGHT_RED "  !!  " ANSI_NORMAL,
be0c12
+                                      "%s: %s", message, reason);
be0c12
 }
be0c12
 
be0c12
 int emergency_action(
be0c12
@@ -41,17 +42,19 @@ int emergency_action(
be0c12
                 return -ECANCELED;
be0c12
         }
be0c12
 
be0c12
+        bool warn = FLAGS_SET(options, EMERGENCY_ACTION_WARN);
be0c12
+
be0c12
         switch (action) {
be0c12
 
be0c12
         case EMERGENCY_ACTION_REBOOT:
be0c12
-                log_and_status(m, "Rebooting", reason);
be0c12
+                log_and_status(m, warn, "Rebooting", reason);
be0c12
 
be0c12
                 (void) update_reboot_parameter_and_warn(reboot_arg);
be0c12
                 (void) manager_add_job_by_name_and_warn(m, JOB_START, SPECIAL_REBOOT_TARGET, JOB_REPLACE_IRREVERSIBLY, NULL, NULL);
be0c12
                 break;
be0c12
 
be0c12
         case EMERGENCY_ACTION_REBOOT_FORCE:
be0c12
-                log_and_status(m, "Forcibly rebooting", reason);
be0c12
+                log_and_status(m, warn, "Forcibly rebooting", reason);
be0c12
 
be0c12
                 (void) update_reboot_parameter_and_warn(reboot_arg);
be0c12
                 m->exit_code = MANAGER_REBOOT;
be0c12
@@ -59,7 +62,7 @@ int emergency_action(
be0c12
                 break;
be0c12
 
be0c12
         case EMERGENCY_ACTION_REBOOT_IMMEDIATE:
be0c12
-                log_and_status(m, "Rebooting immediately", reason);
be0c12
+                log_and_status(m, warn, "Rebooting immediately", reason);
be0c12
 
be0c12
                 sync();
be0c12
 
be0c12
@@ -75,7 +78,7 @@ int emergency_action(
be0c12
 
be0c12
         case EMERGENCY_ACTION_EXIT:
be0c12
                 if (MANAGER_IS_USER(m) || detect_container() > 0) {
be0c12
-                        log_and_status(m, "Exiting", reason);
be0c12
+                        log_and_status(m, warn, "Exiting", reason);
be0c12
                         (void) manager_add_job_by_name_and_warn(m, JOB_START, SPECIAL_EXIT_TARGET, JOB_REPLACE_IRREVERSIBLY, NULL, NULL);
be0c12
                         break;
be0c12
                 }
be0c12
@@ -84,13 +87,13 @@ int emergency_action(
be0c12
                 _fallthrough_;
be0c12
 
be0c12
         case EMERGENCY_ACTION_POWEROFF:
be0c12
-                log_and_status(m, "Powering off", reason);
be0c12
+                log_and_status(m, warn, "Powering off", reason);
be0c12
                 (void) manager_add_job_by_name_and_warn(m, JOB_START, SPECIAL_POWEROFF_TARGET, JOB_REPLACE_IRREVERSIBLY, NULL, NULL);
be0c12
                 break;
be0c12
 
be0c12
         case EMERGENCY_ACTION_EXIT_FORCE:
be0c12
                 if (MANAGER_IS_USER(m) || detect_container() > 0) {
be0c12
-                        log_and_status(m, "Exiting immediately", reason);
be0c12
+                        log_and_status(m, warn, "Exiting immediately", reason);
be0c12
                         m->exit_code = MANAGER_EXIT;
be0c12
                         break;
be0c12
                 }
be0c12
@@ -99,12 +102,12 @@ int emergency_action(
be0c12
                 _fallthrough_;
be0c12
 
be0c12
         case EMERGENCY_ACTION_POWEROFF_FORCE:
be0c12
-                log_and_status(m, "Forcibly powering off", reason);
be0c12
+                log_and_status(m, warn, "Forcibly powering off", reason);
be0c12
                 m->exit_code = MANAGER_POWEROFF;
be0c12
                 break;
be0c12
 
be0c12
         case EMERGENCY_ACTION_POWEROFF_IMMEDIATE:
be0c12
-                log_and_status(m, "Powering off immediately", reason);
be0c12
+                log_and_status(m, warn, "Powering off immediately", reason);
be0c12
 
be0c12
                 sync();
be0c12
 
be0c12
diff --git a/src/core/emergency-action.h b/src/core/emergency-action.h
be0c12
index efbfaf6c6a..2aa1497118 100644
be0c12
--- a/src/core/emergency-action.h
be0c12
+++ b/src/core/emergency-action.h
be0c12
@@ -22,6 +22,7 @@ typedef enum EmergencyAction {
be0c12
 
be0c12
 typedef enum EmergencyActionFlags {
be0c12
         EMERGENCY_ACTION_IS_WATCHDOG = 1 << 0,
be0c12
+        EMERGENCY_ACTION_WARN        = 1 << 1,
be0c12
 } EmergencyActionFlags;
be0c12
 
be0c12
 #include "macro.h"
be0c12
diff --git a/src/core/job.c b/src/core/job.c
be0c12
index d647aac42d..43ab55ed18 100644
be0c12
--- a/src/core/job.c
be0c12
+++ b/src/core/job.c
be0c12
@@ -1076,7 +1076,8 @@ static int job_dispatch_timer(sd_event_source *s, uint64_t monotonic, void *user
be0c12
         u = j->unit;
be0c12
         job_finish_and_invalidate(j, JOB_TIMEOUT, true, false);
be0c12
 
be0c12
-        emergency_action(u->manager, u->job_timeout_action, EMERGENCY_ACTION_IS_WATCHDOG,
be0c12
+        emergency_action(u->manager, u->job_timeout_action,
be0c12
+                         EMERGENCY_ACTION_IS_WATCHDOG|EMERGENCY_ACTION_WARN,
be0c12
                          u->job_timeout_reboot_arg, "job timed out");
be0c12
 
be0c12
         return 0;
be0c12
diff --git a/src/core/manager.c b/src/core/manager.c
be0c12
index ac1b198b21..ee976f70b3 100644
be0c12
--- a/src/core/manager.c
be0c12
+++ b/src/core/manager.c
be0c12
@@ -2528,7 +2528,7 @@ static void manager_handle_ctrl_alt_del(Manager *m) {
be0c12
         if (ratelimit_below(&m->ctrl_alt_del_ratelimit) || m->cad_burst_action == EMERGENCY_ACTION_NONE)
be0c12
                 manager_start_target(m, SPECIAL_CTRL_ALT_DEL_TARGET, JOB_REPLACE_IRREVERSIBLY);
be0c12
         else
be0c12
-                emergency_action(m, m->cad_burst_action, 0, NULL, 
be0c12
+                emergency_action(m, m->cad_burst_action, EMERGENCY_ACTION_WARN, NULL,
be0c12
                                 "Ctrl-Alt-Del was pressed more than 7 times within 2s");
be0c12
 }
be0c12
 
be0c12
diff --git a/src/core/unit.c b/src/core/unit.c
be0c12
index dc5c89c195..23afa24c77 100644
be0c12
--- a/src/core/unit.c
be0c12
+++ b/src/core/unit.c
be0c12
@@ -1669,7 +1669,8 @@ int unit_start_limit_test(Unit *u) {
be0c12
         log_unit_warning(u, "Start request repeated too quickly.");
be0c12
         u->start_limit_hit = true;
be0c12
 
be0c12
-        return emergency_action(u->manager, u->start_limit_action, EMERGENCY_ACTION_IS_WATCHDOG,
be0c12
+        return emergency_action(u->manager, u->start_limit_action,
be0c12
+                                EMERGENCY_ACTION_IS_WATCHDOG|EMERGENCY_ACTION_WARN,
be0c12
                                 u->reboot_arg, "unit failed");
be0c12
 }
be0c12