parser_json: use stdin buffer if available
Pablo Neira Ayuso
maintainerPATCH 1/2 parser_json: use stdin buffer if available
Since 5c2b2b0a2ba7 ("src: error reporting with -f and read from stdin") stdin is stored in a buffer, update json support to use it instead of reading from /dev/stdin.
Some systems do not provide /dev/stdin symlink to /proc/self/fd/0 according to reporter (that mentions Yocto Linux as example).
--- src/parser_json.c | 7 +++++++ 1 file changed, 7 insertions(+)
src/parser_json.c +7 -0
@@ -4357,6 +4357,13 @@ int nft_parse_json_filename(struct nft_ctx *nft, const char *filename,json_error_t err;int ret; +if (nft->stdin_buf) { + json_indesc.type =INDESC_STDIN ; + json_indesc.name ="/dev/stdin" ; + +return nft_parse_json_buffer (nft, nft->stdin_buf, msgs, cmds); + } + json_indesc.type =INDESC_FILE ; json_indesc.name = filename;
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 2/2 libnftables: skip useable checks for /dev/stdin
/dev/stdin is a placeholder, read() from STDIN_FILENO is used to fetch the standard input into a buffer.
Since 5c2b2b0a2ba7 ("src: error reporting with -f and read from stdin") stdin is stored in a buffer to fix error reporting.
--- src/libnftables.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
src/libnftables.c +4 -3
@@ -664,6 +664,7 @@ retry:/* need to use stat() to, fopen() will block for named fifos and * libjansson makes no checks before or after open either. +* /dev/stdin is *never* used, read() from STDIN_FILENO is used instead. */ static struct error_record *filename_is_useable (struct nft_ctx *nft,const char *name) {
@@ -671,6 +672,9 @@ static struct error_record *filename_is_useable(struct nft_ctx *nft, const charstruct stat sb;int err; +if (!strcmp (name,"/dev/stdin" )) +return NULL ; + err =stat (name, &sb);if (err) return error(&internal_location, "Could not open file \"%s\": %s\n",
@@ -681,9 +685,6 @@ static struct error_record *filename_is_useable(struct nft_ctx *nft, const charif (type ==S_IFREG || type ==S_IFIFO )return NULL ; -if (type ==S_IFCHR &&0 ==strcmp (name,"/dev/stdin" )) -return NULL ; -return error (&internal_location,"Not a regular file: \"%s\"\n" , name); }
Signature · 1 line
2.30.2
Phil Sutter
reviewerHi Pablo,
Quoted 17 lines · Pablo Neira Ayuso
Since
5c2b2b0a2ba7("src: error reporting with -f and read from stdin") stdin is stored in a buffer, update json support to use it instead of reading from/dev/stdin.Some systems do not provide
/dev/stdinsymlink to/proc/self/fd/0according to reporter (that mentions Yocto Linux as example).Signed-off-by Pablo Neira Ayuso <[email protected]>--- src/parser_json.c | 7 +++++++ 1 file changed, 7 insertions(+)src/parser_json.c +0
-0
src/parser_json.c int nft_parse_json_filename(struct nft_ctx *nft, const char *filename, · line 4365
Quoted 9 lines
src/parser_json.c +6
-0@@ -4357,6 +4357,13 @@ int nft_parse_json_filename(struct nft_ctx *nft, const char *filename,json_error_t err;int ret; +if (nft->stdin_buf) { + json_indesc.type =INDESC_STDIN ; + json_indesc.name ="/dev/stdin" ; + +return nft_parse_json_buffer (nft, nft->stdin_buf, msgs, cmds); + }
Is this sufficient? In nft_run_cmd_from_filename(), nft->stdin_buf is populated conditionally:
| if (!strcmp(filename, "/dev/stdin") && | !nft_output_json(&nft->output)) | nft->stdin_buf = stdin_to_buffer();
Later (in the wrapped __nft_run_cmd_from_filename()), we try JSON parsing conditionally:
| if (nft_output_json(&nft->output) || nft_input_json(&nft->input)) | rc = nft_parse_json_filename(nft, filename, &msgs, &cmds);
Things got complicated by commit 2034d8c60ed91 ("src: add input flag NFT_CTX_INPUT_JSON to enable JSON parsing") and my request to remain compatible, i.e. '-j' flag which enables JSON output shall continue to make JSON the assumed input format.
So long story short, I guess in order to cover all cases, we have to enable nft->stdin_buf population also if nft_input_json(...) returns true, i.e. cover for library users requesting JSON input (but standard output). WDYT?
Cheers, Phil
Phil Sutter
reviewerQuoted 48 lines · Phil Sutter
Hi Pablo,
Quoted 17 lines · Pablo Neira Ayuso
Since
5c2b2b0a2ba7("src: error reporting with -f and read from stdin") stdin is stored in a buffer, update json support to use it instead of reading from/dev/stdin.Some systems do not provide
/dev/stdinsymlink to/proc/self/fd/0according to reporter (that mentions Yocto Linux as example).Signed-off-by Pablo Neira Ayuso <[email protected]>--- src/parser_json.c | 7 +++++++ 1 file changed, 7 insertions(+)src/parser_json.c +0
-0src/parser_json.c int nft_parse_json_filename(struct nft_ctx *nft, const char *filename, · line 4365
Quoted 9 lines
src/parser_json.c +6
-0@@ -4357,6 +4357,13 @@ int nft_parse_json_filename(struct nft_ctx *nft, const char *filename,json_error_t err;int ret; +if (nft->stdin_buf) { + json_indesc.type =INDESC_STDIN ; + json_indesc.name ="/dev/stdin" ; + +return nft_parse_json_buffer (nft, nft->stdin_buf, msgs, cmds); + }Is this sufficient? In
nft_run_cmd_from_filename(),nft->stdin_bufis populated conditionally:| if (!strcmp(filename, "/dev/stdin") && | !nft_output_json(&nft->output)) | nft->stdin_buf = stdin_to_buffer();Later (in the wrapped
__nft_run_cmd_from_filename()), we try JSON parsing conditionally:| if (nft_output_json(&nft->output) || nft_input_json(&nft->input)) | rc = nft_parse_json_filename(nft, filename, &msgs, &cmds);Things got complicated by commit
2034d8c60ed91("src: add input flagNFT_CTX_INPUT_JSONto enable JSON parsing") and my request to remain compatible, i.e. '-j' flag which enables JSON output shall continue to make JSON the assumed input format.
Quoted 4 lines
So long story short, I guess in order to cover all cases, we have to enable
nft->stdin_bufpopulation also ifnft_input_json(...)returns true, i.e. cover for library users requesting JSON input (but standard output). WDYT?
On second review, I think the right change is to make nft_run_cmd_from_filename() *always* populate nft->stdin_buf if 'filename' is '/dev/stdin', i.e. drop the !nft_output_json(...) clause.
Sorry for the confusion.
Cheers, Phil
Pablo Neira Ayuso
maintainerQuoted 54 lines · Phil Sutter
Quoted 48 lines · Phil Sutter
Hi Pablo,
Quoted 17 lines · Pablo Neira Ayuso
Since
5c2b2b0a2ba7("src: error reporting with -f and read from stdin") stdin is stored in a buffer, update json support to use it instead of reading from/dev/stdin.Some systems do not provide
/dev/stdinsymlink to/proc/self/fd/0according to reporter (that mentions Yocto Linux as example).Signed-off-by Pablo Neira Ayuso <[email protected]>--- src/parser_json.c | 7 +++++++ 1 file changed, 7 insertions(+)src/parser_json.c +0
-0src/parser_json.c int nft_parse_json_filename(struct nft_ctx *nft, const char *filename, · line 4365
Quoted 9 lines
src/parser_json.c +6
-0@@ -4357,6 +4357,13 @@ int nft_parse_json_filename(struct nft_ctx *nft, const char *filename,json_error_t err;int ret; +if (nft->stdin_buf) { + json_indesc.type =INDESC_STDIN ; + json_indesc.name ="/dev/stdin" ; + +return nft_parse_json_buffer (nft, nft->stdin_buf, msgs, cmds); + }Is this sufficient? In
nft_run_cmd_from_filename(),nft->stdin_bufis populated conditionally:| if (!strcmp(filename, "/dev/stdin") && | !nft_output_json(&nft->output)) | nft->stdin_buf = stdin_to_buffer();Later (in the wrapped
__nft_run_cmd_from_filename()), we try JSON parsing conditionally:| if (nft_output_json(&nft->output) || nft_input_json(&nft->input)) | rc = nft_parse_json_filename(nft, filename, &msgs, &cmds);Things got complicated by commit
2034d8c60ed91("src: add input flagNFT_CTX_INPUT_JSONto enable JSON parsing") and my request to remain compatible, i.e. '-j' flag which enables JSON output shall continue to make JSON the assumed input format.Quoted 4 lines
So long story short, I guess in order to cover all cases, we have to enable
nft->stdin_bufpopulation also ifnft_input_json(...)returns true, i.e. cover for library users requesting JSON input (but standard output). WDYT?
Quoted 5 lines
On second review, I think the right change is to make
nft_run_cmd_from_filename()*always* populatenft->stdin_bufif 'filename' is '/dev/stdin', i.e. drop the !nft_output_json(...) clause.Sorry for the confusion.
I can squash this incremental fix to 1/2 send post a v2.
Thanks.
src/libnftables.c +1 -2
@@ -807,8 +807,7 @@ int nft_run_cmd_from_filename(struct nft_ctx *nft, const char *filename)if (!strcmp (filename,"-" )) filename ="/dev/stdin" ; -if (!strcmp (filename,"/dev/stdin" ) && - !nft_output_json (&nft->output)) +if (!strcmp (filename,"/dev/stdin" )) nft->stdin_buf =stdin_to_buffer (); if (!nft->stdin_buf &&
Quoted 55 lines · Pablo Neira Ayuso
Quoted 54 lines · Phil Sutter
Quoted 52 lines · Phil Sutter
Hi Pablo,
Quoted 17 lines · Pablo Neira Ayuso
Since
5c2b2b0a2ba7("src: error reporting with -f and read from stdin") stdin is stored in a buffer, update json support to use it instead of reading from/dev/stdin.Some systems do not provide
/dev/stdinsymlink to/proc/self/fd/0according to reporter (that mentions Yocto Linux as example).Signed-off-by Pablo Neira Ayuso <[email protected]>--- src/parser_json.c | 7 +++++++ 1 file changed, 7 insertions(+)src/parser_json.c +0
-0src/parser_json.c int nft_parse_json_filename(struct nft_ctx *nft, const char *filename, · line 4365
Quoted 9 lines
src/parser_json.c +6
-0@@ -4357,6 +4357,13 @@ int nft_parse_json_filename(struct nft_ctx *nft, const char *filename,json_error_t err;int ret; +if (nft->stdin_buf) { + json_indesc.type =INDESC_STDIN ; + json_indesc.name ="/dev/stdin" ; + +return nft_parse_json_buffer (nft, nft->stdin_buf, msgs, cmds); + }Is this sufficient? In
nft_run_cmd_from_filename(),nft->stdin_bufis populated conditionally:| if (!strcmp(filename, "/dev/stdin") && | !nft_output_json(&nft->output)) | nft->stdin_buf = stdin_to_buffer();Later (in the wrapped
__nft_run_cmd_from_filename()), we try JSON parsing conditionally:| if (nft_output_json(&nft->output) || nft_input_json(&nft->input)) | rc = nft_parse_json_filename(nft, filename, &msgs, &cmds);Things got complicated by commit
2034d8c60ed91("src: add input flagNFT_CTX_INPUT_JSONto enable JSON parsing") and my request to remain compatible, i.e. '-j' flag which enables JSON output shall continue to make JSON the assumed input format.So long story short, I guess in order to cover all cases, we have to enable
nft->stdin_bufpopulation also ifnft_input_json(...)returns true, i.e. cover for library users requesting JSON input (but standard output). WDYT?
Quoted 7 lines
Quoted 5 lines
On second review, I think the right change is to make
nft_run_cmd_from_filename()*always* populatenft->stdin_bufif 'filename' is '/dev/stdin', i.e. drop the !nft_output_json(...) clause.Sorry for the confusion.
I can squash this incremental fix to 1/2 send post a v2.
Thanks, Phil