cdown / rpms / util-linux

Forked from rpms/util-linux 2 years ago
Clone

Blame SOURCES/0151-lsns-Fix-parser-for-proc-pid-stat-which-is-including.patch

05ad79
From 7a151a3d74b2972410103b684803e6d6b8fda15b Mon Sep 17 00:00:00 2001
05ad79
From: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
05ad79
Date: Wed, 23 Nov 2016 14:13:34 +0900
05ad79
Subject: [PATCH 151/173] lsns: Fix parser for /proc/<pid>/stat which is
05ad79
 including space in comm
05ad79
05ad79
For example, child process of spamd has
05ad79
05ad79
    32031 (spamd child) S 32026 32026 32026 0 -1 4210752 338 0 0 0 ...
05ad79
05ad79
fscanf("%d %*s %c %d*[^\n]") in read_process() can't parse above as we
05ad79
expected, because %s only skips non-whitespace. I.e. it parses like
05ad79
following,
05ad79
05ad79
    32031 (spamd child) S 32026 32026 32026 0 -1 4210752 338 0 0 0 ...
05ad79
    +---+ +----+ +
05ad79
      %d    %*s  %c
05ad79
05ad79
and returns 2 (pid=32031, state=c).
05ad79
05ad79
This fixes it by skipping task->comm part manually.
05ad79
05ad79
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1543428
05ad79
Upstream: http://github.com/karelzak/util-linux/commit/3fcbd7978980dc1a29c626b701333e27599e506d
05ad79
Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
05ad79
---
05ad79
 sys-utils/lsns.c | 30 ++++++++++++++++++++++++++----
05ad79
 1 file changed, 26 insertions(+), 4 deletions(-)
05ad79
05ad79
diff --git a/sys-utils/lsns.c b/sys-utils/lsns.c
05ad79
index b8841b7a3..d32756508 100644
05ad79
--- a/sys-utils/lsns.c
05ad79
+++ b/sys-utils/lsns.c
05ad79
@@ -217,6 +217,30 @@ static int get_ns_ino(int dir, const char *nsname, ino_t *ino)
05ad79
 	return 0;
05ad79
 }
05ad79
 
05ad79
+static int parse_proc_stat(FILE *fp, pid_t *pid, char *state, pid_t *ppid)
05ad79
+{
05ad79
+	char *line = NULL, *p;
05ad79
+	size_t len = 0;
05ad79
+	int rc;
05ad79
+
05ad79
+	if (getline(&line, &len, fp) < 0) {
05ad79
+		rc = -errno;
05ad79
+		goto error;
05ad79
+	}
05ad79
+
05ad79
+	p = strrchr(line, ')');
05ad79
+	if (p == NULL ||
05ad79
+	    sscanf(line, "%d (", pid) != 1 ||
05ad79
+	    sscanf(p, ") %c %d*[^\n]", state, ppid) != 2) {
05ad79
+		rc = -EINVAL;
05ad79
+		goto error;
05ad79
+	}
05ad79
+	rc = 0;
05ad79
+
05ad79
+error:
05ad79
+	free(line);
05ad79
+	return rc;
05ad79
+}
05ad79
 
05ad79
 static int read_process(struct lsns *ls, pid_t pid)
05ad79
 {
05ad79
@@ -255,11 +279,9 @@ static int read_process(struct lsns *ls, pid_t pid)
05ad79
 		rc = -errno;
05ad79
 		goto done;
05ad79
 	}
05ad79
-	rc = fscanf(f, "%d %*s %c %d*[^\n]", &p->pid, &p->state, &p->ppid);
05ad79
-	if (rc != 3) {
05ad79
-		rc = rc < 0 ? -errno : -EINVAL;
05ad79
+	rc = parse_proc_stat(f, &p->pid, &p->state, &p->ppid);
05ad79
+	if (rc < 0)
05ad79
 		goto done;
05ad79
-	}
05ad79
 	rc = 0;
05ad79
 
05ad79
 	for (i = 0; i < ARRAY_SIZE(p->ns_ids); i++) {
05ad79
-- 
05ad79
2.14.4
05ad79