]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
networkctl: merge three functions for calling D-Bus methods
authorYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 16 Feb 2026 04:10:41 +0000 (13:10 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 16 Feb 2026 09:37:17 +0000 (18:37 +0900)
src/network/networkctl-misc.c
src/network/networkctl-misc.h
src/network/networkctl.c

index e3a8b0a903c1aa42d942b465fc328be9f0c55c9b..9844789c592afc542231cb019cd4d53213242f27 100644 (file)
@@ -17,7 +17,6 @@
 #include "ordered-set.h"
 #include "parse-util.h"
 #include "polkit-agent.h"
-#include "set.h"
 #include "string-util.h"
 #include "strv.h"
 #include "varlink-util.h"
@@ -124,86 +123,59 @@ int link_delete(int argc, char *argv[], void *userdata) {
         return ret;
 }
 
-static int link_renew_one(sd_bus *bus, int index, const char *name) {
-        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
-        int r;
-
-        assert(bus);
-        assert(index >= 0);
-        assert(name);
-
-        r = bus_call_method(bus, bus_network_mgr, "RenewLink", &error, NULL, "i", index);
-        if (r < 0)
-                return log_error_errno(r, "Failed to renew dynamic configuration of interface %s: %s",
-                                       name, bus_error_message(&error, r));
-
-        return 0;
-}
+int link_bus_simple_method(int argc, char *argv[], void *userdata) {
+        int r, ret = 0;
 
-int link_renew(int argc, char *argv[], void *userdata) {
-        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
-        _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
-        int r;
+        typedef struct LinkBusAction {
+                const char *verb;
+                const char *bus_method;
+                const char *error_message;
+        } LinkBusAction;
+
+        static const LinkBusAction link_bus_action_table[] = {
+                { "renew",       "RenewLink",       "Failed to renew dynamic configuration of interface"          },
+                { "forcerenew",  "ForceRenewLink",  "Failed to forcibly renew dynamic configuration of interface" },
+                { "reconfigure", "ReconfigureLink", "Failed to reconfigure network interface"                     },
+        };
+
+        /* Common implementation for 'simple' method calls that just take an ifindex, and nothing else. */
+
+        const LinkBusAction *a = NULL;
+        FOREACH_ELEMENT(i, link_bus_action_table)
+                if (streq(argv[0], i->verb)) {
+                        a = i;
+                        break;
+                }
+        assert(a);
 
-        r = acquire_bus(&bus);
+        _cleanup_ordered_set_free_ OrderedSet *indexes = NULL;
+        r = parse_interfaces(/* rtnl= */ NULL, argv, &indexes);
         if (r < 0)
                 return r;
 
-        (void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password);
-
-        r = 0;
-
-        for (int i = 1; i < argc; i++) {
-                int index;
-
-                index = rtnl_resolve_interface_or_warn(&rtnl, argv[i]);
-                if (index < 0)
-                        return index;
-
-                RET_GATHER(r, link_renew_one(bus, index, argv[i]));
-        }
-
-        return r;
-}
-
-static int link_force_renew_one(sd_bus *bus, int index, const char *name) {
-        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
-        int r;
-
-        assert(bus);
-        assert(index >= 0);
-        assert(name);
-
-        r = bus_call_method(bus, bus_network_mgr, "ForceRenewLink", &error, NULL, "i", index);
-        if (r < 0)
-                return log_error_errno(r, "Failed to force renew dynamic configuration of interface %s: %s",
-                                       name, bus_error_message(&error, r));
-
-        return 0;
-}
-
-int link_force_renew(int argc, char *argv[], void *userdata) {
         _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
-        _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
-        int k = 0, r;
-
         r = acquire_bus(&bus);
         if (r < 0)
                 return r;
 
         (void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password);
 
-        for (int i = 1; i < argc; i++) {
-                int index = rtnl_resolve_interface_or_warn(&rtnl, argv[i]);
-                if (index < 0)
-                        return index;
+        void *p;
+        ORDERED_SET_FOREACH(p, indexes) {
+                _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
+                int index = PTR_TO_INT(p);
 
-                r = link_force_renew_one(bus, index, argv[i]);
-                if (r < 0 && k >= 0)
-                        k = r;
+                r = bus_call_method(bus, bus_network_mgr, a->bus_method, &error, /* ret_reply= */ NULL, "i", index);
+                if (r < 0) {
+                        RET_GATHER(ret, r);
+                        log_error_errno(r, "%s %s: %s",
+                                        a->error_message,
+                                        FORMAT_IFNAME_FULL(index, FORMAT_IFNAME_IFINDEX),
+                                        bus_error_message(&error, r));
+                }
         }
 
-        return k;
+        return ret;
 }
 
 int verb_reload(int argc, char *argv[], void *userdata) {
@@ -224,46 +196,6 @@ int verb_reload(int argc, char *argv[], void *userdata) {
         return 0;
 }
 
-int verb_reconfigure(int argc, char *argv[], void *userdata) {
-        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
-        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
-        _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
-        _cleanup_set_free_ Set *indexes = NULL;
-        int index, r;
-        void *p;
-
-        r = acquire_bus(&bus);
-        if (r < 0)
-                return r;
-
-        (void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password);
-
-        indexes = set_new(NULL);
-        if (!indexes)
-                return log_oom();
-
-        for (int i = 1; i < argc; i++) {
-                index = rtnl_resolve_interface_or_warn(&rtnl, argv[i]);
-                if (index < 0)
-                        return index;
-
-                r = set_put(indexes, INT_TO_PTR(index));
-                if (r < 0)
-                        return log_oom();
-        }
-
-        SET_FOREACH(p, indexes) {
-                index = PTR_TO_INT(p);
-                r = bus_call_method(bus, bus_network_mgr, "ReconfigureLink", &error, NULL, "i", index);
-                if (r < 0)
-                        return log_error_errno(r, "Failed to reconfigure network interface %s: %s",
-                                               FORMAT_IFNAME_FULL(index, FORMAT_IFNAME_IFINDEX),
-                                               bus_error_message(&error, r));
-        }
-
-        return 0;
-}
-
 int verb_persistent_storage(int argc, char *argv[], void *userdata) {
         _cleanup_(sd_varlink_flush_close_unrefp) sd_varlink *vl = NULL;
         bool ready;
index a7cae1c43ab94686b290102f43fcb491268d860d..4860c99ce393c02003f536945a35deb21d673ad8 100644 (file)
@@ -3,8 +3,6 @@
 
 int link_up_down(int argc, char *argv[], void *userdata);
 int link_delete(int argc, char *argv[], void *userdata);
-int link_renew(int argc, char *argv[], void *userdata);
-int link_force_renew(int argc, char *argv[], void *userdata);
+int link_bus_simple_method(int argc, char *argv[], void *userdata);
 int verb_reload(int argc, char *argv[], void *userdata);
-int verb_reconfigure(int argc, char *argv[], void *userdata);
 int verb_persistent_storage(int argc, char *argv[], void *userdata);
index f9aa40ba1bd62aac8c198fd308c281ba26445b1b..68acdcf60a7a2b68aa177314511322892e6ee259 100644 (file)
@@ -230,9 +230,9 @@ static int networkctl_main(int argc, char *argv[]) {
                 { "delete",             2,        VERB_ANY, 0,                             link_delete             },
                 { "up",                 2,        VERB_ANY, 0,                             link_up_down            },
                 { "down",               2,        VERB_ANY, 0,                             link_up_down            },
-                { "renew",              2,        VERB_ANY, VERB_ONLINE_ONLY,              link_renew              },
-                { "forcerenew",         2,        VERB_ANY, VERB_ONLINE_ONLY,              link_force_renew        },
-                { "reconfigure",        2,        VERB_ANY, VERB_ONLINE_ONLY,              verb_reconfigure        },
+                { "renew",              2,        VERB_ANY, VERB_ONLINE_ONLY,              link_bus_simple_method  },
+                { "forcerenew",         2,        VERB_ANY, VERB_ONLINE_ONLY,              link_bus_simple_method  },
+                { "reconfigure",        2,        VERB_ANY, VERB_ONLINE_ONLY,              link_bus_simple_method  },
                 { "reload",             1,        1,        VERB_ONLINE_ONLY,              verb_reload             },
                 { "edit",               2,        VERB_ANY, 0,                             verb_edit               },
                 { "cat",                1,        VERB_ANY, 0,                             verb_cat                },