Netdev List
 help / color / mirror / Atom feed
From: Stephen Hemminger <stephen@networkplumber.org>
To: netdev@vger.kernel.org
Cc: dsahern@kernel.org, Stephen Hemminger <stephen@networkplumber.org>
Subject: [PATCH iproute2 2/2] ss: use get_task_name instead of parsing stat
Date: Wed, 21 Oct 2026 08:57:23 -0700	[thread overview]
Message-ID: <20261021160020.3838946-3-stephen@networkplumber.org> (raw)
In-Reply-To: <20261021160020.3838946-1-stephen@networkplumber.org>

Parsing /proc/<pid>/stat with fscanf("%*d (%[^)])") is fragile:
the conversion is unbounded and stops at the first ')', so a task
whose name contains ')' is reported truncated.

Use get_task_name() to read comm directly, for both the socket
owner (per-thread with -T) and the netlink socket owner.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 misc/ss.c | 34 ++++++++++++----------------------
 1 file changed, 12 insertions(+), 22 deletions(-)

diff --git a/misc/ss.c b/misc/ss.c
index 26520cee..cffee883 100644
--- a/misc/ss.c
+++ b/misc/ss.c
@@ -592,19 +592,15 @@ static void user_ent_hash_build_task(char *path, int pid, int tid)
 			sock_context = strdup(no_ctx);
 
 		if (task[0] == '\0') {
-			char stat[MAX_PATH_LEN];
+			char task_root[MAX_PATH_LEN];
 			char name[16];
-			FILE *fp;
 
-			strlcpy(stat, path, pos_id + 1);
-			snprintf(stat + pos_id, sizeof(stat) - pos_id, "stat");
+			/* $PROC_ROOT/$PID/task/$TID/comm */
+			snprintf(task_root, sizeof(task_root), "%s/%d/task",
+				 getenv("PROC_ROOT") ? : "/proc", pid);
 
-			fp = fopen(stat, "r");
-			if (fp) {
-				if (fscanf(fp, "%*d (%[^)])", name) == 1)
-					escape_str(task, name, sizeof(task));
-				fclose(fp);
-			}
+			if (get_task_name(task_root, tid, name, sizeof(name)) == 0)
+				escape_str(task, name, sizeof(task));
 		}
 
 		user_ent_add(ino, task, pid, tid, fd, task_context, sock_context);
@@ -5169,18 +5165,12 @@ static int netlink_show_one(struct filter *f,
 			done = 1;
 			strncpy(procname, "kernel", 7);
 		} else if (pid > 0) {
-			FILE *fp;
-
-			snprintf(procname, sizeof(procname), "%s/%d/stat",
-				getenv("PROC_ROOT") ? : "/proc", pid);
-			if ((fp = fopen(procname, "r")) != NULL) {
-				if (fscanf(fp, "%*d (%[^)])", procname) == 1) {
-					snprintf(procname+strlen(procname),
-						sizeof(procname)-strlen(procname),
-						"/%d", pid);
-					done = 1;
-				}
-				fclose(fp);
+			if (get_task_name(getenv("PROC_ROOT"), pid,
+					  procname, sizeof(procname)) == 0) {
+				snprintf(procname + strlen(procname),
+					 sizeof(procname) - strlen(procname),
+					 "/%d", pid);
+				done = 1;
 			}
 		}
 		if (!done)
-- 
2.53.0


  parent reply	other threads:[~2026-10-21 16:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-21 15:57 [PATCH iproute2 0/2] use get_task_name in ss Stephen Hemminger
2026-10-21 15:57 ` [PATCH iproute2 1/2] lib: add root path argument to get_task_name Stephen Hemminger
2026-10-21 15:57 ` Stephen Hemminger [this message]
2026-10-22 18:30 ` [PATCH iproute2 0/2] use get_task_name in ss patchwork-bot+netdevbpf

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=20261021160020.3838946-3-stephen@networkplumber.org \
    --to=stephen@networkplumber.org \
    --cc=dsahern@kernel.org \
    --cc=netdev@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