use get_task_name in ss
Subsystems (3)
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
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.
--- 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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
PATCH 2/2 ss: use get_task_name instead of parsing stat
In reply to Stephen HemmingerParsing /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.
--- misc/ss.c | 34 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 22 deletions(-)
misc/ss.c +12 -22
@@ -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
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 toget_task_name
ss: useget_task_nameinstead 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