-
Notifications
You must be signed in to change notification settings - Fork 33
/
command_virtual.go
306 lines (248 loc) · 6.37 KB
/
command_virtual.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package command
import (
"context"
"io"
"os"
"os/exec"
"reflect"
"sync"
"syscall"
"github.com/creack/pty"
"github.com/pkg/errors"
"go.uber.org/multierr"
"go.uber.org/zap"
)
type VirtualCommand struct {
cfg *Config
opts Options
// cmd is populated when the command is started.
cmd *exec.Cmd
// stdin is [VirtualCommandOptions.Stdin] wrapped in readCloser.
stdin io.ReadCloser
cleanFuncs []func() error
pty *os.File
tty *os.File
wg sync.WaitGroup // watch goroutines copying I/O
mu sync.Mutex // protect err
err error
}
var _ Command = (*VirtualCommand)(nil)
func NewVirtual(cfg *Config, opts Options) *VirtualCommand {
var stdin io.ReadCloser
if opts.Stdin != nil {
stdin = &readCloser{r: opts.Stdin, done: make(chan struct{})}
}
// Stdout must be set, otherwise the command will hang trying to copy data from pty.
if opts.Stdout == nil {
opts.Stdout = io.Discard
}
if opts.Kernel == nil {
opts.Kernel = NewLocalKernel(nil)
}
if opts.Logger == nil {
opts.Logger = zap.NewNop()
}
return &VirtualCommand{
cfg: cfg,
opts: opts,
stdin: stdin,
}
}
func (c *VirtualCommand) Running() bool {
return c.cmd != nil && c.cmd.ProcessState == nil
}
func (c *VirtualCommand) Pid() int {
if c.cmd == nil || c.cmd.Process == nil {
return 0
}
return c.cmd.Process.Pid
}
func (c *VirtualCommand) Start(ctx context.Context) (err error) {
cfg, cleanups, err := normalizeConfig(
c.cfg,
newPathNormalizer(c.opts.Kernel),
modeNormalizer,
newArgsNormalizer(c.opts.Session, c.opts.Logger),
newEnvNormalizer(c.opts.Kernel, c.opts.Session.GetEnv),
)
if err != nil {
return
}
c.cleanFuncs = append(c.cleanFuncs, cleanups...)
c.pty, c.tty, err = pty.Open()
if err != nil {
return errors.WithStack(err)
}
if err := disableEcho(c.tty.Fd()); err != nil {
return err
}
c.cmd = exec.CommandContext(
ctx,
cfg.ProgramName,
cfg.Arguments...,
)
c.cmd.Dir = cfg.Directory
c.cmd.Env = cfg.Env
c.cmd.Stdin = c.tty
c.cmd.Stdout = c.tty
c.cmd.Stderr = c.tty
setSysProcAttrCtty(c.cmd)
c.opts.Logger.Info("starting a virtual command", zap.Any("config", redactConfig(cfg)))
if err := c.cmd.Start(); err != nil {
return errors.WithStack(err)
}
if !isNil(c.stdin) {
c.wg.Add(1)
go func() {
defer c.wg.Done()
n, err := io.Copy(c.pty, c.stdin)
c.opts.Logger.Info("finished copying from stdin to pty", zap.Error(err), zap.Int64("count", n))
if err != nil {
c.setErr(errors.WithStack(err))
}
}()
}
if !isNil(c.opts.Stdout) {
c.wg.Add(1)
go func() {
defer c.wg.Done()
n, err := io.Copy(c.opts.Stdout, c.pty)
if err != nil {
// Linux kernel returns EIO when attempting to read from
// a master pseudo-terminal which no longer has an open slave.
// See https://github.com/creack/pty/issues/21.
if errors.Is(err, syscall.EIO) {
c.opts.Logger.Debug("failed to copy from pty to stdout; handled EIO")
return
}
if errors.Is(err, os.ErrClosed) {
c.opts.Logger.Debug("failed to copy from pty to stdout; handled ErrClosed")
return
}
c.opts.Logger.Info("failed to copy from pty to stdout", zap.Error(err))
c.setErr(errors.WithStack(err))
} else {
c.opts.Logger.Debug("finished copying from pty to stdout", zap.Int64("count", n))
}
}()
}
c.opts.Logger.Info("a virtual command started")
return nil
}
func (c *VirtualCommand) Signal(sig os.Signal) error {
c.opts.Logger.Info("stopping the virtual command with signal", zap.String("signal", sig.String()))
// Try to terminate the whole process group. If it fails, fall back to stdlib methods.
if err := signalPgid(c.cmd.Process.Pid, sig); err != nil {
c.opts.Logger.Info("failed to terminate process group; trying Process.Signal()", zap.Error(err))
if err := c.cmd.Process.Signal(sig); err != nil {
c.opts.Logger.Info("failed to signal process; trying Process.Kill()", zap.Error(err))
return errors.WithStack(c.cmd.Process.Kill())
}
}
return nil
}
func (c *VirtualCommand) Wait() (err error) {
c.opts.Logger.Info("waiting for the virtual command to finish")
defer func() {
errC := errors.WithStack(c.cleanup())
c.opts.Logger.Info("cleaned up the virtual command", zap.Error(errC))
if err == nil && errC != nil {
err = errC
}
}()
err = errors.WithStack(c.cmd.Wait())
c.opts.Logger.Info("the virtual command finished", zap.Error(err))
errIO := c.closeIO()
c.opts.Logger.Info("closed IO of the virtual command", zap.Error(errIO))
if err == nil && errIO != nil {
err = errIO
}
c.wg.Wait()
c.mu.Lock()
if err == nil && c.err != nil {
err = c.err
}
c.mu.Unlock()
return
}
func (c *VirtualCommand) setErr(err error) {
if err == nil {
return
}
c.mu.Lock()
if c.err == nil {
c.err = err
}
c.mu.Unlock()
}
func (c *VirtualCommand) closeIO() (err error) {
if !isNil(c.stdin) {
if errClose := c.stdin.Close(); errClose != nil {
err = multierr.Append(err, errors.WithMessage(errClose, "failed to close stdin"))
}
}
if errClose := c.tty.Close(); errClose != nil {
err = multierr.Append(err, errors.WithMessage(errClose, "failed to close tty"))
}
// if err := c.pty.Close(); err != nil {
// return errors.WithMessage(err, "failed to close pty")
// }
return
}
func (c *VirtualCommand) cleanup() (err error) {
for _, fn := range c.cleanFuncs {
if errFn := fn(); errFn != nil {
err = multierr.Append(err, errFn)
}
}
return
}
type Winsize pty.Winsize
func SetWinsize(cmd *VirtualCommand, winsize *Winsize) error {
if cmd.pty == nil {
return nil
}
err := pty.Setsize(cmd.pty, (*pty.Winsize)(winsize))
return errors.WithStack(err)
}
func isNil(val any) bool {
if val == nil {
return true
}
v := reflect.ValueOf(val)
if v.Type().Kind() == reflect.Struct {
return false
}
return reflect.ValueOf(val).IsNil()
}
// readCloser wraps [io.Reader] into [io.ReadCloser].
//
// When Close is called, the underlying read operation is ignored.
// It might discard some read data, or read might hang indefinitely.
// It is caller's responsibility to interrupt the underlying [io.Reader]
// when [VirtualCommand] exits.
type readCloser struct {
r io.Reader
done chan struct{}
}
func (r *readCloser) Read(p []byte) (int, error) {
var (
n int
err error
)
readc := make(chan struct{})
go func() {
n, err = r.r.Read(p)
close(readc)
}()
select {
case <-readc:
return n, err
case <-r.done:
return 0, io.EOF
}
}
func (r *readCloser) Close() error {
close(r.done)
return nil
}