All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] lsns: Fix parser for /proc/<pid>/stat which is including space in comm
@ 2016-11-19 14:38 OGAWA Hirofumi
  2016-11-22 20:43 ` Mike Frysinger
  0 siblings, 1 reply; 5+ messages in thread
From: OGAWA Hirofumi @ 2016-11-19 14:38 UTC (permalink / raw)
  To: util-linux


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>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] lsns: Fix parser for /proc/<pid>/stat which is including space in comm
  2016-11-19 14:38 [PATCH] lsns: Fix parser for /proc/<pid>/stat which is including space in comm OGAWA Hirofumi
@ 2016-11-22 20:43 ` Mike Frysinger
  2016-11-23  5:04   ` OGAWA Hirofumi
  2016-11-23  5:13   ` [PATCH v2] " OGAWA Hirofumi
  0 siblings, 2 replies; 5+ messages in thread
From: Mike Frysinger @ 2016-11-22 20:43 UTC (permalink / raw)
  To: OGAWA Hirofumi; +Cc: util-linux

[-- Attachment #1: Type: text/plain, Size: 185 bytes --]

On 19 Nov 2016 23:38, OGAWA Hirofumi wrote:
> 
> Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>

can you please include an example line in the commit message ?
-mike

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] lsns: Fix parser for /proc/<pid>/stat which is including space in comm
  2016-11-22 20:43 ` Mike Frysinger
@ 2016-11-23  5:04   ` OGAWA Hirofumi
  2016-11-23  5:13   ` [PATCH v2] " OGAWA Hirofumi
  1 sibling, 0 replies; 5+ messages in thread
From: OGAWA Hirofumi @ 2016-11-23  5:04 UTC (permalink / raw)
  To: util-linux

Mike Frysinger <vapier@gentoo.org> writes:

> On 19 Nov 2016 23:38, OGAWA Hirofumi wrote:
>> 
>> Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
>
> can you please include an example line in the commit message ?

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2] lsns: Fix parser for /proc/<pid>/stat which is including space in comm
  2016-11-22 20:43 ` Mike Frysinger
  2016-11-23  5:04   ` OGAWA Hirofumi
@ 2016-11-23  5:13   ` OGAWA Hirofumi
  2016-12-02 12:23     ` Karel Zak
  1 sibling, 1 reply; 5+ messages in thread
From: OGAWA Hirofumi @ 2016-11-23  5:13 UTC (permalink / raw)
  To: util-linux

For example, child process of spamd has

    32031 (spamd child) S 32026 32026 32026 0 -1 4210752 338 0 0 0 ...

fscanf("%d %*s %c %d*[^\n]") in read_process() can't parse above as we
expected, because %s only skips non-whitespace. I.e. it parses like
following,

    32031 (spamd child) S 32026 32026 32026 0 -1 4210752 338 0 0 0 ...
    +---+ +----+ +
      %d    %*s  %c

and returns 2 (pid=32031, state=c).

This fixes it by skipping task->comm part manually.

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~a sys-utils/lsns.c
--- util-linux2/sys-utils/lsns.c~a	2016-11-23 13:54:46.030143424 +0900
+++ util-linux2-hirofumi/sys-utils/lsns.c	2016-11-23 13:54:46.032143437 +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>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] lsns: Fix parser for /proc/<pid>/stat which is including space in comm
  2016-11-23  5:13   ` [PATCH v2] " OGAWA Hirofumi
@ 2016-12-02 12:23     ` Karel Zak
  0 siblings, 0 replies; 5+ messages in thread
From: Karel Zak @ 2016-12-02 12:23 UTC (permalink / raw)
  To: OGAWA Hirofumi; +Cc: util-linux

On Wed, Nov 23, 2016 at 02:13:34PM +0900, OGAWA Hirofumi wrote:
>  sys-utils/lsns.c |   30 ++++++++++++++++++++++++++----
>  1 file changed, 26 insertions(+), 4 deletions(-)

Applied, thanks.

    Karel

-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2016-12-02 12:23 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-11-19 14:38 [PATCH] lsns: Fix parser for /proc/<pid>/stat which is including space in comm OGAWA Hirofumi
2016-11-22 20:43 ` 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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.