[CRIU] [PATCH] page-xfer: write pidfile from a process which starts a service

Andrey Vagin avagin at openvz.org
Fri Oct 31 14:19:57 PDT 2014


When a process, which starts a service, exits, we know that we can
connect to the page server and we expect to find pidfile.

If we create pidfile from a page server process, we have a race window.

Signed-off-by: Andrey Vagin <avagin at openvz.org>
---
 page-xfer.c  | 19 ++++++++++++-------
 test/zdtm.sh |  4 ++--
 2 files changed, 14 insertions(+), 9 deletions(-)

diff --git a/page-xfer.c b/page-xfer.c
index b584eff..c3faf90 100644
--- a/page-xfer.c
+++ b/page-xfer.c
@@ -4,6 +4,8 @@
 #include <linux/falloc.h>
 #include <unistd.h>
 #include <fcntl.h>
+#include <sys/types.h>
+#include <sys/wait.h>
 
 #include "cr_options.h"
 #include "servicefd.h"
@@ -309,14 +311,17 @@ no_server:
 			pr_perror("Can't run in the background");
 			goto out;
 		}
-		if (ret > 0) /* parent task, daemon started */
-			return ret;
-	}
+		if (ret > 0) { /* parent task, daemon started */
+			if (opts.pidfile) {
+				if (write_pidfile(ret) == -1) {
+					pr_perror("Can't write pidfile");
+					kill(ret, SIGKILL);
+					waitpid(ret, NULL, 0);
+					return -1;
+				}
+			}
 
-	if (opts.pidfile) {
-		if (write_pidfile(getpid()) == -1) {
-			pr_perror("Can't write pidfile");
-			return -1;
+			return ret;
 		}
 	}
 
diff --git a/test/zdtm.sh b/test/zdtm.sh
index 60dba66..9a70805 100755
--- a/test/zdtm.sh
+++ b/test/zdtm.sh
@@ -644,8 +644,8 @@ EOF
 		[ -n "$DUMP_ONLY" ] && dump_only=1
 
 		if [ $PAGE_SERVER -eq 1 ]; then
-			$CRIU page-server -D $ddump -o page_server.log -v4 --port $PS_PORT $ps_args --daemon || return 1
-			ps_pid=`lsof -s TCP:LISTEN -i :$PS_PORT -t`
+			$CRIU page-server -D $ddump -o page_server.log -v4 --port $PS_PORT $ps_args --daemon --pidfile $ddump/page-server.pid || return 1
+			ps_pid=`cat $ddump/page-server.pid`
 			ps -p "$ps_pid" -o cmd h | grep -q page-server || {
 				echo "Unable to determing PID of page-server"
 				return 1
-- 
1.9.3



More information about the CRIU mailing list