[CRIU] [PATCH 11/11] dump: Move task_comm_info helpers into seize.c
Cyrill Gorcunov
gorcunov at openvz.org
Tue Aug 2 08:34:37 PDT 2016
From: Cyrill Gorcunov <gorcunov at virtuozzo.com>
We will print task comm for seizing problem as well.
Signed-off-by: Cyrill Gorcunov <gorcunov at virtuozzo.com>
---
criu/cr-dump.c | 41 +++--------------------------------------
criu/include/ptrace.h | 3 +++
criu/ptrace.c | 45 +++++++++++++++++++++++++++++++++++++++++++--
criu/seize.c | 9 ++++++---
4 files changed, 55 insertions(+), 43 deletions(-)
diff --git a/criu/cr-dump.c b/criu/cr-dump.c
index be47f1874d34..bd5efd51039e 100644
--- a/criu/cr-dump.c
+++ b/criu/cr-dump.c
@@ -1113,41 +1113,6 @@ err:
return ret;
}
-/*
- * NOTE: Don't run simultaneously, it uses local static buffer!
- * The only purpose of the helper is to print out task command
- * for debug purpose when error happens early before task
- * statistics gathered.
- */
-static char *task_comm_info(pid_t pid)
-{
- static char comm[PROC_TASK_COMM_LEN];
- int ret = 0;
-
- if (!pr_quelled(LOG_INFO)) {
- char path[64];
- int fd;
-
- snprintf(path, sizeof(path), "/proc/%d/comm", pid);
- fd = open(path, O_RDONLY);
- if (fd >= 0) {
- ssize_t n = read(fd, comm, sizeof(comm));
- if (n > 0)
- comm[n-1] = '\0';
- else
- ret = -1;
- close(fd);
- } else {
- pr_perror("Can't open %s\n", path);
- ret = -1;
- }
- }
-
- if (ret && comm[0])
- comm[0] = '\0';
- return comm;
-}
-
static int pre_dump_one_task(struct pstree_item *item)
{
pid_t pid = item->pid.real;
@@ -1160,7 +1125,7 @@ static int pre_dump_one_task(struct pstree_item *item)
vmas.nr = 0;
pr_info("========================================\n");
- pr_info("Pre-dumping task (pid: %d comm: %s)\n", pid, task_comm_info(pid));
+ pr_info("Pre-dumping task (pid: %d comm: %s)\n", pid, __task_comm_info(pid));
pr_info("========================================\n");
if (item->pid.state == TASK_STOPPED) {
@@ -1236,7 +1201,7 @@ static int dump_one_task(struct pstree_item *item)
vmas.nr = 0;
pr_info("========================================\n");
- pr_info("Dumping task (pid: %d comm: %s)\n", pid, task_comm_info(pid));
+ pr_info("Dumping task (pid: %d comm: %s)\n", pid, __task_comm_info(pid));
pr_info("========================================\n");
if (item->pid.state == TASK_DEAD)
@@ -1712,7 +1677,7 @@ int cr_dump_tasks(pid_t pid)
int ret = -1;
pr_info("========================================\n");
- pr_info("Dumping processes (pid: %d comm: %s)\n", pid, task_comm_info(pid));
+ pr_info("Dumping processes (pid: %d comm: %s)\n", pid, __task_comm_info(pid));
pr_info("========================================\n");
root_item = alloc_pstree_item();
diff --git a/criu/include/ptrace.h b/criu/include/ptrace.h
index bf416b3ad00c..09585a1e3d3b 100644
--- a/criu/include/ptrace.h
+++ b/criu/include/ptrace.h
@@ -83,4 +83,7 @@ extern int ptrace_peek_area(pid_t pid, void *dst, void *addr, long bytes);
extern int ptrace_poke_area(pid_t pid, void *src, void *addr, long bytes);
extern int ptrace_swap_area(pid_t pid, void *dst, void *src, long bytes);
+extern char *task_comm_info(pid_t pid, char *comm, size_t size);
+extern char *__task_comm_info(pid_t pid);
+
#endif /* __CR_PTRACE_H__ */
diff --git a/criu/ptrace.c b/criu/ptrace.c
index 45271c60e1c6..2506daf48449 100644
--- a/criu/ptrace.c
+++ b/criu/ptrace.c
@@ -7,6 +7,7 @@
#include <stdbool.h>
#include <limits.h>
#include <signal.h>
+#include <fcntl.h>
#include <sys/ptrace.h>
#include <sys/types.h>
@@ -23,6 +24,44 @@
#include "seccomp.h"
#include "cr_options.h"
+char *task_comm_info(pid_t pid, char *comm, size_t size)
+{
+ int ret = 0;
+
+ if (!pr_quelled(LOG_INFO)) {
+ int saved_errno = errno;
+ char path[64];
+ int fd;
+
+ snprintf(path, sizeof(path), "/proc/%d/comm", pid);
+ fd = open(path, O_RDONLY);
+ if (fd >= 0) {
+ ssize_t n = read(fd, comm, size);
+ if (n > 0)
+ comm[n-1] = '\0';
+ else
+ ret = -1;
+ close(fd);
+ } else
+ ret = -1;
+ errno = saved_errno;
+ }
+
+ if (ret || (pr_quelled(LOG_INFO) && comm[0]))
+ comm[0] = '\0';
+
+ return comm;
+}
+
+/*
+ * NOTE: Don't run simultaneously, it uses local static buffer!
+ */
+char *__task_comm_info(pid_t pid)
+{
+ static char comm[32];
+ return task_comm_info(pid, comm, sizeof(comm));
+}
+
int unseize_task(pid_t pid, int orig_st, int st)
{
pr_debug("\tUnseizing %d into %d\n", pid, st);
@@ -79,7 +118,8 @@ int seize_catch_task(pid_t pid)
* attaching to zombie from other errors.
* All errors will be handled in seize_wait_task().
*/
- pr_warn("Unable to interrupt task: %d (%s)\n", pid, strerror(errno));
+ pr_warn("Unable to interrupt task: %d (comm %s) (%s)\n",
+ pid, __task_comm_info(pid), strerror(errno));
return ret;
}
@@ -94,7 +134,8 @@ int seize_catch_task(pid_t pid)
*/
ret = ptrace(PTRACE_INTERRUPT, pid, NULL, NULL);
if (ret < 0) {
- pr_warn("SEIZE %d: can't interrupt task: %s", pid, strerror(errno));
+ pr_warn("SEIZE %d (comm %s): can't interrupt task: %s",
+ pid, __task_comm_info(pid), strerror(errno));
if (ptrace(PTRACE_DETACH, pid, NULL, NULL))
pr_perror("Unable to detach from %d", pid);
}
diff --git a/criu/seize.c b/criu/seize.c
index e31d2f88b2bc..050ac84e8991 100644
--- a/criu/seize.c
+++ b/criu/seize.c
@@ -117,13 +117,15 @@ static int seize_cgroup_tree(char *root_path, const char *state)
if (ret == 0)
continue;
if (errno != ESRCH) {
- pr_perror("Unexpected error");
+ pr_perror("Unexpected error for pid %d (comm %s)",
+ pid, __task_comm_info(pid));
fclose(f);
return -1;
}
if (!seize_catch_task(pid)) {
- pr_debug("SEIZE %d: success\n", pid);
+ pr_debug("SEIZE %d (comm %s): success\n",
+ pid, __task_comm_info(pid));
processes_to_wait++;
} else if (state == frozen) {
char buf[] = "/proc/XXXXXXXXXX/exe";
@@ -135,7 +137,8 @@ static int seize_cgroup_tree(char *root_path, const char *state)
continue;
/* fails when meets a zombie */
- pr_err("zombie found while seizing\n");
+ pr_err("zombie %d (comm %s) found while seizing\n",
+ pid, __task_comm_info(pid));
fclose(f);
return -1;
}
--
2.7.4
More information about the CRIU
mailing list