923a60
From 7bc07eb6c9a31f2c26d0fe3e6d7a26a13cbb2369 Mon Sep 17 00:00:00 2001
923a60
From: Michal Schmidt <mschmidt@redhat.com>
923a60
Date: Thu, 16 Jul 2015 20:08:30 +0200
923a60
Subject: [PATCH] core: fix confusing logging of instantaneous jobs
923a60
923a60
For instantaneous jobs (e.g. starting of targets, sockets, slices, or
923a60
Type=simple services) the log shows the job completion
923a60
before starting:
923a60
923a60
        systemd[1]: Created slice -.slice.
923a60
        systemd[1]: Starting -.slice.
923a60
        systemd[1]: Created slice System Slice.
923a60
        systemd[1]: Starting System Slice.
923a60
        systemd[1]: Listening on Journal Audit Socket.
923a60
        systemd[1]: Starting Journal Audit Socket.
923a60
        systemd[1]: Reached target Timers.
923a60
        systemd[1]: Starting Timers.
923a60
        ...
923a60
923a60
The reason is that the job completes before the ->start() method returns
923a60
and only then does unit_start() print the "Starting ..." message.
923a60
The same thing happens when stopping units.
923a60
923a60
Rather than fixing the order of the messages, let's just not emit the
923a60
Starting/Stopping message at all when the job completes instantaneously.
923a60
The job completion message is sufficient in this case.
923a60
923a60
(cherry picked from commit d1a34ae9c20f1c02aab17884919eccef572b1d21)
923a60
923a60
Resolves: #1506256
923a60
---
923a60
 src/core/job.c  | 65 ++++++++++++++++++++++++++++++++++---------------
923a60
 src/core/unit.c | 36 +++++++++------------------
923a60
 src/core/unit.h |  1 +
923a60
 3 files changed, 58 insertions(+), 44 deletions(-)
923a60
923a60
diff --git a/src/core/job.c b/src/core/job.c
923a60
index c9a43a4cb5..612caa6048 100644
923a60
--- a/src/core/job.c
923a60
+++ b/src/core/job.c
923a60
@@ -504,10 +504,48 @@ static void job_change_type(Job *j, JobType newtype) {
923a60
         j->type = newtype;
923a60
 }
923a60
 
