[CRIU] [PATCH v2 06/36] restore: Implement set_next_pid() helper

Kirill Tkhai ktkhai at virtuozzo.com
Fri Feb 3 08:12:29 PST 2017


Refacoring: move next pid setting functionality to separate function.

Signed-off-by: Kirill Tkhai <ktkhai at virtuozzo.com>
---
 criu/cr-restore.c     |   38 ++++++++++++++++++++++++--------------
 criu/include/pstree.h |    1 +
 2 files changed, 25 insertions(+), 14 deletions(-)

diff --git a/criu/cr-restore.c b/criu/cr-restore.c
index 7ce0c6abc..1f1fb0787 100644
--- a/criu/cr-restore.c
+++ b/criu/cr-restore.c
@@ -277,6 +277,28 @@ static int root_prepare_shared(void)
 	return ret;
 }
 
+int set_next_pid(struct pid *pid)
+{
+	pid_t virt = pid->ns[0].virt;
+	char buf[32];
+	int len, fd;
+
+	fd = open_proc_rw(PROC_GEN, LAST_PID_PATH);
+	if (fd < 0)
+		return -1;
+
+	len = snprintf(buf, sizeof(buf), "%d", virt - 1);
+	len -= write(fd, buf, len);
+	close(fd);
+
+	if (len) {
+		pr_perror("%d: Write %s to %s", virt, buf, LAST_PID_PATH);
+		return -1;
+	}
+
+	return 0;
+}
+
 static rt_sigaction_t sigchld_act;
 /*
  * If parent's sigaction has blocked SIGKILL (which is non-sence),
@@ -1007,21 +1029,10 @@ static inline int fork_with_pid(struct pstree_item *item)
 	pr_info("Forking task with %d pid (flags 0x%lx)\n", pid, ca.clone_flags);
 
 	if (!(ca.clone_flags & CLONE_NEWPID)) {
-		char buf[32];
-		int len, fd;
-
-		fd = open_proc_rw(PROC_GEN, LAST_PID_PATH);
-		if (fd < 0)
-			goto err;
-
 		mutex_lock(&task_entries->last_pid_mutex);
 		locked = true;
-
-		len = snprintf(buf, sizeof(buf), "%d", pid - 1);
-		len -= write(fd, buf, len);
-		close(fd);
-		if (len) {
-			pr_perror("%d: Write %s to %s", pid, buf, LAST_PID_PATH);
+		if (set_next_pid(item->pid) < 0) {
+			pr_err("Can't set next pid\n");
 			goto err_unlock;
 		}
 	} else {
@@ -1059,7 +1070,6 @@ static inline int fork_with_pid(struct pstree_item *item)
 err_unlock:
 	if (locked)
 		mutex_unlock(&task_entries->last_pid_mutex);
-err:
 	if (ca.core)
 		core_entry__free_unpacked(ca.core, NULL);
 	return ret;
diff --git a/criu/include/pstree.h b/criu/include/pstree.h
index f441d5397..eeef6f87e 100644
--- a/criu/include/pstree.h
+++ b/criu/include/pstree.h
@@ -120,4 +120,5 @@ extern void pstree_free_cores(struct pstree_item *item);
 extern int collect_pstree_ids(void);
 
 extern int preorder_pstree_traversal(struct pstree_item *item, int (*f)(struct pstree_item *));
+extern int set_next_pid(struct pid *pid);
 #endif /* __CR_PSTREE_H__ */



More information about the CRIU mailing list