valeriyvdovin / rpms / systemd

Forked from rpms/systemd 4 years ago
Clone
923a60
From 50ce13182e07af7f240c61d03bf113e86a269917 Mon Sep 17 00:00:00 2001
923a60
From: Michal Schmidt <mschmidt@redhat.com>
923a60
Date: Tue, 21 Jul 2015 14:54:24 +0200
923a60
Subject: [PATCH] core: try harder to get job completion messages too
923a60
923a60
This is similar to "core: always try harder to get unit status
923a60
message format string", but for job completion status messages.
923a60
It makes generic status messages applicable for printing to the console.
923a60
And it rewrites the functions in a more table-based style.
923a60
923a60
(cherry picked from commit aa49ab5f22c0fdc7a5381d4e452f40705f3d7bf8)
923a60
923a60
Related: #1506256
923a60
---
923a60
 src/core/job.c | 192 ++++++++++++++++++-------------------------------
923a60
 1 file changed, 68 insertions(+), 124 deletions(-)
923a60
923a60
diff --git a/src/core/job.c b/src/core/job.c
923a60
index 612caa6048..f371f914d4 100644
923a60
--- a/src/core/job.c
923a60
+++ b/src/core/job.c
923a60
@@ -622,156 +622,100 @@ int job_run_and_invalidate(Job *j) {
923a60
 }
923a60
 
923a60
 _pure_ static const char *job_get_status_message_format(Unit *u, JobType t, JobResult result) {
923a60
+        const char *format;
923a60
         const UnitStatusMessageFormats *format_table;
923a60
+        static const char *const generic_finished_start_job[_JOB_RESULT_MAX] = {
923a60
+                [JOB_DONE]        = "Started %s.",
923a60
+                [JOB_TIMEOUT]     = "Timed out starting %s.",
923a60
+                [JOB_FAILED]      = "Failed to start %s.",
923a60
+                [JOB_DEPENDENCY]  = "Dependency failed for %s.",
923a60
+                [JOB_ASSERT]      = "Assertion failed for %s.",
923a60
+                [JOB_UNSUPPORTED] = "Starting of %s not supported.",
923a60
+        };
923a60
+        static const char *const generic_finished_stop_job[_JOB_RESULT_MAX] = {
923a60
+                [JOB_DONE]        = "Stopped %s.",
923a60
+                [JOB_FAILED]      = "Stopped (with error) %s.",
923a60
+                [JOB_TIMEOUT]     = "Timed out stoppping %s.",
923a60
+        };
923a60
+        static const char *const generic_finished_reload_job[_JOB_RESULT_MAX] = {
923a60
+                [JOB_DONE]        = "Reloaded %s.",
923a60
+                [JOB_FAILED]      = "Reload failed for %s.",
923a60
+                [JOB_TIMEOUT]     = "Timed out reloading %s.",
923a60
+        };
923a60
+        /* When verify-active detects the unit is inactive, report it.
923a60
+         * Most likely a DEPEND warning from a requisiting unit will
923a60
+         * occur next and it's nice to see what was requisited. */
923a60
+        static const char *const generic_finished_verify_active_job[_JOB_RESULT_MAX] = {
923a60
+                [JOB_SKIPPED]     = "%s is not active.",
923a60
+        };
923a60
 
923a60
         assert(u);
923a60
         assert(t >= 0);
923a60
         assert(t < _JOB_TYPE_MAX);
923a60
 
923a60
-        format_table = &UNIT_VTABLE(u)->status_message_formats;
923a60
-        if (!format_table)
923a60
-                return NULL;
923a60
+        if (t == JOB_START || t == JOB_STOP || t == JOB_RESTART) {
923a60
+                format_table = &UNIT_VTABLE(u)->status_message_formats;
923a60
+                if (format_table) {
923a60
+                        format = t == JOB_START ? format_table->finished_start_job[result] :
923a60
+                                                  format_table->finished_stop_job[result];
923a60
+                        if (format)
923a60
+                                return format;
923a60
+                }
923a60
+        }
923a60
 
923a60
+        /* Return generic strings */
923a60
         if (t == JOB_START)
923a60
-                return format_table->finished_start_job[result];
923a60
+                return generic_finished_start_job[result];
923a60
         else if (t == JOB_STOP || t == JOB_RESTART)
923a60
-                return format_table->finished_stop_job[result];
923a60
-
923a60
-        return NULL;
923a60
-}
923a60
-
923a60
-_pure_ static const char *job_get_status_message_format_try_harder(Unit *u, JobType t, JobResult result) {
923a60
-        const char *format;
923a60
-
923a60
-        assert(u);
923a60
-        assert(t >= 0);
923a60
-        assert(t < _JOB_TYPE_MAX);
923a60
-
923a60
-        format = job_get_status_message_format(u, t, result);
923a60
-        if (format)
923a60
-                return format;
923a60
-
923a60
-        /* Return generic strings */
923a60
-        if (t == JOB_START) {
923a60
-                if (result == JOB_DONE)
923a60
-                        return "Started %s.";
923a60
-                else if (result == JOB_TIMEOUT)
923a60
-                        return "Timed out starting %s.";
923a60
-                else if (result == JOB_FAILED)
923a60
-                        return "Failed to start %s.";
923a60
-                else if (result == JOB_DEPENDENCY)
923a60
-                        return "Dependency failed for %s.";
923a60
-                else if (result == JOB_ASSERT)
923a60
-                        return "Assertion failed for %s.";
923a60
-                else if (result == JOB_UNSUPPORTED)
923a60
-                        return "Starting of %s not supported.";
923a60
-        } else if (t == JOB_STOP || t == JOB_RESTART) {
923a60
-                if (result == JOB_DONE)
923a60
-                        return "Stopped %s.";
923a60
-                else if (result == JOB_FAILED)
923a60
-                        return "Stopped (with error) %s.";
923a60
-                else if (result == JOB_TIMEOUT)
923a60
-                        return "Timed out stoppping %s.";
923a60
-        } else if (t == JOB_RELOAD) {
923a60
-                if (result == JOB_DONE)
923a60
-                        return "Reloaded %s.";
923a60
-                else if (result == JOB_FAILED)
923a60
-                        return "Reload failed for %s.";
923a60
-                else if (result == JOB_TIMEOUT)
923a60
-                        return "Timed out reloading %s.";
923a60
-        }
923a60
+                return generic_finished_stop_job[result];
923a60
+        else if (t == JOB_RELOAD)
923a60
+                return generic_finished_reload_job[result];
923a60
+        else if (t == JOB_VERIFY_ACTIVE)
923a60
+                return generic_finished_verify_active_job[result];
923a60
 
923a60
         return NULL;
923a60
 }
