diff mbox series

tools: fix reading file from stdin for client example

Message ID 20190405093235.16226-1-jneuhauser@dh-electronics.com
State Changes Requested
Headers show
Series tools: fix reading file from stdin for client example | expand

Commit Message

Johann Neuhauser April 5, 2019, 9:32 a.m. UTC
Signed-off-by: Johann Neuhauser <jneuhauser@dh-electronics.com>
---
 tools/client.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Stefano Babic April 5, 2019, 9:52 a.m. UTC | #1
Hallo Johan,

On 05/04/19 11:32, Johann Neuhauser wrote:
> Signed-off-by: Johann Neuhauser <jneuhauser@dh-electronics.com>
> ---
>  tools/client.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/tools/client.c b/tools/client.c
> index 0536cb2..279e422 100644
> --- a/tools/client.c
> +++ b/tools/client.c
> @@ -38,7 +38,7 @@ static void usage(void) {
>  }
>  
>  char buf[256];
> -int fd;
> +int fd = STDIN_FILENO;
>  int verbose = 1;
>  bool dryrun = false;
>  int end_status = EXIT_SUCCESS;
> @@ -99,7 +99,7 @@ static int end(RECOVERY_STATUS status)
>   */
>  static int send_file(const char* filename) {
>  	int rc;
> -	if ( (fd = open(filename, O_RDONLY)) < 0) {
> +	if (filename && (fd = open(filename, O_RDONLY)) < 0) {
>  		printf ("I cannot open %s\n", filename);
>  		return EXIT_FAILURE;
>  	}
> 

I think we need to extend it with a clean help. After this patch, client
hangs waiting for data from STDIN, but the user cannot know. It should
be better to print this information like "no input given, reading from
STDIN" to notify this and to fix "client -h" (this is cuzrrently broken).

Best regards,
Stefano Babic
'Darko Komljenovic' via swupdate April 5, 2019, 9:58 a.m. UTC | #2
Hi Johann,

Thanks for posting this patch.

On Friday, 5 April 2019 20:32:08 UTC+11, Johann Neuhauser wrote:
>
> Signed-off-by: Johann Neuhauser <jneuh...@dh-electronics.com <javascript:>> 
>
> --- 
>  tools/client.c | 4 ++-- 
>  1 file changed, 2 insertions(+), 2 deletions(-) 
>
> diff --git a/tools/client.c b/tools/client.c 
> index 0536cb2..279e422 100644 
> --- a/tools/client.c 
> +++ b/tools/client.c 
> @@ -38,7 +38,7 @@ static void usage(void) { 
>  } 
>   
>  char buf[256]; 
> -int fd; 
> +int fd = STDIN_FILENO; 
>  int verbose = 1; 
>  bool dryrun = false; 
>  int end_status = EXIT_SUCCESS; 
> @@ -99,7 +99,7 @@ static int end(RECOVERY_STATUS status) 
>   */ 
>  static int send_file(const char* filename) { 
>          int rc; 
> -        if ( (fd = open(filename, O_RDONLY)) < 0) { 
> +        if (filename && (fd = open(filename, O_RDONLY)) < 0) { 
>

This optionally opens a file descriptor (fd) depending on whether filename 
is NULL.

The patch should also include the optional close of this file during 
cleanup.  As it
stands, stdin will be closed in close(fd) calls, even though it wasn't 
opened by
this function.

i.e.  If you didn't open fd, done close it.  The converse is also true, if 
fd was
opened by the function, it should be closed by the function.
 

>                  printf ("I cannot open %s\n", filename); 
>                  return EXIT_FAILURE; 
>          } 
> -- 
> 2.11.0 
>
>
There have been recent changes in this area so ensure the patch applies to 
the
tip of master if you haven't done so already.

Regards
Austin
Johann Neuhauser April 26, 2019, 6:48 a.m. UTC | #3
On Friday, April 5, 2019 at 11:32:08 AM UTC+2, Johann Neuhauser wrote:
> Signed-off-by: Johann Neuhauser <jneuhauser@dh-electronics.com>
> ---
>  tools/client.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/tools/client.c b/tools/client.c
> index 0536cb2..279e422 100644
> --- a/tools/client.c
> +++ b/tools/client.c
> @@ -38,7 +38,7 @@ static void usage(void) {
>  }
>  
>  char buf[256];
> -int fd;
> +int fd = STDIN_FILENO;
>  int verbose = 1;
>  bool dryrun = false;
>  int end_status = EXIT_SUCCESS;
> @@ -99,7 +99,7 @@ static int end(RECOVERY_STATUS status)
>   */
>  static int send_file(const char* filename) {
>  	int rc;
> -	if ( (fd = open(filename, O_RDONLY)) < 0) {
> +	if (filename && (fd = open(filename, O_RDONLY)) < 0) {
>  		printf ("I cannot open %s\n", filename);
>  		return EXIT_FAILURE;
>  	}
> -- 
> 2.11.0

Superseded by: 
http://patchwork.ozlabs.org/patch/1091358/ 
https://groups.google.com/forum/#!topic/swupdate/ZfzLm9fKi8M
diff mbox series

Patch

diff --git a/tools/client.c b/tools/client.c
index 0536cb2..279e422 100644
--- a/tools/client.c
+++ b/tools/client.c
@@ -38,7 +38,7 @@  static void usage(void) {
 }
 
 char buf[256];
-int fd;
+int fd = STDIN_FILENO;
 int verbose = 1;
 bool dryrun = false;
 int end_status = EXIT_SUCCESS;
@@ -99,7 +99,7 @@  static int end(RECOVERY_STATUS status)
  */
 static int send_file(const char* filename) {
 	int rc;
-	if ( (fd = open(filename, O_RDONLY)) < 0) {
+	if (filename && (fd = open(filename, O_RDONLY)) < 0) {
 		printf ("I cannot open %s\n", filename);
 		return EXIT_FAILURE;
 	}