Skip to content

Commit

Permalink
dump: improve error printing and readability of task_comm_info
Browse files Browse the repository at this point in the history
This addresses Andrei comments from
checkpoint-restore#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 <[email protected]>
  • Loading branch information
Snorch authored and avagin committed Apr 16, 2023
1 parent 11c7165 commit 1ae9bac
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions criu/seize.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 1ae9bac

Please sign in to comment.