[CRIU] [PATCH 1/3]v2 libcriu: criu_dump_me() and library header

Ruslan Kuprieiev kupruser at gmail.com
Thu Aug 29 14:23:41 EDT 2013


Hi!

Patch 1\2 contains criu_dump_me() for library and library header.
        -- criu_dump_me() gets an argument of  type struct criu_dump_args
from user program,
          processes it and sends request to criu through unix domain socket.
          Then, it gets flag from criu and returns it back to program.
        -- libcriu.h contains definitions of flags, struct criu_dump_args etc.
           So, it is to be used in user program.

Signed-off-by: Ruslan Kuprieiev <kupruser at gmail.com>

-------------- next part --------------
diff --git a/include/libcriu.h b/include/libcriu.h
new file mode 100644
index 0000000..3f4ccf7
--- /dev/null
+++ b/include/libcriu.h
@@ -0,0 +1,25 @@
+#ifndef __CRIU_LIB__
+#define __CRIU_LIB__
+
+#define CR_DEFAULT_SERVICE_ADDRESS "/tmp/criu_service.socket"
+
+/* dump flags for struct criu_dump_args */
+#define CRIU_DUMP_AND_CONTINUE 1
+
+/* return codes */
+#define CRIU_DUMP_FAIL -1
+#define CRIU_RESUME 1		/* when program is restored */
+#define CRIU_DUMP_SUCCESS 0	/* when CRIU_DUMP_AND_CONTINUE is set */
+
+struct criu_dump_args {
+	/* opened dir fd for images. must be set*/
+	int			images_dir_fd;
+	/* dump flags. see macros above. */
+	unsigned long		flags;
+	/* sock path. CR_DEFAULT_SERVICE_ADDRESS by def */
+	char			*service_addr;
+};
+
+int criu_dump_me(struct criu_dump_args *arg);
+
+#endif
diff --git a/lib/criu.c b/lib/criu.c
index be629d9..b73dc6e 100644
--- a/lib/criu.c
+++ b/lib/criu.c
@@ -1,3 +1,82 @@
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <stdint.h>
+#include <stdbool.h>
+
+#include "libcriu.h"
 #include "version.h"
+#include "util-pie.h"
 
 const char *criu_lib_version = version;
+
+static int criu_send_dump_arg(int socket_fd, struct criu_dump_args *arg)
+{
+	if (send_fd(socket_fd, NULL, 0, arg->images_dir_fd) == -1) {
+		perror("Can't send file descriptor");
+		return -1;
+	}
+
+	if (send(socket_fd, &(arg->flags), sizeof(arg->flags), 0) == -1) {
+		perror("Can't send flags");
+		return -1;
+	}
+
+	return 0;
+}
+
+int criu_dump_me(struct criu_dump_args *arg)
+{
+	int socket_fd;
+	struct sockaddr_un server_addr;
+	socklen_t server_addr_len;
+
+	socket_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
+	if (socket_fd == -1) {
+		perror("Can't create socket");
+		return CRIU_DUMP_FAIL;
+	}
+
+	memset(&server_addr, 0, sizeof(server_addr));
+	server_addr.sun_family = AF_LOCAL;
+
+	if (!arg->service_addr)
+		arg->service_addr = CR_DEFAULT_SERVICE_ADDRESS;
+
+	strcpy(server_addr.sun_path, arg->service_addr);
+
+	server_addr_len = strlen(server_addr.sun_path)
+			+ sizeof(server_addr.sun_family);
+
+	if (connect(socket_fd, (struct sockaddr *) &server_addr,
+						server_addr_len) != 0) {
+		perror("Can't connect to service socket");
+		return CRIU_DUMP_FAIL;
+	}
+
+	if (criu_send_dump_arg(socket_fd, arg) == -1) {
+		perror("Can't send dump arg");
+		close(socket_fd);
+		return CRIU_DUMP_FAIL;
+	}
+
+	int ret;
+	int c;
+
+	c = read(socket_fd, &ret, sizeof(ret));
+	if (c == -1)
+		goto err;
+
+	close(socket_fd);
+	return ret;
+
+err:
+	close(socket_fd);
+	return -1;
+}
+


More information about the CRIU mailing list