[CRIU] [PATCH v4 21/31] utils: Move getting real pid functionality to separate function

Kirill Tkhai ktkhai at virtuozzo.com
Wed Feb 22 03:33:36 PST 2017


This is refactoring

Signed-off-by: Kirill Tkhai <ktkhai at virtuozzo.com>
---
 criu/cr-restore.c   |   19 +++----------------
 criu/include/util.h |    1 +
 criu/util.c         |   18 ++++++++++++++++++
 3 files changed, 22 insertions(+), 16 deletions(-)

diff --git a/criu/cr-restore.c b/criu/cr-restore.c
index d0f567fba..f451effea 100644
--- a/criu/cr-restore.c
+++ b/criu/cr-restore.c
@@ -1337,24 +1337,11 @@ static int restore_task_with_children(void *_arg)
 	current = ca->item;
 
 	if (current != root_item) {
-		char buf[12];
-		int fd;
-
-		/* Determine PID in CRIU's namespace */
-		fd = get_service_fd(CR_PROC_FD_OFF);
-		if (fd < 0)
-			goto err;
-
-		ret = readlinkat(fd, "self", buf, sizeof(buf) - 1);
-		if (ret < 0) {
-			pr_perror("Unable to read the /proc/self link");
-			goto err;
-		}
-		buf[ret] = '\0';
-
-		current->pid->real = atoi(buf);
+		current->pid->real = get_self_real_pid();
 		pr_debug("PID: real %d virt %d\n",
 				current->pid->real, vpid(current));
+		if (current->pid->real < 0)
+			goto err;
 	}
 
 	if ( !(ca->clone_flags & CLONE_FILES))
diff --git a/criu/include/util.h b/criu/include/util.h
index 20b028e4e..d9f50f6e0 100644
--- a/criu/include/util.h
+++ b/criu/include/util.h
@@ -189,6 +189,7 @@ extern int is_empty_dir(int dirfd);
 #define PSFDS	(sizeof("/proc/self/fd/2147483647"))
 
 extern int read_fd_link(int lfd, char *buf, size_t size);
+extern pid_t get_self_real_pid(void);
 
 #define USEC_PER_SEC	1000000L
 #define NSEC_PER_SEC    1000000000L
diff --git a/criu/util.c b/criu/util.c
index aba17c458..93a1f7257 100644
--- a/criu/util.c
+++ b/criu/util.c
@@ -587,6 +587,24 @@ int is_anon_link_type(char *link, char *type)
 	return !strcmp(link, aux);
 }
 
+pid_t get_self_real_pid(void)
+{
+	char buf[12];
+	int fd, ret;
+
+	fd = get_service_fd(CR_PROC_FD_OFF);
+	if (fd < 0)
+		return -1;
+
+	ret = readlinkat(fd, "self", buf, sizeof(buf) - 1);
+	if (ret < 0) {
+		pr_perror("Unable to read the /proc/self link");
+		return -1;
+	}
+	buf[ret] = '\0';
+	return atoi(buf);
+}
+
 #define DUP_SAFE(fd, out)						\
 	({							\
 		int ret__;					\



More information about the CRIU mailing list