[CRIU] [PATCH 3/8] lib: add check

Ruslan Kuprieiev kupruser at gmail.com
Wed Dec 4 02:57:26 PST 2013


Signed-off-by: Ruslan Kuprieiev <kupruser at gmail.com>
---
 lib/criu.c | 136 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 136 insertions(+)

diff --git a/lib/criu.c b/lib/criu.c
index 2bd224e..19a39b0 100644
--- a/lib/criu.c
+++ b/lib/criu.c
@@ -1,3 +1,139 @@
 #include "version.h"
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <limits.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+
+#include "criu.h"
+
+#define MAX_MSG_SIZE 1024
+#define CR_DEFAULT_SERVICE_ADDRESS "/var/run/criu_service.socket"
 
 const char *criu_lib_version = CRIU_VERSION;
+
+static char *service_address = CR_DEFAULT_SERVICE_ADDRESS;
+
+void criu_set_service_address(char *path)
+{
+	if (path)
+		service_address = path;
+	else
+		service_address = CR_DEFAULT_SERVICE_ADDRESS;
+}
+
+static CriuResp *recv_resp(int socket_fd)
+{
+	unsigned char buf[MAX_MSG_SIZE];
+	int len;
+	CriuResp *msg = 0;
+
+	len = read(socket_fd, buf, MAX_MSG_SIZE);
+	if (len == -1) {
+		perror("Can't read response");
+		return NULL;
+	}
+
+	msg = criu_resp__unpack(NULL, len, buf);
+	if (!msg) {
+		perror("Failed unpacking response");
+		return NULL;
+	}
+
+	return msg;
+}
+
+static int send_req(int socket_fd, CriuReq *req)
+{
+	unsigned char buf[MAX_MSG_SIZE];
+	int len;
+
+	len = criu_req__get_packed_size(req);
+
+	if (criu_req__pack(req, buf) != len) {
+		perror("Failed packing request");
+		return -1;
+	}
+
+	if (write(socket_fd, buf, len)  == -1) {
+		perror("Can't send request");
+		return -1;
+	}
+
+	return 0;
+}
+
+static int criu_connect(void)
+{
+	int fd, ret;
+	struct sockaddr_un addr;
+	socklen_t addr_len;
+
+	fd = socket(AF_LOCAL, SOCK_SEQPACKET, 0);
+	if (fd < 0) {
+		perror("Can't create socket");
+		return -1;
+	}
+
+	memset(&addr, 0, sizeof(addr));
+	addr.sun_family = AF_LOCAL;
+
+	strcpy(addr.sun_path, service_address);
+
+	addr_len = strlen(addr.sun_path) + sizeof(addr.sun_family);
+
+	ret = connect(fd, (struct sockaddr *) &addr, addr_len);
+	if (ret < 0) {
+		perror("Can't connect to socket");
+		close(fd);
+	}
+
+	return fd;
+}
+
+int criu_check(void)
+{
+	int fd;
+	int ret	= 0;
+	CriuReq req	= CRIU_REQ__INIT;
+	CriuResp *resp	= NULL;
+
+	fd = criu_connect();
+	if (fd < 0) {
+		perror("Can't connect to criu");
+		ret = CRIU_ECONNECT;
+		goto exit;
+	}
+
+	req.type	= CRIU_REQ_TYPE__CHECK;
+
+	if (send_req(fd, &req) < 0) {
+		ret = CRIU_EREQ;
+		goto exit;
+	}
+
+	resp = recv_resp(fd);
+	if (!resp) {
+		perror("Can't receive response");
+		ret = CRIU_ERESP;
+		goto exit;
+	}
+
+	if (resp->type != CRIU_REQ_TYPE__CHECK) {
+		perror("Unexpected response type");
+		ret = CRIU_ERESP;
+		goto exit;
+	}
+
+	if (!resp->success)
+		ret = CRIU_ECHECK;
+
+exit:
+	if (fd > 0)
+		close(fd);
+	if (resp)
+		free(resp);
+
+	return ret;
+}
-- 
1.8.1.2



More information about the CRIU mailing list