923a60
 
923a60
 static void job_print_status_message(Unit *u, JobType t, JobResult result) {
923a60
         const char *format;
923a60
+        static const char* const job_result_status_table[_JOB_RESULT_MAX] = {
923a60
+                [JOB_DONE]        = ANSI_GREEN_ON            "  OK  " ANSI_HIGHLIGHT_OFF,
923a60
+                [JOB_TIMEOUT]     = ANSI_HIGHLIGHT_RED_ON    " TIME " ANSI_HIGHLIGHT_OFF,
923a60
+                [JOB_FAILED]      = ANSI_HIGHLIGHT_RED_ON    "FAILED" ANSI_HIGHLIGHT_OFF,
923a60
+                [JOB_DEPENDENCY]  = ANSI_HIGHLIGHT_YELLOW_ON "DEPEND" ANSI_HIGHLIGHT_OFF,
923a60
+                [JOB_SKIPPED]     = ANSI_HIGHLIGHT_ON        " INFO " ANSI_HIGHLIGHT_OFF,
923a60
+                [JOB_ASSERT]      = ANSI_HIGHLIGHT_YELLOW_ON "ASSERT" ANSI_HIGHLIGHT_OFF,
923a60
+                [JOB_UNSUPPORTED] = ANSI_HIGHLIGHT_YELLOW_ON "UNSUPP" ANSI_HIGHLIGHT_OFF,
923a60
+        };
923a60
 
923a60
         assert(u);
923a60
         assert(t >= 0);
923a60
         assert(t < _JOB_TYPE_MAX);
923a60
 
923a60
-        DISABLE_WARNING_FORMAT_NONLITERAL;
923a60
-
923a60
-        if (t == JOB_START) {
923a60
-                format = job_get_status_message_format(u, t, result);
923a60
-                if (!format)
923a60
-                        return;
923a60
-
923a60
-                switch (result) {
923a60
-
923a60
-                case JOB_DONE:
923a60
-                        if (u->condition_result)
923a60
-                                unit_status_printf(u, ANSI_GREEN_ON "  OK  " ANSI_HIGHLIGHT_OFF, format);
923a60
-                        break;
923a60
-
923a60
-                case JOB_TIMEOUT:
923a60
-                        manager_flip_auto_status(u->manager, true);
923a60
-                        unit_status_printf(u, ANSI_HIGHLIGHT_RED_ON " TIME " ANSI_HIGHLIGHT_OFF, format);
923a60
-                        break;
923a60
-
923a60
-                case JOB_FAILED: {
923a60
-                        _cleanup_free_ char *quoted = NULL;
923a60
-
923a60
-                        quoted = shell_maybe_quote(u->id);
923a60
-
923a60
-                        manager_flip_auto_status(u->manager, true);
923a60
-                        unit_status_printf(u, ANSI_HIGHLIGHT_RED_ON "FAILED" ANSI_HIGHLIGHT_OFF, format);
923a60
-                        manager_status_printf(u->manager, STATUS_TYPE_NORMAL, NULL, "See 'systemctl status %s' for details.", strna(quoted));
923a60
-                        break;
923a60
-                }
923a60
-
923a60
-                case JOB_DEPENDENCY:
923a60
-                        manager_flip_auto_status(u->manager, true);
923a60
-                        unit_status_printf(u, ANSI_HIGHLIGHT_YELLOW_ON "DEPEND" ANSI_HIGHLIGHT_OFF, format);
923a60
-                        break;
923a60
-
923a60
-                case JOB_ASSERT:
923a60
-                        manager_flip_auto_status(u->manager, true);
923a60
-                        unit_status_printf(u, ANSI_HIGHLIGHT_YELLOW_ON "ASSERT" ANSI_HIGHLIGHT_OFF, format);
923a60
-                        break;
923a60
-
923a60
-                case JOB_UNSUPPORTED:
923a60
-                        manager_flip_auto_status(u->manager, true);
923a60
-                        unit_status_printf(u, ANSI_HIGHLIGHT_YELLOW_ON "UNSUPP" ANSI_HIGHLIGHT_OFF, format);
923a60
-                        break;
923a60
-
923a60
-                default:
923a60
-                        ;
923a60
-                }
923a60
-
923a60
-        } else if (t == JOB_STOP || t == JOB_RESTART) {
923a60
-
923a60
-                format = job_get_status_message_format(u, t, result);
923a60
-                if (!format)
923a60
-                        return;
923a60
+        /* Reload status messages have traditionally not been printed to console. */
923a60
+        if (t == JOB_RELOAD)
923a60
+                return;
923a60
 
923a60
-                switch (result) {
923a60
+        if (t == JOB_START && result == JOB_DONE && !u->condition_result)
923a60
+                return;
923a60
 
923a60
-                case JOB_TIMEOUT:
923a60
-                        manager_flip_auto_status(u->manager, true);
923a60
-                        unit_status_printf(u, ANSI_HIGHLIGHT_RED_ON " TIME " ANSI_HIGHLIGHT_OFF, format);
923a60
-                        break;
923a60
+        format = job_get_status_message_format(u, t, result);
923a60
+        if (!format)
923a60
+                return;
923a60
 
923a60
-                case JOB_DONE:
923a60
-                case JOB_FAILED:
923a60
-                        unit_status_printf(u, ANSI_GREEN_ON "  OK  " ANSI_HIGHLIGHT_OFF, format);
923a60
-                        break;
923a60
+        if (result != JOB_DONE)
923a60
+                manager_flip_auto_status(u->manager, true);
923a60
 
923a60
-                default:
923a60
-                        ;
923a60
-                }
923a60
+        DISABLE_WARNING_FORMAT_NONLITERAL;
923a60
+        unit_status_printf(u, job_result_status_table[result], format);
923a60
+        REENABLE_WARNING;
923a60
 
923a60
-        } else if (t == JOB_VERIFY_ACTIVE) {
923a60
+        if (t == JOB_START && result == JOB_FAILED) {
923a60
+                _cleanup_free_ char *quoted = shell_maybe_quote(u->id);
923a60
 
923a60
-                /* When verify-active detects the unit is inactive, report it.
923a60
-                 * Most likely a DEPEND warning from a requisiting unit will
923a60
-                 * occur next and it's nice to see what was requisited. */
923a60
-                if (result == JOB_SKIPPED)
923a60
-                        unit_status_printf(u, ANSI_HIGHLIGHT_ON " INFO " ANSI_HIGHLIGHT_OFF, "%s is not active.");
923a60
+                manager_status_printf(u->manager, STATUS_TYPE_NORMAL, NULL,
923a60
+                                      "See 'systemctl status %s' for details.", strna(quoted));
923a60
         }
923a60
-
923a60
-        REENABLE_WARNING;
923a60
 }
923a60
 
923a60
 static void job_log_status_message(Unit *u, JobType t, JobResult result) {
923a60
@@ -788,7 +732,7 @@ static void job_log_status_message(Unit *u, JobType t, JobResult result) {
923a60
         if (log_on_console())
923a60
                 return;
923a60
 
923a60
-        format = job_get_status_message_format_try_harder(u, t, result);
923a60
+        format = job_get_status_message_format(u, t, result);
923a60
         if (!format)
923a60
                 return;
923a60