[v2,40/57] pid: Always lock last pid file on clone()

Submitted by Kirill Tkhai on March 28, 2017, 3:40 p.m.

Details

Message ID 149071560791.12770.14909064356288292475.stgit@localhost.localdomain
State New
Series "Nested pid namespaces support"
Headers show

Commit Message

Kirill Tkhai March 28, 2017, 3:40 p.m.
We will need this, when pid has more than one level.
Even if we create task with CLONE_NEWPID, we will
need to populate parent pid levels, i.e., to have
this file, which we use for synchronization, locked.

Signed-off-by: Kirill Tkhai <ktkhai@virtuozzo.com>
---
 criu/cr-restore.c |   37 +++++++++++++++----------------------
 1 file changed, 15 insertions(+), 22 deletions(-)

Patch hide | download patch | download mbox

diff --git a/criu/cr-restore.c b/criu/cr-restore.c
index 740951de..612fd1d4 100644
--- a/criu/cr-restore.c
+++ b/criu/cr-restore.c
@@ -1049,27 +1049,22 @@  static inline int fork_with_pid(struct pstree_item *item)
 	ca.clone_flags = rsti(item)->clone_flags;
 
 	BUG_ON(ca.clone_flags & CLONE_VM);
+	BUG_ON(!!(ca.clone_flags & CLONE_NEWPID) != (last_level_pid(item->pid) == INIT_PID));
 
 	pr_info("Forking task with %d pid (flags 0x%lx)\n", pid, ca.clone_flags);
 
-	if (!(ca.clone_flags & CLONE_NEWPID)) {
-		ca.fd = open_proc_rw(PROC_GEN, LAST_PID_PATH);
-		if (ca.fd < 0)
-			goto err;
+	ca.fd = open_proc_rw(PROC_GEN, LAST_PID_PATH);
+	if (ca.fd < 0)
+		goto err;
 
-		if (flock(ca.fd, LOCK_EX)) {
-			close(ca.fd);
-			pr_perror("%d: Can't lock %s", pid, LAST_PID_PATH);
-			goto err;
-		}
+	if (flock(ca.fd, LOCK_EX)) {
+		pr_perror("%d: Can't lock %s", pid, LAST_PID_PATH);
+		goto err_close;
+	}
 
-		if (set_next_pid(pid_ns, item->pid) < 0) {
-			pr_err("Can't set next pid\n");
-			goto err_unlock;
-		}
-	} else {
-		ca.fd = -1;
-		BUG_ON(pid != INIT_PID);
+	if (set_next_pid(pid_ns, item->pid) < 0) {
+		pr_err("Can't set next pid\n");
+		goto err_unlock;
 	}
 
 	if (ca.clone_flags & CLONE_FILES)
@@ -1089,12 +1084,10 @@  static inline int fork_with_pid(struct pstree_item *item)
 	}
 
 err_unlock:
-	if (ca.fd >= 0) {
-		if (flock(ca.fd, LOCK_UN))
-			pr_perror("%d: Can't unlock %s", pid, LAST_PID_PATH);
-
-		close(ca.fd);
-	}
+	if (flock(ca.fd, LOCK_UN))
+		pr_perror("%d: Can't unlock %s", pid, LAST_PID_PATH);
+err_close:
+	close(ca.fd);
 err:
 	if (ca.core)
 		core_entry__free_unpacked(ca.core, NULL);