Blame SOURCES/0001-pidstat-Now-handle-processes-with-spaces-in-their-na.patch

b87a4b
From c16eb6cf753d5df0779c284126d7356d2377fdb3 Mon Sep 17 00:00:00 2001
b87a4b
From: Sebastien GODARD <sysstat@users.noreply.github.com>
b87a4b
Date: Mon, 22 Oct 2018 18:06:48 +0200
b87a4b
Subject: [PATCH] pidstat: Now handle processes with spaces in their name
b87a4b
 properly
b87a4b
b87a4b
pidstat had a bug which made it unable to handle processes with spaces
b87a4b
in their name, eg:
b87a4b
b87a4b
$ cat /proc/5768/stat
b87a4b
5768 (Plex New Transc) S 1264 1252 1252 0 -1 1077960704 17317 0 18 0
b87a4b
9260 137 0 0 15 -5 8 0 1430749 186589184 16704 18446744073709551615 1 1
b87a4b
0 0 0 0 0 0 8404998 18446744073709551615 0 0 17 1 0 0 22 0 0 0 0 0 0 0 0
b87a4b
0 0
b87a4b
b87a4b
This patch fixes the problem.
b87a4b
b87a4b
Reported by Chris Grindstaff (@cgrinds).
b87a4b
b87a4b
Signed-off-by: Sebastien GODARD <sysstat@users.noreply.github.com>
b87a4b
---
b87a4b
 pidstat.c | 59 +++++++++++++++++++++++++++++++++++------------------------
b87a4b
 1 file changed, 35 insertions(+), 24 deletions(-)
b87a4b
b87a4b
diff --git a/pidstat.c b/pidstat.c
b87a4b
index bc22aa8..af48f29 100644
b87a4b
--- a/pidstat.c
b87a4b
+++ b/pidstat.c
b87a4b
@@ -23,6 +23,7 @@
b87a4b
 #include <string.h>
b87a4b
 #include <stdlib.h>
b87a4b
 #include <unistd.h>
b87a4b
+#include <fcntl.h>
b87a4b
 #include <signal.h>
b87a4b
 #include <dirent.h>
b87a4b
 #include <ctype.h>
b87a4b
@@ -307,9 +308,10 @@ void read_proc_meminfo(void)
b87a4b
 int read_proc_pid_stat(unsigned int pid, struct pid_stats *pst,
b87a4b
 		       unsigned int *thread_nr, unsigned int tgid)
b87a4b
 {
b87a4b
-	FILE *fp;
b87a4b
-	char filename[128], format[256], comm[MAX_COMM_LEN + 1];
b87a4b
-	size_t len;
b87a4b
+	int fd, sz, rc, commsz;
b87a4b
+	char filename[128];
b87a4b
+	static char buffer[1024 + 1];
b87a4b
+	char *start, *end;
b87a4b
 
b87a4b
 	if (tgid) {
b87a4b
 		sprintf(filename, TASK_STAT, tgid, pid);
b87a4b
@@ -318,36 +320,45 @@ int read_proc_pid_stat(unsigned int pid, struct pid_stats *pst,
b87a4b
 		sprintf(filename, PID_STAT, pid);
b87a4b
 	}
b87a4b
 
b87a4b
-	if ((fp = fopen(filename, "r")) == NULL)
b87a4b
+	if ((fd = open(filename, O_RDONLY)) < 0)
b87a4b
 		/* No such process */
b87a4b
 		return 1;
b87a4b
 
b87a4b
-	sprintf(format, "%%*d (%%%ds %%*s %%*d %%*d %%*d %%*d %%*d %%*u %%lu %%lu"
b87a4b
-		" %%lu %%lu %%lu %%lu %%lu %%lu %%*d %%*d %%u %%*u %%*d %%lu %%lu"
b87a4b
-		" %%*u %%*u %%*u %%*u %%*u %%*u %%*u %%*u %%*u %%*u %%*u %%*u %%*u"
b87a4b
-		" %%*u %%u %%*u %%*u %%*u %%lu %%lu\\n", MAX_COMM_LEN);
b87a4b
-
b87a4b
-	fscanf(fp, format, comm,
b87a4b
-	       &pst->minflt, &pst->cminflt, &pst->majflt, &pst->cmajflt,
b87a4b
-	       &pst->utime,  &pst->stime, &pst->cutime, &pst->cstime,
b87a4b
-	       thread_nr, &pst->vsz, &pst->rss, &pst->processor,
b87a4b
-	       &pst->gtime, &pst->cgtime);
b87a4b
+	sz = read(fd, buffer, 1024);
b87a4b
+	close(fd);
b87a4b
+	if (sz <= 0)
b87a4b
+		return 1;
b87a4b
+	buffer[sz] = '\0';
b87a4b
 
b87a4b
-	fclose(fp);
b87a4b
+	if ((start = strchr(buffer, '(')) == NULL)
b87a4b
+		return 1;
b87a4b
+	start += 1;
b87a4b
+	if ((end = strrchr(start, ')')) == NULL)
b87a4b
+		return 1;
b87a4b
+	commsz = end - start;
b87a4b
+	if (commsz >= MAX_COMM_LEN)
b87a4b
+		return 1;
b87a4b
+	memcpy(pst->comm, start, commsz);
b87a4b
+	pst->comm[commsz] = '\0';
b87a4b
+	start = end + 2;
b87a4b
+
b87a4b
+	rc = sscanf(start,
b87a4b
+		    "%*s %*d %*d %*d %*d %*d %*u %lu %lu"
b87a4b
+		    " %lu %lu %lu %lu %ld %ld %*d %*d %u %*u %*d %lu %lu"
b87a4b
+		    " %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u"
b87a4b
+		    " %*u %u %*u %*u %*lu %lu %ld\n",
b87a4b
+		    &pst->minflt, &pst->cminflt, &pst->majflt, &pst->cmajflt,
b87a4b
+		    &pst->utime,  &pst->stime, &pst->cutime, &pst->cstime,
b87a4b
+		    thread_nr, &pst->vsz, &pst->rss, &pst->processor,
b87a4b
+		    &pst->gtime, &pst->cgtime);
b87a4b
+
b87a4b
+	if (rc < 14)
b87a4b
+		return 1;
b87a4b
 
b87a4b
 	/* Convert to kB */
b87a4b
 	pst->vsz >>= 10;
b87a4b
 	pst->rss = PG_TO_KB(pst->rss);
b87a4b
 
b87a4b
-	strncpy(pst->comm, comm, MAX_COMM_LEN);
b87a4b
-	pst->comm[MAX_COMM_LEN - 1] = '\0';
b87a4b
-
b87a4b
-	/* Remove trailing ')' */
b87a4b
-	len = strlen(pst->comm);
b87a4b
-	if (len && (pst->comm[len - 1] == ')')) {
b87a4b
-		pst->comm[len - 1] = '\0';
b87a4b
-	}
b87a4b
-
b87a4b
 	pst->pid = pid;
b87a4b
 	pst->tgid = tgid;
b87a4b
 	return 0;
b87a4b
-- 
b87a4b
2.14.4
b87a4b