2
0
mirror of https://github.com/checkpoint-restore/criu synced 2025-08-22 01:51:51 +00:00

dump: improve error printing and readability of task_comm_info

This addresses Andrei comments from
https://github.com/checkpoint-restore/criu/pull/2064

- Add comment about '\n' fixing
- Replace ret with more self explainting is_read
- Print warings if we failed to print comm for some reason

Signed-off-by: Pavel Tikhomirov <ptikhomirov@virtuozzo.com>
This commit is contained in:
Pavel Tikhomirov 2023-02-27 12:14:21 +08:00 committed by Andrei Vagin
parent 11c71656bd
commit 1ae9bac548

View File

@ -26,7 +26,7 @@
char *task_comm_info(pid_t pid, char *comm, size_t size)
{
int ret = 0;
bool is_read = false;
if (!pr_quelled(LOG_INFO)) {
int saved_errno = errno;
@ -37,18 +37,21 @@ char *task_comm_info(pid_t pid, char *comm, size_t size)
fd = open(path, O_RDONLY);
if (fd >= 0) {
ssize_t n = read(fd, comm, size);
if (n > 0)
if (n > 0) {
is_read = true;
/* Replace '\n' printed by kernel with '\0' */
comm[n - 1] = '\0';
else
ret = -1;
} else {
pr_warn("Failed to read %s: %s\n", path, strerror(errno));
}
close(fd);
} else {
ret = -1;
pr_warn("Failed to open %s: %s\n", path, strerror(errno));
}
errno = saved_errno;
}
if (ret || (pr_quelled(LOG_INFO) && comm[0]))
if (!is_read)
comm[0] = '\0';
return comm;