]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
libnetlink: flush stdout only in monitor mode main
authorStephen Hemminger <stephen@networkplumber.org>
Mon, 27 Apr 2026 16:26:58 +0000 (09:26 -0700)
committerStephen Hemminger <stephen@networkplumber.org>
Mon, 27 Apr 2026 18:39:56 +0000 (11:39 -0700)
As Eric pointed out, fflush() only makes sense in monitor mode.
In other modes buffering the output helps with large outputs
like route dumps.

Move the fflush() into rtnl_listen() which is the common code
used by all monitoring subcommands. This also ensures any future
monitor subcommand gets correct flushing behavior automatically.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
35 files changed:
bridge/fdb.c
bridge/link.c
bridge/mdb.c
bridge/mst.c
bridge/vlan.c
bridge/vni.c
genl/ctrl.c
ip/ipaddress.c
ip/ipfou.c
ip/ipila.c
ip/ipioam6.c
ip/ipl2tp.c
ip/iplink.c
ip/ipmptcp.c
ip/ipmroute.c
ip/ipneigh.c
ip/ipnetconf.c
ip/ipnetns.c
ip/ipnexthop.c
ip/ipntable.c
ip/ipprefix.c
ip/iproute.c
ip/iprule.c
ip/ipseg6.c
ip/ipstats.c
ip/iptoken.c
ip/iptuntap.c
ip/tcp_metrics.c
ip/xfrm_monitor.c
ip/xfrm_policy.c
ip/xfrm_state.c
lib/libnetlink.c
tc/tc_class.c
tc/tc_filter.c
tc/tc_qdisc.c

index d57b5750319803d34564dce78f60f513ae138fb9..05f093b58a15431f6813873f7bfe0462e6aac148 100644 (file)
@@ -309,7 +309,6 @@ int print_fdb(struct nlmsghdr *n, void *arg)
        print_string(PRINT_ANY, "state", "%s\n",
                           state_n2a(r->ndm_state));
        close_json_object();
-       fflush(fp);
        return 0;
 }
 
@@ -417,7 +416,6 @@ static int fdb_show(int argc, char **argv)
                exit(1);
        }
        delete_json_obj();
-       fflush(stdout);
 
        return 0;
 }
index 1c8faa85e7a52b22d3bcd9a4551daab16615c1e4..7638797d24f4a20ce980ef55143611ad96b7a7f0 100644 (file)
@@ -292,7 +292,6 @@ int print_linkinfo(struct nlmsghdr *n, void *arg)
 
        print_string(PRINT_FP, NULL, "%s", "\n");
        close_json_object();
-       fflush(fp);
        return 0;
 }
 
@@ -669,7 +668,6 @@ static int brlink_show(int argc, char **argv)
        }
 
        delete_json_obj();
-       fflush(stdout);
        return 0;
 }
 
index 72490971c0f15cfd163edaab051ac1a191e903ac..112deeb9cdac17696e40cb4b93d3316fc73085e6 100644 (file)
@@ -501,7 +501,6 @@ static int mdb_show(int argc, char **argv)
 
        close_json_object();
        delete_json_obj();
-       fflush(stdout);
 
        return 0;
 }
index 37362c45e9423e49da239d11d729f939cb8cdc0e..8e46c7620ccf973b29eb48a20e6c5e9e00b0f0fc 100644 (file)
@@ -158,7 +158,6 @@ static int mst_show(int argc, char **argv)
        }
 
        delete_json_obj();
-       fflush(stdout);
        return 0;
 }
 
index 3c240207028b72b09a7b6ed74d1728487fd159ce..27d31ba8c002decee6b63192c12061542f45d31a 100644 (file)
@@ -756,7 +756,6 @@ static int print_vlan_stats(struct nlmsghdr *n, void *arg)
        struct if_stats_msg *ifsm = NLMSG_DATA(n);
        struct rtattr *tb[IFLA_STATS_MAX+1];
        int len = n->nlmsg_len;
