portlist: reduce log spam/scariness for portlist in mac sandbox

Fixes tailscale/corp#235
reviewable/pr373/r3
Brad Fitzpatrick 5 years ago
parent bfdc8175b1
commit 64f6104e63

@ -13,6 +13,7 @@ import (
"log" "log"
"os" "os"
"strings" "strings"
"sync/atomic"
"time" "time"
exec "tailscale.com/tempfork/osexec" exec "tailscale.com/tempfork/osexec"
@ -25,6 +26,8 @@ func listPorts() (List, error) {
return listPortsNetstat("-na") return listPortsNetstat("-na")
} }
var lsofFailed int64 // atomic bool
// In theory, lsof could replace the function of both listPorts() and // In theory, lsof could replace the function of both listPorts() and
// addProcesses(), since it provides a superset of the netstat output. // addProcesses(), since it provides a superset of the netstat output.
// However, "netstat -na" runs ~100x faster than lsof on my machine, so // However, "netstat -na" runs ~100x faster than lsof on my machine, so
@ -33,19 +36,24 @@ func listPorts() (List, error) {
// TODO(apenwarr): this fails in a macOS sandbox (ie. our usual case). // TODO(apenwarr): this fails in a macOS sandbox (ie. our usual case).
// We might as well just delete this code if we can't find a solution. // We might as well just delete this code if we can't find a solution.
func addProcesses(pl []Port) ([]Port, error) { func addProcesses(pl []Port) ([]Port, error) {
if atomic.LoadInt64(&lsofFailed) != 0 {
// This previously failed in the macOS sandbox, so don't try again.
return pl, nil
}
exe, err := exec.LookPath("lsof") exe, err := exec.LookPath("lsof")
if err != nil { if err != nil {
return nil, fmt.Errorf("lsof: lookup: %v", err) return nil, fmt.Errorf("lsof: lookup: %v", err)
} }
output, err := exec.Command(exe, "-F", "-n", "-P", "-O", "-S2", "-T", "-i4", "-i6").Output() output, err := exec.Command(exe, "-F", "-n", "-P", "-O", "-S2", "-T", "-i4", "-i6").Output()
if err != nil { if err != nil {
xe, ok := err.(*exec.ExitError) var stderr []byte
stderr := "" if xe, ok := err.(*exec.ExitError); ok {
if ok { stderr = xe.Stderr
stderr = strings.TrimSpace(string(xe.Stderr))
} }
// fails when run in a macOS sandbox, so make this non-fatal. // fails when run in a macOS sandbox, so make this non-fatal.
log.Printf("portlist: lsof: %v (%q)\n", err, stderr) if atomic.CompareAndSwapInt64(&lsofFailed, 0, 1) {
log.Printf("portlist: can't run lsof in Mac sandbox; omitting process names from service list. Error details: %v, %s", err, bytes.TrimSpace(stderr))
}
return pl, nil return pl, nil
} }

Loading…
Cancel
Save