[CRIU] [PATCH v2 25/36] utils: Move getting real pid functionality to separate function
Kirill Tkhai
ktkhai at virtuozzo.com
Fri Feb 3 08:15:17 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 f175b417f..ab05ebfd1 100644
--- a/criu/cr-restore.c
+++ b/criu/cr-restore.c
@@ -1334,24 +1334,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 e16707a17..21ea3d98e 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 ca61a1c58..b171781aa 100644
--- a/criu/util.c
+++ b/criu/util.c
@@ -568,6 +568,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