-       FILE *fp = arg;
 
        len -= NLMSG_LENGTH(sizeof(*ifsm));
        if (len < 0) {
@@ -778,7 +777,6 @@ static int print_vlan_stats(struct nlmsghdr *n, void *arg)
                print_vlan_stats_attr(tb[IFLA_STATS_LINK_XSTATS_SLAVE],
                                      ifsm->ifindex);
 
-       fflush(fp);
        return 0;
 }
 
@@ -1205,7 +1203,6 @@ static int vlan_show(int argc, char **argv, int subject)
 
 out:
        delete_json_obj();
-       fflush(stdout);
        return 0;
 }
 
@@ -1260,7 +1257,6 @@ static int vlan_global_show(int argc, char **argv)
                close_vlan_port();
 
        delete_json_obj();
-       fflush(stdout);
        return 0;
 }
 
index 57b04c8c2b7ed58a5102370b4cc9de5d309d36dc..b28647fd2f41c3ac3fdee44497f476d6c2a11c60 100644 (file)
@@ -333,7 +333,6 @@ int print_vnifilter_rtm(struct nlmsghdr *n, void *arg)
        if (opened)
                close_vni_port();
 
-       fflush(stdout);
        return 0;
 }
 
@@ -381,7 +380,6 @@ static int vni_show(int argc, char **argv)
        }
 
        delete_json_obj();
-       fflush(stdout);
        return 0;
 }
 
index 9412c2f0a0e293c2d824ab50c4e2c6fb6327c9ca..88ff4cb85102be1b2f1850c70df24031c50998f2 100644 (file)
@@ -275,7 +275,6 @@ static int print_ctrl(struct rtnl_ctrl_data *ctrl,
        struct genlmsghdr *ghdr = NLMSG_DATA(n);
        int len = n->nlmsg_len;
        struct rtattr *attrs;
-       FILE *fp = (FILE *) arg;
 
        if (n->nlmsg_type !=  GENL_ID_CTRL) {
                fprintf(stderr, "Not a controller message, nlmsg_len=%d "
@@ -339,7 +338,6 @@ static int print_ctrl(struct rtnl_ctrl_data *ctrl,
        if (tb[CTRL_ATTR_MCAST_GROUPS])
                print_ctrl_mcast(tb[CTRL_ATTR_MCAST_GROUPS]);
 
-       fflush(fp);
        return 0;
 }
 
index 4d93a04a8555e141ae30a4aaeb42ac607a181c9b..6017bc8370cb6abedd68e8a25c82de0725639cda 100644 (file)
@@ -894,7 +894,6 @@ static int print_linkinfo_brief(FILE *fp, const char *name,
                print_string(PRINT_FP, NULL, "%s", "\n");
        }
 
-       fflush(fp);
        return 0;
 }
 
@@ -1346,7 +1345,6 @@ int print_linkinfo(struct nlmsghdr *n, void *arg)
        }
 
        print_string(PRINT_FP, NULL, "%s", "\n");
-       fflush(fp);
        /* prettier here if stderr and stdout go to the same place */
        if (truncated_vfs)
                fprintf(stderr, "Truncated VF list: %s\n", name);
@@ -1732,7 +1730,6 @@ int print_addrinfo(struct nlmsghdr *n, void *arg)
        }
        print_string(PRINT_FP, NULL, "%s", "\n");
 brief_exit:
-       fflush(fp);
        return 0;
 }
 
@@ -1768,7 +1765,6 @@ static int print_selected_addrinfo(struct ifinfomsg *ifi,
 
        if (brief) {
                print_string(PRINT_FP, NULL, "%s", "\n");
-               fflush(fp);
        }
        return 0;
 }
@@ -2351,7 +2347,6 @@ static int ipaddr_list_flush_or_save(int argc, char **argv, int action)
                        print_link_stats(stdout, n);
                close_json_object();
        }
-       fflush(stdout);
 
 out:
        free_nlmsg_chain(ainfo);
index 760cfee277dc9394d442e38bd41b596a40c90024..8c1e061a7778e2a7d6cadcf2127930332c296b7f 100644 (file)
@@ -322,7 +322,6 @@ static int do_show(int argc, char **argv)
                return 1;
        }
        delete_json_obj();
-       fflush(stdout);
 
        return 0;
 }
