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

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