Util-Linux package development
 help / color / mirror / Atom feed
From: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
To: util-linux@vger.kernel.org
Subject: [PATCH] lsns: Fix parser for /proc/<pid>/stat which is including space in comm
Date: Sat, 19 Nov 2016 23:38:08 +0900	[thread overview]
Message-ID: <87a8cvk01b.fsf@mail.parknet.co.jp> (raw)


Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
---

 sys-utils/lsns.c |   30 ++++++++++++++++++++++++++----
 1 file changed, 26 insertions(+), 4 deletions(-)

diff -puN sys-utils/lsns.c~fix-stat-parse sys-utils/lsns.c
--- util-linux/sys-utils/lsns.c~fix-stat-parse	2016-11-19 22:49:35.287971970 +0900
+++ util-linux-hirofumi/sys-utils/lsns.c	2016-11-19 23:14:35.996917806 +0900
@@ -217,6 +217,30 @@ static int get_ns_ino(int dir, const cha
 	return 0;
 }
 
+static int parse_proc_stat(FILE *fp, pid_t *pid, char *state, pid_t *ppid)
+{
+	char *line = NULL, *p;
+	size_t len = 0;
+	int rc;
+
+	if (getline(&line, &len, fp) < 0) {
+		rc = -errno;
+		goto error;
+	}
+
+	p = strrchr(line, ')');
+	if (p == NULL ||
+	    sscanf(line, "%d (", pid) != 1 ||
+	    sscanf(p, ") %c %d*[^\n]", state, ppid) != 2) {
+		rc = -EINVAL;
+		goto error;
+	}
+	rc = 0;
+
+error:
+	free(line);
+	return rc;
+}
 
 static int read_process(struct lsns *ls, pid_t pid)
 {
@@ -255,11 +279,9 @@ static int read_process(struct lsns *ls,
 		rc = -errno;
 		goto done;
 	}
-	rc = fscanf(f, "%d %*s %c %d*[^\n]", &p->pid, &p->state, &p->ppid);
-	if (rc != 3) {
-		rc = rc < 0 ? -errno : -EINVAL;
+	rc = parse_proc_stat(f, &p->pid, &p->state, &p->ppid);
+	if (rc < 0)
 		goto done;
-	}
 	rc = 0;
 
 	for (i = 0; i < ARRAY_SIZE(p->ns_ids); i++) {
_

-- 
OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>

             reply	other threads:[~2016-11-19 15:05 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-11-19 14:38 OGAWA Hirofumi [this message]
2016-11-22 20:43 ` [PATCH] lsns: Fix parser for /proc/<pid>/stat which is including space in comm Mike Frysinger
2016-11-23  5:04   ` OGAWA Hirofumi
2016-11-23  5:13   ` [PATCH v2] " OGAWA Hirofumi
2016-12-02 12:23     ` Karel Zak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87a8cvk01b.fsf@mail.parknet.co.jp \
    --to=hirofumi@mail.parknet.co.jp \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox