[CRIU] [PATCH 1/4] service: some preparations for multirequest support

Ruslan Kuprieiev kupruser at gmail.com
Sun Feb 2 02:55:55 PST 2014


I've removed pre_dump_loop() because the same mechanism is provided by multireq.

Signed-off-by: Ruslan Kuprieiev <kupruser at gmail.com>
---
 cr-service.c | 148 ++++++++++++++++++++++-------------------------------------
 1 file changed, 56 insertions(+), 92 deletions(-)

diff --git a/cr-service.c b/cr-service.c
index 74ea3c3..6c682c6 100644
--- a/cr-service.c
+++ b/cr-service.c
@@ -71,19 +71,7 @@ static int send_criu_msg(int socket_fd, CriuResp *msg)
 	return 0;
 }
 
-static void send_criu_err(int sk, char *msg)
-{
-	CriuResp resp = CRIU_RESP__INIT;
-
-	pr_perror("RPC error: %s", msg);
-
-	resp.type = CRIU_REQ_TYPE__EMPTY;
-	resp.success = false;
-	/* XXX -- add optional error code to CriuResp */
-
-	send_criu_msg(sk, &resp);
-}
-
+/* send_criu_dump_resp is used only in sk-unix.c to restore criu socket */
 int send_criu_dump_resp(int socket_fd, bool success, bool restored)
 {
 	CriuResp msg = CRIU_RESP__INIT;
@@ -99,20 +87,6 @@ int send_criu_dump_resp(int socket_fd, bool success, bool restored)
 	return send_criu_msg(socket_fd, &msg);
 }
 
-int send_criu_restore_resp(int socket_fd, bool success, int pid)
-{
-	CriuResp msg = CRIU_RESP__INIT;
-	CriuRestoreResp resp = CRIU_RESTORE_RESP__INIT;
-
-	msg.type = CRIU_REQ_TYPE__RESTORE;
-	msg.success = success;
-	msg.restore = &resp;
-
-	resp.pid = pid;
-
-	return send_criu_msg(socket_fd, &msg);
-}
-
 int send_criu_rpc_script(char *script, int fd)
 {
 	int ret;
@@ -241,7 +215,7 @@ static int setup_opts_from_req(int sk, CriuOpts *req)
 	return 0;
 }
 
-static int dump_using_req(int sk, CriuOpts *req)
+static int dump_using_req(int sk, CriuOpts *req, CriuResp *resp)
 {
 	bool success = false;
 	bool self_dump = !req->pid;
@@ -263,16 +237,18 @@ static int dump_using_req(int sk, CriuOpts *req)
 	success = true;
 exit:
 	if (req->leave_running  || !self_dump || !success) {
-		if (send_criu_dump_resp(sk, success, false) == -1) {
-			pr_perror("Can't send response");
-			success = false;
-		}
+		resp->dump = xmalloc(sizeof(CriuDumpResp));
+		criu_dump_resp__init(resp->dump);
+
+		resp->success			= success;
+		resp->dump->has_restored	= true;
+		resp->dump->restored		= false;
 	}
 
-	return success ? 0 : 1;
+	return success ? 0 : -1;
 }
 
-static int restore_using_req(int sk, CriuOpts *req)
+static int restore_using_req(int sk, CriuOpts *req, CriuResp *resp)
 {
 	bool success = false;
 
@@ -294,31 +270,27 @@ static int restore_using_req(int sk, CriuOpts *req)
 
 	success = true;
 exit:
-	if (send_criu_restore_resp(sk, success,
-				   root_item ? root_item->pid.real : -1) == -1) {
-		pr_perror("Can't send response");
-		success = false;
-	}
+	resp->restore = xmalloc(sizeof(CriuRestoreResp));
+	criu_restore_resp__init(resp->restore);
+
+	resp->success = success;
+	resp->restore->pid = root_item ? root_item->pid.real : -1;
 
-	return success ? 0 : 1;
+	return success ? 0 : -1;
 }
 
-static int check(int sk)
+static int check(int sk, CriuResp *resp)
 {
-	CriuResp resp = CRIU_RESP__INIT;
-
-	resp.type = CRIU_REQ_TYPE__CHECK;
-
 	/* Check only minimal kernel support */
 	opts.check_ms_kernel = true;
 
 	if (!cr_check())
-		resp.success = true;
+		resp->success = true;
 
-	return send_criu_msg(sk, &resp);
+	return 0;
 }
 
