@@ -15,12 +15,16 @@ func getListeningPortProcessCmdline(port uint32) (string, error) {
15
15
acceptFn := func (s * netstat.SockTabEntry ) bool {
16
16
return s .LocalAddr != nil && uint32 (s .LocalAddr .Port ) == port
17
17
}
18
- tabs , err := netstat .TCPSocks (acceptFn )
18
+ tabs , err4 := netstat .TCPSocks (acceptFn )
19
19
tabs6 , err6 := netstat .TCP6Socks (acceptFn )
20
20
21
- // Only return the error if the other method found nothing.
22
- if (err != nil && len (tabs6 ) == 0 ) || (err6 != nil && len (tabs ) == 0 ) {
23
- return "" , xerrors .Errorf ("inspect port %d: %w" , port , errors .Join (err , err6 ))
21
+ // In the common case, we want to check ipv4 listening addresses. If this
22
+ // fails, we should return an error. We also need to check ipv6. The
23
+ // assumption is, if we have an err4, and 0 ipv6 addresses listed, then we are
24
+ // interested in the err4 (and vice versa). So return both errors (at least 1
25
+ // is non-nil) if the other list is empty.
26
+ if (err4 != nil && len (tabs6 ) == 0 ) || (err6 != nil && len (tabs ) == 0 ) {
27
+ return "" , xerrors .Errorf ("inspect port %d: %w" , port , errors .Join (err4 , err6 ))
24
28
}
25
29
26
30
var proc * netstat.Process
@@ -41,7 +45,7 @@ func getListeningPortProcessCmdline(port uint32) (string, error) {
41
45
pid := proc .Pid
42
46
data , err := os .ReadFile (fmt .Sprintf ("/proc/%d/cmdline" , pid ))
43
47
if err != nil {
44
- return "" , xerrors .Errorf ("read /proc/%d/cmdline: %w" , pid , err )
48
+ return "" , xerrors .Errorf ("read /proc/%d/cmdline: %w" , pid , err4 )
45
49
}
46
50
return string (data ), nil
47
51
}
0 commit comments