`netstat -ntp` odd 'Program name' presentation

Cristian Ionescu-Idbohrn cristian.ionescu-idbohrn at axis.com
Sun Apr 22 15:13:51 UTC 2012


On Sat, 21 Apr 2012, Cristian Ionescu-Idbohrn wrote:
>
> The 'Program name' part is fetched from /proc/<pid>/cmdline, then mangled
> before presentation.  Mangling is done in netstat.c, function 'dir_act'.
> The function assumes cmdline is a file path and extracts the basename.
> Obviously that assumption is not always correct.  It's not unusual a
> cmdline looks like this:
>
> 	sshd: foo at pts/48
>
> or like this:
>
> 	sshd: foo [priv]
>
> Function 'bb_basename' uses strrchr everything up to and including the
> last occurrence och character '/'.  The result is (see first example
> above):
>
> 	PID/Program name
> 	12345/48
>
> or (second example):
>
> 	PID/Program name
> 	12345/sshd: foo [priv]
>
> Neither is desirable.
> I'd like to propose a different way to do that:
>
> 1. strip everything after and including the first whitespace character
> 2. maybe remove even odd ':' character at the end of the string
> 3. call bb_basename after that
>
> Output would then look like:
>
> 	PID/Program name
> 	12345/sshd
>
> which is what one would expect.
>
> Thoughts?

Would this fit in somewhere?

[PATCH] Mangle 'Program name' to show the right thing.

Signed-off-by: Cristian Ionescu-Idbohrn <cristian.ionescu-idbohrn at axis.com>
---
 networking/netstat.c |    8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/networking/netstat.c b/networking/netstat.c
index 9c23957..31131cd 100644
--- a/networking/netstat.c
+++ b/networking/netstat.c
@@ -288,6 +288,14 @@ static int FAST_FUNC dir_act(const char *fileName,
 	if (n < 0)
 		return FALSE;
 	cmdline_buf[n] = '\0';
+	{
+		char *cp = strchr(cmdline_buf, ' ');
+		if (cp)
+			*cp = '\0';
+		cp = strrchr(cmdline_buf, ':');
+		if (cp)
+			*cp = '\0';
+	}

 	/* go through all files in /proc/PID/fd and check whether they are sockets */
 	strcpy(proc_pid_fname + len - (sizeof("cmdline")-1), "fd");
-- 
1.7.10


Cheers,

-- 
Cristian


More information about the busybox mailing list