index 80f34f2935c13b0fbdb16c445dba1fd879c76cee..2590de9aec0ca5d763eb6ba5ddc0be0d23611aa6 100644 (file)
@@ -159,7 +159,6 @@ static int do_list(int argc, char **argv)
                return 1;
        }
        delete_json_obj();
-       fflush(stdout);
 
        return 0;
 }
index 188609892150fd2bc6cd272d387dc93aed613aed..118fcd1c38dec5b94a4fb417856d241a8a5024b9 100644 (file)
@@ -261,7 +261,6 @@ static int ioam6_do_cmd(void)
                        exit(1);
                }
                delete_json_obj();
-               fflush(stdout);
        }
 
        return 0;
index 87a4b898052dbf55ff7657f9d46fe4d56efc4fd0..97f7ff0805bf4435efb6299481a4f0f0b8d75711 100644 (file)
@@ -467,7 +467,6 @@ static int get_session(struct l2tp_data *p)
                exit(1);
        }
        delete_json_obj();
-       fflush(stdout);
 
        return 0;
 }
@@ -502,7 +501,6 @@ static int get_tunnel(struct l2tp_data *p)
                exit(1);
        }
        delete_json_obj();
-       fflush(stdout);
 
        return 0;
 }
index fce6631d2d821a8739d28805e13d98f6804bda30..eae51438d0be2323c44e2db8434a1ad0a34ed380 100644 (file)
@@ -1296,7 +1296,6 @@ static void print_mpls_stats(FILE *fp, struct rtattr *attr)
        print_nl();
        print_mpls_link_stats(fp, stats, "        ");
        print_string(PRINT_FP, NULL, "%s", "\n");
-       fflush(fp);
 }
 
 static void print_af_stats_attr(FILE *fp, int ifindex, struct rtattr *attr)
@@ -1359,7 +1358,6 @@ static int print_af_stats(struct nlmsghdr *n, void *arg)
        if (tb[IFLA_STATS_AF_SPEC])
                print_af_stats_attr(fp, ifsm->ifindex, tb[IFLA_STATS_AF_SPEC]);
 
-       fflush(fp);
        return 0;
 }
 
index 3df89db54b4e00f8fe2d18828621999512622d59..02cf72ce540385f21807edf8764e50bf7a5dc098 100644 (file)
@@ -276,7 +276,6 @@ static int print_mptcp_addrinfo(struct rtattr *addrinfo)
 
        print_nl();
        close_json_object();
-       fflush(stdout);
 
        return 0;
 }
@@ -320,12 +319,10 @@ static int mptcp_addr_dump(void)
        if (rtnl_dump_filter(&genl_rth, print_mptcp_addr, stdout) < 0) {
                fprintf(stderr, "Dump terminated\n");
                delete_json_obj();
-               fflush(stdout);
                return -2;
        }
 
        delete_json_obj();
-       fflush(stdout);
        return 0;
 }
 
@@ -349,7 +346,6 @@ static int mptcp_addr_show(int argc, char **argv)
        ret = print_mptcp_addr(answer, stdout);
        delete_json_obj();
        free(answer);
-       fflush(stdout);
        return ret;
 }
 
@@ -430,7 +426,6 @@ static int print_mptcp_limit(struct nlmsghdr *n, void *arg)
 
        print_nl();
        close_json_object();
-       fflush(stdout);
 
        return 0;
 }
@@ -592,7 +587,6 @@ static int mptcp_monitor_msg(struct rtnl_ctrl_data *ctrl,
 out:
        print_nl();
        close_json_object();
-       fflush(stdout);
 
        return 0;
 }
index c540eab133a41942e084ac45acd3035038ce7745..164632e6f2dc317fbdd6367145e3cb0b67db5ef4 100644 (file)
@@ -206,7 +206,6 @@ int print_mroute(struct nlmsghdr *n, void *arg)
 
        print_string(PRINT_FP, NULL, "\n", NULL);
        close_json_object();
-       fflush(fp);
        return 0;
 }
 
index e678545ad535ad83722d1daef51f08c569df8236..be537187082801b36e8e69645973b4068ac9ac3e 100644 (file)
@@ -298,7 +298,6 @@ static int print_neigh_brief(FILE *fp, struct ndmsg *r, struct rtattr *tb[])
 
        print_string(PRINT_FP, NULL, "%s", "\n");
        close_json_object();
