[v3,23/33] utils: Move getting real pid functionality to separate function

Submitted by Kirill Tkhai on Feb. 16, 2017, 12:09 p.m.

Details

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

Commit Message

Kirill Tkhai Feb. 16, 2017, 12:09 p.m.
This is refactoring

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

Patch hide | download patch | download mbox

diff --git a/criu/cr-restore.c b/criu/cr-restore.c
index 57926a6e0..de9086f7c 100644
--- a/criu/cr-restore.c
+++ b/criu/cr-restore.c
@@ -1341,24 +1341,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, current->pid->ns[0].virt);
+		if (current->pid->real < 0)
+			goto err;
 	}
 
 	if (current->pid->state != TASK_HELPER) {
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 6b98a08f2..6e36b2654 100644
--- a/criu/util.c
+++ b/criu/util.c
@@ -571,6 +571,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__;					\