803fb7
From a7ec486dede56ab2ec28132133becf11e5685884 Mon Sep 17 00:00:00 2001
803fb7
From: Michal Sekletar <msekletar@users.noreply.github.com>
803fb7
Date: Mon, 16 May 2016 17:24:51 +0200
803fb7
Subject: [PATCH] core: don't log job status message in case job was
803fb7
 effectively NOP (#3199)
803fb7
803fb7
We currently generate log message about unit being started even when
803fb7
unit was started already and job didn't do anything. This is because job
803fb7
was requested explicitly and hence became anchor job of the transaction
803fb7
thus we could not eliminate it. That is fine but, let's not pollute
803fb7
journal with useless log messages.
803fb7
803fb7
$ systemctl start systemd-resolved
803fb7
$ systemctl start systemd-resolved
803fb7
$ systemctl start systemd-resolved
803fb7
803fb7
Current state:
803fb7
$ journalctl -u systemd-resolved | grep Started
803fb7
803fb7
May 05 15:31:42 rawhide systemd[1]: Started Network Name Resolution.
803fb7
May 05 15:31:59 rawhide systemd[1]: Started Network Name Resolution.
803fb7
May 05 15:32:01 rawhide systemd[1]: Started Network Name Resolution.
803fb7
803fb7
After patch applied:
803fb7
$ journalctl -u systemd-resolved | grep Started
803fb7
803fb7
May 05 16:42:12 rawhide systemd[1]: Started Network Name Resolution.
803fb7
803fb7
Fixes #1723
803fb7
803fb7
Cherry-picked from: 833f92ad39beca0e954e91e5764ffc83f8d0c1cf
803fb7
Resolves: #1280014
803fb7
---
803fb7
 src/core/dbus-job.c    |  2 +-
803fb7
 src/core/job.c         | 33 ++++++++++++++++++---------------
803fb7
 src/core/job.h         |  2 +-
803fb7
 src/core/manager.c     |  2 +-
803fb7
 src/core/transaction.c |  2 +-
803fb7
 src/core/unit.c        | 12 ++++++------
803fb7
 6 files changed, 28 insertions(+), 25 deletions(-)
803fb7
803fb7
diff --git a/src/core/dbus-job.c b/src/core/dbus-job.c
803fb7
index 8b5ea2566..7ce5d5772 100644
803fb7
--- a/src/core/dbus-job.c
803fb7
+++ b/src/core/dbus-job.c
803fb7
@@ -84,7 +84,7 @@ int bus_job_method_cancel(sd_bus *bus, sd_bus_message *message, void *userdata,
803fb7
         if (r < 0)
803fb7
                 return r;
803fb7
 
803fb7
-        job_finish_and_invalidate(j, JOB_CANCELED, true);
803fb7
+        job_finish_and_invalidate(j, JOB_CANCELED, true, false);
803fb7
 
803fb7
         return sd_bus_reply_method_return(message, NULL);
803fb7
 }
803fb7
diff --git a/src/core/job.c b/src/core/job.c
803fb7
index 7416386a1..c2876dec1 100644
803fb7
--- a/src/core/job.c
803fb7
+++ b/src/core/job.c
803fb7
@@ -190,7 +190,7 @@ Job* job_install(Job *j) {
803fb7
 
803fb7
         if (uj) {
803fb7
                 if (job_type_is_conflicting(uj->type, j->type))
803fb7
-                        job_finish_and_invalidate(uj, JOB_CANCELED, false);
803fb7
+                        job_finish_and_invalidate(uj, JOB_CANCELED, false, false);
803fb7
                 else {
803fb7
                         /* not conflicting, i.e. mergeable */
803fb7
 
803fb7
@@ -571,19 +571,19 @@ int job_run_and_invalidate(Job *j) {
803fb7
         j = manager_get_job(m, id);
803fb7
         if (j) {
803fb7
                 if (r == -EALREADY)
803fb7
-                        r = job_finish_and_invalidate(j, JOB_DONE, true);
803fb7
+                        r = job_finish_and_invalidate(j, JOB_DONE, true, true);
803fb7
                 else if (r == -EBADR)
803fb7
-                        r = job_finish_and_invalidate(j, JOB_SKIPPED, true);
803fb7
+                        r = job_finish_and_invalidate(j, JOB_SKIPPED, true, false);
803fb7
                 else if (r == -ENOEXEC)
803fb7
-                        r = job_finish_and_invalidate(j, JOB_INVALID, true);
803fb7
+                        r = job_finish_and_invalidate(j, JOB_INVALID, true, false);
803fb7
                 else if (r == -EPROTO)
803fb7
-                        r = job_finish_and_invalidate(j, JOB_ASSERT, true);
803fb7
+                        r = job_finish_and_invalidate(j, JOB_ASSERT, true, false);
803fb7
                 else if (r == -ENOTSUP)
803fb7
-                        r = job_finish_and_invalidate(j, JOB_UNSUPPORTED, true);
803fb7
+                        r = job_finish_and_invalidate(j, JOB_UNSUPPORTED, true, false);
803fb7
                 else if (r == -EAGAIN)
803fb7
                         job_set_state(j, JOB_WAITING);
803fb7
                 else if (r < 0)
803fb7
-                        r = job_finish_and_invalidate(j, JOB_FAILED, true);
803fb7
+                        r = job_finish_and_invalidate(j, JOB_FAILED, true, false);
803fb7
         }
803fb7
 
803fb7
         return r;
803fb7
@@ -792,7 +792,7 @@ static void job_log_status_message(Unit *u, JobType t, JobResult result) {
803fb7
                                 NULL);
803fb7
 }
803fb7
 
803fb7
-int job_finish_and_invalidate(Job *j, JobResult result, bool recursive) {
803fb7
+int job_finish_and_invalidate(Job *j, JobResult result, bool recursive, bool already) {
803fb7
         Unit *u;
803fb7
         Unit *other;
803fb7
         JobType t;
803fb7
@@ -810,8 +810,11 @@ int job_finish_and_invalidate(Job *j, JobResult result, bool recursive) {
803fb7
         log_unit_debug(u->id, "Job %s/%s finished, result=%s",
803fb7
                        u->id, job_type_to_string(t), job_result_to_string(result));
803fb7
 
803fb7
-        job_print_status_message(u, t, result);
803fb7
-        job_log_status_message(u, t, result);
803fb7
+        /* If this job did nothing to respective unit we don't log the status message */
803fb7
+        if (!already) {
803fb7
+                job_print_status_message(u, t, result);
803fb7
+                job_log_status_message(u, t, result);
803fb7
+        }
803fb7
 
803fb7
         job_add_to_dbus_queue(j);
803fb7
 
803fb7
@@ -842,20 +845,20 @@ int job_finish_and_invalidate(Job *j, JobResult result, bool recursive) {
803fb7
                                 if (other->job &&
803fb7
                                     (other->job->type == JOB_START ||
803fb7
                                      other->job->type == JOB_VERIFY_ACTIVE))
803fb7
-                                        job_finish_and_invalidate(other->job, JOB_DEPENDENCY, true);
803fb7
+                                        job_finish_and_invalidate(other->job, JOB_DEPENDENCY, true, false);
803fb7
 
803fb7
                         SET_FOREACH(other, u->dependencies[UNIT_BOUND_BY], i)
803fb7
                                 if (other->job &&
803fb7
                                     (other->job->type == JOB_START ||
803fb7
                                      other->job->type == JOB_VERIFY_ACTIVE))
803fb7
-                                        job_finish_and_invalidate(other->job, JOB_DEPENDENCY, true);
803fb7
+                                        job_finish_and_invalidate(other->job, JOB_DEPENDENCY, true, false);
803fb7
 
803fb7
                         SET_FOREACH(other, u->dependencies[UNIT_REQUIRED_BY_OVERRIDABLE], i)
803fb7
                                 if (other->job &&
803fb7
                                     !other->job->override &&
803fb7
                                     (other->job->type == JOB_START ||
803fb7
                                      other->job->type == JOB_VERIFY_ACTIVE))
803fb7
-                                        job_finish_and_invalidate(other->job, JOB_DEPENDENCY, true);
803fb7
+                                        job_finish_and_invalidate(other->job, JOB_DEPENDENCY, true, false);
803fb7
 
803fb7
                 } else if (t == JOB_STOP) {
803fb7
 
803fb7
@@ -863,7 +866,7 @@ int job_finish_and_invalidate(Job *j, JobResult result, bool recursive) {
803fb7
                                 if (other->job &&
803fb7
                                     (other->job->type == JOB_START ||
803fb7
                                      other->job->type == JOB_VERIFY_ACTIVE))
803fb7
-                                        job_finish_and_invalidate(other->job, JOB_DEPENDENCY, true);
803fb7
+                                        job_finish_and_invalidate(other->job, JOB_DEPENDENCY, true, false);
803fb7
                 }
803fb7
         }
803fb7
 
803fb7
@@ -911,7 +914,7 @@ static int job_dispatch_timer(sd_event_source *s, uint64_t monotonic, void *user
803fb7
         log_unit_warning(j->unit->id, "Job %s/%s timed out.", j->unit->id, job_type_to_string(j->type));
803fb7
 
803fb7
         u = j->unit;
803fb7
-        job_finish_and_invalidate(j, JOB_TIMEOUT, true);
803fb7
+        job_finish_and_invalidate(j, JOB_TIMEOUT, true, false);
803fb7
 
803fb7
         failure_action(u->manager, u->job_timeout_action, u->job_timeout_reboot_arg);
803fb7
 
803fb7
diff --git a/src/core/job.h b/src/core/job.h
803fb7
index d967b68a3..e4191ee77 100644
803fb7
--- a/src/core/job.h
803fb7
+++ b/src/core/job.h
803fb7
@@ -220,7 +220,7 @@ void job_add_to_dbus_queue(Job *j);
803fb7
 int job_start_timer(Job *j);
803fb7
 
803fb7
 int job_run_and_invalidate(Job *j);
803fb7
-int job_finish_and_invalidate(Job *j, JobResult result, bool recursive);
803fb7
+int job_finish_and_invalidate(Job *j, JobResult result, bool recursive, bool already);
803fb7
 
803fb7
 char *job_dbus_path(Job *j);
803fb7
 
803fb7
diff --git a/src/core/manager.c b/src/core/manager.c
803fb7
index a1504bf17..ee456fb79 100644
803fb7
--- a/src/core/manager.c
803fb7
+++ b/src/core/manager.c
803fb7
@@ -1426,7 +1426,7 @@ void manager_clear_jobs(Manager *m) {
803fb7
 
803fb7
         while ((j = hashmap_first(m->jobs)))
803fb7
                 /* No need to recurse. We're cancelling all jobs. */
803fb7
-                job_finish_and_invalidate(j, JOB_CANCELED, false);
803fb7
+                job_finish_and_invalidate(j, JOB_CANCELED, false, false);
803fb7
 }
803fb7
 
803fb7
 static int manager_dispatch_run_queue(sd_event_source *source, void *userdata) {
803fb7
diff --git a/src/core/transaction.c b/src/core/transaction.c
803fb7
index b0b3d6bd6..aed64fa41 100644
803fb7
--- a/src/core/transaction.c
803fb7
+++ b/src/core/transaction.c
803fb7
@@ -592,7 +592,7 @@ static int transaction_apply(Transaction *tr, Manager *m, JobMode mode) {
803fb7
                         /* Not invalidating recursively. Avoids triggering
803fb7
                          * OnFailure= actions of dependent jobs. Also avoids
803fb7
                          * invalidating our iterator. */
803fb7
-                        job_finish_and_invalidate(j, JOB_CANCELED, false);
803fb7
+                        job_finish_and_invalidate(j, JOB_CANCELED, false, false);
803fb7
                 }
803fb7
         }
803fb7
 
803fb7
diff --git a/src/core/unit.c b/src/core/unit.c
803fb7
index db5aa987e..d6ead7d67 100644
803fb7
--- a/src/core/unit.c
803fb7
+++ b/src/core/unit.c
803fb7
@@ -1851,12 +1851,12 @@ void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns, bool reload_su
803fb7
                 case JOB_VERIFY_ACTIVE:
803fb7
 
803fb7
                         if (UNIT_IS_ACTIVE_OR_RELOADING(ns))
803fb7
-                                job_finish_and_invalidate(u->job, JOB_DONE, true);
803fb7
+                                job_finish_and_invalidate(u->job, JOB_DONE, true, false);
803fb7
                         else if (u->job->state == JOB_RUNNING && ns != UNIT_ACTIVATING) {
803fb7
                                 unexpected = true;
803fb7
 
803fb7
                                 if (UNIT_IS_INACTIVE_OR_FAILED(ns))
803fb7
-                                        job_finish_and_invalidate(u->job, ns == UNIT_FAILED ? JOB_FAILED : JOB_DONE, true);
803fb7
+                                        job_finish_and_invalidate(u->job, ns == UNIT_FAILED ? JOB_FAILED : JOB_DONE, true, false);
803fb7
                         }
803fb7
 
803fb7
                         break;
803fb7
@@ -1866,12 +1866,12 @@ void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns, bool reload_su
803fb7
 
803fb7
                         if (u->job->state == JOB_RUNNING) {
803fb7
                                 if (ns == UNIT_ACTIVE)
803fb7
-                                        job_finish_and_invalidate(u->job, reload_success ? JOB_DONE : JOB_FAILED, true);
803fb7
+                                        job_finish_and_invalidate(u->job, reload_success ? JOB_DONE : JOB_FAILED, true, false);
803fb7
                                 else if (ns != UNIT_ACTIVATING && ns != UNIT_RELOADING) {
803fb7
                                         unexpected = true;
803fb7
 
803fb7
                                         if (UNIT_IS_INACTIVE_OR_FAILED(ns))
803fb7
-                                                job_finish_and_invalidate(u->job, ns == UNIT_FAILED ? JOB_FAILED : JOB_DONE, true);
803fb7
+                                                job_finish_and_invalidate(u->job, ns == UNIT_FAILED ? JOB_FAILED : JOB_DONE, true, false);
803fb7
                                 }
803fb7
                         }
803fb7
 
803fb7
@@ -1882,10 +1882,10 @@ void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns, bool reload_su
803fb7
                 case JOB_TRY_RESTART:
803fb7
 
803fb7
                         if (UNIT_IS_INACTIVE_OR_FAILED(ns))
803fb7
-                                job_finish_and_invalidate(u->job, JOB_DONE, true);
803fb7
+                                job_finish_and_invalidate(u->job, JOB_DONE, true, false);
803fb7
                         else if (u->job->state == JOB_RUNNING && ns != UNIT_DEACTIVATING) {
803fb7
                                 unexpected = true;
803fb7
-                                job_finish_and_invalidate(u->job, JOB_FAILED, true);
803fb7
+                                job_finish_and_invalidate(u->job, JOB_FAILED, true, false);
803fb7
                         }
803fb7
 
803fb7
                         break;