diff --git a/portlist/netstat_test.go b/portlist/netstat_test.go index c24f8732e..2b131331e 100644 --- a/portlist/netstat_test.go +++ b/portlist/netstat_test.go @@ -53,12 +53,12 @@ udp46 0 0 *.146 *.* func TestParsePortsNetstat(t *testing.T) { want := List{ - Port{"tcp", 23, ""}, - Port{"tcp", 24, ""}, - Port{"udp", 104, ""}, - Port{"udp", 106, ""}, - Port{"udp", 146, ""}, - Port{"tcp", 8185, ""}, // but not 8186, 8187, 8188 on localhost + Port{"tcp", 23, "", 0}, + Port{"tcp", 24, "", 0}, + Port{"udp", 104, "", 0}, + Port{"udp", 106, "", 0}, + Port{"udp", 146, "", 0}, + Port{"tcp", 8185, "", 0}, // but not 8186, 8187, 8188 on localhost } pl, err := appendParsePortsNetstat(nil, bufio.NewReader(strings.NewReader(netstatOutput))) diff --git a/portlist/portlist.go b/portlist/portlist.go index dd200d191..aa5d11330 100644 --- a/portlist/portlist.go +++ b/portlist/portlist.go @@ -18,6 +18,7 @@ type Port struct { Proto string // "tcp" or "udp" Port uint16 // port number Process string // optional process name, if found + Pid int // process id, if known } // List is a list of Ports. diff --git a/portlist/portlist_macos.go b/portlist/portlist_macos.go index 13df1c173..0bcb05bd4 100644 --- a/portlist/portlist_macos.go +++ b/portlist/portlist_macos.go @@ -11,6 +11,7 @@ import ( "fmt" "log" "os/exec" + "strconv" "strings" "sync/atomic" "time" @@ -170,6 +171,7 @@ func (im *macOSImpl) addProcesses() error { im.br.Reset(outPipe) var cmd, proto string + var pid int for { line, err := im.br.ReadBytes('\n') if err != nil { @@ -184,6 +186,10 @@ func (im *macOSImpl) addProcesses() error { // starting a new process cmd = "" proto = "" + pid = 0 + if p, err := strconv.Atoi(string(val)); err == nil { + pid = p + } case 'c': cmd = string(val) // TODO(bradfitz): avoid garbage; cache process names between runs? case 'P': @@ -202,6 +208,7 @@ func (im *macOSImpl) addProcesses() error { switch { case m != nil: m.port.Process = cmd + m.port.Pid = pid default: // ignore: processes and ports come and go }