Blob Blame History Raw
From fa5ccccd164b7285c4d105265ece4ea7ccdd996a Mon Sep 17 00:00:00 2001
From: Phil Sutter <psutter@redhat.com>
Date: Wed, 20 Jun 2018 09:22:00 +0200
Subject: [PATCH] monitor: Make JSON/XML output respect output_fp

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1504157
Upstream Status: nftables commit 59034b47590d7

commit 59034b47590d7cd19ba2fda24bf4426c355f95bc
Author: Phil Sutter <phil@nwl.cc>
Date:   Wed Feb 28 16:04:28 2018 +0100

    monitor: Make JSON/XML output respect output_fp

    Make sure events callbacks print to output_ctx-defined stream for any
    type of output format.

    Since all of them use nft_print() as last call (if anything is printed
    at all), the final call to fflush() in netlink_events_cb() can be
    dropped.

    Signed-off-by: Phil Sutter <phil@nwl.cc>
    Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
---
 src/netlink.c | 37 ++++++++++++++++++-------------------
 1 file changed, 18 insertions(+), 19 deletions(-)

diff --git a/src/netlink.c b/src/netlink.c
index d839ded..ca0e207 100644
--- a/src/netlink.c
+++ b/src/netlink.c
@@ -2015,9 +2015,9 @@ static int netlink_events_table_cb(const struct nlmsghdr *nlh, int type,
 		break;
 	case NFTNL_OUTPUT_XML:
 	case NFTNL_OUTPUT_JSON:
-		nftnl_table_fprintf(stdout, nlt, monh->format,
-				    netlink_msg2nftnl_of(type));
-		fprintf(stdout, "\n");
+		nftnl_table_fprintf(monh->ctx->octx->output_fp, nlt,
+				    monh->format, netlink_msg2nftnl_of(type));
+		nft_mon_print(monh, "\n");
 		break;
 	}
 
@@ -2057,9 +2057,9 @@ static int netlink_events_chain_cb(const struct nlmsghdr *nlh, int type,
 		break;
 	case NFTNL_OUTPUT_XML:
 	case NFTNL_OUTPUT_JSON:
-		nftnl_chain_fprintf(stdout, nlc, monh->format,
-				    netlink_msg2nftnl_of(type));
-		fprintf(stdout, "\n");
+		nftnl_chain_fprintf(monh->ctx->octx->output_fp, nlc,
+				    monh->format, netlink_msg2nftnl_of(type));
+		nft_mon_print(monh, "\n");
 		break;
 	}
 
@@ -2104,9 +2104,9 @@ static int netlink_events_set_cb(const struct nlmsghdr *nlh, int type,
 		break;
 	case NFTNL_OUTPUT_XML:
 	case NFTNL_OUTPUT_JSON:
-		nftnl_set_fprintf(stdout, nls, monh->format,
-				netlink_msg2nftnl_of(type));
-		fprintf(stdout, "\n");
+		nftnl_set_fprintf(monh->ctx->octx->output_fp, nls,
+				  monh->format, netlink_msg2nftnl_of(type));
+		nft_mon_print(monh, "\n");
 		break;
 	}
 out:
@@ -2253,9 +2253,9 @@ static int netlink_events_setelem_cb(const struct nlmsghdr *nlh, int type,
 		break;
 	case NFTNL_OUTPUT_XML:
 	case NFTNL_OUTPUT_JSON:
-		nftnl_set_fprintf(stdout, nls, monh->format,
-				  netlink_msg2nftnl_of(type));
-		fprintf(stdout, "\n");
+		nftnl_set_fprintf(monh->ctx->octx->output_fp, nls,
+				  monh->format, netlink_msg2nftnl_of(type));
+		nft_mon_print(monh, "\n");
 		break;
 	}
 out:
@@ -2298,9 +2298,9 @@ static int netlink_events_obj_cb(const struct nlmsghdr *nlh, int type,
 		break;
 	case NFTNL_OUTPUT_XML:
 	case NFTNL_OUTPUT_JSON:
-		nftnl_obj_fprintf(stdout, nlo, monh->format,
-				  netlink_msg2nftnl_of(type));
-		fprintf(stdout, "\n");
+		nftnl_obj_fprintf(monh->ctx->octx->output_fp, nlo,
+				  monh->format, netlink_msg2nftnl_of(type));
+		nft_mon_print(monh, "\n");
 		break;
 	}
 
@@ -2354,9 +2354,9 @@ static int netlink_events_rule_cb(const struct nlmsghdr *nlh, int type,
 		break;
 	case NFTNL_OUTPUT_XML:
 	case NFTNL_OUTPUT_JSON:
-		nftnl_rule_fprintf(stdout, nlr, monh->format,
-				 netlink_msg2nftnl_of(type));
-		fprintf(stdout, "\n");
+		nftnl_rule_fprintf(monh->ctx->octx->output_fp, nlr,
+				   monh->format, netlink_msg2nftnl_of(type));
+		nft_mon_print(monh, "\n");
 		break;
 	}
 
@@ -2985,7 +2985,6 @@ static int netlink_events_cb(const struct nlmsghdr *nlh, void *data)
 		ret = netlink_events_newgen_cb(nlh, type, monh);
 		break;
 	}
-	fflush(stdout);
 
 	return ret;
 }
-- 
1.8.3.1