Skip to contentSkip to conversation

ss: Introduce -T, --threads option

PATCH Applied Peilin Ye 2 participants 9 messages May 25, 2022 – May 30, 2022

Subsystems (2)
lore
Share & tools
lore

Peilin Ye

author
#0 lore raw

cover ss: Introduce -T, --threads option

From: Peilin Ye <[email protected]>

Hi all,

This patchset adds a new ss option, -T (--threads), to show thread information. It extends the -p (--processes) option, and should be useful for debugging, monitoring multi-threaded applications. Example output:

  $ ss -ltT "sport = 1234"
  State   Recv-Q  Send-Q  Local Address:Port      Peer Address:Port       Process
  LISTEN  0       100           0.0.0.0:1234           0.0.0.0:*           users:(("test",pid=2932547,tid=2932548,fd=3),("test",pid=2932547,tid=2932547,fd=3))

It implies -p i.e. it outputs all threads in the thread group, including the thread group leader. When -T is used, -Z and -z also show SELinux contexts for threads.

[1-5/7] are small clean-ups for the user_ent_hash_build() function. [6/7] factors out logic iterating $PROC_ROOT/$PID/fd/ from user_ent_hash_build() to make [7/7] easier. [7/7] actually implements the feature.

Thanks,
Peilin Ye (7):
ss: Use assignment-suppression character in sscanf()
ss: Remove unnecessary stack variable 'p' in user_ent_hash_build()
ss: Do not call user_ent_hash_build() more than once
ss: Delete unnecessary call to snprintf() in user_ent_hash_build()
ss: Fix coding style issues in user_ent_hash_build()
ss: Factor out fd iterating logic from user_ent_hash_build()
ss: Introduce -T, --threads option

 man/man8/ss.8 |   8 +-
 misc/ss.c     | 230 +++++++++++++++++++++++++++++---------------------
 2 files changed, 142 insertions(+), 96 deletions(-)
Signature · 1 line
2.20.1
author
#1 lore raw

PATCH 1/7 ss: Use assignment-suppression character in sscanf()

From: Peilin Ye <[email protected]>

Use the '*' assignment-suppression character, instead of an inappropriately named temporary variable.

---
 misc/ss.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
misc/ss.c +2 -3
index 4b3ca9c4e86b..aa9da7e45e53 100644
@@ -621,9 +621,8 @@ static void user_ent_hash_build(void)
 		char *p;
 		int pid, pos;
 		DIR *dir1;
-		char crap;
 
-		if (sscanf(d->d_name, "%d%c", &pid, &crap) != 1)
+		if (sscanf(d->d_name, "%d%*c", &pid) != 1)
 			continue;
 
 		if (getpidcon(pid, &pid_context) != 0)
@@ -647,7 +646,7 @@ static void user_ent_hash_build(void)
 			ssize_t link_len;
 			char tmp[1024];
 
-			if (sscanf(d1->d_name, "%d%c", &fd, &crap) != 1)
+			if (sscanf(d1->d_name, "%d%*c", &fd) != 1)
 				continue;
 
 			snprintf(name+pos, sizeof(name) - pos, "%d", fd);
Signature · 1 line
2.20.1
author
#2 lore raw

PATCH 2/7 ss: Remove unnecessary stack variable 'p' in user_ent_hash_build()

In reply to Peilin Ye

From: Peilin Ye <[email protected]>

Commit 116ac9270b6d ("ss: Add support for retrieving SELinux contexts") added an unnecessary stack variable, 'char *p', in user_ent_hash_build(). Delete it for readability.

---
 misc/ss.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
misc/ss.c +3 -5
index aa9da7e45e53..bccf01bb5efa 100644
@@ -618,7 +618,6 @@ static void user_ent_hash_build(void)
 	while ((d = readdir(dir)) != NULL) {
 		struct dirent *d1;
 		char process[16];
-		char *p;
 		int pid, pos;
 		DIR *dir1;
 
@@ -636,7 +635,6 @@ static void user_ent_hash_build(void)
 		}
 
 		process[0] = '\0';