-       fflush(fp);
 
        return 0;
 }
@@ -472,7 +471,6 @@ int print_neigh(struct nlmsghdr *n, void *arg)
 
        print_string(PRINT_FP, NULL, "\n", "");
        close_json_object();
-       fflush(fp);
 
        return 0;
 }
index 020eff786d0b82f3033a71c2f4fc3d5b1af5f099..57bb99d1f061d655178a0631061ad501a6b684d4 100644 (file)
@@ -140,7 +140,6 @@ int print_netconf(struct rtnl_ctrl_data *ctrl, struct nlmsghdr *n, void *arg)
 
        close_json_object();
        print_string(PRINT_FP, NULL, "\n", NULL);
-       fflush(fp);
        return 0;
 }
 
index 3a33a3adacee31080c3ea9d0a52e455243423a75..587534ae016b50a1cd2cc92a737aa04788838b67 100644 (file)
@@ -327,7 +327,6 @@ int print_nsid(struct nlmsghdr *n, void *arg)
 
        print_string(PRINT_FP, NULL, "\n", NULL);
        close_json_object();
-       fflush(fp);
        return 0;
 }
 
index d5c429369d10f629823639fa7e6bfb70be5226d6..14b525aa0d67ca77640c3ae4b0d4f02efbde18df 100644 (file)
@@ -826,7 +826,6 @@ int print_cache_nexthop(struct nlmsghdr *n, void *arg, bool process_cache)
 
        __print_nexthop_entry(fp, NULL, &nhe, n->nlmsg_type == RTM_DELNEXTHOP);
        print_string(PRINT_FP, NULL, "%s", "\n");
-       fflush(fp);
 
        if (process_cache)
                ipnh_cache_process_nlmsg(n, &nhe);
@@ -882,7 +881,6 @@ int print_nexthop_bucket(struct nlmsghdr *n, void *arg)
 
        print_string(PRINT_FP, NULL, "%s", "\n");
        close_json_object();
-       fflush(fp);
 
        return 0;
 }
@@ -1178,7 +1176,6 @@ static int ipnh_get_id(__u32 id)
        }
 
        delete_json_obj();
-       fflush(stdout);
 
        free(answer);
 
@@ -1266,7 +1263,6 @@ static int ipnh_list_flush(int argc, char **argv, int action)
        }
 
        delete_json_obj();
-       fflush(stdout);
 
        return 0;
 }
@@ -1342,7 +1338,6 @@ static int ipnh_bucket_list(int argc, char **argv)
        }
 
        delete_json_obj();
-       fflush(stdout);
 
        return 0;
 }
@@ -1383,7 +1378,6 @@ static int ipnh_bucket_get_id(__u32 id, __u16 bucket_index)
        }
 
        delete_json_obj();
-       fflush(stdout);
 
        free(answer);
 
index 54db9b62c837dba555f673902cb84598f31eaf01..51fa067cc53de88c4b67cfe1dd21df5da3c201d7 100644 (file)
@@ -532,7 +532,6 @@ static void print_ndtstats(const struct ndt_stats *ndts)
 
 static int print_ntable(struct nlmsghdr *n, void *arg)
 {
-       FILE *fp = (FILE *)arg;
        struct ndtmsg *ndtm = NLMSG_DATA(n);
        int len = n->nlmsg_len;
        struct rtattr *tb[NDTA_MAX+1];
@@ -633,7 +632,6 @@ static int print_ntable(struct nlmsghdr *n, void *arg)
 
        print_string(PRINT_FP, NULL, "\n", "");
        close_json_object();
-       fflush(fp);
 
        return 0;
 }
index 4ec305241cebf656cc65c3a21c0e16914dba75c8..5704f2f951b3677fd36be7943fb4c3e54ded9ef1 100644 (file)
@@ -81,7 +81,6 @@ int print_prefix(struct nlmsghdr *n, void *arg)
        }
 
        fprintf(fp, "\n");
-       fflush(fp);
 
        return 0;
 }
