mirror of
https://github.com/moby/buildkit.git
synced 2026-08-05 07:10:23 +00:00
Merge pull request #1731 from coryb/issue-1714
add tty support for runc executor
This commit is contained in:
@@ -272,10 +272,7 @@ func (w *runcExecutor) Run(ctx context.Context, id string, root cache.Mountable,
|
||||
}
|
||||
}
|
||||
|
||||
if meta.Tty {
|
||||
return errors.New("tty with runc not implemented")
|
||||
}
|
||||
|
||||
spec.Process.Terminal = meta.Tty
|
||||
spec.Process.OOMScoreAdj = w.oomScoreAdj
|
||||
if w.rootless {
|
||||
if err := rootlessspecconv.ToRootless(spec); err != nil {
|
||||
@@ -326,10 +323,8 @@ func (w *runcExecutor) Run(ctx context.Context, id string, root cache.Mountable,
|
||||
close(started)
|
||||
})
|
||||
}
|
||||
status, err := w.runc.Run(runCtx, id, bundle, &runc.CreateOpts{
|
||||
IO: &forwardIO{stdin: process.Stdin, stdout: process.Stdout, stderr: process.Stderr},
|
||||
NoPivot: w.noPivot,
|
||||
})
|
||||
|
||||
status, err := w.run(runCtx, id, bundle, process)
|
||||
close(ended)
|
||||
|
||||
if status != 0 || err != nil {
|
||||
@@ -413,21 +408,14 @@ func (w *runcExecutor) Exec(ctx context.Context, id string, process executor.Pro
|
||||
spec.Process.Env = process.Meta.Env
|
||||
}
|
||||
|
||||
err = w.runc.Exec(ctx, id, *spec.Process, &runc.ExecOpts{
|
||||
IO: &forwardIO{stdin: process.Stdin, stdout: process.Stdout, stderr: process.Stderr},
|
||||
})
|
||||
|
||||
var exitError *exec.ExitError
|
||||
if errors.As(err, &exitError) {
|
||||
err = &errdefs.ExitError{
|
||||
ExitCode: uint32(exitError.ExitCode()),
|
||||
Err: err,
|
||||
}
|
||||
return err
|
||||
} else if err != nil {
|
||||
return err
|
||||
status, err := w.exec(ctx, id, state.Bundle, spec.Process, process)
|
||||
if status == 0 && err == nil {
|
||||
return nil
|
||||
}
|
||||
return &errdefs.ExitError{
|
||||
ExitCode: uint32(status),
|
||||
Err: err,
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type forwardIO struct {
|
||||
|
||||
44
executor/runcexecutor/executor_common.go
Normal file
44
executor/runcexecutor/executor_common.go
Normal file
@@ -0,0 +1,44 @@
|
||||
// +build !linux
|
||||
|
||||
package runcexecutor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os/exec"
|
||||
|
||||
"github.com/containerd/containerd"
|
||||
runc "github.com/containerd/go-runc"
|
||||
"github.com/moby/buildkit/executor"
|
||||
"github.com/opencontainers/runtime-spec/specs-go"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
var unsupportedConsoleError = errors.New("tty for runc is only supported on linux")
|
||||
|
||||
func (w *runcExecutor) run(ctx context.Context, id, bundle string, process executor.ProcessInfo) (int, error) {
|
||||
if process.Meta.Tty {
|
||||
return 0, unsupportedConsoleError
|
||||
}
|
||||
return w.runc.Run(ctx, id, bundle, &runc.CreateOpts{
|
||||
IO: &forwardIO{stdin: process.Stdin, stdout: process.Stdout, stderr: process.Stderr},
|
||||
NoPivot: w.noPivot,
|
||||
})
|
||||
}
|
||||
|
||||
func (w *runcExecutor) exec(ctx context.Context, id, bundle string, specsProcess *specs.Process, process executor.ProcessInfo) (int, error) {
|
||||
if process.Meta.Tty {
|
||||
return 0, unsupportedConsoleError
|
||||
}
|
||||
err := w.runc.Exec(ctx, id, *specsProcess, &runc.ExecOpts{
|
||||
IO: &forwardIO{stdin: process.Stdin, stdout: process.Stdout, stderr: process.Stderr},
|
||||
})
|
||||
|
||||
var exitError *exec.ExitError
|
||||
if errors.As(err, &exitError) {
|
||||
return exitError.ExitCode(), err
|
||||
}
|
||||
if err != nil {
|
||||
return containerd.UnknownExitStatus, err
|
||||
}
|
||||
return 0, nil
|
||||
}
|
||||
206
executor/runcexecutor/executor_linux.go
Normal file
206
executor/runcexecutor/executor_linux.go
Normal file
@@ -0,0 +1,206 @@
|
||||
package runcexecutor
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/containerd/console"
|
||||
"github.com/containerd/containerd"
|
||||
runc "github.com/containerd/go-runc"
|
||||
"github.com/docker/docker/pkg/signal"
|
||||
"github.com/moby/buildkit/executor"
|
||||
"github.com/opencontainers/runtime-spec/specs-go"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sirupsen/logrus"
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
func (w *runcExecutor) run(ctx context.Context, id, bundle string, process executor.ProcessInfo) (int, error) {
|
||||
return w.callWithIO(ctx, id, bundle, process, func(ctx context.Context, pidfile string, io runc.IO) (int, error) {
|
||||
return w.runc.Run(ctx, id, bundle, &runc.CreateOpts{
|
||||
NoPivot: w.noPivot,
|
||||
PidFile: pidfile,
|
||||
IO: io,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func (w *runcExecutor) exec(ctx context.Context, id, bundle string, specsProcess *specs.Process, process executor.ProcessInfo) (int, error) {
|
||||
return w.callWithIO(ctx, id, bundle, process, func(ctx context.Context, pidfile string, io runc.IO) (int, error) {
|
||||
err := w.runc.Exec(ctx, id, *specsProcess, &runc.ExecOpts{
|
||||
PidFile: pidfile,
|
||||
IO: io,
|
||||
})
|
||||
var exitError *exec.ExitError
|
||||
if errors.As(err, &exitError) {
|
||||
return exitError.ExitCode(), err
|
||||
}
|
||||
if err != nil {
|
||||
return containerd.UnknownExitStatus, err
|
||||
}
|
||||
return 0, nil
|
||||
})
|
||||
}
|
||||
|
||||
type runcCall func(ctx context.Context, pidfile string, io runc.IO) (int, error)
|
||||
|
||||
func (w *runcExecutor) callWithIO(ctx context.Context, id, bundle string, process executor.ProcessInfo, call runcCall) (int, error) {
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
|
||||
pidfile, err := ioutil.TempFile(bundle, "*.pid")
|
||||
if err != nil {
|
||||
return containerd.UnknownExitStatus, errors.Wrap(err, "failed to create pidfile")
|
||||
}
|
||||
defer os.Remove(pidfile.Name())
|
||||
pidfile.Close()
|
||||
|
||||
if !process.Meta.Tty {
|
||||
return call(ctx, pidfile.Name(), &forwardIO{stdin: process.Stdin, stdout: process.Stdout, stderr: process.Stderr})
|
||||
}
|
||||
|
||||
ptm, ptsName, err := console.NewPty()
|
||||
if err != nil {
|
||||
return containerd.UnknownExitStatus, err
|
||||
}
|
||||
|
||||
pts, err := os.OpenFile(ptsName, os.O_RDWR|syscall.O_NOCTTY, 0)
|
||||
if err != nil {
|
||||
ptm.Close()
|
||||
return containerd.UnknownExitStatus, err
|
||||
}
|
||||
|
||||
eg, ctx := errgroup.WithContext(ctx)
|
||||
|
||||
defer func() {
|
||||
if process.Stdin != nil {
|
||||
process.Stdin.Close()
|
||||
}
|
||||
pts.Close()
|
||||
ptm.Close()
|
||||
cancel() // this will shutdown resize loop
|
||||
err := eg.Wait()
|
||||
if err != nil {
|
||||
logrus.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 {
|
||||
// need to poll until the pidfile has the pid written to it
|
||||
pidfileCtx, timeout := context.WithTimeout(ctx, 10*time.Second)
|
||||
defer timeout()
|
||||
|
||||
var runcProcess *os.Process
|
||||
for {
|
||||
st, err := os.Stat(pidfile.Name())
|
||||
if err == nil && st.Size() > 0 {
|
||||
pid, err := runc.ReadPidFile(pidfile.Name())
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "unable to read pid file: %s", pidfile.Name())
|
||||
}
|
||||
// pid will be for the process in process.Meta, not the parent runc process.
|
||||
// We need to send SIGWINCH to the runc process, not the process.Meta process.
|
||||
ppid, err := getppid(pid)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "unable to find runc process (parent of %d)", pid)
|
||||
}
|
||||
runcProcess, err = os.FindProcess(ppid)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "unable to find process for pid %d", ppid)
|
||||
}
|
||||
break
|
||||
}
|
||||
select {
|
||||
case <-pidfileCtx.Done():
|
||||
return errors.New("pidfile never updated")
|
||||
case <-time.After(100 * time.Microsecond):
|
||||
}
|
||||
}
|
||||
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 {
|
||||
logrus.Errorf("failed to resize ptm: %s", err)
|
||||
}
|
||||
err = runcProcess.Signal(signal.SIGWINCH)
|
||||
if err != nil {
|
||||
logrus.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, pidfile.Name(), runcIO)
|
||||
}
|
||||
|
||||
const PPidStatusPrefix = "PPid:\t"
|
||||
|
||||
func getppid(pid int) (int, error) {
|
||||
fh, err := os.Open(fmt.Sprintf("/proc/%d/status", pid))
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
defer fh.Close()
|
||||
scanner := bufio.NewScanner(fh)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if strings.HasPrefix(line, PPidStatusPrefix) {
|
||||
return strconv.Atoi(strings.TrimPrefix(line, PPidStatusPrefix))
|
||||
}
|
||||
}
|
||||
return -1, errors.Errorf("PPid line not found in /proc/%d/status", pid)
|
||||
}
|
||||
Reference in New Issue
Block a user