[CRIU] [PATCH 3/5] service: add support for restore request

Ruslan Kuprieiev kupruser at gmail.com
Mon Sep 30 04:06:31 PDT 2013


Signed-off-by: Ruslan Kuprieiev <kupruser at gmail.com>
-------------- next part --------------
---
diff --git a/cr-service.c b/cr-service.c
index 693be54..33bfec6 100644
--- a/cr-service.c
+++ b/cr-service.c
@@ -17,6 +17,7 @@
 #include "util-pie.h"
 #include "log.h"
 #include "cr-service.h"
+#include "pstree.h"
 
 struct _cr_service_client *cr_service_client;
 
@@ -75,7 +76,21 @@ int send_criu_dump_resp(int socket_fd, bool success, bool restored)
 	return send_criu_msg(socket_fd, &msg);
 }
 
-static int setup_dump_from_req(CriuDumpReq *req)
+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);
+}
+
+static int setup_from_req(CriuOpts *req)
 {
 	struct ucred ids;
 	struct stat st;
@@ -98,7 +113,7 @@ static int setup_dump_from_req(CriuDumpReq *req)
 
 	cr_service_client->sk_ino = st.st_ino;
 
-	/* going to dir, where to place images*/
+	/* going to dir, where to place/get images*/
 	sprintf(images_dir_path, "/proc/%d/fd/%d",
 		cr_service_client->pid, req->images_dir_fd);
 
@@ -113,21 +128,26 @@ static int setup_dump_from_req(CriuDumpReq *req)
 	log_closedir();
 
 	/* initiate log file in imgs dir */
-	opts.output = "./dump.log";
-
-	log_set_loglevel(req->log_level);
+	opts.output = req->log_file;
 	if (log_init(opts.output) == -1) {
 		pr_perror("Can't initiate log.");
 		return -1;
 	}
 
-	/* checking dump flags from client */
+	log_set_loglevel(req->log_level);
+
 	if (req->has_leave_running && req->leave_running)
 		opts.final_state = TASK_ALIVE;
 
 	if (!req->has_pid) {
+		/*
+		 * If request type is dump and no pid is set,
+		 * criu will dump a client.
+		 * But if type is smth else,
+		 * this won't do any harm(at least for now).
+		 */
 		req->has_pid = true;
-		req->pid = ids.pid;
+		req->pid = cr_service_client->pid;
 	}
 
 	if (req->has_ext_unix_sk)
@@ -148,19 +168,20 @@ static int setup_dump_from_req(CriuDumpReq *req)
 	return 0;
 }
 
-static int dump_using_req(CriuDumpReq *req)
+static int dump_using_req(CriuOpts *req)
 {
 	bool success = false;
 
-	if (setup_dump_from_req(req) == -1) {
+	if (!req->log_file)
+		req->log_file = DEFAULT_DUMP_LOG_FILENAME;
+
+	if (setup_from_req(req) == -1) {
 		pr_perror("Arguments treating fail");
 		goto exit;
 	}
 
-	if (cr_dump_tasks(req->pid) == -1) {
-		pr_perror("Dump fail");
+	if (cr_dump_tasks(req->pid))
 		goto exit;
-	}
 
 	if (req->has_leave_running && req->leave_running) {
 		success = true;
@@ -176,25 +197,53 @@ exit:
 	return success ? 0 : 1;
 }
 
+static int restore_using_req(CriuOpts *req)
+{
+	bool success = false;
+
+	if (!req->log_file)
+		req->log_file = DEFAULT_RESTORE_LOG_FILENAME;
+
+	opts.restore_detach = true;
+
+	if (setup_from_req(req) == -1) {
+		pr_perror("Arguments treating fail");
+		goto exit;
+	}
+
+	if (cr_restore_tasks())
+		goto exit;
+
+	success = true;
+exit:
+	if (send_criu_restore_resp(cr_service_client->sk_fd,
+				success, root_item->pid.real) == -1) {
+		pr_perror("Can't send response");
+		success = false;
+	}
+
+	close(cr_service_client->sk_fd);
+	return success ? 0 : 1;
+}
+
 static int cr_service_work(void)
 {
 	CriuReq *msg = 0;
 
-	if (recv_criu_msg(cr_service_client->sk_fd,
-					&msg) == -1) {
+	if (recv_criu_msg(cr_service_client->sk_fd, &msg) == -1) {
 		pr_perror("Can't recv request");
 		goto err;
 	}
 
 	switch (msg->type) {
 	case CRIU_REQ_TYPE__DUMP:
-		return dump_using_req(msg->dump);
-
+		return dump_using_req(msg->opts);
+	case CRIU_REQ_TYPE__RESTORE:
+		return restore_using_req(msg->opts);
 	default:
-		pr_perror("Invalid request");
+		pr_perror("Empty request");
 		goto err;
 	}
-
 err:
 	/*
 	 * FIXME -- add generic error report


More information about the CRIU mailing list