index c2538894da633b56f5b926ecafda4960a48e9fbd..5b9e7ac1134a3ff01f8a08ba971fc41ca99aa59e 100644 (file)
@@ -1012,7 +1012,6 @@ int print_route(struct nlmsghdr *n, void *arg)
 
        print_string(PRINT_FP, NULL, "\n", NULL);
        close_json_object();
-       fflush(fp);
        return 0;
 }
 
@@ -1792,6 +1791,7 @@ static int iproute_flush(int family, rtnl_filter_t filter_fn)
                if (time(0) - start > 30) {
                        printf("\n*** Flush not completed after %ld seconds, %d entries remain ***\n",
                               (long)(time(0) - start), filter.flushed);
+                       fflush(stdout);
                        return -1;
                }
 
@@ -2035,7 +2035,6 @@ static int iproute_list_flush_or_save(int argc, char **argv, int action)
        }
 
        delete_json_obj();
-       fflush(stdout);
        return 0;
 }
 
index 3af02da24950ae224614e8e45e3de400675eee57..b56b1b1831ece22eb5e72d84955381a44fc05ff8 100644 (file)
@@ -599,7 +599,6 @@ int print_rule(struct nlmsghdr *n, void *arg)
 
        print_string(PRINT_FP, NULL, "\n", "");
        close_json_object();
-       fflush(fp);
        return 0;
 }
 
index 305b89614af5a184239dc4b175888ca9b0e65210..efd3722759437087e030518c9be16a39773aad5a 100644 (file)
@@ -197,7 +197,6 @@ static int seg6_do_cmd(void)
                        exit(1);
                }
                delete_json_obj();
-               fflush(stdout);
        }
 
        return 0;
index f0f8dcdcbd2a9e1c83eed6ca54df28b91c058d9c..8a2ac85e20724991a9c1e449a93b2cc381ce3ce5 100644 (file)
@@ -879,7 +879,6 @@ static int ipstats_dump(struct ipstats_stat_enabled *enabled)
                rc = -2;
        }
 
-       fflush(stdout);
        return rc;
 }
 
@@ -1355,6 +1354,5 @@ int ipstats_print(struct nlmsghdr *n, void *arg)
        if (rc)
                return rc;
 
-       fflush(fp);
        return 0;
 }
index f25a7c8b21f57c941d975a0255b0613f8a38f999..8bd9b4dc9d1fda223f11059c9969b73a82e05ab7 100644 (file)
@@ -41,7 +41,6 @@ static void usage(void)
 static int print_token(struct nlmsghdr *n, void *arg)
 {
        struct rtnl_dump_args *args = arg;
-       FILE *fp = args->fp;
        int ifindex = args->ifindex;
        struct ifinfomsg *ifi = NLMSG_DATA(n);
        int len = n->nlmsg_len;
@@ -85,7 +84,6 @@ static int print_token(struct nlmsghdr *n, void *arg)
                           "ifname", "%s\n",
                           ll_index_to_name(ifi->ifi_index));
        close_json_object();
-       fflush(fp);
 
        return 0;
 }
index 6718ec6cb316b58fc020e6acf66a61deb2dc1a7a..0f46fe6a03e4b9dd49e3a83f34a0d2915f4c9f5d 100644 (file)
@@ -444,7 +444,6 @@ static int do_show(int argc, char **argv)
        }
 
        delete_json_obj();
-       fflush(stdout);
 
        return 0;
 }
index 9c8fb07240bf01511aa2fc8cc2e80603db3252c9..6b9e68316df5fc750eb6bd4924a277fbb36adf46 100644 (file)
@@ -156,7 +156,6 @@ static void print_tcp_metrics(struct rtattr *a)
 
 static int process_msg(struct nlmsghdr *n, void *arg)
 {
-       FILE *fp = (FILE *) arg;
        struct genlmsghdr *ghdr;
        struct rtattr *attrs[TCP_METRICS_ATTR_MAX + 1], *a;
        const char *h;
@@ -334,7 +333,6 @@ static int process_msg(struct nlmsghdr *n, void *arg)
 
        print_string(PRINT_FP, NULL, "\n", "");
        close_json_object();
-       fflush(fp);
        return 0;
 }
 
