diff --git a/cmd/ctr/run.go b/cmd/ctr/run.go index 95d129dc2..f2c7002d7 100644 --- a/cmd/ctr/run.go +++ b/cmd/ctr/run.go @@ -5,11 +5,8 @@ import ( "encoding/json" "fmt" "os" - "os/signal" "runtime" - "golang.org/x/sys/unix" - "google.golang.org/grpc" "google.golang.org/grpc/codes" @@ -192,39 +189,3 @@ var runCommand = cli.Command{ return nil }, } - -func handleConsoleResize(ctx gocontext.Context, service execution.ContainerServiceClient, id string, pid uint32, con console.Console) error { - // do an initial resize of the console - size, err := con.Size() - if err != nil { - return err - } - if _, err := service.Pty(ctx, &execution.PtyRequest{ - ID: id, - Pid: pid, - Width: uint32(size.Width), - Height: uint32(size.Height), - }); err != nil { - return err - } - s := make(chan os.Signal, 16) - signal.Notify(s, unix.SIGWINCH) - go func() { - for range s { - size, err := con.Size() - if err != nil { - logrus.WithError(err).Error("get pty size") - continue - } - if _, err := service.Pty(ctx, &execution.PtyRequest{ - ID: id, - Pid: pid, - Width: uint32(size.Width), - Height: uint32(size.Height), - }); err != nil { - logrus.WithError(err).Error("resize pty") - } - } - }() - return nil -} diff --git a/cmd/ctr/run_unix.go b/cmd/ctr/run_unix.go index b9bdd2280..fc2c91004 100644 --- a/cmd/ctr/run_unix.go +++ b/cmd/ctr/run_unix.go @@ -3,16 +3,22 @@ package main import ( + "context" "encoding/json" "fmt" "io/ioutil" + "os" + "os/signal" "path/filepath" "runtime" "strconv" "strings" + "golang.org/x/sys/unix" + "github.com/Sirupsen/logrus" "github.com/containerd/containerd/api/services/execution" + "github.com/crosbymichael/console" protobuf "github.com/gogo/protobuf/types" ocispec "github.com/opencontainers/image-spec/specs-go/v1" specs "github.com/opencontainers/runtime-spec/specs-go" @@ -262,3 +268,39 @@ func newCreateRequest(context *cli.Context, imageConfig *ocispec.ImageConfig, id return create, nil } + +func handleConsoleResize(ctx context.Context, service execution.ContainerServiceClient, id string, pid uint32, con console.Console) error { + // do an initial resize of the console + size, err := con.Size() + if err != nil { + return err + } + if _, err := service.Pty(ctx, &execution.PtyRequest{ + ID: id, + Pid: pid, + Width: uint32(size.Width), + Height: uint32(size.Height), + }); err != nil { + return err + } + s := make(chan os.Signal, 16) + signal.Notify(s, unix.SIGWINCH) + go func() { + for range s { + size, err := con.Size() + if err != nil { + logrus.WithError(err).Error("get pty size") + continue + } + if _, err := service.Pty(ctx, &execution.PtyRequest{ + ID: id, + Pid: pid, + Width: uint32(size.Width), + Height: uint32(size.Height), + }); err != nil { + logrus.WithError(err).Error("resize pty") + } + } + }() + return nil +} diff --git a/cmd/ctr/run_windows.go b/cmd/ctr/run_windows.go index 70f0be1cf..2623262dd 100644 --- a/cmd/ctr/run_windows.go +++ b/cmd/ctr/run_windows.go @@ -1,15 +1,19 @@ package main import ( + "context" "encoding/json" "fmt" "io/ioutil" "runtime" + "time" "github.com/Sirupsen/logrus" "github.com/containerd/containerd/api/services/execution" + "github.com/containerd/containerd/log" "github.com/containerd/containerd/windows" "github.com/containerd/containerd/windows/hcs" + "github.com/crosbymichael/console" protobuf "github.com/gogo/protobuf/types" ocispec "github.com/opencontainers/image-spec/specs-go/v1" specs "github.com/opencontainers/runtime-spec/specs-go" @@ -148,3 +152,36 @@ func newCreateRequest(context *cli.Context, imageConfig *ocispec.ImageConfig, id return create, nil } + +func handleConsoleResize(ctx context.Context, service execution.ContainerServiceClient, id string, pid uint32, con console.Console) error { + // do an initial resize of the console + size, err := con.Size() + if err != nil { + return err + } + go func() { + prevSize := size + for { + time.Sleep(time.Millisecond * 250) + + size, err := con.Size() + if err != nil { + log.G(ctx).WithError(err).Error("get pty size") + continue + } + + if size.Width != prevSize.Width || size.Height != prevSize.Height { + if _, err := service.Pty(ctx, &execution.PtyRequest{ + ID: id, + Pid: pid, + Width: uint32(size.Width), + Height: uint32(size.Height), + }); err != nil { + log.G(ctx).WithError(err).Error("resize pty") + } + prevSize = size + } + } + }() + return nil +} diff --git a/vendor.conf b/vendor.conf index 26f96b5ca..4d8183016 100644 --- a/vendor.conf +++ b/vendor.conf @@ -1,5 +1,5 @@ github.com/crosbymichael/go-runc 65847bfc51952703ca24b564d10de50d3f2db6e7 -github.com/crosbymichael/console 3e9d1bd6d181492458dbc171150185c14139fd46 +github.com/crosbymichael/console f13f890e20a94bdec6c328cdf9410b7158f0cfa4 github.com/crosbymichael/cgroups a692a19766b072b86d89620c97a7916b2e2de3e7 github.com/docker/go-metrics 8fd5772bf1584597834c6f7961a530f06cbfbb87 github.com/prometheus/client_golang v0.8.0 diff --git a/vendor/github.com/crosbymichael/console/console_windows.go b/vendor/github.com/crosbymichael/console/console_windows.go index daaeaa728..0dcefff03 100644 --- a/vendor/github.com/crosbymichael/console/console_windows.go +++ b/vendor/github.com/crosbymichael/console/console_windows.go @@ -105,7 +105,7 @@ func (m *master) Reset() error { } func (m *master) Size() (WinSize, error) { - info, err := winterm.GetConsoleScreenBufferInfo(m.in) + info, err := winterm.GetConsoleScreenBufferInfo(m.out) if err != nil { return WinSize{}, errors.Wrap(err, "unable to get console info") } diff --git a/windows/container.go b/windows/container.go index 63a7ba382..9e18f5c99 100644 --- a/windows/container.go +++ b/windows/container.go @@ -142,6 +142,14 @@ func (c *container) Exec(ctx context.Context, opts containerd.ExecOpts) (contain return &process{p}, nil } +func (c *container) CloseStdin(ctx context.Context, pid uint32) error { + return c.ctr.CloseStdin(ctx, pid) +} + +func (c *container) Pty(ctx context.Context, pid uint32, size containerd.ConsoleSize) error { + return c.ctr.Pty(ctx, pid, size) +} + func (c *container) Status() containerd.Status { return c.getStatus() } diff --git a/windows/hcs/hcs.go b/windows/hcs/hcs.go index c682b6ae9..cf1c94621 100644 --- a/windows/hcs/hcs.go +++ b/windows/hcs/hcs.go @@ -15,6 +15,7 @@ import ( "github.com/Microsoft/hcsshim" "github.com/Sirupsen/logrus" + "github.com/containerd/containerd" "github.com/containerd/containerd/log" "github.com/containerd/containerd/windows/pid" "github.com/opencontainers/runtime-spec/specs-go" @@ -194,6 +195,40 @@ func (c *Container) Stop(ctx context.Context) error { return nil } +func (c *Container) CloseStdin(ctx context.Context, pid uint32) error { + var proc *Process + c.Lock() + for _, p := range c.processes { + if p.Pid() == pid { + proc = p + break + } + } + c.Unlock() + if proc == nil { + return errors.Errorf("no such process %v", pid) + } + + return proc.CloseStdin() +} + +func (c *Container) Pty(ctx context.Context, pid uint32, size containerd.ConsoleSize) error { + var proc *Process + c.Lock() + for _, p := range c.processes { + if p.Pid() == pid { + proc = p + break + } + } + c.Unlock() + if proc == nil { + return errors.Errorf("no such process %v", pid) + } + + return proc.ResizeConsole(uint16(size.Width), uint16(size.Height)) +} + func (c *Container) Delete(ctx context.Context) { defer func() { if err := c.Stop(ctx); err != nil {