richardphibel / rpms / systemd

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