52b84b
From 883dfbff6e3e9763d21f9d029a824c63e016cfd9 Mon Sep 17 00:00:00 2001
52b84b
From: Lennart Poettering <lennart@poettering.net>
52b84b
Date: Tue, 13 Nov 2018 19:57:43 +0100
52b84b
Subject: [PATCH] core: move unit_status_emit_starting_stopping_reloading() and
52b84b
 related calls to job.c
52b84b
MIME-Version: 1.0
52b84b
Content-Type: text/plain; charset=UTF-8
52b84b
Content-Transfer-Encoding: 8bit
52b84b
52b84b
This call is only used by job.c and very specific to job handling.
52b84b
Moreover the very similar logic of job_emit_status_message() is already
52b84b
in job.c.
52b84b
52b84b
Hence, let's clean this up, and move both sets of functions to job.c,
52b84b
and rename them a bit so that they express precisely what they do:
52b84b
52b84b
1. unit_status_emit_starting_stopping_reloading() →
52b84b
   job_emit_begin_status_message()
52b84b
2. job_emit_status_message() → job_emit_done_status_message()
52b84b
52b84b
The first call is after all what we call when we begin with the
52b84b
execution of a job, and the second call what we call when we are done
52b84b
wiht it.
52b84b
52b84b
Just some moving and renaming, not other changes, and hence no change in
52b84b
behaviour.
52b84b
52b84b
(cherry picked from commit 33a3fdd9781329379f74e11a7a2707816aad8c61)
52b84b
52b84b
Related: #1737283
52b84b
---
52b84b
 src/core/job.c  | 119 +++++++++++++++++++++++++++++++++++++++++-------
52b84b
 src/core/unit.c |  86 ----------------------------------
52b84b
 src/core/unit.h |   1 -
52b84b
 3 files changed, 103 insertions(+), 103 deletions(-)
52b84b
52b84b
diff --git a/src/core/job.c b/src/core/job.c
52b84b
index 769ed6d603..561ea14682 100644
52b84b
--- a/src/core/job.c
52b84b
+++ b/src/core/job.c
52b84b
@@ -507,6 +507,93 @@ static void job_change_type(Job *j, JobType newtype) {
52b84b
         j->type = newtype;
52b84b
 }
52b84b
 
