dcavalca / rpms / util-linux

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