Skip to contentSkip to conversation

use get_task_name in ss

PATCH Applied Stephen Hemminger 2 participants 4 messages Oct 21 – Oct 22

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover use get_task_name in ss

Recent proposed patch by Prashanth showed that ss command parsing of proc with fscanf is fragile. Here is a pair of patches which allows reusing existing code in iproute2 lib/fs for that instead.

Stephen Hemminger (2):
lib: add root path argument to get_task_name
ss: use get_task_name instead of parsing stat

 include/utils.h |  2 +-
 ip/iptuntap.c   |  2 +-
 lib/fs.c        |  9 +++++++--
 misc/ss.c       | 34 ++++++++++++----------------------
 rdma/res-cmid.c |  2 +-
 rdma/res-cq.c   |  2 +-
 rdma/res-ctx.c  |  2 +-
 rdma/res-mr.c   |  2 +-
 rdma/res-pd.c   |  2 +-
 rdma/res-qp.c   |  2 +-
 rdma/res-srq.c  |  2 +-
 rdma/stat.c     |  2 +-
 12 files changed, 29 insertions(+), 34 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/2 lib: add root path argument to get_task_name

Allow callers to read comm from somewhere other than /proc, so ss can honor PROC_ROOT and look up per-thread names. Existing callers pass NULL to keep using /proc.

Assisted-by Claude:claude-opus-5-5
---
 include/utils.h | 2 +-
 ip/iptuntap.c   | 2 +-
 lib/fs.c        | 9 +++++++--
 rdma/res-cmid.c | 2 +-
 rdma/res-cq.c   | 2 +-
 rdma/res-ctx.c  | 2 +-
 rdma/res-mr.c   | 2 +-
 rdma/res-pd.c   | 2 +-
 rdma/res-qp.c   | 2 +-
 rdma/res-srq.c  | 2 +-
 rdma/stat.c     | 2 +-
 11 files changed, 17 insertions(+), 12 deletions(-)
include/utils.h +1 -1
index 5812d1a8..87b115d9 100644
@@ -312,7 +312,7 @@ char *find_cgroup2_mount(bool do_mount);
 __u64 get_cgroup2_id(const char *path);
 char *get_cgroup2_path(__u64 id);
 int get_command_name(const char *pid, char *comm, size_t len);
-int get_task_name(pid_t pid, char *name, size_t len);
+int get_task_name(const char *root, pid_t pid, char *name, size_t len);
 
 int get_rtnl_link_stats_rta(struct rtnl_link_stats64 *stats64,
 			    struct rtattr *tb[]);
ip/iptuntap.c +1 -1
index 0f46fe6a..d1ff48b3 100644
@@ -315,7 +315,7 @@ static void show_processes(const char *name)
 				SPRINT_BUF(pname);
 
 				open_json_object(NULL);
-				if (get_task_name(pid, pname, sizeof(pname)))
+				if (get_task_name(NULL, pid, pname, sizeof(pname)))
 					print_string(PRINT_ANY, "name",
 						     "%s", "<NULL>");
 				else
lib/fs.c +7 -2
index 86f9fb65..a7d23498 100644
@@ -343,7 +343,11 @@ int get_command_name(const char *pid, char *comm, size_t len)
 	return 0;
 }
 
-int get_task_name(pid_t pid, char *name, size_t len)
+/*
+ * Read task name from <root>/<pid>/comm.
+ * If root is NULL, use /proc.
+ */
+int get_task_name(const char *root, pid_t pid, char *name, size_t len)
 {
 	char path[PATH_MAX];
 	FILE *f;
@@ -351,7 +355,8 @@ int get_task_name(pid_t pid, char *name, size_t len)
 	if (!pid)
 		return -1;
 
-	if (snprintf(path, sizeof(path), "/proc/%d/comm", pid) >= sizeof(path))
+	if (snprintf(path, sizeof(path), "%s/%d/comm",
+		     root ? : "/proc", pid) >= sizeof(path))
 		return -1;
 
 	f = fopen(path, "r");
rdma/res-cmid.c +1 -1
index 17a89cc4..da783567 100644
@@ -158,7 +158,7 @@ static int res_cm_id_line(struct rd *rd, const char *name, int idx,
 
 	if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) {
 		pid = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_PID]);
-		if (!get_task_name(pid, b, sizeof(b)))
+		if (!get_task_name(NULL, pid, b, sizeof(b)))
 			comm = b;
 	} else if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]) {
 		/* discard const from mnl_attr_get_str */
rdma/res-cq.c +1 -1
index 0cab3fe0..49f3ef6b 100644
@@ -86,7 +86,7 @@ static int res_cq_line(struct rd *rd, const char *name, int idx,
 
 	if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) {
 		pid = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_PID]);
