1 // Copyright 2020 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 //go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
6 // +build darwin dragonfly freebsd linux netbsd openbsd solaris
14 exec "golang.org/x/sys/execabs"
22 "golang.org/x/xerrors"
26 startRemote = startRemotePosix
27 autoNetworkAddress = autoNetworkAddressPosix
28 verifyRemoteOwnership = verifyRemoteOwnershipPosix
31 func startRemotePosix(goplsPath string, args ...string) error {
32 cmd := exec.Command(goplsPath, args...)
33 cmd.SysProcAttr = &syscall.SysProcAttr{
36 if err := cmd.Start(); err != nil {
37 return xerrors.Errorf("starting remote gopls: %w", err)
42 // autoNetworkAddress resolves an id on the 'auto' pseduo-network to a
43 // real network and address. On unix, this uses unix domain sockets.
44 func autoNetworkAddressPosix(goplsPath, id string) (network string, address string) {
45 // Especially when doing local development or testing, it's important that
46 // the remote gopls instance we connect to is running the same binary as our
47 // forwarder. So we encode a short hash of the binary path into the daemon
48 // socket name. If possible, we also include the buildid in this hash, to
49 // account for long-running processes where the binary has been subsequently
52 cmd := exec.Command("go", "tool", "buildid", goplsPath)
55 if err := cmd.Run(); err == nil {
58 log.Printf("error getting current buildid: %v", err)
59 sum := sha256.Sum256([]byte(goplsPath))
62 shortHash := fmt.Sprintf("%x", pathHash)[:6]
63 user := os.Getenv("USER")
67 basename := filepath.Base(goplsPath)
70 idComponent = "-" + id
72 runtimeDir := os.TempDir()
73 if xdg := os.Getenv("XDG_RUNTIME_DIR"); xdg != "" {
76 return "unix", filepath.Join(runtimeDir, fmt.Sprintf("%s-%s-daemon.%s%s", basename, shortHash, user, idComponent))
79 func verifyRemoteOwnershipPosix(network, address string) (bool, error) {
80 if network != "unix" {
83 fi, err := os.Stat(address)
85 if os.IsNotExist(err) {
88 return false, xerrors.Errorf("checking socket owner: %w", err)
90 stat, ok := fi.Sys().(*syscall.Stat_t)
92 return false, errors.New("fi.Sys() is not a Stat_t")
94 user, err := user.Current()
96 return false, xerrors.Errorf("checking current user: %w", err)
98 uid, err := strconv.ParseUint(user.Uid, 10, 32)
100 return false, xerrors.Errorf("parsing current UID: %w", err)
102 return stat.Uid == uint32(uid), nil