-		p = process;
 
 		while ((d1 = readdir(dir1)) != NULL) {
 			const char *pattern = "socket:[";
@@ -667,18 +665,18 @@ static void user_ent_hash_build(void)
 			if (getfilecon(tmp, &sock_context) <= 0)
 				sock_context = strdup(no_ctx);
 
-			if (*p == '\0') {
+			if (process[0] == '\0') {
 				FILE *fp;
 
 				snprintf(tmp, sizeof(tmp), "%s/%d/stat",
 					root, pid);
 				if ((fp = fopen(tmp, "r")) != NULL) {
-					if (fscanf(fp, "%*d (%[^)])", p) < 1)
+					if (fscanf(fp, "%*d (%[^)])", process) < 1)
 						; /* ignore */
 					fclose(fp);
 				}
 			}
-			user_ent_add(ino, p, pid, fd,
+			user_ent_add(ino, process, pid, fd,
 					pid_context, sock_context);
 			freecon(sock_context);
 		}
Signature · 1 line
2.20.1
author
#3 lore raw

PATCH 3/7 ss: Do not call user_ent_hash_build() more than once

In reply to Peilin Ye

From: Peilin Ye <[email protected]>

Call user_ent_hash_build() once after the getopt_long() loop if -p, -z or -Z is used.

---
 misc/ss.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)
misc/ss.c +3 -9
index bccf01bb5efa..dd7b67a76255 100644
@@ -596,13 +596,6 @@ static void user_ent_hash_build(void)
 	char *pid_context;
 	char *sock_context;
 	const char *no_ctx = "unavailable";
-	static int user_ent_hash_build_init;
-
-	/* If show_users & show_proc_ctx set only do this once */
-	if (user_ent_hash_build_init != 0)
-		return;
-
-	user_ent_hash_build_init = 1;
 
 	strlcpy(name, root, sizeof(name));
 
@@ -5509,7 +5502,6 @@ int main(int argc, char *argv[])
 			break;
 		case 'p':
 			show_users++;
-			user_ent_hash_build();
 			break;
 		case 'b':
 			show_options = 1;
@@ -5644,7 +5636,6 @@ int main(int argc, char *argv[])
 				exit(1);
 			}
 			show_proc_ctx++;
-			user_ent_hash_build();
 			break;
 		case 'N':
 			if (netns_switch(optarg))
@@ -5679,6 +5670,9 @@ int main(int argc, char *argv[])
 		}
 	}
 
+	if (show_users || show_proc_ctx || show_sock_ctx)
+		user_ent_hash_build();
+
 	argc -= optind;
 	argv += optind;
 
Signature · 1 line
2.20.1
author
#4 lore raw

PATCH 4/7 ss: Delete unnecessary call to snprintf() in user_ent_hash_build()

In reply to Peilin Ye

From: Peilin Ye <[email protected]>

'name' is already $PROC_ROOT/$PID/fd/$FD there, no need to rebuild the string.

---
 misc/ss.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
misc/ss.c +1 -4
index dd7b67a76255..ec14d746c669 100644
@@ -652,10 +652,7 @@ static void user_ent_hash_build(void)
 
 			sscanf(lnk, "socket:[%u]", &ino);
 
-			snprintf(tmp, sizeof(tmp), "%s/%d/fd/%s",
-					root, pid, d1->d_name);
-
-			if (getfilecon(tmp, &sock_context) <= 0)
+			if (getfilecon(name, &sock_context) <= 0)
 				sock_context = strdup(no_ctx);
 
 			if (process[0] == '\0') {
Signature · 1 line
2.20.1
author
#5 lore raw

PATCH 5/7 ss: Fix coding style issues in user_ent_hash_build()

In reply to Peilin Ye

From: Peilin Ye <[email protected]>

Make checkpatch.pl --strict happy about user_ent_hash_build().

---
 misc/ss.c | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)
misc/ss.c +12 -10
index ec14d746c669..42d04bf432eb 100644
@@ -599,7 +599,7 @@ static void user_ent_hash_build(void)
 
 	strlcpy(name, root, sizeof(name));
 
-	if (strlen(name) == 0 || name[strlen(name)-1] != '/')
+	if (strlen(name) == 0 || name[strlen(name) - 1] != '/')
 		strcat(name, "/");
 
 	nameoff = strlen(name);
@@ -622,7 +622,8 @@ static void user_ent_hash_build(void)
 
 		snprintf(name + nameoff, sizeof(name) - nameoff, "%d/fd/", pid);
 		pos = strlen(name);
