[CRIU] [PATCH]v2 crtools: write pidfile, when service/page server is run as daemon and "--pidfile" is set

Ruslan Kuprieiev kupruser at gmail.com
Sun Sep 15 16:58:49 EDT 2013


When service/page server becomes daemon, we may need to know it's pid.

Signed-off-by: Ruslan Kuprieiev<kupruser at gmail.com>

-------------- next part --------------
diff --git a/cr-restore.c b/cr-restore.c
index 12b3462..c35ec7f 100644
--- a/cr-restore.c
+++ b/cr-restore.c
@@ -885,21 +885,6 @@ struct cr_clone_arg {
 	CoreEntry *core;
 };
 
-static void write_pidfile(char *pfname, int pid)
-{
-	int fd;
-
-	fd = open(pfname, O_WRONLY | O_TRUNC | O_CREAT, 0600);
-	if (fd == -1) {
-		pr_perror("Can't open %s", pfname);
-		kill(pid, SIGKILL);
-		return;
-	}
-
-	dprintf(fd, "%d", pid);
-	close(fd);
-}
-
 static inline int fork_with_pid(struct pstree_item *item)
 {
 	int ret = -1, fd;
@@ -971,8 +956,11 @@ static inline int fork_with_pid(struct pstree_item *item)
 	if (ca.clone_flags & CLONE_NEWPID)
 		item->pid.real = ret;
 
-	if (opts.pidfile && root_item == item)
-		write_pidfile(opts.pidfile, ret);
+	if (opts.pidfile && root_item == item) {
+		ret = write_pidfile(opts.pidfile, ret);
+		if (ret < 0)
+			pr_perror("Can't write pidfile");
+	}
 
 err_unlock:
 	if (ca.fd >= 0) {
diff --git a/cr-service.c b/cr-service.c
index b1f4bdf..4bcf7f2 100644
--- a/cr-service.c
+++ b/cr-service.c
@@ -229,12 +229,19 @@ int cr_service(bool daemon_mode)
 	}
 
 	if (daemon_mode) {
-		if (daemon(0, 0) == -1) {
+		if (daemon(1, 1) == -1) {
 			pr_perror("Can't run service server in the background");
 			return -errno;
 		}
 	}
 
+	if (opts.pidfile) {
+		if (write_pidfile(opts.pidfile, getpid()) == -1) {
+			pr_perror("Can't write pidfile");
+			return -1;
+		}
+	}
+
 	/* FIXME Do not ignore children's return values */
 	signal(SIGCHLD, SIG_IGN);
 
diff --git a/include/log.h b/include/log.h
index a38f441..17f624b 100644
--- a/include/log.h
+++ b/include/log.h
@@ -19,6 +19,8 @@ extern int vprint_num(char *buf, int blen, int num, char **ps);
 extern void print_on_level(unsigned int loglevel, const char *format, ...)
 	__attribute__ ((__format__ (__printf__, 2, 3)));
 
+extern int write_pidfile(char *pfname, int pid);
+
 #ifndef LOG_PREFIX
 # define LOG_PREFIX
 #endif
diff --git a/log.c b/log.c
index feb2263..1e5fcad 100644
--- a/log.c
+++ b/log.c
@@ -191,3 +191,19 @@ void print_on_level(unsigned int loglevel, const char *format, ...)
 	__print_on_level(loglevel, format, params);
 	va_end(params);
 }
+
+int write_pidfile(char *pfname, int pid)
+{
+	int fd;
+
+	fd = open(pfname, O_WRONLY | O_TRUNC | O_CREAT, 0600);
+	if (fd == -1) {
+		pr_perror("Can't open %s", pfname);
+		kill(pid, SIGKILL);
+		return -1;
+	}
+
+	dprintf(fd, "%d", pid);
+	close(fd);
+	return 0;
+}
diff --git a/page-xfer.c b/page-xfer.c
index c110c08..087595f 100644
--- a/page-xfer.c
+++ b/page-xfer.c
@@ -273,6 +273,13 @@ int cr_page_server(bool daemon_mode)
 			return -errno;
 		}
 
+	if (opts.pidfile) {
+		if (write_pidfile(opts.pidfile, getpid()) == -1) {
+			pr_perror("Can't write pidfile");
+			return -1;
+		}
+	}
+
 	ask = accept(sk, (struct sockaddr *)&caddr, &clen);
 	if (ask < 0)
 		pr_perror("Can't accept connection to server");


More information about the CRIU mailing list