Thread View: linux.kernel.netdev
4 messages
4 total messages
Started by Stephen Hemminger
Wed, 21 Oct 2026 15:57
[PATCH iproute2 0/2] use get_task_name in ss
Author: Stephen Hemminger
Date: Wed, 21 Oct 2026 15:57
Date: Wed, 21 Oct 2026 15:57
25 lines
11283 bytes
11283 bytes
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(-) -- 2.53.0
[PATCH iproute2 1/2] lib: add root path argument to get_task_name
Author: Stephen Hemminger
Date: Wed, 21 Oct 2026 15:57
Date: Wed, 21 Oct 2026 15:57
180 lines
17425 bytes
17425 bytes
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
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
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(-)
diff --git a/include/utils.h b/include/utils.h
index 5812d1a8..87b115d9 100644
--- a/include/utils.h
+++ b/include/utils.h
@@ -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[]);
diff --git a/ip/iptuntap.c b/ip/iptuntap.c
index 0f46fe6a..d1ff48b3 100644
--- a/ip/iptuntap.c
+++ b/ip/iptuntap.c
@@ -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
diff --git a/lib/fs.c b/lib/fs.c
index 86f9fb65..a7d23498 100644
--- a/lib/fs.c
+++ b/lib/fs.c
@@ -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");
diff --git a/rdma/res-cmid.c b/rdma/res-cmid.c
index 17a89cc4..da783567 100644
--- a/rdma/res-cmid.c
+++ b/rdma/res-cmid.c
@@ -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 */
diff --git a/rdma/res-cq.c b/rdma/res-cq.c
index 0cab3fe0..49f3ef6b 100644
--- a/rdma/res-cq.c
+++ b/rdma/res-cq.c
@@ -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 */
diff --git a/rdma/res-ctx.c b/rdma/res-ctx.c
index 235c837a..e44fd8f7 100644
--- a/rdma/res-ctx.c
+++ b/rdma/res-ctx.c
@@ -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 */
diff --git a/rdma/res-mr.c b/rdma/res-mr.c
index f6c2534a..17b2242f 100644
--- a/rdma/res-mr.c
+++ b/rdma/res-mr.c
@@ -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 */
diff --git a/rdma/res-pd.c b/rdma/res-pd.c
index 8b9f7aa6..f3a17055 100644
--- a/rdma/res-pd.c
+++ b/rdma/res-pd.c
@@ -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 */
diff --git a/rdma/res-qp.c b/rdma/res-qp.c
index 231ae99c..2593b6ed 100644
--- a/rdma/res-qp.c
+++ b/rdma/res-qp.c
@@ -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 */
diff --git a/rdma/res-srq.c b/rdma/res-srq.c
index 8ab2538a..3c6333ad 100644
--- a/rdma/res-srq.c
+++ b/rdma/res-srq.c
@@ -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 */
diff --git a/rdma/stat.c b/rdma/stat.c
index 2c1bf68e..f2c2d173 100644
--- a/rdma/stat.c
+++ b/rdma/stat.c
@@ -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 */
--
2.53.0
[PATCH iproute2 2/2] ss: use get_task_name instead of parsing stat
Author: Stephen Hemminger
Date: Wed, 21 Oct 2026 15:57
Date: Wed, 21 Oct 2026 15:57
71 lines
13046 bytes
13046 bytes
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
Re: [PATCH iproute2 0/2] use get_task_name in ss
Author: patchwork-bot+netdevbpf
Date: Thu, 22 Oct 2026 18:30
Date: Thu, 22 Oct 2026 18:30
27 lines
8700 bytes
8700 bytes
Hello:
This series was applied to iproute2/iproute2.git (main)
by Stephen Hemminger <stephen@networkplumber.org>:
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!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads