Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools@v0.0.0-20201028153306-37f0764111ff / internal / lsp / lsprpc / autostart_posix.go
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.
4
5 // +build darwin dragonfly freebsd linux netbsd openbsd solaris
6
7 package lsprpc
8
9 import (
10         "crypto/sha256"
11         "errors"
12         "fmt"
13         "log"
14         "os"
15         "os/exec"
16         "os/user"
17         "path/filepath"
18         "strconv"
19         "syscall"
20
21         "golang.org/x/xerrors"
22 )
23
24 func init() {
25         startRemote = startRemotePosix
26         autoNetworkAddress = autoNetworkAddressPosix
27         verifyRemoteOwnership = verifyRemoteOwnershipPosix
28 }
29
30 func startRemotePosix(goplsPath string, args ...string) error {
31         cmd := exec.Command(goplsPath, args...)
32         cmd.SysProcAttr = &syscall.SysProcAttr{
33                 Setsid: true,
34         }
35         if err := cmd.Start(); err != nil {
36                 return xerrors.Errorf("starting remote gopls: %w", err)
37         }
38         return nil
39 }
40
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
49         // rebuilt.
50         h := sha256.New()
51         cmd := exec.Command("go", "tool", "buildid", goplsPath)
52         cmd.Stdout = h
53         var pathHash []byte
54         if err := cmd.Run(); err == nil {
55                 pathHash = h.Sum(nil)
56         } else {
57                 log.Printf("error getting current buildid: %v", err)
58                 sum := sha256.Sum256([]byte(goplsPath))
59                 pathHash = sum[:]
60         }
61         shortHash := fmt.Sprintf("%x", pathHash)[:6]
62         user := os.Getenv("USER")
63         if user == "" {
64                 user = "shared"
65         }
66         basename := filepath.Base(goplsPath)
67         idComponent := ""
68         if id != "" {
69                 idComponent = "-" + id
70         }
71         return "unix", filepath.Join(os.TempDir(), fmt.Sprintf("%s-%s-daemon.%s%s", basename, shortHash, user, idComponent))
72 }
73
74 func verifyRemoteOwnershipPosix(network, address string) (bool, error) {
75         if network != "unix" {
76                 return true, nil
77         }
78         fi, err := os.Stat(address)
79         if err != nil {
80                 if os.IsNotExist(err) {
81                         return true, nil
82                 }
83                 return false, xerrors.Errorf("checking socket owner: %w", err)
84         }
85         stat, ok := fi.Sys().(*syscall.Stat_t)
86         if !ok {
87                 return false, errors.New("fi.Sys() is not a Stat_t")
88         }
89         user, err := user.Current()
90         if err != nil {
91                 return false, xerrors.Errorf("checking current user: %w", err)
92         }
93         uid, err := strconv.ParseUint(user.Uid, 10, 32)
94         if err != nil {
95                 return false, xerrors.Errorf("parsing current UID: %w", err)
96         }
97         return stat.Uid == uint32(uid), nil
98 }