[Devel] [PATCH 2/6] vzctl: save PID of init in a state file

Andrey Vagin avagin at openvz.org
Tue May 21 09:13:29 PDT 2013


CRIU requires a pid of the init.

Signed-off-by: Andrey Vagin <avagin at openvz.org>
---
 include/types.h    |  2 ++
 include/util.h     |  1 +
 src/lib/hooks_ct.c | 18 +++++++++++++++++-
 src/lib/util.c     |  5 +++++
 4 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/include/types.h b/include/types.h
index fa511aa..583dc36 100644
--- a/include/types.h
+++ b/include/types.h
@@ -61,6 +61,8 @@ typedef unsigned envid_t;
 #define DEF_DUMPDIR	VZDIR "/dump"
 #define DEF_DUMPFILE	"Dump.%d"
 
+#define DEF_STATEDIR	VZDIR "/state"
+
 /* CT states */
 enum {
 	STATE_STARTING = 1,
diff --git a/include/util.h b/include/util.h
index 0f87e6e..4136245 100644
--- a/include/util.h
+++ b/include/util.h
@@ -57,6 +57,7 @@ int get_num_cpu(void);
 int get_lowmem(unsigned long long *mem);
 unsigned long max_ul(unsigned long val1, unsigned long val2);
 int get_dump_file(unsigned veid, const char *dumpdir, char *buf, int size);
+int get_state_file(unsigned veid, char *buf, int size);
 int set_not_blk(int fd);
 void close_fds(int close_std, ...);
 int move_config(int veid, int action);
diff --git a/src/lib/hooks_ct.c b/src/lib/hooks_ct.c
index 837ec18..299dede 100644
--- a/src/lib/hooks_ct.c
+++ b/src/lib/hooks_ct.c
@@ -89,6 +89,10 @@ static int ct_destroy(vps_handler *h, envid_t veid)
 
 	snprintf(ctpath, STR_SIZE, "%s/%d", NETNS_RUN_DIR, veid);
 	unlink(ctpath);
+
+	get_state_file(veid, ctpath, sizeof(ctpath));
+	unlink(ctpath);
+
 	return destroy_container(veid);
 }
 
@@ -383,7 +387,8 @@ static int ct_env_create_real(struct arg_start *arg)
 	char *child_stack;
 	int clone_flags;
 	int userns_p[2];
-	int ret, err;
+	int ret, err, fd;
+	char pidpath[STR_SIZE];
 
 	stack_size = get_pagesize();
 	if (stack_size < 0)
@@ -416,16 +421,27 @@ static int ct_env_create_real(struct arg_start *arg)
 	}
 	arg->userns_p = userns_p[0];
 
+	get_state_file(arg->veid, pidpath, sizeof(pidpath));
+	fd = open(pidpath, O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, 0600);
+	if (fd == -1) {
+		logger(-1, errno, "Unable to create a state file %s", pidpath);
+		return VZ_RESOURCE_ERROR;
+	}
+
 	ret = clone(_env_create, child_stack, clone_flags, arg);
 	close(userns_p[0]);
 	if (ret < 0) {
 		logger(-1, errno, "Unable to clone");
+		close(fd);
 		/* FIXME: remove ourselves from container first */
 		close(userns_p[1]);
 		destroy_container(arg->veid);
 		return -VZ_RESOURCE_ERROR;
 	}
 
+	dprintf(fd, "%d", ret);
+	close(fd);
+
 	if (arg->h->can_join_userns) {
 		/*
 		 * Now we need to write to the mapping file. It has to be us,
diff --git a/src/lib/util.c b/src/lib/util.c
index df410ff..f58f7e4 100644
--- a/src/lib/util.c
+++ b/src/lib/util.c
@@ -589,6 +589,11 @@ int get_dump_file(unsigned veid, const char *dumpdir, char *buf, int size)
 			dumpdir != NULL ? dumpdir : DEF_DUMPDIR, veid);
 }
 
+int get_state_file(unsigned veid, char *buf, int size)
+{
+	return snprintf(buf, size, DEF_STATEDIR "/%d.pid", veid);
+}
+
 int set_not_blk(int fd)
 {
 	int oldfl, ret;
-- 
1.8.2




More information about the Devel mailing list