From dab3cff7a17364cd87201e3f58080d523eaf44a6 Mon Sep 17 00:00:00 2001 From: Sebastien GODARD Date: Sat, 4 Apr 2020 10:45:48 +0200 Subject: [PATCH] Fix #228: pidstat's -l replaces "\0" with " ", displaying unnecesary data Fix issue #228. Also add use cases to simulation test environment. Signed-off-by: Sebastien GODARD --- pidstat.c | 9 ++++++--- tests/root1/proc/8407/cmdline | Bin 35 -> 99 bytes tests/root1/proc/8407/task/8407/cmdline | Bin 35 -> 99 bytes tests/root2/proc/7900/task/10334/cmdline | Bin 27 -> 14 bytes tests/root2/proc/8407/cmdline | Bin 35 -> 99 bytes tests/root2/proc/8407/task/8407/cmdline | Bin 35 -> 99 bytes tests/root3/proc/7900/task/10334/cmdline | Bin 27 -> 14 bytes 7 files changed, 6 insertions(+), 3 deletions(-) diff --git a/pidstat.c b/pidstat.c index 46b8771..0b4362e 100644 --- a/pidstat.c +++ b/pidstat.c @@ -650,7 +650,7 @@ int read_proc_pid_cmdline(pid_t pid, struct st_pid *plist, pid_t tgid) FILE *fp; char filename[128], line[MAX_CMDLINE_LEN]; size_t len; - int i; + int i, found = FALSE; if (tgid) { sprintf(filename, TASK_CMDLINE, tgid, pid); @@ -669,8 +669,11 @@ int read_proc_pid_cmdline(pid_t pid, struct st_pid *plist, pid_t tgid) fclose(fp); if (len) { - for (i = 0; i < len - 1; i++) { - if (line[i] == '\0') { + for (i = len - 2; i >= 0; i--) { + if (line[i]) { + found = TRUE; + } + else if (found) { line[i] = ' '; } } diff --git a/tests/root1/proc/8407/cmdline b/tests/root1/proc/8407/cmdline index 8d4f0e64e8b76ad18e3d37bcc9f2773ae0477391..349685443ad97ccf07765f9579579087034e85d4 100644 GIT binary patch literal 99 tcmdNdEiTf}$xKSENKMvH&&$tE)h$Ua%FWD6%+W1QEh13Upb diff --git a/tests/root1/proc/8407/task/8407/cmdline b/tests/root1/proc/8407/task/8407/cmdline index 8d4f0e64e8b76ad18e3d37bcc9f2773ae0477391..349685443ad97ccf07765f9579579087034e85d4 100644 GIT binary patch literal 99 tcmdNdEiTf}$xKSENKMvH&&$tE)h$Ua%FWD6%+W1QEh13Upb diff --git a/tests/root2/proc/7900/task/10334/cmdline b/tests/root2/proc/7900/task/10334/cmdline index f6669f2a99b1b2056fcbe8cc684bf27e254bb96a..334128be67ace9881b3ba105fa1791c64845a1b8 100644 GIT binary patch literal 14 VcmYdE&d*C@D913Upb diff --git a/tests/root2/proc/8407/task/8407/cmdline b/tests/root2/proc/8407/task/8407/cmdline index 8d4f0e64e8b76ad18e3d37bcc9f2773ae0477391..349685443ad97ccf07765f9579579087034e85d4 100644 GIT binary patch literal 99 tcmdNdEiTf}$xKSENKMvH&&$tE)h$Ua%FWD6%+W1QEh13Upb diff --git a/tests/root3/proc/7900/task/10334/cmdline b/tests/root3/proc/7900/task/10334/cmdline index f6669f2a99b1b2056fcbe8cc684bf27e254bb96a..334128be67ace9881b3ba105fa1791c64845a1b8 100644 GIT binary patch literal 14 VcmYdE&d*C@D9