-		if ((dir1 = opendir(name)) == NULL) {
+		dir1 = opendir(name);
+		if (!dir1) {
 			freecon(pid_context);
 			continue;
 		}
@@ -640,9 +641,9 @@ static void user_ent_hash_build(void)
 			if (sscanf(d1->d_name, "%d%*c", &fd) != 1)
 				continue;
 
-			snprintf(name+pos, sizeof(name) - pos, "%d", fd);
+			snprintf(name + pos, sizeof(name) - pos, "%d", fd);
 
-			link_len = readlink(name, lnk, sizeof(lnk)-1);
+			link_len = readlink(name, lnk, sizeof(lnk) - 1);
 			if (link_len == -1)
 				continue;
 			lnk[link_len] = '\0';
@@ -650,7 +651,8 @@ static void user_ent_hash_build(void)
 			if (strncmp(lnk, pattern, strlen(pattern)))
 				continue;
 
-			sscanf(lnk, "socket:[%u]", &ino);
+			if (sscanf(lnk, "socket:[%u]", &ino) != 1)
+				continue;
 
 			if (getfilecon(name, &sock_context) <= 0)
 				sock_context = strdup(no_ctx);
@@ -658,16 +660,16 @@ static void user_ent_hash_build(void)
 			if (process[0] == '\0') {
 				FILE *fp;
 
-				snprintf(tmp, sizeof(tmp), "%s/%d/stat",
-					root, pid);
-				if ((fp = fopen(tmp, "r")) != NULL) {
+				snprintf(tmp, sizeof(tmp), "%s/%d/stat", root, pid);
+
+				fp = fopen(tmp, "r");
+				if (fp) {
 					if (fscanf(fp, "%*d (%[^)])", process) < 1)
 						; /* ignore */
 					fclose(fp);
 				}
 			}
-			user_ent_add(ino, process, pid, fd,
-					pid_context, sock_context);
+			user_ent_add(ino, process, pid, fd, pid_context, sock_context);
 			freecon(sock_context);
 		}
 		freecon(pid_context);
Signature · 1 line
2.20.1
author
#6 lore raw

PATCH 6/7 ss: Factor out fd iterating logic from user_ent_hash_build()

In reply to Peilin Ye

From: Peilin Ye <[email protected]>

We are planning to add a thread version of the -p, --process option. Move the logic iterating $PROC_ROOT/$PID/fd/ into a new function, user_ent_hash_build_task(), to make it easier.

Since we will use this function for both processes and threads, rename local variables as such (e.g. from "process" to "task").

---
 misc/ss.c | 144 ++++++++++++++++++++++++++++++------------------------
 1 file changed, 79 insertions(+), 65 deletions(-)
misc/ss.c +79 -65
index 42d04bf432eb..07d0057b22cc 100644
@@ -567,6 +567,81 @@ static void user_ent_add(unsigned int ino, char *process,
 	*pp = p;
 }
 
+#define MAX_PATH_LEN	1024
+
+static void user_ent_hash_build_task(char *path, int pid)
+{
+	const char *no_ctx = "unavailable";
+	char task[16] = {'\0', };
+	char stat[MAX_PATH_LEN];
+	int pos_id, pos_fd;
+	char *task_context;
+	struct dirent *d;
+	DIR *dir;
+
+	if (getpidcon(pid, &task_context) != 0)
+		task_context = strdup(no_ctx);
+
+	pos_id = strlen(path);	/* $PROC_ROOT/$ID/ */
+
+	snprintf(path + pos_id, MAX_PATH_LEN - pos_id, "fd/");
+	dir = opendir(path);
+	if (!dir) {
+		freecon(task_context);
+		return;
+	}
+
+	pos_fd = strlen(path);	/* $PROC_ROOT/$ID/fd/ */
+
+	while ((d = readdir(dir)) != NULL) {
+		const char *pattern = "socket:[";
+		char *sock_context;
+		unsigned int ino;
+		ssize_t link_len;
+		char lnk[64];
+		int fd;
+
+		if (sscanf(d->d_name, "%d%*c", &fd) != 1)
+			continue;
+
+		snprintf(path + pos_fd, MAX_PATH_LEN - pos_fd, "%d", fd);
+
+		link_len = readlink(path, lnk, sizeof(lnk) - 1);
+		if (link_len == -1)
+			continue;
+		lnk[link_len] = '\0';
+
+		if (strncmp(lnk, pattern, strlen(pattern)))
+			continue;
+
+		if (sscanf(lnk, "socket:[%u]", &ino) != 1)
+			continue;
+
+		if (getfilecon(path, &sock_context) <= 0)
+			sock_context = strdup(no_ctx);
+
+		if (task[0] == '\0') {
+			FILE *fp;
+
+			strlcpy(stat, path, pos_id + 1);
+			snprintf(stat + pos_id, sizeof(stat) - pos_id, "stat");
+
+			fp = fopen(stat, "r");
+			if (fp) {
+				if (fscanf(fp, "%*d (%[^)])", task) < 1)
+					; /* ignore */
+				fclose(fp);
+			}
+		}
+
+		user_ent_add(ino, task, pid, fd, task_context, sock_context);
+		freecon(sock_context);
+	}
+
+	freecon(task_context);
+	closedir(dir);
+}
+
 static void user_ent_destroy(void)
 {
 	struct user_ent *p, *p_next;
@@ -589,13 +664,10 @@ static void user_ent_destroy(void)
 static void user_ent_hash_build(void)
 {
 	const char *root = getenv("PROC_ROOT") ? : "/proc/";
+	char name[MAX_PATH_LEN];
 	struct dirent *d;
-	char name[1024];
 	int nameoff;
 	DIR *dir;
-	char *pid_context;
-	char *sock_context;
-	const char *no_ctx = "unavailable";
 
 	strlcpy(name, root, sizeof(name));
 
@@ -609,71 +681,13 @@ static void user_ent_hash_build(void)
 		return;
 
 	while ((d = readdir(dir)) != NULL) {
-		struct dirent *d1;
-		char process[16];
-		int pid, pos;
-		DIR *dir1;
+		int pid;
 
 		if (sscanf(d->d_name, "%d%*c", &pid) != 1)
 			continue;
 
-		if (getpidcon(pid, &pid_context) != 0)
-			pid_context = strdup(no_ctx);
-
-		snprintf(name + nameoff, sizeof(name) - nameoff, "%d/fd/", pid);
-		pos = strlen(name);
-		dir1 = opendir(name);
-		if (!dir1) {
-			freecon(pid_context);
-			continue;
-		}
-
-		process[0] = '\0';
-
-		while ((d1 = readdir(dir1)) != NULL) {
-			const char *pattern = "socket:[";
-			unsigned int ino;
-			char lnk[64];
-			int fd;
-			ssize_t link_len;
-			char tmp[1024];
-
-			if (sscanf(d1->d_name, "%d%*c", &fd) != 1)
-				continue;
-
-			snprintf(name + pos, sizeof(name) - pos, "%d", fd);
-
-			link_len = readlink(name, lnk, sizeof(lnk) - 1);
-			if (link_len == -1)
-				continue;
-			lnk[link_len] = '\0';
-
-			if (strncmp(lnk, pattern, strlen(pattern)))
-				continue;
-
-			if (sscanf(lnk, "socket:[%u]", &ino) != 1)
-				continue;
-
-			if (getfilecon(name, &sock_context) <= 0)
-				sock_context = strdup(no_ctx);
-
-			if (process[0] == '\0') {
-				FILE *fp;
-
-				snprintf(tmp, sizeof(tmp), "%s/%d/stat", root, pid);
-
-				fp = fopen(tmp, "r");
-				if (fp) {
-					if (fscanf(fp, "%*d (%[^)])", process) < 1)
-						; /* ignore */
-					fclose(fp);
-				}
-			}
-			user_ent_add(ino, process, pid, fd, pid_context, sock_context);
-			freecon(sock_context);
-		}
-		freecon(pid_context);
-		closedir(dir1);
+		snprintf(name + nameoff, sizeof(name) - nameoff, "%d/", pid);
+		user_ent_hash_build_task(name, pid);
 	}
 	closedir(dir);
 }
Signature · 1 line
2.20.1
author
#7 lore raw

PATCH 7/7 ss: Introduce -T, --threads option

In reply to Peilin Ye

From: Peilin Ye <[email protected]>

The -p, -Z and -z options only show process (thread group leader) information. For example, if the thread group leader has exited, but another thread in the group is still using a socket, ss -[pZz] does not show it.

Add a new option, -T (--threads), to show thread information. It implies the -p option. For example, imagine process A and thread B (in the same group) using the same socket. ss -p only shows A:

  $ ss -ltp "sport = 1234"
  State   Recv-Q  Send-Q  Local Address:Port      Peer Address:Port       Process
  LISTEN  0       100           0.0.0.0:1234           0.0.0.0:*           users:(("test",pid=2932547,fd=3))

ss -T shows A and B:

  $ ss -ltT "sport = 1234"
  State   Recv-Q  Send-Q  Local Address:Port      Peer Address:Port       Process
  LISTEN  0       100           0.0.0.0:1234           0.0.0.0:*           users:(("test",pid=2932547,tid=2932548,fd=3),("test",pid=2932547,tid=2932547,fd=3))

If -T is used, -Z and -z also show SELinux contexts for threads.

Rename some variables (from "process" to "task", for example) since we use them for both processes and threads.

---
 man/man8/ss.8 |  8 ++++-
 misc/ss.c     | 94 +++++++++++++++++++++++++++++++++++----------------
 2 files changed, 72 insertions(+), 30 deletions(-)
man/man8/ss.8 +7 -1
index 68f0a699e61b..026530ed77bd 100644
@@ -155,6 +155,10 @@ the number of packets dropped before they are de-multiplexed into the socket
 .B \-p, \-\-processes
 Show process using socket.
 .TP
+.B \-T, \-\-threads
+Show thread using socket. Implies \-p.
+.BR \-p .
+.TP
 .B \-i, \-\-info
 Show internal TCP information. Below fields may appear:
 .RS
@@ -311,7 +315,9 @@ Continually display sockets as they are destroyed
 .B \-Z, \-\-context
 As the
 .B \-p
-option but also shows process security context.
+option but also shows process security context. If the
+.B \-T
+option is used, also shows thread security context.
 .sp
 For
 .BR netlink (7)
misc/ss.c +65 -29

219 lines not on this page · raw message

Signature · 1 line
2.20.1
#8 lore raw
In reply to Peilin Ye
This series was applied to iproute2/iproute2-next.git (main) by David Ahern <[email protected]>:

Hello:

This series was applied to iproute2/iproute2-next.git (main) by David Ahern <[email protected]>:

Quoted 9 lines · On Tue, 24 May 2022 19:51:15 -0700 you wrote:

From: Peilin Ye <[email protected]>

Hi all,

This patchset adds a new ss option, -T (--threads), to show thread information. It extends the -p (--processes) option, and should be useful for debugging, monitoring multi-threaded applications. Example output:

[...]

Here is the summary with links:
- [iproute2-next,1/7] ss: Use assignment-suppression character in sscanf()

    https://git.kernel.org/pub/scm/network/iproute2/iproute2-next.git/commit/?id=2d866c6d93db
  - [iproute2-next,2/7] ss: Remove unnecessary stack variable 'p' in user_ent_hash_build()
    https://git.kernel.org/pub/scm/network/iproute2/iproute2-next.git/commit/?id=b38831bc23c4
  - [iproute2-next,3/7] ss: Do not call user_ent_hash_build() more than once
    https://git.kernel.org/pub/scm/network/iproute2/iproute2-next.git/commit/?id=cd845a85681b
  - [iproute2-next,4/7] ss: Delete unnecessary call to snprintf() in user_ent_hash_build()
    https://git.kernel.org/pub/scm/network/iproute2/iproute2-next.git/commit/?id=ea3b57ec3991
  - [iproute2-next,5/7] ss: Fix coding style issues in user_ent_hash_build()
    https://git.kernel.org/pub/scm/network/iproute2/iproute2-next.git/commit/?id=210018bfe99b
  - [iproute2-next,6/7] ss: Factor out fd iterating logic from user_ent_hash_build()
    https://git.kernel.org/pub/scm/network/iproute2/iproute2-next.git/commit/?id=12d491e58ff5
  - [iproute2-next,7/7] ss: Introduce -T, --threads option
    https://git.kernel.org/pub/scm/network/iproute2/iproute2-next.git/commit/?id=e2267e68b9b5

You are awesome, thank you!

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