[Toybox] [PATCH] Fix netstat -p.

enh enh at google.com
Tue Sep 8 19:19:04 PDT 2015


On Tue, Sep 8, 2015 at 10:53 AM, Rob Landley <rob at landley.net> wrote:
> On 09/08/2015 11:17 AM, enh wrote:
>> On Tue, Sep 8, 2015 at 8:36 AM, Rob Landley <rob at landley.net> wrote:
>>> On 09/07/2015 07:16 PM, enh wrote:
>>>> aw, man. i thought that -- in addition to not mangling tabs -- the old
>>>> gmail UI had the advantage of not introducing wordwrap. i guess i
>>>> should give up on inline diffs and just always send attachments
>>>> instead...
>>>>
>>>> anyway, attached.
>>>
>>> Blah, this is the same command I just applied the other patch to. Not
>>> awake yet. (Up until 3:30am massaging a stack of "hg export" files until
>>> I figured out I needed to start over with "hg export --git" files
>>> because hg doesn't export binary files otherwise.)
>>>
>>> I'll fix it up.
>>>
>>> Thanks,
>>>
>>> Rob
>>
>> yeah, the other patch looks sensible but is wrong. (that was what i
>> tried first too :-) )
>
> Basically what I told the other committer, but I'm trying to be more
> encouraging to other developers and less of a bottleneck.
> (I still totally should have named this project dorodango.)

want to commit the other lsof patch then? :-)

> Besides, "fixed it for them" is valuable, and the point of the pending
> directory is so I'm not going to forget to fix it properly if I ever
> make it that far down my todo list. :)

fwiw, people definitely use netstat on Android and this was the first
bug report i've had. (and they were only trying to use netstat -p
because lsof couldn't decode sockets, which is now fixed too.)

that said, i did just notice that -e doesn't work right (it doesn't
add the extra user/inode columns to the tcp output), and non-wide mode
(i.e. without -W) doesn't truncate IP addresses. patch attached.


> Rob



-- 
Elliott Hughes - http://who/enh - http://jessies.org/~enh/
Android native code/tools questions? Mail me/drop by/add me as a reviewer.
-------------- next part --------------
Fix netstat -e.

The -e flag should add "user" and "inode" columns to the TCP output.

Also truncate IP addresses in non-wide (no -W) mode.

This also removes a bit more of the duplication, though there's still
plenty left in this file!

diff --git a/toys/pending/netstat.c b/toys/pending/netstat.c
index 98db54d..735f1b1 100644
--- a/toys/pending/netstat.c
+++ b/toys/pending/netstat.c
@@ -121,11 +121,16 @@ static const char *get_pid_name(unsigned long inode)
 /*
  * For TCP/UDP/RAW display data.
  */
