mirror of
https://github.com/tailscale/tailscale.git
synced 2024-11-29 04:55:31 +00:00
46ce80758d
In prep for reducing garbage, being able to reuse memory. So far this doesn't actually reuse much. This is just changing signatures around. But some improvement in any case: bradfitz@tsdev:~/src/tailscale.com$ ~/go/bin/benchstat before after name old time/op new time/op delta GetList-8 11.8ms ± 9% 9.9ms ± 3% -15.98% (p=0.000 n=10+10) name old alloc/op new alloc/op delta GetList-8 99.5kB ± 2% 91.9kB ± 0% -7.62% (p=0.000 n=9+9) name old allocs/op new allocs/op delta GetList-8 3.05k ± 1% 2.93k ± 0% -3.83% (p=0.000 n=8+9) More later, once parsers can reuse strings from previous parses. Updates #5958 Change-Id: I76cd5048246dd24d11c4e263d8bb8041747fb2b0 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
45 lines
1.1 KiB
Go
45 lines
1.1 KiB
Go
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
//go:build (windows || freebsd || openbsd || darwin) && !ios && !js
|
|
// +build windows freebsd openbsd darwin
|
|
// +build !ios
|
|
// +build !js
|
|
|
|
package portlist
|
|
|
|
import (
|
|
"fmt"
|
|
"os/exec"
|
|
"strings"
|
|
)
|
|
|
|
var osHideWindow func(*exec.Cmd) // non-nil on Windows; see portlist_windows.go
|
|
|
|
// hideWindow returns c. On Windows it first sets SysProcAttr.HideWindow.
|
|
func hideWindow(c *exec.Cmd) *exec.Cmd {
|
|
if osHideWindow != nil {
|
|
osHideWindow(c)
|
|
}
|
|
return c
|
|
}
|
|
|
|
func appendListeningPortsNetstat(base []Port, arg string) ([]Port, error) {
|
|
exe, err := exec.LookPath("netstat")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("netstat: lookup: %v", err)
|
|
}
|
|
output, err := hideWindow(exec.Command(exe, arg)).Output()
|
|
if err != nil {
|
|
xe, ok := err.(*exec.ExitError)
|
|
stderr := ""
|
|
if ok {
|
|
stderr = strings.TrimSpace(string(xe.Stderr))
|
|
}
|
|
return nil, fmt.Errorf("netstat: %v (%q)", err, stderr)
|
|
}
|
|
|
|
return appendParsePortsNetstat(base, string(output)), nil
|
|
}
|