-
Notifications
You must be signed in to change notification settings - Fork 893
Expand file tree
/
Copy pathexecutor.go
More file actions
796 lines (706 loc) · 20.6 KB
/
Copy pathexecutor.go
File metadata and controls
796 lines (706 loc) · 20.6 KB
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
package engineutil
/*
The original implementation of this is derived from:
https://github.com/dagger/dagger/internal/buildkit/blob/08180a774253a8199ebdb629d21cd9f378a14419/executor/runcexecutor/executor.go
*/
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"slices"
"strconv"
"strings"
"syscall"
"time"
"github.com/containerd/console"
runc "github.com/containerd/go-runc"
"github.com/dagger/dagger/dagql"
"github.com/dagger/dagger/engine"
"github.com/dagger/dagger/internal/buildkit/executor"
"github.com/dagger/dagger/internal/buildkit/executor/oci"
gatewayapi "github.com/dagger/dagger/internal/buildkit/frontend/gateway/pb"
randid "github.com/dagger/dagger/internal/buildkit/identity"
"github.com/dagger/dagger/internal/buildkit/solver/pb"
"github.com/dagger/dagger/internal/buildkit/util/bklog"
"github.com/dagger/dagger/internal/buildkit/util/entitlements"
"github.com/dagger/dagger/internal/buildkit/util/stack"
"github.com/dagger/dagger/util/cleanups"
"github.com/moby/sys/signal"
"github.com/opencontainers/go-digest"
"github.com/opencontainers/runtime-spec/specs-go"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"golang.org/x/sync/errgroup"
)
type ExecutionMetadata struct {
// Internal execution initiated by dagger and not the user.
// Used when executing the module runtime itself.
Internal bool
// UseRecipeIDsByDefault configures nested clients started by this exec to
// resolve id() as recipe-form IDs unless explicitly requested otherwise.
UseRecipeIDsByDefault bool
CallDigest digest.Digest
// hostname -> list of aliases
HostAliases map[string][]string
// search domains to install prior to the session's domain
ExtraSearchDomains []string
RedirectStdinPath string
RedirectStdoutPath string
RedirectStderrPath string
SecretEnvNames []string
SecretFilePaths []string
SystemEnvNames []string
EnabledGPUs []string
// If true, skip injecting dagger-init into the container.
NoInit bool
}
func (c *Client) Run(
ctx context.Context,
id string,
rootMount executor.Mount,
mounts []executor.Mount,
procInfo executor.ProcessInfo,
started chan<- struct{},
causeCtx trace.SpanContext,
execMD *ExecutionMetadata,
sessionID string,
callerClientID string,
nestedClientMetadata *engine.ClientMetadata,
nestedClientModule dagql.AnyObjectResult,
nestedClientFunctionCall dagql.Typed,
nestedClientEnv dagql.AnyObjectResult,
) (rerr error) {
if id == "" {
id = randid.NewID()
}
if err := c.validateEntitlements(procInfo.Meta); err != nil {
return err
}
state := newExecState(
id,
&procInfo,
rootMount,
mounts,
started,
causeCtx,
execMD,
sessionID,
callerClientID,
nestedClientMetadata,
nestedClientModule,
nestedClientFunctionCall,
nestedClientEnv,
)
return c.run(ctx, state,
c.setupNetwork,
c.injectInit,
c.generateBaseSpec,
c.filterEnvs,
c.setupRootfs,
c.setUserGroup,
c.setExitCodePath,
c.setupStdio,
c.setupOTel,
c.setupSecretScrubbing,
c.setProxyEnvs,
c.enableGPU,
c.createCWD,
c.setupNestedClient,
c.installCACerts,
c.runContainer,
)
}
func (c *Client) run(
ctx context.Context,
state *execState,
setupFuncs ...executorSetupFunc,
) (rerr error) {
c.runningMu.Lock()
c.running[state.id] = state
c.runningMu.Unlock()
defer func() {
c.runningMu.Lock()
delete(c.running, state.id)
c.runningMu.Unlock()
close(state.done)
if err := state.cleanups.Run(); err != nil {
bklog.G(ctx).Errorf("executor run failed to cleanup: %v", err)
rerr = errors.Join(rerr, err)
}
state.doneErr = rerr
if state.startedCh != nil {
state.startedOnce.Do(func() {
close(state.startedCh)
})
}
}()
for _, f := range setupFuncs {
if err := f(ctx, state); err != nil {
bklog.G(ctx).WithError(err).Error("executor run")
return err
}
}
return nil
}
// Namespaced is something that has Linux namespaces set up.
// Currently this is either a full-blown container or just a raw
// network namespace that's setns'd into to support service tunneling
// and similar.
type Namespaced interface {
NamespaceID() string
Release(context.Context) error
}
// NewDirectNS creates a namespace, that's externally managed.
func NewDirectNS(id string) Namespaced {
return &networkNamespace{
id: id,
cleanup: &cleanups.Cleanups{},
}
}
type networkNamespace struct {
id string
cleanup *cleanups.Cleanups
}
var _ Namespaced = (*networkNamespace)(nil)
func (n *networkNamespace) NamespaceID() string {
return n.id
}
func (n *networkNamespace) Release(_ context.Context) error {
return n.cleanup.Run()
}
func (c *Client) newNetNS(ctx context.Context, hostname string) (_ *networkNamespace, rerr error) {
provider, ok := c.NetworkProviders[pb.NetMode_UNSET] // get default CNI provider
if !ok {
return nil, fmt.Errorf("no default network provider found")
}
cleanup := &cleanups.Cleanups{}
defer func() {
if rerr != nil {
rerr = errors.Join(rerr, cleanup.Run())
}
}()
netNS, err := provider.New(ctx, hostname)
if err != nil {
return nil, fmt.Errorf("failed to create network namespace: %w", err)
}
cleanup.Add("close netns", netNS.Close)
state := &execState{
done: make(chan struct{}),
networkNamespace: netNS,
cleanups: cleanup,
}
cleanup.Add("mark run state done", cleanups.Infallible(func() {
close(state.done)
}))
id := randid.NewID()
c.runningMu.Lock()
c.running[id] = state
c.runningMu.Unlock()
cleanup.Add("delete run state", cleanups.Infallible(func() {
c.runningMu.Lock()
delete(c.running, id)
c.runningMu.Unlock()
}))
return &networkNamespace{
id: id,
cleanup: cleanup,
}, nil
}
func (c *Client) Exec(ctx context.Context, id string, process executor.ProcessInfo) (err error) {
if err := c.validateEntitlements(process.Meta); err != nil {
return err
}
// first verify the container is running, if we get an error assume the container
// is in the process of being created and check again every 100ms or until
// context is canceled.
var runcState *runc.Container
for {
c.runningMu.RLock()
execState, ok := c.running[id]
c.runningMu.RUnlock()
if !ok {
return fmt.Errorf("container %s not found", id)
}
runcState, _ = c.Runc.State(ctx, id)
if runcState != nil && runcState.Status == "running" {
break
}
select {
case <-ctx.Done():
return context.Cause(ctx)
case <-execState.done:
if execState.doneErr == nil {
return fmt.Errorf("container %s has stopped", id)
}
return fmt.Errorf("container %s has exited with error: %w", id, execState.doneErr)
case <-time.After(100 * time.Millisecond):
}
}
// load default process spec (for Env, Cwd etc) from bundle
spec := &specs.Spec{}
f, err := os.Open(filepath.Join(runcState.Bundle, "config.json"))
if err != nil {
return err
}
defer f.Close()
if err := json.NewDecoder(f).Decode(spec); err != nil {
return err
}
spec.Process.Terminal = process.Meta.Tty
spec.Process.Args = process.Meta.Args
if process.Meta.Cwd != "" {
spec.Process.Cwd = process.Meta.Cwd
}
if len(process.Meta.Env) > 0 {
spec.Process.Env = process.Meta.Env
}
if process.Meta.User != "" {
uid, gid, sgids, err := oci.GetUser(runcState.Rootfs, process.Meta.User)
if err != nil {
return err
}
spec.Process.User = specs.User{
UID: uid,
GID: gid,
AdditionalGids: sgids,
}
}
err = c.exec(ctx, id, spec.Process, process, nil)
return exitError(ctx, "", err, process.Meta.ValidExitCodes)
}
func (c *Client) exec(ctx context.Context, id string, specsProcess *specs.Process, process executor.ProcessInfo, started func()) error {
killer, err := newExecProcKiller(c.Runc, id)
if err != nil {
return fmt.Errorf("failed to initialize process killer: %w", err)
}
defer killer.Cleanup()
return c.callWithIO(ctx, &process, started, killer, func(ctx context.Context, started chan<- int, io runc.IO, pidfile string) error {
return c.Runc.Exec(ctx, id, *specsProcess, &runc.ExecOpts{
Started: started,
IO: io,
PidFile: pidfile,
})
})
}
func (c *Client) validateEntitlements(meta executor.Meta) error {
return c.Entitlements.Check(entitlements.Values{
NetworkHost: meta.NetMode == pb.NetMode_HOST,
SecurityInsecure: meta.SecurityMode == pb.SecurityMode_INSECURE,
})
}
func exitError(ctx context.Context, exitCodePath string, err error, validExitCodes []int) error {
exitErr := &gatewayapi.ExitError{ExitCode: uint32(gatewayapi.UnknownExitStatus), Err: err}
if err == nil {
exitErr.ExitCode = 0
} else {
var runcExitError *runc.ExitError
if errors.As(err, &runcExitError) {
exitErr = &gatewayapi.ExitError{ExitCode: uint32(runcExitError.Status)}
}
}
if exitCodePath != "" {
if err := os.WriteFile(exitCodePath, fmt.Appendf(nil, "%d", exitErr.ExitCode), 0o600); err != nil {
bklog.G(ctx).Errorf("failed to write exit code %d to %s: %v", exitErr.ExitCode, exitCodePath, err)
}
}
trace.SpanFromContext(ctx).AddEvent(
"Container exited",
trace.WithAttributes(attribute.Int("exit.code", int(exitErr.ExitCode))),
)
if validExitCodes == nil {
// no exit codes specified, so only 0 is allowed
if exitErr.ExitCode == 0 {
return nil
}
} else {
// exit code in allowed list, so exit cleanly
if slices.Contains(validExitCodes, int(exitErr.ExitCode)) {
return nil
}
}
select {
case <-ctx.Done():
exitErr.Err = fmt.Errorf("%s: %w", exitErr.Error(), context.Cause(ctx))
return exitErr
default:
return stack.Enable(exitErr)
}
}
type forwardIO struct {
stdin io.ReadCloser
stdout, stderr io.WriteCloser
}
func (s *forwardIO) Close() error {
return nil
}
func (s *forwardIO) Set(cmd *exec.Cmd) {
cmd.Stdin = s.stdin
cmd.Stdout = s.stdout
cmd.Stderr = s.stderr
}
func (s *forwardIO) Stdin() io.WriteCloser {
return nil
}
func (s *forwardIO) Stdout() io.ReadCloser {
return nil
}
func (s *forwardIO) Stderr() io.ReadCloser {
return nil
}
// newRunProcKiller returns an abstraction for sending SIGKILL to the
// process inside the container initiated from `runc run`.
func newRunProcKiller(runC *runc.Runc, id string) procKiller {
return procKiller{runC: runC, id: id}
}
// newExecProcKiller returns an abstraction for sending SIGKILL to the
// process inside the container initiated from `runc exec`.
func newExecProcKiller(runC *runc.Runc, id string) (procKiller, error) {
// for `runc exec` we need to create a pidfile and read it later to kill
// the process
tdir, err := os.MkdirTemp("", "runc")
if err != nil {
return procKiller{}, fmt.Errorf("failed to create directory for runc pidfile: %w", err)
}
return procKiller{
runC: runC,
id: id,
pidfile: filepath.Join(tdir, "pidfile"),
cleanup: func() {
os.RemoveAll(tdir)
},
}, nil
}
type procKiller struct {
runC *runc.Runc
id string
pidfile string
cleanup func()
}
// Cleanup will delete any tmp files created for the pidfile allocation
// if this killer was for a `runc exec` process.
func (k procKiller) Cleanup() {
if k.cleanup != nil {
k.cleanup()
}
}
// Kill will send SIGKILL to the process running inside the container.
// If the process was created by `runc run` then we will use `runc kill`,
// otherwise for `runc exec` we will read the pid from a pidfile and then
// send the signal directly that process.
func (k procKiller) Kill(ctx context.Context) (err error) {
bklog.G(ctx).Tracef("sending sigkill to process in container %s", k.id)
defer func() {
if err != nil {
bklog.G(ctx).Errorf("failed to kill process in container id %s: %+v", k.id, err)
}
}()
// this timeout is generally a no-op, the Kill ctx should already have a
// shorter timeout but here as a fail-safe for future refactoring.
ctx, cancel := context.WithCancelCause(ctx)
ctx, _ = context.WithTimeoutCause(ctx, 10*time.Second, context.DeadlineExceeded)
defer cancel(nil)
if k.pidfile == "" {
// for `runc run` process we use `runc kill` to terminate the process
return k.runC.Kill(ctx, k.id, int(syscall.SIGKILL), nil)
}
// `runc exec` will write the pidfile a few milliseconds after we
// get the runc pid via the startedCh, so we might need to retry until
// it appears in the edge case where we want to kill a process
// immediately after it was created.
var pidData []byte
for {
pidData, err = os.ReadFile(k.pidfile)
if err != nil {
if os.IsNotExist(err) {
select {
case <-ctx.Done():
return errors.New("context cancelled before runc wrote pidfile")
case <-time.After(10 * time.Millisecond):
continue
}
}
return fmt.Errorf("failed to read pidfile from runc: %w", err)
}
break
}
pid, err := strconv.Atoi(string(pidData))
if err != nil {
return fmt.Errorf("read invalid pid from pidfile: %w", err)
}
process, err := os.FindProcess(pid)
if err != nil {
// error only possible on non-unix hosts
return fmt.Errorf("failed to find process for pid %d from pidfile: %w", pid, err)
}
defer process.Release()
return process.Signal(syscall.SIGKILL)
}
// procHandle is to track the process so we can send signals to it
// and handle graceful shutdown.
type procHandle struct {
// this is for the runc process (not the process in-container)
monitorProcess *os.Process
ready chan struct{}
ended chan struct{}
shutdown func(error)
// this this only used when the request context is canceled and we need
// to kill the in-container process.
killer procKiller
}
// runcProcessHandle will create a procHandle that will be monitored, where
// on ctx.Done the in-container process will receive a SIGKILL. The returned
// context should be used for the go-runc.(Run|Exec) invocations. The returned
// context will only be canceled in the case where the request context is
// canceled and we are unable to send the SIGKILL to the in-container process.
// The goal is to allow for runc to gracefully shutdown when the request context
// is cancelled.
func runcProcessHandle(ctx context.Context, killer procKiller) (*procHandle, context.Context) {
runcCtx, cancel := context.WithCancelCause(context.Background())
p := &procHandle{
ready: make(chan struct{}),
ended: make(chan struct{}),
shutdown: cancel,
killer: killer,
}
// preserve the logger on the context used for the runc process handling
runcCtx = bklog.WithLogger(runcCtx, bklog.G(ctx))
go func() {
// Wait for pid
select {
case <-ctx.Done():
return // nothing to kill
case <-p.ready:
}
for {
select {
case <-ctx.Done():
killCtx, timeout := context.WithCancelCause(context.Background())
killCtx, _ = context.WithTimeoutCause(killCtx, 7*time.Second, context.DeadlineExceeded)
if err := p.killer.Kill(killCtx); err != nil {
// If kill fails with "container not running", the container is already dead
// Short-circuit to prevent infinite loop
if strings.Contains(err.Error(), "container not running") {
bklog.G(ctx).Debug("container already dead, stopping kill loop")
cancel(context.Cause(ctx))
timeout(context.Canceled)
return
}
select {
case <-killCtx.Done():
cancel(context.Cause(ctx))
timeout(context.Canceled)
return
default:
}
}
timeout(context.Canceled)
select {
case <-time.After(50 * time.Millisecond):
case <-p.ended:
return
}
case <-p.ended:
return
}
}
}()
return p, runcCtx
}
// Release will free resources with a procHandle.
func (p *procHandle) Release() {
close(p.ended)
if p.monitorProcess != nil {
p.monitorProcess.Release()
}
}
// Shutdown should be called after the runc process has exited. This will allow
// the signal handling and tty resize loops to exit, terminating the
// goroutines.
func (p *procHandle) Shutdown() {
if p.shutdown != nil {
p.shutdown(context.Canceled)
}
}
// WaitForReady will wait until we have received the runc pid via the go-runc
// Started channel, or until the request context is canceled. This should
// return without errors before attempting to send signals to the runc process.
func (p *procHandle) WaitForReady(ctx context.Context) error {
select {
case <-ctx.Done():
return context.Cause(ctx)
case <-p.ready:
return nil
}
}
// WaitForStart will record the runc pid reported by go-runc via the channel.
// We wait for up to 10s for the runc pid to be reported. If the started
// callback is non-nil it will be called after receiving the pid.
func (p *procHandle) WaitForStart(ctx context.Context, startedCh <-chan int, started func()) error {
ctx, cancel := context.WithCancelCause(ctx)
ctx, _ = context.WithTimeoutCause(ctx, 10*time.Second, context.DeadlineExceeded)
defer cancel(context.Canceled)
select {
case <-ctx.Done():
return errors.New("go-runc started message never received")
case runcPid, ok := <-startedCh:
if !ok {
return errors.New("go-runc failed to send pid")
}
if started != nil {
started()
}
var err error
p.monitorProcess, err = os.FindProcess(runcPid)
if err != nil {
// error only possible on non-unix hosts
return fmt.Errorf("failed to find runc process %d: %w", runcPid, err)
}
close(p.ready)
}
return nil
}
// handleSignals will wait until the procHandle is ready then will
// send each signal received on the channel to the runc process (not directly
// to the in-container process)
func handleSignals(ctx context.Context, runcProcess *procHandle, signals <-chan syscall.Signal) error {
if signals == nil {
return nil
}
err := runcProcess.WaitForReady(ctx)
if err != nil {
return err
}
for {
select {
case <-ctx.Done():
return nil
case sig := <-signals:
if sig == syscall.SIGKILL {
// never send SIGKILL directly to runc, it needs to go to the
// process in-container
if err := runcProcess.killer.Kill(ctx); err != nil {
return err
}
continue
}
if err := runcProcess.monitorProcess.Signal(sig); err != nil {
bklog.G(ctx).Errorf("failed to signal %s to process: %s", sig, err)
return err
}
}
}
}
type runcCall func(ctx context.Context, started chan<- int, io runc.IO, pidfile string) error
func (c *Client) callWithIO(ctx context.Context, process *executor.ProcessInfo, started func(), killer procKiller, call runcCall) error {
runcProcess, ctx := runcProcessHandle(ctx, killer)
defer runcProcess.Release()
eg, ctx := errgroup.WithContext(ctx)
defer func() {
if err := eg.Wait(); err != nil && !errors.Is(err, context.Canceled) {
bklog.G(ctx).Errorf("runc process monitoring error: %s", err)
}
}()
defer runcProcess.Shutdown()
startedCh := make(chan int, 1)
eg.Go(func() error {
return runcProcess.WaitForStart(ctx, startedCh, started)
})
eg.Go(func() error {
return handleSignals(ctx, runcProcess, process.Signal)
})
if !process.Meta.Tty {
return call(ctx, startedCh, &forwardIO{stdin: process.Stdin, stdout: process.Stdout, stderr: process.Stderr}, killer.pidfile)
}
ptm, ptsName, err := console.NewPty()
if err != nil {
return err
}
pts, err := os.OpenFile(ptsName, os.O_RDWR|syscall.O_NOCTTY, 0)
if err != nil {
ptm.Close()
return err
}
defer func() {
if process.Stdin != nil {
process.Stdin.Close()
}
pts.Close()
ptm.Close()
runcProcess.Shutdown()
err := eg.Wait()
if err != nil {
bklog.G(ctx).Warningf("error while shutting down tty io: %s", err)
}
}()
if process.Stdin != nil {
eg.Go(func() error {
_, err := io.Copy(ptm, process.Stdin)
// stdin might be a pipe, so this is like EOF
if errors.Is(err, io.ErrClosedPipe) {
return nil
}
return err
})
}
if process.Stdout != nil {
eg.Go(func() error {
_, err := io.Copy(process.Stdout, ptm)
// ignore `read /dev/ptmx: input/output error` when ptm is closed
var ptmClosedError *os.PathError
if errors.As(err, &ptmClosedError) {
if ptmClosedError.Op == "read" &&
ptmClosedError.Path == "/dev/ptmx" &&
ptmClosedError.Err == syscall.EIO {
return nil
}
}
return err
})
}
eg.Go(func() error {
err := runcProcess.WaitForReady(ctx)
if err != nil {
return err
}
for {
select {
case <-ctx.Done():
return nil
case resize := <-process.Resize:
err = ptm.Resize(console.WinSize{
Height: uint16(resize.Rows),
Width: uint16(resize.Cols),
})
if err != nil {
bklog.G(ctx).Errorf("failed to resize ptm: %s", err)
}
// SIGWINCH must be sent to the runc monitor process, as
// terminal resizing is done in runc.
err = runcProcess.monitorProcess.Signal(signal.SIGWINCH)
if err != nil {
bklog.G(ctx).Errorf("failed to send SIGWINCH to process: %s", err)
}
}
}
})
runcIO := &forwardIO{}
if process.Stdin != nil {
runcIO.stdin = pts
}
if process.Stdout != nil {
runcIO.stdout = pts
}
if process.Stderr != nil {
runcIO.stderr = pts
}
return call(ctx, startedCh, runcIO, killer.pidfile)
}
type nopCloser struct {
io.Writer
}
func (nopCloser) Close() error { return nil }