@@ -18,9 +18,12 @@ package tasks
1818
1919import (
2020 "errors"
21+ "io"
2122 "net/url"
23+ "os"
2224
2325 "github.com/containerd/console"
26+ "github.com/containerd/containerd"
2427 "github.com/containerd/containerd/cio"
2528 "github.com/containerd/containerd/cmd/ctr/commands"
2629 "github.com/sirupsen/logrus"
@@ -91,7 +94,12 @@ var execCommand = cli.Command{
9194 pspec .Terminal = tty
9295 pspec .Args = args
9396
94- var ioCreator cio.Creator
97+ var (
98+ ioCreator cio.Creator
99+ stdinC = & stdinCloser {
100+ stdin : os .Stdin ,
101+ }
102+ )
95103
96104 if logURI := context .String ("log-uri" ); logURI != "" {
97105 uri , err := url .Parse (logURI )
@@ -109,7 +117,7 @@ var execCommand = cli.Command{
109117
110118 ioCreator = cio .LogURI (uri )
111119 } else {
112- cioOpts := []cio.Opt {cio .WithStdio , cio .WithFIFODir (context .String ("fifo-dir" ))}
120+ cioOpts := []cio.Opt {cio .WithStreams ( stdinC , os . Stdout , os . Stderr ) , cio .WithFIFODir (context .String ("fifo-dir" ))}
113121 if tty {
114122 cioOpts = append (cioOpts , cio .WithTerminal )
115123 }
@@ -120,6 +128,9 @@ var execCommand = cli.Command{
120128 if err != nil {
121129 return err
122130 }
131+ stdinC .closer = func () {
132+ process .CloseIO (ctx , containerd .WithStdinCloser )
133+ }
123134 // if detach, we should not call this defer
124135 if ! detach {
125136 defer process .Delete (ctx )
@@ -166,3 +177,18 @@ var execCommand = cli.Command{
166177 return nil
167178 },
168179}
180+
181+ type stdinCloser struct {
182+ stdin * os.File
183+ closer func ()
184+ }
185+
186+ func (s * stdinCloser ) Read (p []byte ) (int , error ) {
187+ n , err := s .stdin .Read (p )
188+ if err == io .EOF {
189+ if s .closer != nil {
190+ s .closer ()
191+ }
192+ }
193+ return n , err
194+ }
0 commit comments