-static int pre_dump_using_req(int sk, CriuOpts *opts)
+static int pre_dump_using_req(int sk, CriuOpts *opts, CriuResp *resp)
 {
 	int pid, status;
 	bool success = false;
@@ -353,43 +325,20 @@ cout:
 
 	success = true;
 out:
-	if (send_criu_dump_resp(sk, success, false) == -1) {
-		pr_perror("Can't send pre-dump resp");
-		success = false;
-	}
+	resp->dump = xmalloc(sizeof(CriuDumpResp));
+	criu_dump_resp__init(resp->dump);
 
-	return success ? 0 : -1;
-}
-
-static int pre_dump_loop(int sk, CriuReq *msg)
-{
-	int ret;
-
-	do {
-		ret = pre_dump_using_req(sk, msg->opts);
-		if (ret < 0)
-			return ret;
-
-		criu_req__free_unpacked(msg, NULL);
-		if (recv_criu_msg(sk, &msg) == -1) {
-			pr_perror("Can't recv request");
-			return -1;
-		}
-	} while (msg->type == CRIU_REQ_TYPE__PRE_DUMP);
-
-	if (msg->type != CRIU_REQ_TYPE__DUMP) {
-		send_criu_err(sk, "Bad req seq");
-		return -1;
-	}
+	resp->success			= success;
+	resp->dump->has_restored	= true;
+	resp->dump->restored		= false;
 
-	return dump_using_req(sk, msg->opts);
+	return success ? 0 : -1;
 }
 
-static int start_page_server_req(int sk, CriuOpts *opts)
+static int start_page_server_req(int sk, CriuOpts *opts, CriuResp *resp)
 {
 	int ret;
 	bool success = false;
-	CriuResp resp = CRIU_RESP__INIT;
 	CriuPageServerInfo ps = CRIU_PAGE_SERVER_INFO__INIT;
 
 	if (!opts->ps) {
@@ -407,46 +356,61 @@ static int start_page_server_req(int sk, CriuOpts *opts)
 		success = true;
 		ps.has_pid = true;
 		ps.pid = ret;
-		resp.ps = &ps;
+		resp->ps = &ps;
 	}
 
 	pr_debug("Page server started\n");
 out:
-	resp.type = CRIU_REQ_TYPE__PAGE_SERVER;
-	resp.success = success;
-	return send_criu_msg(sk, &resp);
+	resp->success = success;
+	return success ? 0 : -1;
 }
 
 static int cr_service_work(int sk)
 {
+	int ret = -1;
+
 	CriuReq *msg = 0;
+	CriuResp resp = CRIU_RESP__INIT;
 
 	init_opts();
 
 	if (recv_criu_msg(sk, &msg) == -1) {
 		pr_perror("Can't recv request");
-		goto err;
+		return -1;
 	}
 
+	resp.success	= false;
+	resp.type	= msg->type;
+
 	switch (msg->type) {
 	case CRIU_REQ_TYPE__DUMP:
-		return dump_using_req(sk, msg->opts);
+		ret = dump_using_req(sk, msg->opts, &resp);
+		break;
 	case CRIU_REQ_TYPE__RESTORE:
-		return restore_using_req(sk, msg->opts);
+		ret = restore_using_req(sk, msg->opts, &resp);
+		break;
 	case CRIU_REQ_TYPE__CHECK:
-		return check(sk);
+		ret = check(sk, &resp);
+		break;
 	case CRIU_REQ_TYPE__PRE_DUMP:
-		return pre_dump_loop(sk, msg);
+		ret = pre_dump_using_req(sk, msg->opts, &resp);
+		break;
 	case CRIU_REQ_TYPE__PAGE_SERVER:
-		return start_page_server_req(sk, msg->opts);
+		ret = start_page_server_req(sk, msg->opts, &resp);
+		break;
 
 	default:
-		send_criu_err(sk, "Invalid req");
-		goto err;
+		/*FIXME add optional error codes */
+		pr_perror("Invalid req");
+		ret = -1;
 	}
 
-err:
-	return -1;
+	if (send_criu_msg(sk, &resp)) {
+		pr_perror("Can't send resp");
+		ret = -1;
+	}
+
+	return ret;
 }
 
 static void reap_worker(int signo)
-- 
1.8.3.2



More information about the CRIU mailing list