2016-03-18 14:53:27 -04:00
|
|
|
package libcontainerd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2016-05-20 22:04:20 -04:00
|
|
|
|
2016-05-23 19:12:06 -04:00
|
|
|
"github.com/Microsoft/hcsshim"
|
2016-03-18 14:53:27 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// process keeps the state for both main container process and exec process.
|
|
|
|
type process struct {
|
|
|
|
processCommon
|
2016-03-18 23:29:27 -04:00
|
|
|
|
2016-03-20 18:58:23 -04:00
|
|
|
// Platform specific fields are below here.
|
|
|
|
|
|
|
|
// commandLine is to support returning summary information for docker top
|
|
|
|
commandLine string
|
2016-05-23 19:12:06 -04:00
|
|
|
hcsProcess hcsshim.Process
|
2016-03-18 14:53:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func openReaderFromPipe(p io.ReadCloser) io.Reader {
|
|
|
|
r, w := io.Pipe()
|
|
|
|
go func() {
|
|
|
|
if _, err := io.Copy(w, p); err != nil {
|
|
|
|
r.CloseWithError(err)
|
|
|
|
}
|
|
|
|
w.Close()
|
|
|
|
p.Close()
|
|
|
|
}()
|
|
|
|
return r
|
|
|
|
}
|
2016-05-20 22:04:20 -04:00
|
|
|
|
|
|
|
// fixStdinBackspaceBehavior works around a bug in Windows before build 14350
|
|
|
|
// where it interpreted DEL as VK_DELETE instead of as VK_BACK. This replaces
|
|
|
|
// DEL with BS to work around this.
|
2016-05-25 15:15:34 -04:00
|
|
|
func fixStdinBackspaceBehavior(w io.WriteCloser, osversion string, tty bool) io.WriteCloser {
|
|
|
|
if !tty {
|
2016-05-20 22:04:20 -04:00
|
|
|
return w
|
|
|
|
}
|
2016-06-24 18:24:06 -04:00
|
|
|
if build := buildFromVersion(osversion); build == 0 || build >= 14350 {
|
2016-05-25 15:15:34 -04:00
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
2016-05-20 22:04:20 -04:00
|
|
|
return &delToBsWriter{w}
|
|
|
|
}
|
|
|
|
|
|
|
|
type delToBsWriter struct {
|
|
|
|
io.WriteCloser
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *delToBsWriter) Write(b []byte) (int, error) {
|
|
|
|
const (
|
|
|
|
backspace = 0x8
|
|
|
|
del = 0x7f
|
|
|
|
)
|
|
|
|
bc := make([]byte, len(b))
|
|
|
|
for i, c := range b {
|
|
|
|
if c == del {
|
|
|
|
bc[i] = backspace
|
|
|
|
} else {
|
|
|
|
bc[i] = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return w.WriteCloser.Write(bc)
|
|
|
|
}
|
2016-05-23 19:12:06 -04:00
|
|
|
|
|
|
|
type stdInCloser struct {
|
|
|
|
io.WriteCloser
|
|
|
|
hcsshim.Process
|
|
|
|
}
|
|
|
|
|
|
|
|
func createStdInCloser(pipe io.WriteCloser, process hcsshim.Process) *stdInCloser {
|
|
|
|
return &stdInCloser{
|
|
|
|
WriteCloser: pipe,
|
|
|
|
Process: process,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (stdin *stdInCloser) Close() error {
|
|
|
|
if err := stdin.WriteCloser.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return stdin.Process.CloseStdin()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (stdin *stdInCloser) Write(p []byte) (n int, err error) {
|
|
|
|
return stdin.WriteCloser.Write(p)
|
|
|
|
}
|