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