teknoraver / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone

Blame SOURCES/0103-networkctl-open-the-bus-just-once.patch

594167
From 3852f94de9582dc1acb44844579873cd0e2f3162 Mon Sep 17 00:00:00 2001
594167
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
594167
Date: Tue, 11 Jan 2022 15:12:42 +0100
594167
Subject: [PATCH] networkctl: open the bus just once
594167
594167
We'd connect to the bus twice: the first time to check networkd namespace,
594167
and then the second time to do the deed we were asked to do. It's nicer
594167
to open the bus just once, for efficience and also to avoid the open call
594167
in all functions.
594167
594167
An ASSERT_PTR helper is added:
594167
- sd_bus *bus = userdata;
594167
  ...
594167
- assert(bus);
594167
+ sd_bus *bus = ASSERT_PTR(userdata);
594167
  ...
594167
594167
It can be used in other place too, but I'm leaving that for a later
594167
refactoring.
594167
594167
(cherry picked from commit d821e40ca96d2b14216f7a18e4512364bfb83628)
594167
Related: #2087652
594167
---
594167
 src/fundamental/macro-fundamental.h |  8 ++++
594167
 src/network/networkctl.c            | 74 ++++++++++-------------------
594167
 2 files changed, 33 insertions(+), 49 deletions(-)
594167
594167
diff --git a/src/fundamental/macro-fundamental.h b/src/fundamental/macro-fundamental.h
594167
index f87839d47b..d597c743bb 100644
594167
--- a/src/fundamental/macro-fundamental.h
594167
+++ b/src/fundamental/macro-fundamental.h
594167
@@ -66,6 +66,14 @@
594167
         #define free(a) FreePool(a)
594167
 #endif
594167
 
594167
+/* This passes the argument through after (if asserts are enabled) checking that it is not null. */
594167
+#define ASSERT_PTR(expr)                        \
594167
+        ({                                      \
594167
+                typeof(expr) _expr_ = (expr);   \
594167
+                assert(_expr_);                 \
594167
+                _expr_;                         \
594167
+        })
594167
+
594167
 #if defined(static_assert)
594167
 #define assert_cc(expr)                                                 \