52b84b
+_pure_ static const char* job_get_begin_status_message_format(Unit *u, JobType t) {
52b84b
+        const char *format;
52b84b
+        const UnitStatusMessageFormats *format_table;
52b84b
+
52b84b
+        assert(u);
52b84b
+        assert(IN_SET(t, JOB_START, JOB_STOP, JOB_RELOAD));
52b84b
+
52b84b
+        if (t != JOB_RELOAD) {
52b84b
+                format_table = &UNIT_VTABLE(u)->status_message_formats;
52b84b
+                if (format_table) {
52b84b
+                        format = format_table->starting_stopping[t == JOB_STOP];
52b84b
+                        if (format)
52b84b
+                                return format;
52b84b
+                }
52b84b
+        }
52b84b
+
52b84b
+        /* Return generic strings */
52b84b
+        if (t == JOB_START)
52b84b
+                return "Starting %s.";
52b84b
+        else if (t == JOB_STOP)
52b84b
+                return "Stopping %s.";
52b84b
+        else
52b84b
+                return "Reloading %s.";
52b84b
+}
52b84b
+
52b84b
+static void job_print_begin_status_message(Unit *u, JobType t) {
52b84b
+        const char *format;
52b84b
+
52b84b
+        assert(u);
52b84b
+
52b84b
+        /* Reload status messages have traditionally not been printed to console. */
52b84b
+        if (!IN_SET(t, JOB_START, JOB_STOP))
52b84b
+                return;
52b84b
+
52b84b
+        format = job_get_begin_status_message_format(u, t);
52b84b
+
52b84b
+        DISABLE_WARNING_FORMAT_NONLITERAL;
52b84b
+        unit_status_printf(u, "", format);
52b84b
+        REENABLE_WARNING;
52b84b
+}
52b84b
+
52b84b
+static void job_log_begin_status_message(Unit *u, JobType t) {
52b84b
+        const char *format, *mid;
52b84b
+        char buf[LINE_MAX];
52b84b
+
52b84b
+        assert(u);
52b84b
+
52b84b
+        if (!IN_SET(t, JOB_START, JOB_STOP, JOB_RELOAD))
52b84b
+                return;
52b84b
+
52b84b
+        if (log_on_console())
52b84b
+                return;
52b84b
+
52b84b
+        /* We log status messages for all units and all operations. */
52b84b
+
52b84b
+        format = job_get_begin_status_message_format(u, t);
52b84b
+
52b84b
+        DISABLE_WARNING_FORMAT_NONLITERAL;
52b84b
+        (void) snprintf(buf, sizeof buf, format, unit_description(u));
52b84b
+        REENABLE_WARNING;
52b84b
+
52b84b
+        mid = t == JOB_START ? "MESSAGE_ID=" SD_MESSAGE_UNIT_STARTING_STR :
52b84b
+              t == JOB_STOP  ? "MESSAGE_ID=" SD_MESSAGE_UNIT_STOPPING_STR :
52b84b
+                               "MESSAGE_ID=" SD_MESSAGE_UNIT_RELOADING_STR;
52b84b
+
52b84b
+        /* Note that we deliberately use LOG_MESSAGE() instead of
52b84b
+         * LOG_UNIT_MESSAGE() here, since this is supposed to mimic
52b84b
+         * closely what is written to screen using the status output,
52b84b
+         * which is supposed the highest level, friendliest output
52b84b
+         * possible, which means we should avoid the low-level unit
52b84b
+         * name. */
52b84b
+        log_struct(LOG_INFO,
52b84b
+                   LOG_MESSAGE("%s", buf),
52b84b
+                   LOG_UNIT_ID(u),
52b84b
+                   LOG_UNIT_INVOCATION_ID(u),
52b84b
+                   mid);
52b84b
+}
52b84b
+
52b84b
+static void job_emit_begin_status_message(Unit *u, JobType t) {
52b84b
+        assert(u);
52b84b
+        assert(t >= 0);
52b84b
+        assert(t < _JOB_TYPE_MAX);
52b84b
+
52b84b
+        job_log_begin_status_message(u, t);
52b84b
+        job_print_begin_status_message(u, t);
52b84b
+}
52b84b
+
52b84b
 static int job_perform_on_unit(Job **j) {
52b84b
         uint32_t id;
52b84b
         Manager *m;
52b84b
@@ -552,7 +639,7 @@ static int job_perform_on_unit(Job **j) {
52b84b
          * actually did something. */
52b84b
         *j = manager_get_job(m, id);
52b84b
         if (*j && r > 0)
52b84b
-                unit_status_emit_starting_stopping_reloading(u, t);
52b84b
+                job_emit_begin_status_message(u, t);
52b84b
 
52b84b
         return r;
52b84b
 }
52b84b
@@ -638,7 +725,7 @@ int job_run_and_invalidate(Job *j) {
52b84b
         return r;
52b84b
 }
52b84b
 
52b84b
-_pure_ static const char *job_get_status_message_format(Unit *u, JobType t, JobResult result) {
52b84b
+_pure_ static const char *job_get_done_status_message_format(Unit *u, JobType t, JobResult result) {
52b84b
 
52b84b
         static const char *const generic_finished_start_job[_JOB_RESULT_MAX] = {
52b84b
                 [JOB_DONE]        = "Started %s.",
52b84b
@@ -699,7 +786,7 @@ _pure_ static const char *job_get_status_message_format(Unit *u, JobType t, JobR
52b84b
 
52b84b
 static const struct {
52b84b
         const char *color, *word;
52b84b
-} job_print_status_messages [_JOB_RESULT_MAX] = {
52b84b
+} job_print_done_status_messages[_JOB_RESULT_MAX] = {
52b84b
         [JOB_DONE]        = { ANSI_OK_COLOR,         "  OK  " },
52b84b
         [JOB_TIMEOUT]     = { ANSI_HIGHLIGHT_RED,    " TIME " },
52b84b
         [JOB_FAILED]      = { ANSI_HIGHLIGHT_RED,    "FAILED" },
52b84b
@@ -711,7 +798,7 @@ static const struct {
52b84b
         [JOB_ONCE]        = { ANSI_HIGHLIGHT_RED,    " ONCE " },
52b84b
 };
52b84b
 
52b84b
-static void job_print_status_message(Unit *u, JobType t, JobResult result) {
52b84b
+static void job_print_done_status_message(Unit *u, JobType t, JobResult result) {
52b84b
         const char *format;
52b84b
         const char *status;
52b84b
 
52b84b
@@ -723,19 +810,19 @@ static void job_print_status_message(Unit *u, JobType t, JobResult result) {
52b84b
         if (t == JOB_RELOAD)
52b84b
                 return;
52b84b
 
52b84b
-        if (!job_print_status_messages[result].word)
52b84b
+        if (!job_print_done_status_messages[result].word)
52b84b
                 return;
52b84b
 
52b84b
-        format = job_get_status_message_format(u, t, result);
52b84b
+        format = job_get_done_status_message_format(u, t, result);
52b84b
         if (!format)
52b84b
                 return;
52b84b
 
52b84b
         if (log_get_show_color())
52b84b
-                status = strjoina(job_print_status_messages[result].color,
52b84b
-                                  job_print_status_messages[result].word,
52b84b
+                status = strjoina(job_print_done_status_messages[result].color,
52b84b
+                                  job_print_done_status_messages[result].word,
52b84b
                                   ANSI_NORMAL);
52b84b
         else
52b84b
-                status = job_print_status_messages[result].word;
52b84b
+                status = job_print_done_status_messages[result].word;
52b84b
 
52b84b
         if (result != JOB_DONE)
52b84b
                 manager_flip_auto_status(u->manager, true);
52b84b
@@ -752,7 +839,7 @@ static void job_print_status_message(Unit *u, JobType t, JobResult result) {
52b84b
         }
52b84b
 }
52b84b
 
52b84b
-static void job_log_status_message(Unit *u, JobType t, JobResult result) {
52b84b
+static void job_log_done_status_message(Unit *u, uint32_t job_id, JobType t, JobResult result) {
52b84b
         const char *format, *mid;
52b84b
         char buf[LINE_MAX];
52b84b
         static const int job_result_log_level[_JOB_RESULT_MAX] = {
52b84b
@@ -775,10 +862,10 @@ static void job_log_status_message(Unit *u, JobType t, JobResult result) {
52b84b
 
52b84b
         /* Skip printing if output goes to the console, and job_print_status_message()
52b84b
            will actually print something to the console. */
52b84b
-        if (log_on_console() && job_print_status_messages[result].word)
52b84b
+        if (log_on_console() && job_print_done_status_messages[result].word)
52b84b
                 return;
52b84b
 
52b84b
-        format = job_get_status_message_format(u, t, result);
52b84b
+        format = job_get_done_status_message_format(u, t, result);
52b84b
         if (!format)
52b84b
                 return;
52b84b
 
52b84b
@@ -827,15 +914,15 @@ static void job_log_status_message(Unit *u, JobType t, JobResult result) {
52b84b
                    mid);
52b84b
 }
52b84b
 
52b84b
-static void job_emit_status_message(Unit *u, JobType t, JobResult result) {
52b84b
+static void job_emit_done_status_message(Unit *u, uint32_t job_id, JobType t, JobResult result) {
52b84b
         assert(u);
52b84b
 
52b84b
         /* No message if the job did not actually do anything due to failed condition. */
52b84b
         if (t == JOB_START && result == JOB_DONE && !u->condition_result)
52b84b
                 return;
52b84b
 
52b84b
-        job_log_status_message(u, t, result);
52b84b
-        job_print_status_message(u, t, result);
52b84b
+        job_log_done_status_message(u, job_id, t, result);
52b84b
+        job_print_done_status_message(u, t, result);
52b84b
 }
52b84b
 
52b84b
 static void job_fail_dependencies(Unit *u, UnitDependency d) {
52b84b
@@ -890,7 +977,7 @@ int job_finish_and_invalidate(Job *j, JobResult result, bool recursive, bool alr
52b84b
 
52b84b
         /* If this job did nothing to respective unit we don't log the status message */
52b84b
         if (!already)
52b84b
-                job_emit_status_message(u, t, result);
52b84b
+                job_emit_done_status_message(u, j->id, t, result);
52b84b
 
52b84b
         /* Patch restart jobs so that they become normal start jobs */
52b84b
         if (result == JOB_DONE && t == JOB_RESTART) {
52b84b
diff --git a/src/core/unit.c b/src/core/unit.c
52b84b
index 40f138d25c..f5e251123d 100644
52b84b
--- a/src/core/unit.c
52b84b
+++ b/src/core/unit.c
52b84b
@@ -1624,92 +1624,6 @@ void unit_status_printf(Unit *u, const char *status, const char *unit_status_msg
52b84b
         REENABLE_WARNING;
52b84b
 }
52b84b
 
52b84b
-_pure_ static const char* unit_get_status_message_format(Unit *u, JobType t) {
52b84b
-        const char *format;
52b84b
-        const UnitStatusMessageFormats *format_table;
52b84b
-
52b84b
-        assert(u);
52b84b
-        assert(IN_SET(t, JOB_START, JOB_STOP, JOB_RELOAD));
52b84b
-
52b84b
-        if (t != JOB_RELOAD) {
52b84b
-                format_table = &UNIT_VTABLE(u)->status_message_formats;
52b84b
-                if (format_table) {
52b84b
-                        format = format_table->starting_stopping[t == JOB_STOP];
52b84b
-                        if (format)
52b84b
-                                return format;
52b84b
-                }
52b84b
-        }
52b84b
-
52b84b
-        /* Return generic strings */
52b84b
-        if (t == JOB_START)
52b84b
-                return "Starting %s.";
52b84b
-        else if (t == JOB_STOP)
52b84b
-                return "Stopping %s.";
52b84b
-        else
52b84b
-                return "Reloading %s.";
52b84b
-}
52b84b
-
52b84b
-static void unit_status_print_starting_stopping(Unit *u, JobType t) {
52b84b
-        const char *format;
52b84b
-
52b84b
-        assert(u);
52b84b
-
52b84b
-        /* Reload status messages have traditionally not been printed to console. */
52b84b
-        if (!IN_SET(t, JOB_START, JOB_STOP))
52b84b
-                return;
52b84b
-
52b84b
-        format = unit_get_status_message_format(u, t);
52b84b
-
52b84b
-        DISABLE_WARNING_FORMAT_NONLITERAL;
52b84b
-        unit_status_printf(u, "", format);
52b84b
-        REENABLE_WARNING;
52b84b
-}
52b84b
-
52b84b
-static void unit_status_log_starting_stopping_reloading(Unit *u, JobType t) {
52b84b
-        const char *format, *mid;
52b84b
-        char buf[LINE_MAX];
52b84b
-
52b84b
-        assert(u);
52b84b
-
52b84b
-        if (!IN_SET(t, JOB_START, JOB_STOP, JOB_RELOAD))
52b84b
-                return;
52b84b
-
52b84b
-        if (log_on_console())
52b84b
-                return;
52b84b
-
52b84b
-        /* We log status messages for all units and all operations. */
52b84b
-
52b84b
-        format = unit_get_status_message_format(u, t);
52b84b
-
52b84b
-        DISABLE_WARNING_FORMAT_NONLITERAL;
52b84b
-        (void) snprintf(buf, sizeof buf, format, unit_description(u));
52b84b
-        REENABLE_WARNING;
52b84b
-
52b84b
-        mid = t == JOB_START ? "MESSAGE_ID=" SD_MESSAGE_UNIT_STARTING_STR :
52b84b
-              t == JOB_STOP  ? "MESSAGE_ID=" SD_MESSAGE_UNIT_STOPPING_STR :
52b84b
-                               "MESSAGE_ID=" SD_MESSAGE_UNIT_RELOADING_STR;
52b84b
-
52b84b
-        /* Note that we deliberately use LOG_MESSAGE() instead of
52b84b
-         * LOG_UNIT_MESSAGE() here, since this is supposed to mimic
52b84b
-         * closely what is written to screen using the status output,
52b84b
-         * which is supposed the highest level, friendliest output
52b84b
-         * possible, which means we should avoid the low-level unit
52b84b
-         * name. */
52b84b
-        log_struct(LOG_INFO,
52b84b
-                   LOG_MESSAGE("%s", buf),
52b84b
-                   LOG_UNIT_ID(u),
52b84b
-                   LOG_UNIT_INVOCATION_ID(u),
52b84b
-                   mid);
52b84b
-}
52b84b
-
52b84b
-void unit_status_emit_starting_stopping_reloading(Unit *u, JobType t) {
52b84b
-        assert(u);
52b84b
-        assert(t >= 0);
52b84b
-        assert(t < _JOB_TYPE_MAX);
52b84b
-
52b84b
-        unit_status_log_starting_stopping_reloading(u, t);
52b84b
-        unit_status_print_starting_stopping(u, t);
52b84b
-}
52b84b
 
52b84b
 int unit_start_limit_test(Unit *u) {
52b84b
         assert(u);
52b84b
diff --git a/src/core/unit.h b/src/core/unit.h
52b84b
index 595ee88d43..4d9539790a 100644
52b84b
--- a/src/core/unit.h
52b84b
+++ b/src/core/unit.h
52b84b
@@ -699,7 +699,6 @@ int unit_coldplug(Unit *u);
52b84b
 void unit_catchup(Unit *u);
52b84b
 
52b84b
 void unit_status_printf(Unit *u, const char *status, const char *unit_status_msg_format) _printf_(3, 0);
52b84b
-void unit_status_emit_starting_stopping_reloading(Unit *u, JobType t);
52b84b
 
52b84b
 bool unit_need_daemon_reload(Unit *u);
52b84b