Debian Codegraph @

conpty/exec_windows.go

raw
1 //go:build windows
2 // +build windows
3
4 package conpty
5
6 import (
7 "os"
8 "os/exec"
9 "path/filepath"
10 "strings"
11 "syscall"
12 "unicode/utf16"
13 "unsafe"
14
15 "golang.org/x/sys/windows"
16 )
17
18 // Below are a bunch of helpers for working with Windows' CreateProcess family
19 // of functions. These are mostly exact copies of the same utilities found in
20 // the go stdlib.
21
22 func lookExtensions(path, dir string) (string, error) {
23 if filepath.Base(path) == path {
24 path = filepath.Join(".", path)
25 }
26
27 if dir == "" {
28 return exec.LookPath(path)
29 }
30
31 if filepath.VolumeName(path) != "" {
32 return exec.LookPath(path)
33 }
34
35 if len(path) > 1 && os.IsPathSeparator(path[0]) {
36 return exec.LookPath(path)
37 }
38
39 dirandpath := filepath.Join(dir, path)
40
41 // We assume that LookPath will only add file extension.
42 lp, err := exec.LookPath(dirandpath)
43 if err != nil {
44 return "", err
45 }
46
47 ext := strings.TrimPrefix(lp, dirandpath)
48
49 return path + ext, nil
50 }
51
52 func execEnvDefault(sys *syscall.SysProcAttr) (env []string, err error) {
53 if sys == nil || sys.Token == 0 {
54 return syscall.Environ(), nil
55 }
56
57 var block *uint16
58 err = windows.CreateEnvironmentBlock(&block, windows.Token(sys.Token), false)
59 if err != nil {
60 return nil, err
61 }
62
63 defer windows.DestroyEnvironmentBlock(block)
64 blockp := uintptr(unsafe.Pointer(block))
65
66 for {
67 // find NUL terminator
68 end := unsafe.Pointer(blockp)
69 for *(*uint16)(end) != 0 {
70 end = unsafe.Pointer(uintptr(end) + 2)
71 }
72
73 n := (uintptr(end) - uintptr(unsafe.Pointer(blockp))) / 2
74 if n == 0 {
75 // environment block ends with empty string
76 break
77 }
78
79 entry := (*[(1 << 30) - 1]uint16)(unsafe.Pointer(blockp))[:n:n]
80 env = append(env, string(utf16.Decode(entry)))
81 blockp += 2 * (uintptr(len(entry)) + 1)
82 }
83 return env, err
84 }
85
86 func isSlash(c uint8) bool {
87 return c == '\\' || c == '/'
88 }
89
90 func normalizeDir(dir string) (name string, err error) {
91 ndir, err := syscall.FullPath(dir)
92 if err != nil {
93 return "", err
94 }
95 if len(ndir) > 2 && isSlash(ndir[0]) && isSlash(ndir[1]) {
96 // dir cannot have \\server\share\path form
97 return "", syscall.EINVAL
98 }
99 return ndir, nil
100 }
101
102 func volToUpper(ch int) int {
103 if 'a' <= ch && ch <= 'z' {
104 ch += 'A' - 'a'
105 }
106 return ch
107 }
108
109 func joinExeDirAndFName(dir, p string) (name string, err error) {
110 if len(p) == 0 {
111 return "", syscall.EINVAL
112 }
113 if len(p) > 2 && isSlash(p[0]) && isSlash(p[1]) {
114 // \\server\share\path form
115 return p, nil
116 }
117 if len(p) > 1 && p[1] == ':' {
118 // has drive letter
119 if len(p) == 2 {
120 return "", syscall.EINVAL
121 }
122 if isSlash(p[2]) {
123 return p, nil
124 } else {
125 d, err := normalizeDir(dir)
126 if err != nil {
127 return "", err
128 }
129 if volToUpper(int(p[0])) == volToUpper(int(d[0])) {
130 return syscall.FullPath(d + "\\" + p[2:])
131 } else {
132 return syscall.FullPath(p)
133 }
134 }
135 } else {
136 // no drive letter
137 d, err := normalizeDir(dir)
138 if err != nil {
139 return "", err
140 }
141 if isSlash(p[0]) {
142 return windows.FullPath(d[:2] + p)
143 } else {
144 return windows.FullPath(d + "\\" + p)
145 }
146 }
147 }
148
149 // createEnvBlock converts an array of environment strings into
150 // the representation required by CreateProcess: a sequence of NUL
151 // terminated strings followed by a nil.
152 // Last bytes are two UCS-2 NULs, or four NUL bytes.
153 func createEnvBlock(envv []string) *uint16 {
154 if len(envv) == 0 {
155 return &utf16.Encode([]rune("\x00\x00"))[0]
156 }
157 length := 0
158 for _, s := range envv {
159 length += len(s) + 1
160 }
161 length++
162
163 b := make([]byte, length)
164 i := 0
165 for _, s := range envv {
166 l := len(s)
167 copy(b[i:i+l], []byte(s))
168 copy(b[i+l:i+l+1], []byte{0})
169 i = i + l + 1
170 }
171 copy(b[i:i+1], []byte{0})
172
173 return &utf16.Encode([]rune(string(b)))[0]
174 }
175
176 // dedupEnvCase is dedupEnv with a case option for testing.
177 // If caseInsensitive is true, the case of keys is ignored.
178 func dedupEnvCase(caseInsensitive bool, env []string) []string {
179 out := make([]string, 0, len(env))
180 saw := make(map[string]int, len(env)) // key => index into out
181 for _, kv := range env {
182 eq := strings.Index(kv, "=")
183 if eq < 0 {
184 out = append(out, kv)
185 continue
186 }
187 k := kv[:eq]
188 if caseInsensitive {
189 k = strings.ToLower(k)
190 }
191 if dupIdx, isDup := saw[k]; isDup {
192 out[dupIdx] = kv
193 continue
194 }
195 saw[k] = len(out)
196 out = append(out, kv)
197 }
198 return out
199 }
200
201 // addCriticalEnv adds any critical environment variables that are required
202 // (or at least almost always required) on the operating system.
203 // Currently this is only used for Windows.
204 func addCriticalEnv(env []string) []string {
205 for _, kv := range env {
206 eq := strings.Index(kv, "=")
207 if eq < 0 {
208 continue
209 }
210 k := kv[:eq]
211 if strings.EqualFold(k, "SYSTEMROOT") {
212 // We already have it.
213 return env
214 }
215 }
216 return append(env, "SYSTEMROOT="+os.Getenv("SYSTEMROOT"))
217 }