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 // +build darwin dragonfly freebsd linux netbsd openbsd solaris
21 "golang.org/x/xerrors"
25 startRemote = startRemotePosix
26 autoNetworkAddress = autoNetworkAddressPosix
27 verifyRemoteOwnership = verifyRemoteOwnershipPosix
30 func startRemotePosix(goplsPath string, args ...string) error {
31 cmd := exec.Command(goplsPath, args...)
32 cmd.SysProcAttr = &syscall.SysProcAttr{
35 if err := cmd.Start(); err != nil {
36 return xerrors.Errorf("starting remote gopls: %w", err)
41 // autoNetworkAddress resolves an id on the 'auto' pseduo-network to a
42 // real network and address. On unix, this uses unix domain sockets.
43 func autoNetworkAddressPosix(goplsPath, id string) (network string, address string) {
44 // Especially when doing local development or testing, it's important that
45 // the remote gopls instance we connect to is running the same binary as our
46 // forwarder. So we encode a short hash of the binary path into the daemon
47 // socket name. If possible, we also include the buildid in this hash, to
48 // account for long-running processes where the binary has been subsequently
51 cmd := exec.Command("go", "tool", "buildid", goplsPath)
54 if err := cmd.Run(); err == nil {
57 log.Printf("error getting current buildid: %v", err)
58 sum := sha256.Sum256([]byte(goplsPath))
61 shortHash := fmt.Sprintf("%x", pathHash)[:6]
62 user := os.Getenv("USER")
66 basename := filepath.Base(goplsPath)
69 idComponent = "-" + id
71 return "unix", filepath.Join(os.TempDir(), fmt.Sprintf("%s-%s-daemon.%s%s", basename, shortHash, user, idComponent))
74 func verifyRemoteOwnershipPosix(network, address string) (bool, error) {
75 if network != "unix" {
78 fi, err := os.Stat(address)
80 if os.IsNotExist(err) {
83 return false, xerrors.Errorf("checking socket owner: %w", err)
85 stat, ok := fi.Sys().(*syscall.Stat_t)
87 return false, errors.New("fi.Sys() is not a Stat_t")
89 user, err := user.Current()
91 return false, xerrors.Errorf("checking current user: %w", err)
93 uid, err := strconv.ParseUint(user.Uid, 10, 32)
95 return false, xerrors.Errorf("parsing current UID: %w", err)
97 return stat.Uid == uint32(uid), nil