From b9378b45296218ba7f23384cf426384bb2102e0f Mon Sep 17 00:00:00 2001 From: Samuel Karp Date: Sat, 22 May 2021 20:44:35 -0700 Subject: [PATCH 1/2] ctr: exec setup IO with console Use cio.WithStreams with explicit console device when --tty is passed, consistent with how ctr run behaves. Signed-off-by: Samuel Karp --- cmd/ctr/commands/tasks/exec.go | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/cmd/ctr/commands/tasks/exec.go b/cmd/ctr/commands/tasks/exec.go index 3f31e2796..ff02dc4d4 100644 --- a/cmd/ctr/commands/tasks/exec.go +++ b/cmd/ctr/commands/tasks/exec.go @@ -114,9 +114,18 @@ var execCommand = cli.Command{ stdinC = &stdinCloser{ stdin: os.Stdin, } + con console.Console ) - if logURI := context.String("log-uri"); logURI != "" { + ioOpts := []cio.Opt{cio.WithFIFODir(context.String("fifo-dir"))} + if tty { + con = console.Current() + defer con.Reset() + if err := con.SetRaw(); err != nil { + return err + } + ioCreator = cio.NewCreator(append([]cio.Opt{cio.WithStreams(con, con, nil), cio.WithTerminal}, ioOpts...)...) + } else if logURI := context.String("log-uri"); logURI != "" { uri, err := url.Parse(logURI) if err != nil { return err @@ -132,11 +141,7 @@ var execCommand = cli.Command{ ioCreator = cio.LogURI(uri) } else { - cioOpts := []cio.Opt{cio.WithStreams(stdinC, os.Stdout, os.Stderr), cio.WithFIFODir(context.String("fifo-dir"))} - if tty { - cioOpts = append(cioOpts, cio.WithTerminal) - } - ioCreator = cio.NewCreator(cioOpts...) + ioCreator = cio.NewCreator(append([]cio.Opt{cio.WithStreams(stdinC, os.Stdout, os.Stderr)}, ioOpts...)...) } process, err := task.Exec(ctx, context.String("exec-id"), pspec, ioCreator) @@ -156,14 +161,6 @@ var execCommand = cli.Command{ return err } - var con console.Console - if tty { - con = console.Current() - defer con.Reset() - if err := con.SetRaw(); err != nil { - return err - } - } if !detach { if tty { if err := HandleConsoleResize(ctx, process, con); err != nil { From 5dec27b6f1f6921ae288a3094b796929b59fc539 Mon Sep 17 00:00:00 2001 From: Samuel Karp Date: Tue, 25 May 2021 00:12:21 -0700 Subject: [PATCH 2/2] ctr: exec handle pty resize after Start Handle initial pty resize after the exec process has started and the pty is available, consistent with the behavior of ctr run. Signed-off-by: Samuel Karp --- cmd/ctr/commands/tasks/exec.go | 46 ++++++++++++++++------------------ 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/cmd/ctr/commands/tasks/exec.go b/cmd/ctr/commands/tasks/exec.go index ff02dc4d4..c1a4b8bbd 100644 --- a/cmd/ctr/commands/tasks/exec.go +++ b/cmd/ctr/commands/tasks/exec.go @@ -117,30 +117,31 @@ var execCommand = cli.Command{ con console.Console ) - ioOpts := []cio.Opt{cio.WithFIFODir(context.String("fifo-dir"))} - if tty { + fifoDir := context.String("fifo-dir") + logURI := context.String("log-uri") + ioOpts := []cio.Opt{cio.WithFIFODir(fifoDir)} + switch { + case tty && logURI != "": + return errors.New("can't use log-uri with tty") + case logURI != "" && fifoDir != "": + return errors.New("can't use log-uri with fifo-dir") + + case tty: con = console.Current() defer con.Reset() if err := con.SetRaw(); err != nil { return err } ioCreator = cio.NewCreator(append([]cio.Opt{cio.WithStreams(con, con, nil), cio.WithTerminal}, ioOpts...)...) - } else if logURI := context.String("log-uri"); logURI != "" { + + case logURI != "": uri, err := url.Parse(logURI) if err != nil { return err } - - if dir := context.String("fifo-dir"); dir != "" { - return errors.New("can't use log-uri with fifo-dir") - } - - if tty { - return errors.New("can't use log-uri with tty") - } - ioCreator = cio.LogURI(uri) - } else { + + default: ioCreator = cio.NewCreator(append([]cio.Opt{cio.WithStreams(stdinC, os.Stdout, os.Stderr)}, ioOpts...)...) } @@ -161,23 +162,20 @@ var execCommand = cli.Command{ return err } - if !detach { - if tty { - if err := HandleConsoleResize(ctx, process, con); err != nil { - logrus.WithError(err).Error("console resize") - } - } else { - sigc := commands.ForwardAllSignals(ctx, process) - defer commands.StopCatch(sigc) - } - } - if err := process.Start(ctx); err != nil { return err } if detach { return nil } + if tty { + if err := HandleConsoleResize(ctx, process, con); err != nil { + logrus.WithError(err).Error("console resize") + } + } else { + sigc := commands.ForwardAllSignals(ctx, process) + defer commands.StopCatch(sigc) + } status := <-statusC code, _, err := status.Result() if err != nil {