add support for pid flag

This commit is contained in:
Doug MacEachern 2006-06-20 22:26:10 +00:00
parent a0de608f82
commit 6f35415499
1 changed files with 32 additions and 2 deletions

View File

@ -21,10 +21,11 @@ public class Netstat extends SigarCommandBase {
"Proto", "Proto",
"Local Address", "Local Address",
"Foreign Address", "Foreign Address",
"State" "State",
""
}; };
private static boolean isNumeric; private static boolean isNumeric, wantPid;
public Netstat(Shell shell) { public Netstat(Shell shell) {
super(shell); super(shell);
@ -46,6 +47,7 @@ public class Netstat extends SigarCommandBase {
public static int getFlags(String[] args, int flags) { public static int getFlags(String[] args, int flags) {
int proto_flags = 0; int proto_flags = 0;
isNumeric = false; isNumeric = false;
wantPid = false;
for (int i=0; i<args.length; i++) { for (int i=0; i<args.length; i++) {
String arg = args[i]; String arg = args[i];
@ -65,6 +67,9 @@ public class Netstat extends SigarCommandBase {
case 'n': case 'n':
isNumeric = true; isNumeric = true;
break; break;
case 'p':
wantPid = true;
break;
case 't': case 't':
proto_flags |= NetFlags.CONN_TCP; proto_flags |= NetFlags.CONN_TCP;
break; break;
@ -167,6 +172,31 @@ public class Netstat extends SigarCommandBase {
conn.getRemotePort(), conn.getRemotePort(),
RADDR_LEN)); RADDR_LEN));
items.add(state); items.add(state);
String process = null;
if (wantPid &&
//XXX only works w/ listen ports
(conn.getState() == NetFlags.TCP_LISTEN))
{
try {
long pid =
this.sigar.getProcPort(conn.getType(),
conn.getLocalPort());
if (pid != 0) { //XXX another bug
String name =
this.sigar.getProcState(pid).getName();
process = pid + "/" + name;
}
} catch (SigarException e) {
}
}
if (process == null) {
process = "";
}
items.add(process);
printf(items); printf(items);
} }
} }