]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
network/nexthop: split out nexthop_to_string()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 4 May 2025 16:30:55 +0000 (01:30 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 5 May 2025 13:07:00 +0000 (22:07 +0900)
src/network/networkd-nexthop.c

index 51ee49ae38f27583a18b958b3ad30395b1128da1..68bcd44ca5b64156c6b049b46ed2ef6a9c1df79f 100644 (file)
@@ -458,9 +458,30 @@ static int nexthop_acquire_id(Manager *manager, NextHop *nexthop) {
         return -EBUSY;
 }
 
-void log_nexthop_debug(const NextHop *nexthop, const char *str, Manager *manager) {
-        _cleanup_free_ char *state = NULL, *group = NULL, *flags = NULL;
+static int nexthop_to_string(const NextHop *nexthop, Manager *manager, char **ret) {
+        _cleanup_free_ char *group = NULL, *flags = NULL;
+
+        assert(nexthop);
+        assert(manager);
+        assert(ret);
+
+        (void) route_flags_to_string_alloc(nexthop->flags, &flags);
+
         struct nexthop_grp *nhg;
+        HASHMAP_FOREACH(nhg, nexthop->group)
+                (void) strextendf_with_separator(&group, ",", "%"PRIu32":%"PRIu32, nhg->id, nhg->weight+1u);
+
+        if (asprintf(ret, "id: %"PRIu32", gw: %s, blackhole: %s, group: %s, flags: %s",
+                     nexthop->id,
+                     IN_ADDR_TO_STRING(nexthop->family, &nexthop->gw.address),
+                     yes_no(nexthop->blackhole), strna(group), strna(flags)) < 0)
+                return -ENOMEM;
+
+        return 0;
+}
+
+void log_nexthop_debug(const NextHop *nexthop, const char *str, Manager *manager) {
+        _cleanup_free_ char *state = NULL, *nexthop_str = NULL;
         Link *link = NULL;
 
         assert(nexthop);
@@ -472,16 +493,10 @@ void log_nexthop_debug(const NextHop *nexthop, const char *str, Manager *manager
 
         (void) link_get_by_index(manager, nexthop->ifindex, &link);
         (void) network_config_state_to_string_alloc(nexthop->state, &state);
-        (void) route_flags_to_string_alloc(nexthop->flags, &flags);
-
-        HASHMAP_FOREACH(nhg, nexthop->group)
-                (void) strextendf_with_separator(&group, ",", "%"PRIu32":%"PRIu32, nhg->id, nhg->weight+1u);
+        (void) nexthop_to_string(nexthop, manager, &nexthop_str);
 
-        log_link_debug(link, "%s %s nexthop (%s): id: %"PRIu32", gw: %s, blackhole: %s, group: %s, flags: %s",
-                       str, strna(network_config_source_to_string(nexthop->source)), strna(state),
-                       nexthop->id,
-                       IN_ADDR_TO_STRING(nexthop->family, &nexthop->gw.address),
-                       yes_no(nexthop->blackhole), strna(group), strna(flags));
+        log_link_debug(link, "%s %s nexthop (%s): %s",
+                       str, strna(network_config_source_to_string(nexthop->source)), strna(state), strna(nexthop_str));
 }
 
 static void nexthop_forget_dependents(NextHop *nexthop, Manager *manager) {