-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
pty_windows.go
164 lines (137 loc) · 3.67 KB
/
pty_windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//go:build windows
// +build windows
package pty
import (
"context"
"errors"
"fmt"
"os"
"sync"
"unsafe"
"golang.org/x/sys/windows"
)
const (
_PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE = 0x20016 // nolint:revive
)
var (
errClosedConPty = errors.New("pseudo console is closed")
errNotStarted = errors.New("process not started")
)
// conPty is a Windows console pseudo-terminal.
// It uses Windows pseudo console API to create a console that can be used to
// start processes attached to it.
//
// See: https://docs.microsoft.com/en-us/windows/console/creating-a-pseudoconsole-session
type conPty struct {
handle windows.Handle
inPipe, outPipe *os.File
mtx sync.RWMutex
}
var _ Pty = &conPty{}
func newPty() (ConPty, error) {
ptyIn, inPipeOurs, err := os.Pipe()
if err != nil {
return nil, fmt.Errorf("failed to create pipes for pseudo console: %w", err)
}
outPipeOurs, ptyOut, err := os.Pipe()
if err != nil {
return nil, fmt.Errorf("failed to create pipes for pseudo console: %w", err)
}
var hpc windows.Handle
coord := windows.Coord{X: 80, Y: 25}
err = windows.CreatePseudoConsole(coord, windows.Handle(ptyIn.Fd()), windows.Handle(ptyOut.Fd()), 0, &hpc)
if err != nil {
return nil, fmt.Errorf("failed to create pseudo console: %w", err)
}
if err := ptyOut.Close(); err != nil {
return nil, fmt.Errorf("failed to close pseudo console handle: %w", err)
}
if err := ptyIn.Close(); err != nil {
return nil, fmt.Errorf("failed to close pseudo console handle: %w", err)
}
return &conPty{
handle: hpc,
inPipe: inPipeOurs,
outPipe: outPipeOurs,
}, nil
}
// Close implements Pty.
func (p *conPty) Close() error {
p.mtx.Lock()
defer p.mtx.Unlock()
windows.ClosePseudoConsole(p.handle)
return errors.Join(p.inPipe.Close(), p.outPipe.Close())
}
// Command implements Pty.
func (p *conPty) Command(name string, args ...string) *Cmd {
c := &Cmd{
pty: p,
Path: name,
Args: append([]string{name}, args...),
}
return c
}
// CommandContext implements Pty.
func (p *conPty) CommandContext(ctx context.Context, name string, args ...string) *Cmd {
if ctx == nil {
panic("nil context")
}
c := p.Command(name, args...)
c.ctx = ctx
c.Cancel = func() error {
return c.Process.Kill()
}
return c
}
// Name implements Pty.
func (*conPty) Name() string {
return "windows-pty"
}
// Read implements Pty.
func (p *conPty) Read(b []byte) (n int, err error) {
return p.outPipe.Read(b)
}
// Resize implements Pty.
func (p *conPty) Resize(width int, height int) error {
p.mtx.RLock()
defer p.mtx.RUnlock()
if err := windows.ResizePseudoConsole(p.handle, windows.Coord{X: int16(width), Y: int16(height)}); err != nil {
return fmt.Errorf("failed to resize pseudo console: %w", err)
}
return nil
}
// Write implements Pty.
func (p *conPty) Write(b []byte) (n int, err error) {
return p.inPipe.Write(b)
}
// Fd implements Pty.
func (p *conPty) Fd() uintptr {
p.mtx.RLock()
defer p.mtx.RUnlock()
return uintptr(p.handle)
}
// InputPipe implements ConPty.
func (p *conPty) InputPipe() *os.File {
return p.inPipe
}
// OutputPipe implements ConPty.
func (p *conPty) OutputPipe() *os.File {
return p.outPipe
}
// updateProcThreadAttribute updates the passed in attribute list to contain the entry necessary for use with
// CreateProcess.
func (p *conPty) updateProcThreadAttribute(attrList *windows.ProcThreadAttributeListContainer) error {
p.mtx.RLock()
defer p.mtx.RUnlock()
if p.handle == 0 {
return errClosedConPty
}
if err := attrList.Update(
_PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE,
unsafe.Pointer(p.handle),
unsafe.Sizeof(p.handle),
); err != nil {
return fmt.Errorf("failed to update proc thread attributes for pseudo console: %w", err)
}
return nil
}