]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/core/dbus.c
Merge pull request #8981 from keszybz/ratelimit-and-dbus
[thirdparty/systemd.git] / src / core / dbus.c
index c7c96a3409869c7c0f8c748caf3f9d3c3ed55e86..4df898f963b6d9500637f5e6cedc49ebcd6664f9 100644 (file)
@@ -3,19 +3,6 @@
   This file is part of systemd.
 
   Copyright 2010 Lennart Poettering
-
-  systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as published by
-  the Free Software Foundation; either version 2.1 of the License, or
-  (at your option) any later version.
-
-  systemd is distributed in the hope that it will be useful, but
-  WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public License
-  along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
 #include <errno.h>
@@ -43,6 +30,7 @@
 #include "mkdir.h"
 #include "process-util.h"
 #include "selinux-access.h"
+#include "service.h"
 #include "special.h"
 #include "string-util.h"
 #include "strv.h"
@@ -242,7 +230,6 @@ static int mac_selinux_filter(sd_bus_message *message, void *userdata, sd_bus_er
         path = sd_bus_message_get_path(message);
 
         if (object_path_startswith("/org/freedesktop/systemd1", path)) {
-
                 r = mac_selinux_access_check(message, verb, error);
                 if (r < 0)
                         return r;
@@ -270,7 +257,6 @@ static int mac_selinux_filter(sd_bus_message *message, void *userdata, sd_bus_er
                 else
                         manager_load_unit_from_dbus_path(m, path, NULL, &u);
         }
-
         if (!u)
                 return 0;
 
@@ -501,8 +487,7 @@ static int bus_job_enumerate(sd_bus *bus, const char *path, void *userdata, char
 
         assert(hashmap_size(m->jobs) == k);
 
-        *nodes = l;
-        l = NULL;
+        *nodes = TAKE_PTR(l);
 
         return k;
 }
@@ -526,8 +511,7 @@ static int bus_unit_enumerate(sd_bus *bus, const char *path, void *userdata, cha
                 k++;
         }
 
-        *nodes = l;
-        l = NULL;
+        *nodes = TAKE_PTR(l);
 
         return k;
 }
@@ -897,9 +881,9 @@ int bus_init_api(Manager *m) {
                 bus = sd_bus_ref(m->system_bus);
         else {
                 if (MANAGER_IS_SYSTEM(m))
-                        r = sd_bus_open_system(&bus);
+                        r = sd_bus_open_system_with_description(&bus, "bus-api-system");
                 else
-                        r = sd_bus_open_user(&bus);
+                        r = sd_bus_open_user_with_description(&bus, "bus-api-user");
                 if (r < 0)
                         return log_error_errno(r, "Failed to connect to API bus: %m");
 
@@ -916,8 +900,7 @@ int bus_init_api(Manager *m) {
         if (r < 0)
                 return log_error_errno(r, "Failed to set up API bus: %m");
 
-        m->api_bus = bus;
-        bus = NULL;
+        m->api_bus = TAKE_PTR(bus);
 
         r = manager_enqueue_sync_bus_names(m);
         if (r < 0)
@@ -958,29 +941,27 @@ int bus_init_system(Manager *m) {
                 return 0;
 
         /* The API and system bus is the same if we are running in system mode */
-        if (MANAGER_IS_SYSTEM(m) && m->api_bus) {
-                m->system_bus = sd_bus_ref(m->api_bus);
-                return 0;
-        }
-
-        r = sd_bus_open_system(&bus);
-        if (r < 0)
-                return log_error_errno(r, "Failed to connect to system bus: %m");
+        if (MANAGER_IS_SYSTEM(m) && m->api_bus)
+                bus = sd_bus_ref(m->api_bus);
+        else {
+                r = sd_bus_open_system_with_description(&bus, "bus-system");
+                if (r < 0)
+                        return log_error_errno(r, "Failed to connect to system bus: %m");
 
-        r = bus_setup_disconnected_match(m, bus);
-        if (r < 0)
-                return r;
+                r = sd_bus_attach_event(bus, m->event, SD_EVENT_PRIORITY_NORMAL);
+                if (r < 0)
+                        return log_error_errno(r, "Failed to attach system bus to event loop: %m");
 
-        r = sd_bus_attach_event(bus, m->event, SD_EVENT_PRIORITY_NORMAL);
-        if (r < 0)
-                return log_error_errno(r, "Failed to attach system bus to event loop: %m");
+                r = bus_setup_disconnected_match(m, bus);
+                if (r < 0)
+                        return r;
+        }
 
         r = bus_setup_system(m, bus);
         if (r < 0)
                 return log_error_errno(r, "Failed to set up system bus: %m");
 
-        m->system_bus = bus;
-        bus = NULL;
+        m->system_bus = TAKE_PTR(bus);
 
         return 0;
 }
@@ -1198,14 +1179,20 @@ int bus_foreach_bus(
 
         /* Send to all direct buses, unconditionally */
         SET_FOREACH(b, m->private_buses, i) {
+
+                /* Don't bother with enqueing these messages to clients that haven't started yet */
+                if (sd_bus_is_ready(b) <= 0)
+                        continue;
+
                 r = send_message(b, userdata);
                 if (r < 0)
                         ret = r;
         }
 
         /* Send to API bus, but only if somebody is subscribed */
-        if (sd_bus_track_count(m->subscribed) > 0 ||
-            sd_bus_track_count(subscribed2) > 0) {
+        if (m->api_bus &&
+            (sd_bus_track_count(m->subscribed) > 0 ||
+             sd_bus_track_count(subscribed2) > 0)) {
                 r = send_message(m->api_bus, userdata);
                 if (r < 0)
                         ret = r;
@@ -1274,3 +1261,34 @@ int bus_verify_reload_daemon_async(Manager *m, sd_bus_message *call, sd_bus_erro
 int bus_verify_set_environment_async(Manager *m, sd_bus_message *call, sd_bus_error *error) {
         return bus_verify_polkit_async(call, CAP_SYS_ADMIN, "org.freedesktop.systemd1.set-environment", NULL, false, UID_INVALID, &m->polkit_registry, error);
 }
+
+uint64_t manager_bus_n_queued_write(Manager *m) {
+        uint64_t c = 0;
+        Iterator i;
+        sd_bus *b;
+        int r;
+
+        /* Returns the total number of messages queued for writing on all our direct and API busses. */
+
+        SET_FOREACH(b, m->private_buses, i) {
+                uint64_t k;
+
+                r = sd_bus_get_n_queued_write(b, &k);
+                if (r < 0)
+                        log_debug_errno(r, "Failed to query queued messages for private bus: %m");
+                else
+                        c += k;
+        }
+
+        if (m->api_bus) {
+                uint64_t k;
+
+                r = sd_bus_get_n_queued_write(m->api_bus, &k);
+                if (r < 0)
+                        log_debug_errno(r, "Failed to query queued messages for API bus: %m");
+                else
+                        c += k;
+        }
+
+        return c;
+}