-		if (!get_task_name(pid, b, sizeof(b)))
+		if (!get_task_name(NULL, pid, b, sizeof(b)))
 			comm = b;
 	} else if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]) {
 		/* discard const from mnl_attr_get_str */
rdma/res-ctx.c +1 -1
index 235c837a..e44fd8f7 100644
@@ -20,7 +20,7 @@ static int res_ctx_line(struct rd *rd, const char *name, int idx,
 
 	if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) {
 		pid = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_PID]);
-		if (!get_task_name(pid, b, sizeof(b)))
+		if (!get_task_name(NULL, pid, b, sizeof(b)))
 			comm = b;
 	} else if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]) {
 		/* discard const from mnl_attr_get_str */
rdma/res-mr.c +1 -1
index f6c2534a..17b2242f 100644
@@ -50,7 +50,7 @@ static int res_mr_line(struct rd *rd, const char *name, int idx,
 
 	if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) {
 		pid = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_PID]);
-		if (!get_task_name(pid, b, sizeof(b)))
+		if (!get_task_name(NULL, pid, b, sizeof(b)))
 			comm = b;
 	} else if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]) {
 		/* discard const from mnl_attr_get_str */
rdma/res-pd.c +1 -1
index 8b9f7aa6..f3a17055 100644
@@ -36,7 +36,7 @@ static int res_pd_line(struct rd *rd, const char *name, int idx,
 
 	if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) {
 		pid = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_PID]);
-		if (!get_task_name(pid, b, sizeof(b)))
+		if (!get_task_name(NULL, pid, b, sizeof(b)))
 			comm = b;
 	} else if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]) {
 		/* discard const from mnl_attr_get_str */
rdma/res-qp.c +1 -1
index 231ae99c..2593b6ed 100644
@@ -160,7 +160,7 @@ static int res_qp_line(struct rd *rd, const char *name, int idx,
 
 	if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) {
 		pid = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_PID]);
-		if (!get_task_name(pid, b, sizeof(b)))
+		if (!get_task_name(NULL, pid, b, sizeof(b)))
 			comm = b;
 	} else if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]) {
 		/* discard const from mnl_attr_get_str */
rdma/res-srq.c +1 -1
index 8ab2538a..3c6333ad 100644
@@ -190,7 +190,7 @@ static int res_srq_line(struct rd *rd, const char *name, int idx,
 
 	if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) {
 		pid = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_PID]);
-		if (!get_task_name(pid, b, sizeof(b)))
+		if (!get_task_name(NULL, pid, b, sizeof(b)))
 			comm = b;
 	} else if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]) {
 		/* discard const from mnl_attr_get_str */
rdma/stat.c +1 -1
index 2c1bf68e..f2c2d173 100644
@@ -259,7 +259,7 @@ static int res_counter_line(struct rd *rd, const char *name, int index,
 
 	if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) {
 		pid = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_PID]);
-		if (!get_task_name(pid, b, sizeof(b)))
+		if (!get_task_name(NULL, pid, b, sizeof(b)))
 			comm = b;
 	} else if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]) {
 		/* discard const from mnl_attr_get_str */
Signature · 1 line
2.53.0
#2 lore raw

PATCH 2/2 ss: use get_task_name instead of parsing stat

In reply to Stephen Hemminger

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
---
 misc/ss.c | 34 ++++++++++++----------------------
 1 file changed, 12 insertions(+), 22 deletions(-)
misc/ss.c +12 -22
index 26520cee..cffee883 100644
@@ -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)
Signature · 1 line
2.53.0
#3 lore raw
In reply to Stephen Hemminger
This series was applied to iproute2/iproute2.git (main) by Stephen Hemminger <[email protected]>:

Hello:

This series was applied to iproute2/iproute2.git (main) by Stephen Hemminger <[email protected]>:

Quoted 9 lines · On Wed, 21 Oct 2026 08:57:21 -0700 you wrote:

Recent proposed patch by Prashanth showed that ss command parsing of proc with fscanf is fragile. Here is a pair of patches which allows reusing existing code in iproute2 lib/fs for that instead.

Stephen Hemminger (2):
lib: add root path argument to get_task_name
ss: use get_task_name instead of parsing stat

[...]

Here is the summary with links:
- [iproute2,1/2] lib: add root path argument to get_task_name

    https://git.kernel.org/pub/scm/network/iproute2/iproute2.git/commit/?id=c92418663848
  - [iproute2,2/2] ss: use get_task_name instead of parsing stat
    https://git.kernel.org/pub/scm/network/iproute2/iproute2.git/commit/?id=89d08bac1661

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html