bfd: bread: read in a loop when running with --remote

Submitted by Mike Rapoport on July 1, 2017, 3:41 p.m.

Details

Message ID 1498923671-31518-1-git-send-email-rppt@linux.vnet.ibm.com
State New
Series "bfd: bread: read in a loop when running with --remote"
Headers show

Commit Message

Mike Rapoport July 1, 2017, 3:41 p.m.
From: Omri Kramer <omri.kramer@gmail.com>

When the --remote option is used, the file descriptor used in bread is a
socket and read from it may return after only part of the data is received.
Keep looping until all the data is received when --remote option is on.

Signed-off-by: Omri Kramer <omri.kramer@gmail.com>
Signed-off-by: Lior Fisch <fischlior@gmail.com>
Reviewed-by: Mike Rapoprot <rppt@linux.vnet.ibm.com>
---
 criu/bfd.c | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)

Patch hide | download patch | download mbox

diff --git a/criu/bfd.c b/criu/bfd.c
index 8269ab1..c03bc88 100644
--- a/criu/bfd.c
+++ b/criu/bfd.c
@@ -298,9 +298,29 @@  int bread(struct bfd *bfd, void *buf, int size)
 {
 	struct xbuf *b = &bfd->b;
 	int more = 1, filled = 0;
+	int ret;
+	size_t curr;
 
-	if (!bfd_buffered(bfd))
-		return read(bfd->fd, buf, size);
+	if (!bfd_buffered(bfd)) {
+		if(!opts.remote)
+			return read(bfd->fd, buf, size);
+
+		/*
+		 * With '--remote' bfd->fd is a socket and we may get
+		 * partial reads here. Keep reading until all the data
+		 * is received from the socket
+		 */
+		while (1) {
+			ret = read(bfd->fd, buf + curr, size - curr);
+			if (ret < 0) {
+				pr_perror("Can't read from buffer\n");
+				return -1;
+			}
+
+			curr += ret;
+			if (curr == size || ret == 0)
+				return curr;
+	}
 
 	while (more > 0) {
 		int chunk;

Comments

Pavel Emelianov July 3, 2017, 10:11 a.m.
On 07/01/2017 08:11 PM, Patchwork wrote:
> == Series Details ==
> 
> Series: bfd: bread: read in a loop when running with --remote
> URL   : https://patchwork.criu.org/series/1719/
> State : failure
> 
> == Logs ==
> 
> For more details see: https://travis-ci.org/criupatchwork/criu/builds/249132454?utm_source=github_status&utm_medium=notification

It looks like everything has failed due to ...

> criu/bfd.c: In function ‘bread’:
> criu/bfd.c:305:7: error: ‘opts’ undeclared (first use in this function)
>    if(!opts.remote)
>        ^
> criu/bfd.c:305:7: note: each undeclared identifier is reported only once for each function it appears in
> criu/bfd.c:348:1: error: expected declaration or statement at end of input
>  }
>  ^
> criu/bfd.c:348:1: error: control reaches end of non-void function [-Werror=return-type]
>  }

... a missing header inclusion.
Pavel Emelianov July 3, 2017, 10:11 a.m.
On 07/01/2017 06:41 PM, Mike Rapoport wrote:
> From: Omri Kramer <omri.kramer@gmail.com>
> 
> When the --remote option is used, the file descriptor used in bread is a
> socket and read from it may return after only part of the data is received.
> Keep looping until all the data is received when --remote option is on.
> 
> Signed-off-by: Omri Kramer <omri.kramer@gmail.com>
> Signed-off-by: Lior Fisch <fischlior@gmail.com>
> Reviewed-by: Mike Rapoprot <rppt@linux.vnet.ibm.com>
> ---
>  criu/bfd.c | 24 ++++++++++++++++++++++--
>  1 file changed, 22 insertions(+), 2 deletions(-)
> 
> diff --git a/criu/bfd.c b/criu/bfd.c
> index 8269ab1..c03bc88 100644
> --- a/criu/bfd.c
> +++ b/criu/bfd.c
> @@ -298,9 +298,29 @@ int bread(struct bfd *bfd, void *buf, int size)
>  {
>  	struct xbuf *b = &bfd->b;
>  	int more = 1, filled = 0;
> +	int ret;
> +	size_t curr;
>  
> -	if (!bfd_buffered(bfd))
> -		return read(bfd->fd, buf, size);
> +	if (!bfd_buffered(bfd)) {
> +		if(!opts.remote)
> +			return read(bfd->fd, buf, size);
> +
> +		/*
> +		 * With '--remote' bfd->fd is a socket and we may get
> +		 * partial reads here. Keep reading until all the data
> +		 * is received from the socket
> +		 */
> +		while (1) {
> +			ret = read(bfd->fd, buf + curr, size - curr);
> +			if (ret < 0) {
> +				pr_perror("Can't read from buffer\n");
> +				return -1;
> +			}
> +
> +			curr += ret;
> +			if (curr == size || ret == 0)
> +				return curr;

I guess here the closing curly brace should go :)

> +	}
>  
>  	while (more > 0) {
>  		int chunk;
>