923a60
+static int job_perform_on_unit(Job **j) {
923a60
+        /* While we execute this operation the job might go away (for
923a60
+         * example: because it finishes immediately or is replaced by a new,
923a60
+         * conflicting job.) To make sure we don't access a freed job later on
923a60
+         * we store the id here, so that we can verify the job is still
923a60
+         * valid. */
923a60
+        Manager *m  = (*j)->manager;
923a60
+        Unit *u     = (*j)->unit;
923a60
+        JobType t   = (*j)->type;
923a60
+        uint32_t id = (*j)->id;
923a60
+        int r;
923a60
+
923a60
+        switch (t) {
923a60
+                case JOB_START:
923a60
+                        r = unit_start(u);
923a60
+                        break;
923a60
+
923a60
+                case JOB_RESTART:
923a60
+                        t = JOB_STOP;
923a60
+                case JOB_STOP:
923a60
+                        r = unit_stop(u);
923a60
+                        break;
923a60
+
923a60
+                case JOB_RELOAD:
923a60
+                        r = unit_reload(u);
923a60
+                        break;
923a60
+
923a60
+                default:
923a60
+                        assert_not_reached("Invalid job type");
923a60
+        }
923a60
+
923a60
+        /* Log if the job still exists and the start/stop/reload function
923a60
+         * actually did something. */
923a60
+        *j = manager_get_job(m, id);
923a60
+        if (*j && r > 0)
923a60
+                unit_status_emit_starting_stopping_reloading(u, t);
923a60
+
923a60
+        return r;
923a60
+}
923a60
+
923a60
 int job_run_and_invalidate(Job *j) {
923a60
         int r;
923a60
-        uint32_t id;
923a60
-        Manager *m = j->manager;
923a60
 
923a60
         assert(j);
923a60
         assert(j->installed);
923a60
@@ -526,23 +564,9 @@ int job_run_and_invalidate(Job *j) {
923a60
         job_set_state(j, JOB_RUNNING);
923a60
         job_add_to_dbus_queue(j);
923a60
 
923a60
-        /* While we execute this operation the job might go away (for
923a60
-         * example: because it is replaced by a new, conflicting
923a60
-         * job.) To make sure we don't access a freed job later on we
923a60
-         * store the id here, so that we can verify the job is still
923a60
-         * valid. */
923a60
-        id = j->id;
923a60
 
923a60
         switch (j->type) {
923a60
 
923a60
-                case JOB_START:
923a60
-                        r = unit_start(j->unit);
923a60
-
923a60
-                        /* If this unit cannot be started, then simply wait */
923a60
-                        if (r == -EBADR)
923a60
-                                r = 0;
923a60
-                        break;
923a60
-
923a60
                 case JOB_VERIFY_ACTIVE: {
923a60
                         UnitActiveState t = unit_active_state(j->unit);
923a60
                         if (UNIT_IS_ACTIVE_OR_RELOADING(t))
923a60
@@ -554,17 +578,19 @@ int job_run_and_invalidate(Job *j) {
923a60
                         break;
923a60
                 }
923a60
 
923a60
+                case JOB_START:
923a60
                 case JOB_STOP:
923a60
                 case JOB_RESTART:
923a60
-                        r = unit_stop(j->unit);
923a60
+                        r = job_perform_on_unit(&j);
923a60
 
923a60
-                        /* If this unit cannot stopped, then simply wait. */
923a60
+                        /* If the unit type does not support starting/stopping,
923a60
+                         * then simply wait. */
923a60
                         if (r == -EBADR)
923a60
                                 r = 0;
923a60
                         break;
923a60
 
923a60
                 case JOB_RELOAD:
923a60
-                        r = unit_reload(j->unit);
923a60
+                        r = job_perform_on_unit(&j);
923a60
                         break;
923a60
 
923a60
                 case JOB_NOP:
923a60
@@ -575,7 +601,6 @@ int job_run_and_invalidate(Job *j) {
923a60
                         assert_not_reached("Unknown job type");
923a60
         }
923a60
 
923a60
-        j = manager_get_job(m, id);
923a60
         if (j) {
923a60
                 if (r == -EALREADY)
923a60
                         r = job_finish_and_invalidate(j, JOB_DONE, true, true);
923a60
diff --git a/src/core/unit.c b/src/core/unit.c
923a60
index 6d535ae12e..907a4bf7fd 100644
923a60
--- a/src/core/unit.c
923a60
+++ b/src/core/unit.c
923a60
@@ -1417,6 +1417,15 @@ static void unit_status_log_starting_stopping_reloading(Unit *u, JobType t) {
923a60
                         NULL);
923a60
 }
923a60
 
923a60
+void unit_status_emit_starting_stopping_reloading(Unit *u, JobType t) {
923a60
+
923a60
+        unit_status_log_starting_stopping_reloading(u, t);
923a60
+
923a60
+        /* Reload status messages have traditionally not been printed to console. */
923a60
+        if (t != JOB_RELOAD)
923a60
+                unit_status_print_starting_stopping(u, t);
923a60
+}
923a60
+
923a60
 /* Errors:
923a60
  *         -EBADR:     This unit type does not support starting.
923a60
  *         -EALREADY:  Unit is already started.
923a60
@@ -1427,7 +1436,6 @@ static void unit_status_log_starting_stopping_reloading(Unit *u, JobType t) {
923a60
 int unit_start(Unit *u) {
923a60
         UnitActiveState state;
923a60
         Unit *following;
923a60
-        int r;
923a60
 
923a60
         assert(u);
923a60
 
923a60
@@ -1481,14 +1489,7 @@ int unit_start(Unit *u) {
923a60
 
923a60
         unit_add_to_dbus_queue(u);
923a60
 
923a60
-        r = UNIT_VTABLE(u)->start(u);
923a60
-        if (r <= 0)
923a60
-                return r;
923a60
-
923a60
-        /* Log if the start function actually did something */
923a60
-        unit_status_log_starting_stopping_reloading(u, JOB_START);
923a60
-        unit_status_print_starting_stopping(u, JOB_START);
923a60
-        return r;
923a60
+        return UNIT_VTABLE(u)->start(u);
923a60
 }
923a60
 
923a60
 bool unit_can_start(Unit *u) {
923a60
@@ -1512,7 +1513,6 @@ bool unit_can_isolate(Unit *u) {
923a60
 int unit_stop(Unit *u) {
923a60
         UnitActiveState state;
923a60
         Unit *following;
923a60
-        int r;
923a60
 
923a60
         assert(u);
923a60
 
923a60
@@ -1531,13 +1531,7 @@ int unit_stop(Unit *u) {
923a60
 
923a60
         unit_add_to_dbus_queue(u);
923a60
 
923a60
-        r = UNIT_VTABLE(u)->stop(u);
923a60
-        if (r <= 0)
923a60
-                return r;
923a60
-
923a60
-        unit_status_log_starting_stopping_reloading(u, JOB_STOP);
923a60
-        unit_status_print_starting_stopping(u, JOB_STOP);
923a60
-        return r;
923a60
+        return UNIT_VTABLE(u)->stop(u);
923a60
 }
923a60
 
923a60
 /* Errors:
923a60
@@ -1548,7 +1542,6 @@ int unit_stop(Unit *u) {
923a60
 int unit_reload(Unit *u) {
923a60
         UnitActiveState state;
923a60
         Unit *following;
923a60
-        int r;
923a60
 
923a60
         assert(u);
923a60
 
923a60
@@ -1575,12 +1568,7 @@ int unit_reload(Unit *u) {
923a60
 
923a60
         unit_add_to_dbus_queue(u);
923a60
 
923a60
-        r = UNIT_VTABLE(u)->reload(u);
923a60
-        if (r <= 0)
923a60
-                return r;
923a60
-
923a60
-        unit_status_log_starting_stopping_reloading(u, JOB_RELOAD);
923a60
-        return r;
923a60
+        return UNIT_VTABLE(u)->reload(u);
923a60
 }
923a60
 
923a60
 bool unit_can_reload(Unit *u) {
923a60
diff --git a/src/core/unit.h b/src/core/unit.h
923a60
index 85f52df187..480e2e95f1 100644
923a60
--- a/src/core/unit.h
923a60
+++ b/src/core/unit.h
923a60
@@ -562,6 +562,7 @@ int unit_add_node_link(Unit *u, const char *what, bool wants, UnitDependency d);
923a60
 int unit_coldplug(Unit *u, Hashmap *deferred_work);
923a60
 
923a60
 void unit_status_printf(Unit *u, const char *status, const char *unit_status_msg_format) _printf_(3, 0);
923a60
+void unit_status_emit_starting_stopping_reloading(Unit *u, JobType t);
923a60
 
923a60
 bool unit_need_daemon_reload(Unit *u);
923a60