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

Andrey Vagin avagin at openvz.org
Thu May 16 05:14:48 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 | 20 ++++++++++++++++++--
 src/lib/util.c     |  5 +++++
 4 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/include/types.h b/include/types.h
index a4bce73..79e22ca 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 83ef20a..8c98289 100644
--- a/src/lib/hooks_ct.c
+++ b/src/lib/hooks_ct.c
@@ -86,6 +86,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);
 }
 
@@ -230,7 +234,8 @@ static int ct_env_create_real(struct arg_start *arg)
 	long stack_size;
 	char *child_stack;
 	int clone_flags;
-	int ret;
+	int ret, fd;
+	char pidpath[STR_SIZE];
 
 	stack_size = get_pagesize();
 	if (stack_size < 0)
@@ -251,15 +256,26 @@ static int ct_env_create_real(struct arg_start *arg)
 	clone_flags |= CLONE_NEWUTS|CLONE_NEWPID|CLONE_NEWIPC;
 	clone_flags |= CLONE_NEWNET|CLONE_NEWNS;
 
+	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);
 	if (ret  < 0) {
 		logger(-1, errno, "Unable to clone");
+		close(fd);
 		/* FIXME: remove ourselves from container first */
 		destroy_container(arg->veid);
 		return -1;
 	}
 
-	return 0;
+	dprintf(fd, "%d", ret);
+	close(fd);
+
+	return ret;
 }
 
 int ct_env_create(struct arg_start *arg)
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