-static void display_data(unsigned rport, char *label, unsigned rxq, unsigned txq, char *lip, char *rip, unsigned state, unsigned long inode)
+static void display_data(unsigned rport, char *label,
+                         unsigned rxq, unsigned txq, char *lip, char *rip,
+                         unsigned state, unsigned uid, unsigned long inode)
 {
   char *ss_state = "UNKNOWN", buf[12];
   char *state_label[] = {"", "ESTABLISHED", "SYN_SENT", "SYN_RECV", "FIN_WAIT1", "FIN_WAIT2",
   		                 "TIME_WAIT", "CLOSE", "CLOSE_WAIT", "LAST_ACK", "LISTEN", "CLOSING", "UNKNOWN"};
+  char user[11];
+  struct passwd *pw;
+
   if (!strcmp(label, "tcp")) {
     int sz = ARRAY_LEN(state_label);
     if (!state || state >= sz) state = sz-1;
@@ -137,24 +142,29 @@ static void display_data(unsigned rport, char *label, unsigned rxq, unsigned txq
   }
   else if (!strcmp(label, "raw")) sprintf(ss_state = buf, "%u", state);
 
-  if ( (toys.optflags & FLAG_W) && (toys.optflags & FLAG_p))
-    xprintf("%3s   %6d %6d %-51s %-51s %-12s%s\n", label, rxq, txq, lip, rip, ss_state, get_pid_name(inode));
-  else if (toys.optflags & FLAG_W)
-    xprintf("%3s   %6d %6d %-51s %-51s %-12s\n", label, rxq, txq, lip, rip, ss_state);
-  else if (toys.optflags & FLAG_p)
-    xprintf("%3s   %6d %6d %-23s %-23s %-12s%s\n", label, rxq, txq, lip, rip, ss_state, get_pid_name(inode));
-  else xprintf("%3s   %6d %6d %-23s %-23s %-12s\n", label, rxq, txq, lip, rip, ss_state);
+  if (!(toys.optflags & FLAG_n) && (pw = getpwuid(uid))) {
+    snprintf(user, sizeof(user), "%s", pw->pw_name);
+  } else snprintf(user, sizeof(user), "%d", uid);
+
+  xprintf("%3s   %6d %6d ", label, rxq, txq);
+  xprintf((toys.optflags & FLAG_W) ? "%-51.51s %-51.51s " : "%-23.23s %-23.23s ", lip, rip);
+  xprintf("%-11s ", ss_state);
+  if ((toys.optflags & FLAG_e)) xprintf("%-10s %-11d ", user, inode);
+  if ((toys.optflags & FLAG_p)) xprintf("%s", get_pid_name(inode));
+  xputc('\n');
 }
 /*
  * For TCP/UDP/RAW show data.
  */
-static void show_data(unsigned rport, char *label, unsigned rxq, unsigned txq, char *lip, char *rip, unsigned state, unsigned long inode)
+static void show_data(unsigned rport, char *label, unsigned rxq, unsigned txq,
+                      char *lip, char *rip, unsigned state, unsigned uid,
+                      unsigned long inode)
 {
   if (toys.optflags & FLAG_l) {
-    if (!rport && (state && 0xA)) display_data(rport, label, rxq, txq, lip, rip, state, inode);
-  } else if (toys.optflags & FLAG_a) display_data(rport, label, rxq, txq, lip, rip, state, inode);
+    if (!rport && (state && 0xA)) display_data(rport, label, rxq, txq, lip, rip, state, uid, inode);
+  } else if (toys.optflags & FLAG_a) display_data(rport, label, rxq, txq, lip, rip, state, uid, inode);
   //rport && (TCP | UDP | RAW)
-  else if (rport && (0x10 | 0x20 | 0x40)) display_data(rport, label, rxq, txq, lip, rip, state, inode);
+  else if (rport && (0x10 | 0x20 | 0x40)) display_data(rport, label, rxq, txq, lip, rip, state, uid, inode);
 }
 /*
  * used to get service name.
@@ -240,7 +250,7 @@ static void show_ipv4(char *fname, char *label)
     if (nitems == 10) {
       addr2str(AF_INET, &laddr, lport, lip, label);
       addr2str(AF_INET, &raddr, rport, rip, label);
-      show_data(rport, label, rxq, txq, lip, rip, state, inode);
+      show_data(rport, label, rxq, txq, lip, rip, state, uid, inode);
     }
   }//End of While
   fclose(fp);
@@ -267,7 +277,7 @@ static void show_ipv6(char *fname, char *label)
     if (nitems == 16) {
       addr2str(AF_INET6, &laddr6, lport, lip, label);
       addr2str(AF_INET6, &raddr6, rport, rip, label);
-      show_data(rport, label, rxq, txq, lip, rip, state, inode);
+      show_data(rport, label, rxq, txq, lip, rip, state, uid, inode);
     }
   }//End of While
   fclose(fp);
@@ -433,13 +443,13 @@ static void clean_pid_list(void)
  */
 static void show_header(void)
 {
-  if ((toys.optflags & FLAG_W) && (toys.optflags & FLAG_p))
-    xprintf("\nProto Recv-Q Send-Q %-51s %-51s %-12s%s\n", "Local Address", "Foreign Address", "State", "PID/Program Name");
-  else if (toys.optflags & FLAG_p)
-    xprintf("\nProto Recv-Q Send-Q %-23s %-23s %-12s%s\n", "Local Address", "Foreign Address", "State", "PID/Program Name");
-  else if (toys.optflags & FLAG_W)
-	  xprintf("\nProto Recv-Q Send-Q %-51s %-51s State     \n", "Local Address", "Foreign Address");
-  else xprintf("\nProto Recv-Q Send-Q %-23s %-23s State     \n", "Local Address", "Foreign Address");
+  xprintf("\nProto Recv-Q Send-Q ");
+  xprintf((toys.optflags & FLAG_W) ? "%-51s %-51s" : "%-23s %-23s",
+          "Local Address", "Foreign Address");
+  xprintf(" State      ");
+  if (toys.optflags & FLAG_e) xprintf(" User       Inode      ");
+  if (toys.optflags & FLAG_p) xprintf(" PID/Program Name");
+  xputc('\n');
 }
 /*
  * used to get the flag values for route command.


More information about the Toybox mailing list