netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Phil Sutter <phil@nwl.cc>
To: Pablo Neira Ayuso <pablo@netfilter.org>
Cc: netfilter-devel@vger.kernel.org
Subject: [nft PATCH] libnftables: Ensure output_fp is never NULL
Date: Mon, 20 Nov 2017 16:54:04 +0100	[thread overview]
Message-ID: <20171120155404.10894-1-phil@nwl.cc> (raw)
In-Reply-To: <20171120124716.GA17684@salvia>

Initialize output_fp to 'stdout' upon context creation and check output
stream validity in nft_ctx_set_output(). This allows to drop checks in
nft_{gmp_,}print() and do_command_export(). While doing so for the
latter, simplify it a bit by using nft_print() which takes care of
flushing the output stream.

If applications desire to drop all output, they are supposed to open
/dev/null and assign that.

Signed-off-by: Phil Sutter <phil@nwl.cc>
---
 src/libnftables.c | 10 ++++------
 src/main.c        |  1 -
 src/rule.c        |  6 +-----
 3 files changed, 5 insertions(+), 12 deletions(-)

diff --git a/src/libnftables.c b/src/libnftables.c
index e8fa6742f7d17..c86d89477e778 100644
--- a/src/libnftables.c
+++ b/src/libnftables.c
@@ -167,6 +167,7 @@ struct nft_ctx *nft_ctx_new(uint32_t flags)
 	ctx->parser_max_errors	= 10;
 	init_list_head(&ctx->cache.list);
 	ctx->flags = flags;
+	ctx->output.output_fp = stdout;
 
 	if (flags == NFT_CTX_DEFAULT)
 		nft_ctx_netlink_init(ctx);
@@ -190,6 +191,9 @@ FILE *nft_ctx_set_output(struct nft_ctx *ctx, FILE *fp)
 {
 	FILE *old = ctx->output.output_fp;
 
+	if (!fp || ferror(fp))
+		return NULL;
+
 	ctx->output.output_fp = fp;
 
 	return old;
@@ -333,9 +337,6 @@ int nft_print(struct output_ctx *octx, const char *fmt, ...)
 	int ret;
 	va_list arg;
 
-	if (!octx->output_fp)
-		return -1;
-
 	va_start(arg, fmt);
 	ret = vfprintf(octx->output_fp, fmt, arg);
 	va_end(arg);
@@ -349,9 +350,6 @@ int nft_gmp_print(struct output_ctx *octx, const char *fmt, ...)
 	int ret;
 	va_list arg;
 
-	if (!octx->output_fp)
-		return -1;
-
 	va_start(arg, fmt);
 	ret = gmp_vfprintf(octx->output_fp, fmt, arg);
 	va_end(arg);
diff --git a/src/main.c b/src/main.c
index ff7878c94ccb3..353b87bc66631 100644
--- a/src/main.c
+++ b/src/main.c
@@ -173,7 +173,6 @@ int main(int argc, char * const *argv)
 	int i, val, rc;
 
 	nft = nft_ctx_new(NFT_CTX_DEFAULT);
-	nft_ctx_set_output(nft, stdout);
 
 	while (1) {
 		val = getopt_long(argc, argv, OPTSTRING, options, NULL);
diff --git a/src/rule.c b/src/rule.c
index eb91be4636e21..37d99c2200471 100644
--- a/src/rule.c
+++ b/src/rule.c
@@ -1153,9 +1153,6 @@ static int do_command_export(struct netlink_ctx *ctx, struct cmd *cmd)
 	struct nftnl_ruleset *rs;
 	FILE *fp = ctx->octx->output_fp;
 
-	if (!fp)
-		return 0;
-
 	do {
 		rs = netlink_dump_ruleset(ctx, &cmd->handle, &cmd->location);
 		if (rs == NULL && errno != EINTR)
@@ -1163,8 +1160,7 @@ static int do_command_export(struct netlink_ctx *ctx, struct cmd *cmd)
 	} while (rs == NULL);
 
 	nftnl_ruleset_fprintf(fp, rs, cmd->export->format, 0);
-	fprintf(fp, "\n");
-	fflush(fp);
+	nft_print(ctx->octx, "\n");
 
 	nftnl_ruleset_free(rs);
 	return 0;
-- 
2.13.1


  reply	other threads:[~2017-11-20 15:54 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-16  8:06 [nft PATCH 0/2] Review code regarding output_fp Phil Sutter
2017-11-16  8:06 ` [nft PATCH 1/2] Make 'nft export' respect output_fp Phil Sutter
2017-11-16  8:06 ` [nft PATCH 2/2] monitor: Make JSON output " Phil Sutter
2017-11-16 13:38   ` Pablo Neira Ayuso
2017-11-16 13:54     ` Phil Sutter
2017-11-16 13:57       ` Pablo Neira Ayuso
2017-11-16 13:54     ` Pablo Neira Ayuso
2017-11-16 13:58       ` Phil Sutter
2017-11-16 14:12         ` Pablo Neira Ayuso
2017-11-16 14:19           ` Phil Sutter
2017-11-16 14:32             ` Pablo Neira Ayuso
2017-11-16 19:14               ` [nft PATCH RFC] libnftables: Make output_fp default to /dev/null Phil Sutter
2017-11-20 12:32                 ` Pablo Neira Ayuso
2017-11-20 12:33                   ` Pablo Neira Ayuso
2017-11-20 12:38                     ` Phil Sutter
2017-11-20 12:47                       ` Pablo Neira Ayuso
2017-11-20 15:54                         ` Phil Sutter [this message]
2017-11-22 12:17                           ` [nft PATCH] libnftables: Ensure output_fp is never NULL Pablo Neira Ayuso
2017-11-16 13:34 ` [nft PATCH 0/2] Review code regarding output_fp Pablo Neira Ayuso

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20171120155404.10894-1-phil@nwl.cc \
    --to=phil@nwl.cc \
    --cc=netfilter-devel@vger.kernel.org \
    --cc=pablo@netfilter.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).