[PATCHv2,6/6] remote: Use tmp file buffer when restore ip dump

Submitted by Radostin Stoyanov on Sept. 3, 2018, 8:35 p.m.

Details

Message ID 20180903203514.32423-7-rstoyanov1@gmail.com
State New
Series "remote: Refactor TCP client/server setup"
Headers show

Commit Message

Radostin Stoyanov Sept. 3, 2018, 8:35 p.m.
When CRIU calls the ip tool on restore, it passes the fd of remote
socket by replacing the STDIN before execvp. The stdin is used by the
ip tool to receive input. However, the ip tool calls ftell(stdin)
which fails with "Illegal seek" since UNIX sockets do not support file
positioning operations. To resolve this issue, read the received
content from the UNIX socket and store it into temporary file, then
replace STDIN with the fd of this tmp file.

Fixes #311

Signed-off-by: Radostin Stoyanov <rstoyanov1@gmail.com>
---
 criu/net.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

Patch hide | download patch | download mbox

diff --git a/criu/net.c b/criu/net.c
index 0431b62f..679212d0 100644
--- a/criu/net.c
+++ b/criu/net.c
@@ -1914,19 +1914,27 @@  out:
 
 static int restore_ip_dump(int type, int pid, char *cmd)
 {
-	int ret = -1;
+	int ret = -1, sockfd, n;
+	FILE *tmp_file;
 	struct cr_img *img;
+	char buf[1024];
 
 	img = open_image(type, O_RSTR, pid);
 	if (empty_image(img)) {
 		close_image(img);
 		return 0;
 	}
+	sockfd = img_raw_fd(img);
+	tmp_file = tmpfile();
+	while ((n = read(sockfd, buf, 1024)) > 0)
+		fwrite(buf, sizeof(char), n, tmp_file);
+	fseek(tmp_file, 0, SEEK_SET);
+
 	if (img) {
-		ret = run_ip_tool(cmd, "restore", NULL, NULL, img_raw_fd(img), -1, 0);
+		ret = run_ip_tool(cmd, "restore", NULL, NULL, fileno(tmp_file), -1, 0);
 		close_image(img);
 	}
-
+	fclose(tmp_file);
 	return ret;
 }