2020-10-11 19:35:22 +00:00
|
|
|
package runcexecutor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/containerd/console"
|
|
|
|
runc "github.com/containerd/go-runc"
|
|
|
|
"github.com/moby/buildkit/executor"
|
2021-07-09 07:48:03 +00:00
|
|
|
"github.com/moby/buildkit/util/bklog"
|
2021-07-27 11:48:11 +00:00
|
|
|
"github.com/moby/sys/signal"
|
2020-10-11 19:35:22 +00:00
|
|
|
"github.com/opencontainers/runtime-spec/specs-go"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
)
|
|
|
|
|
2020-10-19 23:47:00 +00:00
|
|
|
func updateRuncFieldsForHostOS(runtime *runc.Runc) {
|
|
|
|
// PdeathSignal only supported on unix platforms
|
|
|
|
runtime.PdeathSignal = syscall.SIGKILL // this can still leak the process
|
|
|
|
}
|
|
|
|
|
2022-01-31 20:05:17 +00:00
|
|
|
func (w *runcExecutor) run(ctx context.Context, id, bundle string, process executor.ProcessInfo, started func()) error {
|
|
|
|
return w.callWithIO(ctx, id, bundle, process, started, func(ctx context.Context, started chan<- int, io runc.IO) error {
|
2020-10-19 23:47:00 +00:00
|
|
|
_, err := w.runc.Run(ctx, id, bundle, &runc.CreateOpts{
|
2020-10-11 19:35:22 +00:00
|
|
|
NoPivot: w.noPivot,
|
2020-10-21 15:59:17 +00:00
|
|
|
Started: started,
|
2020-10-11 19:35:22 +00:00
|
|
|
IO: io,
|
|
|
|
})
|
2020-10-19 23:47:00 +00:00
|
|
|
return err
|
2020-10-11 19:35:22 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-31 20:05:17 +00:00
|
|
|
func (w *runcExecutor) exec(ctx context.Context, id, bundle string, specsProcess *specs.Process, process executor.ProcessInfo, started func()) error {
|
|
|
|
return w.callWithIO(ctx, id, bundle, process, started, func(ctx context.Context, started chan<- int, io runc.IO) error {
|
2020-10-19 23:47:00 +00:00
|
|
|
return w.runc.Exec(ctx, id, *specsProcess, &runc.ExecOpts{
|
2020-10-21 15:59:17 +00:00
|
|
|
Started: started,
|
2020-10-11 19:35:22 +00:00
|
|
|
IO: io,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-21 15:59:17 +00:00
|
|
|
type runcCall func(ctx context.Context, started chan<- int, io runc.IO) error
|
2020-10-11 19:35:22 +00:00
|
|
|
|
2022-01-31 20:05:17 +00:00
|
|
|
func (w *runcExecutor) callWithIO(ctx context.Context, id, bundle string, process executor.ProcessInfo, started func(), call runcCall) error {
|
|
|
|
runcProcess := &startingProcess{
|
|
|
|
ready: make(chan struct{}),
|
|
|
|
}
|
|
|
|
defer runcProcess.Release()
|
|
|
|
|
|
|
|
var eg errgroup.Group
|
|
|
|
egCtx, cancel := context.WithCancel(ctx)
|
|
|
|
defer eg.Wait()
|
2020-10-11 19:35:22 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2022-01-31 20:05:17 +00:00
|
|
|
startedCh := make(chan int, 1)
|
|
|
|
eg.Go(func() error {
|
|
|
|
return runcProcess.WaitForStart(egCtx, startedCh, started)
|
|
|
|
})
|
|
|
|
|
|
|
|
eg.Go(func() error {
|
|
|
|
return handleSignals(egCtx, runcProcess, process.Signal)
|
|
|
|
})
|
|
|
|
|
2020-10-11 19:35:22 +00:00
|
|
|
if !process.Meta.Tty {
|
2022-01-31 20:05:17 +00:00
|
|
|
return call(ctx, startedCh, &forwardIO{stdin: process.Stdin, stdout: process.Stdout, stderr: process.Stderr})
|
2020-10-11 19:35:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ptm, ptsName, err := console.NewPty()
|
|
|
|
if err != nil {
|
2020-10-19 23:47:00 +00:00
|
|
|
return err
|
2020-10-11 19:35:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pts, err := os.OpenFile(ptsName, os.O_RDWR|syscall.O_NOCTTY, 0)
|
|
|
|
if err != nil {
|
|
|
|
ptm.Close()
|
2020-10-19 23:47:00 +00:00
|
|
|
return err
|
2020-10-11 19:35:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if process.Stdin != nil {
|
|
|
|
process.Stdin.Close()
|
|
|
|
}
|
|
|
|
pts.Close()
|
|
|
|
ptm.Close()
|
2022-01-31 20:05:17 +00:00
|
|
|
cancel() // this will shutdown resize and signal loops
|
2020-10-11 19:35:22 +00:00
|
|
|
err := eg.Wait()
|
|
|
|
if err != nil {
|
2021-07-09 07:48:03 +00:00
|
|
|
bklog.G(ctx).Warningf("error while shutting down tty io: %s", err)
|
2020-10-11 19:35:22 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
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" &&
|
2020-10-19 21:28:39 +00:00
|
|
|
ptmClosedError.Err == syscall.EIO {
|
2020-10-11 19:35:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
eg.Go(func() error {
|
2022-01-31 20:05:17 +00:00
|
|
|
err := runcProcess.WaitForReady(egCtx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-10-11 19:35:22 +00:00
|
|
|
}
|
|
|
|
for {
|
|
|
|
select {
|
2022-01-31 20:05:17 +00:00
|
|
|
case <-egCtx.Done():
|
2020-10-11 19:35:22 +00:00
|
|
|
return nil
|
|
|
|
case resize := <-process.Resize:
|
|
|
|
err = ptm.Resize(console.WinSize{
|
|
|
|
Height: uint16(resize.Rows),
|
|
|
|
Width: uint16(resize.Cols),
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-07-09 07:48:03 +00:00
|
|
|
bklog.G(ctx).Errorf("failed to resize ptm: %s", err)
|
2020-10-11 19:35:22 +00:00
|
|
|
}
|
2022-01-31 20:05:17 +00:00
|
|
|
err = runcProcess.Process.Signal(signal.SIGWINCH)
|
2020-10-11 19:35:22 +00:00
|
|
|
if err != nil {
|
2021-07-09 07:48:03 +00:00
|
|
|
bklog.G(ctx).Errorf("failed to send SIGWINCH to process: %s", err)
|
2020-10-11 19:35:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
runcIO := &forwardIO{}
|
|
|
|
if process.Stdin != nil {
|
|
|
|
runcIO.stdin = pts
|
|
|
|
}
|
|
|
|
if process.Stdout != nil {
|
|
|
|
runcIO.stdout = pts
|
|
|
|
}
|
|
|
|
if process.Stderr != nil {
|
|
|
|
runcIO.stderr = pts
|
|
|
|
}
|
|
|
|
|
2022-01-31 20:05:17 +00:00
|
|
|
return call(ctx, startedCh, runcIO)
|
2020-10-11 19:35:22 +00:00
|
|
|
}
|