index 1f67fe9d14ffd84d1fdaf459ab5c14662cb6538c..a96358b04fe56eb706f95f869c0fb0e47c6ce9df 100644 (file)
@@ -85,7 +85,6 @@ static int xfrm_acquire_print(struct nlmsghdr *n, void *arg)
 
        if (oneline)
                fprintf(fp, "\n");
-       fflush(fp);
 
        return 0;
 }
@@ -114,7 +113,6 @@ static int xfrm_state_flush_print(struct nlmsghdr *n, void *arg)
 
        if (oneline)
                fprintf(fp, "\n");
-       fflush(fp);
 
        return 0;
 }
@@ -151,7 +149,6 @@ static int xfrm_policy_flush_print(struct nlmsghdr *n, void *arg)
 
        if (oneline)
                fprintf(fp, "\n");
-       fflush(fp);
 
        return 0;
 }
@@ -232,7 +229,6 @@ static int xfrm_ae_print(struct nlmsghdr *n, void *arg)
        xfrm_usersa_print(&id->sa_id, id->reqid, fp);
 
        fprintf(fp, "\n");
-       fflush(fp);
 
        return 0;
 }
@@ -257,7 +253,6 @@ static int xfrm_mapping_print(struct nlmsghdr *n, void *arg)
        xfrm_usersa_print(&map->id, map->reqid, fp);
 
        fprintf(fp, "\n");
-       fflush(fp);
        return 0;
 }
 
index 8687ced35a25f8b02b57b991b69b489fede3abba..e60acd5bb98fa81bf791e35d88aa7a4b904f6ba4 100644 (file)
@@ -565,7 +565,6 @@ int xfrm_policy_print(struct nlmsghdr *n, void *arg)
 
        if (oneline)
                fprintf(fp, "\n");
-       fflush(fp);
 
        return 0;
 }
@@ -1232,7 +1231,6 @@ int xfrm_policy_default_print(struct nlmsghdr *n, FILE *fp)
        fprintf(fp, " in:  %s\n", xfrm_policy_to_str(up->in));
        fprintf(fp, " fwd: %s\n", xfrm_policy_to_str(up->fwd));
        fprintf(fp, " out: %s\n", xfrm_policy_to_str(up->out));
-       fflush(fp);
 
        return 0;
 }
index 252a7a5341943033ada792848848fec455a8a977..dcd08d5957385ac1bc2332bc198de6f10f624e9b 100644 (file)
@@ -1051,7 +1051,6 @@ static int __do_xfrm_state_print(struct nlmsghdr *n, void *arg, bool nokeys)
 
        if (oneline)
                fprintf(fp, "\n");
-       fflush(fp);
 
        return 0;
 }
index 6b275a1f1a9ad2d5cbdffd8d6e440b94b55f4a41..8905e297ffb5f0fffefbf9d75240c0df1c9ab787 100644 (file)
@@ -1275,6 +1275,7 @@ int rtnl_listen(struct rtnl_handle *rtnl,
                        }
 
                        err = handler(&ctrl, h, jarg);
+                       fflush(stdout);
                        if (err < 0)
                                return err;
 
index 6d707d8c924f4b5e90201d4de8e9779f82ce17f3..ea6d986ae9cf1f64cc3468dc5aa82478c3208c09 100644 (file)
@@ -380,7 +380,6 @@ int print_class(struct nlmsghdr *n, void *arg)
                close_json_object();
        }
        close_json_object();
-       fflush(fp);
        return 0;
 }
 
index 7db850bda11a3408ec66b457c9fc590f3a734f65..b1dcf1bae54e508e21af89030f9e084dcd17c0d4 100644 (file)
@@ -374,7 +374,6 @@ int print_filter(struct nlmsghdr *n, void *arg)
 
        print_ext_msg(tb);
        close_json_object();
-       fflush(fp);
        return 0;
 }
 
index 7eb9a31baa31fb91a8141c9eca1135e76a52fa8b..634d5f70b48a389bde75fc2349afecd43f97e9b4 100644 (file)
@@ -344,7 +344,6 @@ int print_qdisc(struct nlmsghdr *n, void *arg)
 
        print_ext_msg(tb);
        close_json_object();
-       fflush(fp);
        return 0;
 }