From 209de5256b7ba8600c3e73a85a43b86708998d65 Mon Sep 17 00:00:00 2001
From: Lennart Poettering <lennart@poettering.net>
Date: Tue, 13 Nov 2018 11:59:06 +0100
Subject: [PATCH 1/3] =?UTF-8?q?core:=20rename=20queued=5Fmessage=20?=
 =?UTF-8?q?=E2=86=92=20pending=5Freload=5Fmessage?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This field is only used for pending Reload() replies, hence let's rename
it to be more descriptive and precise.

No change in behaviour.
---
 src/core/dbus-manager.c |  4 ++--
 src/core/dbus.c         | 12 ++++++------
 src/core/dbus.h         |  2 +-
 src/core/manager.c      |  6 +++---
 src/core/manager.h      |  2 +-
 5 files changed, 13 insertions(+), 13 deletions(-)

Index: systemd-228+suse.863.g866467ea64/src/core/dbus-manager.c
===================================================================
--- systemd-228+suse.863.g866467ea64.orig/src/core/dbus-manager.c
+++ systemd-228+suse.863.g866467ea64/src/core/dbus-manager.c
@@ -1117,8 +1117,8 @@ static int method_reload(sd_bus_message
          * is finished. That way the caller knows when the reload
          * finished. */
 
-        assert(!m->queued_message);
-        r = sd_bus_message_new_method_return(message, &m->queued_message);
+        assert(!m->pending_reload_message);
+        r = sd_bus_message_new_method_return(message, &m->pending_reload_message);
         if (r < 0)
                 return r;
 
Index: systemd-228+suse.863.g866467ea64/src/core/dbus.c
===================================================================
--- systemd-228+suse.863.g866467ea64.orig/src/core/dbus.c
+++ systemd-228+suse.863.g866467ea64/src/core/dbus.c
@@ -52,23 +52,22 @@
 
 static void destroy_bus(Manager *m, sd_bus **bus);
 
-int bus_send_queued_message(Manager *m) {
+int bus_send_pending_reload_message(Manager *m) {
         int r;
 
         assert(m);
 
-        if (!m->queued_message)
+        if (!m->pending_reload_message)
                 return 0;
 
-        /* If we cannot get rid of this message we won't dispatch any
-         * D-Bus messages, so that we won't end up wanting to queue
-         * another message. */
+        /* If we cannot get rid of this message we won't dispatch any D-Bus messages, so that we won't end up wanting
+         * to queue another message. */
 
-        r = sd_bus_send(NULL, m->queued_message, NULL);
+        r = sd_bus_send(NULL, m->pending_reload_message, NULL);
         if (r < 0)
-                log_warning_errno(r, "Failed to send queued message: %m");
+                log_warning_errno(r, "Failed to send queued message, ignoring: %m");
 
-        m->queued_message = sd_bus_message_unref(m->queued_message);
+        m->pending_reload_message = sd_bus_message_unref(m->pending_reload_message);
 
         return 0;
 }
@@ -1076,8 +1075,8 @@ static void destroy_bus(Manager *m, sd_b
                         j->clients = sd_bus_track_unref(j->clients);
 
         /* Get rid of queued message on this bus */
-        if (m->queued_message && sd_bus_message_get_bus(m->queued_message) == *bus)
-                m->queued_message = sd_bus_message_unref(m->queued_message);
+        if (m->pending_reload_message && sd_bus_message_get_bus(m->pending_reload_message) == *bus)
+                m->pending_reload_message = sd_bus_message_unref(m->pending_reload_message);
 
         /* Possibly flush unwritten data, but only if we are
          * unprivileged, since we don't want to sync here */
Index: systemd-228+suse.863.g866467ea64/src/core/dbus.h
===================================================================
--- systemd-228+suse.863.g866467ea64.orig/src/core/dbus.h
+++ systemd-228+suse.863.g866467ea64/src/core/dbus.h
@@ -23,7 +23,7 @@
 
 #include "manager.h"
 
-int bus_send_queued_message(Manager *m);
+int bus_send_pending_reload_message(Manager *m);
 
 int bus_init(Manager *m, bool try_bus_connect);
 void bus_done(Manager *m);
Index: systemd-228+suse.863.g866467ea64/src/core/manager.c
===================================================================
--- systemd-228+suse.863.g866467ea64.orig/src/core/manager.c
+++ systemd-228+suse.863.g866467ea64/src/core/manager.c
@@ -1560,57 +1560,71 @@ static unsigned manager_dispatch_dbus_qu
 
         assert(m);
 
+        /* Avoid recursion */
         if (m->dispatching_dbus_queue)
                 return 0;
 
-        /* Anything to do at all? */
-        if (!m->dbus_unit_queue && !m->dbus_job_queue && !m->send_reloading_done && !m->queued_message)
-                return 0;
-
-        /* Do we have overly many messages queued at the moment? If so, let's not enqueue more on top, let's sit this
-         * cycle out, and process things in a later cycle when the queues got a bit emptier. */
-        if (manager_bus_n_queued_write(m) > MANAGER_BUS_BUSY_THRESHOLD)
-                return 0;
-
-        /* Only process a certain number of units/jobs per event loop iteration. Even if the bus queue wasn't overly
-         * full before this call we shouldn't increase it in size too wildly in one step, and we shouldn't monopolize
-         * CPU time with generating these messages. Note the difference in counting of this "budget" and the
-         * "threshold" above: the "budget" is decreased only once per generated message, regardless how many
-         * busses/direct connections it is enqueued on, while the "threshold" is applied to each queued instance of bus
-         * message, i.e. if the same message is enqueued to five busses/direct connections it will be counted five
-         * times. This difference in counting ("references" vs. "instances") is primarily a result of the fact that
-         * it's easier to implement it this way, however it also reflects the thinking that the "threshold" should put
-         * a limit on used queue memory, i.e. space, while the "budget" should put a limit on time. Also note that
-         * the "threshold" is currently chosen much higher than the "budget". */
-        budget = MANAGER_BUS_MESSAGE_BUDGET;
+        /* When we are reloading, let's not wait with generating signals, since we need to exit the manager as quickly
+         * as we can. There's no point in throttling generation of signals in that case. */
+        if ((m)->n_reloading > 0 || m->send_reloading_done || m->pending_reload_message)
+                budget = (unsigned) -1; /* infinite budget in this case */
+        else {
+                /* Anything to do at all? */
+                if (!m->dbus_unit_queue && !m->dbus_job_queue)
+                        return 0;
+
+                /* Do we have overly many messages queued at the moment? If so, let's not enqueue more on top, let's
+                 * sit this cycle out, and process things in a later cycle when the queues got a bit emptier. */
+                if (manager_bus_n_queued_write(m) > MANAGER_BUS_BUSY_THRESHOLD)
+                        return 0;
+
+                /* Only process a certain number of units/jobs per event loop iteration. Even if the bus queue wasn't
+                 * overly full before this call we shouldn't increase it in size too wildly in one step, and we
+                 * shouldn't monopolize CPU time with generating these messages. Note the difference in counting of
+                 * this "budget" and the "threshold" above: the "budget" is decreased only once per generated message,
+                 * regardless how many busses/direct connections it is enqueued on, while the "threshold" is applied to
+                 * each queued instance of bus message, i.e. if the same message is enqueued to five busses/direct
+                 * connections it will be counted five times. This difference in counting ("references"
+                 * vs. "instances") is primarily a result of the fact that it's easier to implement it this way,
+                 * however it also reflects the thinking that the "threshold" should put a limit on used queue memory,
+                 * i.e. space, while the "budget" should put a limit on time. Also note that the "threshold" is
+                 * currently chosen much higher than the "budget". */
+                budget = MANAGER_BUS_MESSAGE_BUDGET;
+        }
 
         m->dispatching_dbus_queue = true;
 
-        while (budget > 0 && (u = m->dbus_unit_queue)) {
+        while (budget != 0 && (u = m->dbus_unit_queue)) {
 
                 assert(u->in_dbus_queue);
 
                 bus_unit_send_change_signal(u);
-                n++, budget--;
+                n++;
+
+                if (budget != (unsigned) -1)
+                        budget--;
         }
 
-        while (budget > 0 && (j = m->dbus_job_queue)) {
+        while (budget != 0 && (j = m->dbus_job_queue)) {
                 assert(j->in_dbus_queue);
 
                 bus_job_send_change_signal(j);
-                n++, budget--;
+                n++;
+
+                if (budget != (unsigned) -1)
+                        budget--;
         }
 
         m->dispatching_dbus_queue = false;
 
-        if (budget > 0 && m->send_reloading_done) {
+        if (m->send_reloading_done) {
                 m->send_reloading_done = false;
                 bus_manager_send_reloading(m, false);
-                n++, budget--;
+                n++;
         }
 
-        if (budget > 0 && m->queued_message) {
-                bus_send_queued_message(m);
+        if (m->pending_reload_message) {
+                bus_send_pending_reload_message(m);
                 n++;
         }
 
Index: systemd-228+suse.863.g866467ea64/src/core/manager.h
===================================================================
--- systemd-228+suse.863.g866467ea64.orig/src/core/manager.h
+++ systemd-228+suse.863.g866467ea64/src/core/manager.h
@@ -202,7 +202,7 @@ struct Manager {
 
         /* This is used during reloading: before the reload we queue
          * the reply message here, and afterwards we send it */
-        sd_bus_message *queued_message;
+        sd_bus_message *pending_reload_message;
 
         Hashmap *watch_bus;  /* D-Bus names => Unit object n:1 */
 
