[CRIU] [PATCH 4/6] Not sure if it is okay, but this patch breaks backward compatibility, as we now discourage people from using criu as a system service by default. But it is better to be done rather sooner than later, considering criu service is not widely used.

Ruslan Kuprieiev rkuprieiev at cloudlinux.com
Mon Oct 12 02:34:13 PDT 2015


On a patch side, we just need to daemonize swrk if self-dump is requested.

Signed-off-by: Ruslan Kuprieiev <rkuprieiev at cloudlinux.com>
---
 lib/criu.c | 42 ++++++++++++++++++++++++++++++++++--------
 lib/criu.h |  2 +-
 2 files changed, 35 insertions(+), 9 deletions(-)

diff --git a/lib/criu.c b/lib/criu.c
index 1f8c917..9c00a0f 100644
--- a/lib/criu.c
+++ b/lib/criu.c
@@ -114,8 +114,8 @@ int criu_local_init_opts(criu_opts **o)
 	opts->rpc	= rpc;
 	opts->notify	= NULL;
 
-	opts->service_comm	= CRIU_COMM_SK;
-	opts->service_address	= CR_DEFAULT_SERVICE_ADDRESS;
+	opts->service_comm	= CRIU_COMM_BIN;
+	opts->service_address	= CR_DEFAULT_SERVICE_BIN;
 
 	*o = opts;
 
@@ -808,7 +808,7 @@ static void swrk_wait(criu_opts *opts)
 		waitpid(opts->swrk_pid, NULL, 0);
 }
 
-static int swrk_connect(criu_opts *opts)
+static int swrk_connect(criu_opts *opts, bool d)
 {
 	int sks[2], pid, ret = -1;
 
@@ -844,23 +844,45 @@ static int swrk_connect(criu_opts *opts)
 		close(sks[0]);
 		sprintf(fds, "%d", sks[1]);
 
+		if (d)
+			if (daemon(0, 1)) {
+				perror("Can't detach for a self-dump");
+				goto child_err;
+			}
+
+		pid = getpid();
+		if (write(sks[1], &pid, sizeof(pid)) != sizeof(pid)) {
+			perror("Can't write swrk pid");
+			goto child_err;
+		}
+
 		execlp(opts->service_binary, opts->service_binary, "swrk", fds, NULL);
 		perror("Can't exec criu swrk");
+child_err:
+		close(sks[1]);
 		exit(1);
 	}
 
 	close(sks[1]);
+
+	if (read(sks[0], &pid, sizeof(pid)) != sizeof(pid)) {
+		perror("Can't read swrk pid");
+		goto err;
+	}
+
 	opts->swrk_pid = pid;
 	ret = sks[0];
+
 out:
 	return ret;
+
 err:
 	close(sks[0]);
 	close(sks[1]);
 	goto out;
 }
 
-static int criu_connect(criu_opts *opts)
+static int criu_connect(criu_opts *opts, bool d)
 {
 	int fd, ret;
 	struct sockaddr_un addr;
@@ -869,7 +891,7 @@ static int criu_connect(criu_opts *opts)
 	if (opts->service_comm == CRIU_COMM_FD)
 		return opts->service_fd;
 	else if (opts->service_comm == CRIU_COMM_BIN)
-		return swrk_connect(opts);
+		return swrk_connect(opts, d);
 
 	fd = socket(AF_LOCAL, SOCK_SEQPACKET, 0);
 	if (fd < 0) {
@@ -945,8 +967,12 @@ static int send_req_and_recv_resp(criu_opts *opts, CriuReq *req, CriuResp **resp
 {
 	int fd;
 	int ret	= 0;
+	bool d = false;
+
+	if (req->type == CRIU_REQ_TYPE__DUMP && req->opts->has_pid == false)
+		d = true;
 
-	fd = criu_connect(opts);
+	fd = criu_connect(opts, d);
 	if (fd < 0) {
 		perror("Can't connect to criu");
 		ret = -ECONNREFUSED;
@@ -1052,7 +1078,7 @@ int criu_local_dump_iters(criu_opts *opts, int (*more)(criu_predump_info pi))
 		goto exit;
 
 	ret = -ECONNREFUSED;
-	fd = criu_connect(opts);
+	fd = criu_connect(opts, false);
 	if (fd < 0)
 		goto exit;
 
@@ -1162,7 +1188,7 @@ int criu_local_restore_child(criu_opts *opts)
 		opts->service_binary = CR_DEFAULT_SERVICE_BIN;
 	}
 
-	sk = swrk_connect(opts);
+	sk = swrk_connect(opts, false);
 	if (save_comm) {
 		/* Restore comm */
 		opts->service_comm = saved_comm;
diff --git a/lib/criu.h b/lib/criu.h
index c2ad84d..0898be0 100644
--- a/lib/criu.h
+++ b/lib/criu.h
@@ -47,7 +47,7 @@ void criu_set_service_binary(char *path);
 
 /*
  * You can choose if you want libcriu to connect to service socket
- * by itself or just use provided file descriptor
+ * by itself, use provided file descriptor or spawn swrk by itself
  */
 void criu_set_service_comm(enum criu_service_comm);
 
-- 
2.4.3



More information about the CRIU mailing list