[CRIU] [PATCH 1/4] criu: Write pidfile later

Pavel Emelyanov xemul at virtuozzo.com
Wed Apr 27 05:23:00 PDT 2016


Next patch will put non-root task's PID into it, so this
one is preparatory. But, as a bonus, we remove the need
to unlink ths pid file in case of error :)

Risk -- scripts might want to have pidfile, but we already
have CRTOOLS_ROOT_PID environment in them for such cases.

Signed-off-by: Pavel Emelyanov <xemul at virtuozzo.com>
---
 criu/cr-restore.c | 36 ++++++++++++++++++++----------------
 1 file changed, 20 insertions(+), 16 deletions(-)

diff --git a/criu/cr-restore.c b/criu/cr-restore.c
index 6ee0850..1b0354c 100644
--- a/criu/cr-restore.c
+++ b/criu/cr-restore.c
@@ -1325,18 +1325,6 @@ static inline int fork_with_pid(struct pstree_item *item)
 				item->pid.real, item->pid.virt);
 	}
 
-	if (opts.pidfile && root_item == item) {
-		int pid;
-
-		pid = ret;
-
-		ret = write_pidfile(pid);
-		if (ret < 0) {
-			pr_perror("Can't write pidfile");
-			kill(pid, SIGKILL);
-		}
-	}
-
 err_unlock:
 	if (ca.fd >= 0) {
 		if (flock(ca.fd, LOCK_UN))
@@ -1997,6 +1985,23 @@ static void restore_origin_ns_hook(void)
 		pr_err("Restore original /proc/self/loginuid failed");
 }
 
+static int write_restored_pid(void)
+{
+	int pid;
+
+	if (!opts.pidfile)
+		return 0;
+
+	pid = root_item->pid.real;
+
+	if (write_pidfile(pid) < 0) {
+		pr_perror("Can't write pidfile");
+		return -1;
+	}
+
+	return 0;
+}
+
 static int restore_root_task(struct pstree_item *init)
 {
 	enum trace_flags flag = TRACE_ALL;
@@ -2157,6 +2162,9 @@ static int restore_root_task(struct pstree_item *init)
 		goto out_kill;
 	}
 
+	if (write_restored_pid())
+		goto out_kill;
+
 	/* Unlock network before disabling repair mode on sockets */
 	network_unlock();
 
@@ -2234,10 +2242,6 @@ out_kill:
 				kill(pi->pid.virt, SIGKILL);
 	}
 
-	if (opts.pidfile) {
-		if (unlink(opts.pidfile))
-			pr_perror("Unable to remove %s", opts.pidfile);
-	}
 out:
 	fini_cgroup();
 	if (clean_remaps)
-- 
2.5.0



More information about the CRIU mailing list