diff mbox series

[v4] client: simply return if async start fails, fix fd leak

Message ID 1554321488-14153-1-git-send-email-awais_belal@mentor.com
State Accepted
Headers show
Series [v4] client: simply return if async start fails, fix fd leak | expand

Commit Message

Awais Belal April 3, 2019, 7:58 p.m. UTC
The client should return after cleaning up if async
start fails for any reason otherwise the caller will
block because the client app will keep on waiting for
the mutex which will never be triggered. Also fix a
leaked file descriptor in case of a successful run
plus align return values.

Signed-off-by: Awais Belal <awais_belal@mentor.com>
---
 tools/client.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

Comments

'Darko Komljenovic' via swupdate April 3, 2019, 11:11 p.m. UTC | #1
On Thursday, 4 April 2019 06:58:40 UTC+11, Awais Belal wrote:
>
> The client should return after cleaning up if async 
> start fails for any reason otherwise the caller will 
> block because the client app will keep on waiting for 
> the mutex which will never be triggered. Also fix a 
> leaked file descriptor in case of a successful run 
> plus align return values. 
>
> Signed-off-by: Awais Belal <awais...@mentor.com <javascript:>> 
> --- 
>  tools/client.c | 12 ++++++++++-- 
>  1 file changed, 10 insertions(+), 2 deletions(-) 
>
> diff --git a/tools/client.c b/tools/client.c 
> index 6dd378c..0536cb2 100644 
> --- a/tools/client.c 
> +++ b/tools/client.c 
> @@ -101,7 +101,7 @@ static int send_file(const char* filename) { 
>          int rc; 
>          if ( (fd = open(filename, O_RDONLY)) < 0) { 
>                  printf ("I cannot open %s\n", filename); 
> -                return 1; 
> +                return EXIT_FAILURE; 
>          } 
>   
>          /* synchronize with a mutex */ 
> @@ -113,8 +113,14 @@ static int send_file(const char* filename) { 
>   
>          rc = swupdate_async_start(readimage, printstatus, 
>                                  end, dryrun); 
> -        if (rc) 
> + 
> +        /* return if we've hit an error scenario */ 
> +        if (rc < 0) { 
>                  printf("swupdate_async_start returns %d\n", rc); 
> +                pthread_mutex_unlock(&mymutex); 
> +                close(fd); 
> +                return EXIT_FAILURE; 
> +        } 
>   
>          /* Now block */ 
>          pthread_mutex_lock(&mymutex); 
> @@ -122,6 +128,8 @@ static int send_file(const char* filename) { 
>          /* End called, unlock and exit */ 
>          pthread_mutex_unlock(&mymutex); 
>   
> +        close(fd); 
> + 
>          return end_status; 
>  } 
>   
> -- 
> 2.7.4 
>
> Tested-by: Austin Phillips <austin.phillips@planetinnovation.com.au>
diff mbox series

Patch

diff --git a/tools/client.c b/tools/client.c
index 6dd378c..0536cb2 100644
--- a/tools/client.c
+++ b/tools/client.c
@@ -101,7 +101,7 @@  static int send_file(const char* filename) {
 	int rc;
 	if ( (fd = open(filename, O_RDONLY)) < 0) {
 		printf ("I cannot open %s\n", filename);
-		return 1;
+		return EXIT_FAILURE;
 	}
 
 	/* synchronize with a mutex */
@@ -113,8 +113,14 @@  static int send_file(const char* filename) {
 
 	rc = swupdate_async_start(readimage, printstatus,
 				end, dryrun);
-	if (rc)
+
+	/* return if we've hit an error scenario */
+	if (rc < 0) {
 		printf("swupdate_async_start returns %d\n", rc);
+		pthread_mutex_unlock(&mymutex);
+		close(fd);
+		return EXIT_FAILURE;
+	}
 
 	/* Now block */
 	pthread_mutex_lock(&mymutex);
@@ -122,6 +128,8 @@  static int send_file(const char* filename) {
 	/* End called, unlock and exit */
 	pthread_mutex_unlock(&mymutex);
 
+	close(fd);
+
 	return end_status;
 }