594167
         static_assert(expr, #expr)
594167
diff --git a/src/network/networkctl.c b/src/network/networkctl.c
594167
index 68dd4b185c..c35f851bdb 100644
594167
--- a/src/network/networkctl.c
594167
+++ b/src/network/networkctl.c
594167
@@ -79,17 +79,12 @@ static bool arg_full = false;
594167
 static unsigned arg_lines = 10;
594167
 static JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF;
594167
 
594167
-static int get_description(JsonVariant **ret) {
594167
+static int get_description(sd_bus *bus, JsonVariant **ret) {
594167
         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
594167
         _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
594167
-        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
594167
         const char *text = NULL;
594167
         int r;
594167
 
594167
-        r = sd_bus_open_system(&bus;;
594167
-        if (r < 0)
594167
-                return log_error_errno(r, "Failed to connect system bus: %m");
594167
-
594167
         r = bus_call_method(bus, bus_network_mgr, "Describe", &error, &reply, NULL);
594167
         if (r < 0)
594167
                 return log_error_errno(r, "Failed to get description: %s", bus_error_message(&error, r));
594167
@@ -105,11 +100,11 @@ static int get_description(JsonVariant **ret) {
594167
         return 0;
594167
 }
594167
 
594167
-static int dump_manager_description(void) {
594167
+static int dump_manager_description(sd_bus *bus) {
594167
         _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
594167
         int r;
594167
 
594167
-        r = get_description(&v);
594167
+        r = get_description(bus, &v);
594167
         if (r < 0)
594167
                 return r;
594167
 
594167
@@ -117,14 +112,14 @@ static int dump_manager_description(void) {
594167
         return 0;
594167
 }
594167
 
594167
-static int dump_link_description(char **patterns) {
594167
+static int dump_link_description(sd_bus *bus, char **patterns) {
594167
         _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
594167
         _cleanup_free_ bool *matched_patterns = NULL;
594167
         JsonVariant *i;
594167
         size_t c = 0;
594167
         int r;
594167
 
594167
-        r = get_description(&v);
594167
+        r = get_description(bus, &v);
594167
         if (r < 0)
594167
                 return r;
594167
 
594167
@@ -790,6 +785,7 @@ static int acquire_link_info(sd_bus *bus, sd_netlink *rtnl, char **patterns, Lin
594167
 }
594167
 
594167
 static int list_links(int argc, char *argv[], void *userdata) {
594167
+        sd_bus *bus = ASSERT_PTR(userdata);
594167
         _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
594167
         _cleanup_(link_info_array_freep) LinkInfo *links = NULL;
594167
         _cleanup_(table_unrefp) Table *table = NULL;
594167
@@ -798,9 +794,9 @@ static int list_links(int argc, char *argv[], void *userdata) {
594167
 
594167
         if (arg_json_format_flags != JSON_FORMAT_OFF) {
594167
                 if (arg_all || argc <= 1)
594167
-                        return dump_manager_description();
594167
+                        return dump_manager_description(bus);
594167
                 else
594167
-                        return dump_link_description(strv_skip(argv, 1));
594167
+                        return dump_link_description(bus, strv_skip(argv, 1));
594167
         }
594167
 
594167
         r = sd_netlink_open(&rtnl);
594167
@@ -2383,7 +2379,7 @@ static int system_status(sd_netlink *rtnl, sd_hwdb *hwdb) {
594167
 }
594167
 
594167
 static int link_status(int argc, char *argv[], void *userdata) {
594167
-        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
594167
+        sd_bus *bus = ASSERT_PTR(userdata);
594167
         _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
594167
         _cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL;
594167
         _cleanup_(link_info_array_freep) LinkInfo *links = NULL;
594167
@@ -2391,17 +2387,13 @@ static int link_status(int argc, char *argv[], void *userdata) {
594167
 
594167
         if (arg_json_format_flags != JSON_FORMAT_OFF) {
594167
                 if (arg_all || argc <= 1)
594167
-                        return dump_manager_description();
594167
+                        return dump_manager_description(bus);
594167
                 else
594167
-                        return dump_link_description(strv_skip(argv, 1));
594167
+                        return dump_link_description(bus, strv_skip(argv, 1));
594167
         }
594167
 
594167
         pager_open(arg_pager_flags);
594167
 
594167
-        r = sd_bus_open_system(&bus;;
594167
-        if (r < 0)
594167
-                return log_error_errno(r, "Failed to connect system bus: %m");
594167
-
594167
         r = sd_netlink_open(&rtnl);
594167
         if (r < 0)
594167
                 return log_error_errno(r, "Failed to connect to netlink: %m");
594167
@@ -2738,14 +2730,10 @@ static int link_renew_one(sd_bus *bus, int index, const char *name) {
594167
 }
594167
 
594167
 static int link_renew(int argc, char *argv[], void *userdata) {
594167
-        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
594167
+        sd_bus *bus = ASSERT_PTR(userdata);
594167
         _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
594167
         int index, k = 0, r;
594167
 
594167
-        r = sd_bus_open_system(&bus;;
594167
-        if (r < 0)
594167
-                return log_error_errno(r, "Failed to connect system bus: %m");
594167
-
594167
         for (int i = 1; i < argc; i++) {
594167
                 index = rtnl_resolve_interface_or_warn(&rtnl, argv[i]);
594167
                 if (index < 0)
594167
@@ -2772,14 +2760,10 @@ static int link_force_renew_one(sd_bus *bus, int index, const char *name) {
594167
 }
594167
 
594167
 static int link_force_renew(int argc, char *argv[], void *userdata) {
594167
-        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
594167
+        sd_bus *bus = ASSERT_PTR(userdata);
594167
         _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
594167
         int k = 0, r;
594167
 
594167
-        r = sd_bus_open_system(&bus;;
594167
-        if (r < 0)
594167
-                return log_error_errno(r, "Failed to connect system bus: %m");
594167
-
594167
         for (int i = 1; i < argc; i++) {
594167
                 int index = rtnl_resolve_interface_or_warn(&rtnl, argv[i]);
594167
                 if (index < 0)
594167
@@ -2794,14 +2778,10 @@ static int link_force_renew(int argc, char *argv[], void *userdata) {
594167
 }
594167
 
594167
 static int verb_reload(int argc, char *argv[], void *userdata) {
594167
+        sd_bus *bus = ASSERT_PTR(userdata);
594167
         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
594167
-        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
594167
         int r;
594167
 
594167
-        r = sd_bus_open_system(&bus;;
594167
-        if (r < 0)
594167
-                return log_error_errno(r, "Failed to connect system bus: %m");
594167
-
594167
         r = bus_call_method(bus, bus_network_mgr, "Reload", &error, NULL, NULL);
594167
         if (r < 0)
594167
                 return log_error_errno(r, "Failed to reload network settings: %m");
594167
@@ -2810,17 +2790,13 @@ static int verb_reload(int argc, char *argv[], void *userdata) {
594167
 }
594167
 
594167
 static int verb_reconfigure(int argc, char *argv[], void *userdata) {
594167
+        sd_bus *bus = ASSERT_PTR(userdata);
594167
         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
594167
-        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
594167
         _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
594167
         _cleanup_set_free_ Set *indexes = NULL;
594167
         int index, r;
594167
         void *p;
594167
 
594167
-        r = sd_bus_open_system(&bus;;
594167
-        if (r < 0)
594167
-                return log_error_errno(r, "Failed to connect system bus: %m");
594167
-
594167
         indexes = set_new(NULL);
594167
         if (!indexes)
594167
                 return log_oom();
594167
@@ -2968,7 +2944,7 @@ static int parse_argv(int argc, char *argv[]) {
594167
         return 1;
594167
 }
594167
 
594167
-static int networkctl_main(int argc, char *argv[]) {
594167
+static int networkctl_main(sd_bus *bus, int argc, char *argv[]) {
594167
         static const Verb verbs[] = {
594167
                 { "list",        VERB_ANY, VERB_ANY, VERB_DEFAULT, list_links          },
594167
                 { "status",      VERB_ANY, VERB_ANY, 0,            link_status         },
594167
@@ -2984,20 +2960,15 @@ static int networkctl_main(int argc, char *argv[]) {
594167
                 {}
594167
         };
594167
 
594167
-        return dispatch_verb(argc, argv, verbs, NULL);
594167
+        return dispatch_verb(argc, argv, verbs, bus);
594167
 }
594167
 
594167
-static int check_netns_match(void) {
594167
+static int check_netns_match(sd_bus *bus) {
594167
         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
594167
-        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
594167
         struct stat st;
594167
         uint64_t id;
594167
         int r;
594167
 
594167
-        r = sd_bus_open_system(&bus;;
594167
-        if (r < 0)
594167
-                return log_error_errno(r, "Failed to connect system bus: %m");
594167
-
594167
         r = sd_bus_get_property_trivial(
594167
                         bus,
594167
                         "org.freedesktop.network1",
594167
@@ -3035,6 +3006,7 @@ static void warn_networkd_missing(void) {
594167
 }
594167
 
594167
 static int run(int argc, char* argv[]) {
594167
+        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
594167
         int r;
594167
 
594167
         log_setup();
594167
@@ -3043,13 +3015,17 @@ static int run(int argc, char* argv[]) {
594167
         if (r <= 0)
594167
                 return r;
594167
 
594167
-        r = check_netns_match();
594167
+        r = sd_bus_open_system(&bus;;
594167
+        if (r < 0)
594167
+                return log_error_errno(r, "Failed to connect system bus: %m");
594167
+
594167
+        r = check_netns_match(bus);
594167
         if (r < 0)
594167
                 return r;
594167
 
594167
         warn_networkd_missing();
594167
 
594167
-        return networkctl_main(argc, argv);
594167
+        return networkctl_main(bus, argc, argv);
594167
 }
594167
 
594167
 DEFINE_MAIN_FUNCTION(run);