2017-10-27 00:26:04 +00:00
|
|
|
package libpod
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2017-10-30 18:31:31 +00:00
|
|
|
"net"
|
2017-10-27 00:26:04 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/term"
|
|
|
|
"github.com/kubernetes-incubator/cri-o/utils"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
"k8s.io/client-go/tools/remotecommand"
|
|
|
|
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
|
|
|
)
|
|
|
|
|
|
|
|
/* Sync with stdpipe_t in conmon.c */
|
|
|
|
const (
|
|
|
|
AttachPipeStdin = 1
|
|
|
|
AttachPipeStdout = 2
|
|
|
|
AttachPipeStderr = 3
|
|
|
|
)
|
|
|
|
|
|
|
|
// attachContainerSocket connects to the container's attach socket and deals with the IO
|
|
|
|
func (c *Container) attachContainerSocket(resize <-chan remotecommand.TerminalSize, noStdIn bool, detachKeys []byte) error {
|
|
|
|
inputStream := os.Stdin
|
|
|
|
outputStream := os.Stdout
|
|
|
|
errorStream := os.Stderr
|
|
|
|
defer inputStream.Close()
|
|
|
|
tty, err := strconv.ParseBool(c.runningSpec.Annotations["io.kubernetes.cri-o.TTY"])
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "unable to parse annotations in %s", c.ID)
|
|
|
|
}
|
|
|
|
if !tty {
|
|
|
|
return errors.Errorf("no tty available for %s", c.ID())
|
|
|
|
}
|
|
|
|
|
|
|
|
oldTermState, err := term.SaveState(inputStream.Fd())
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "unable to save terminal state")
|
|
|
|
}
|
|
|
|
|
|
|
|
defer term.RestoreTerminal(inputStream.Fd(), oldTermState)
|
|
|
|
|
|
|
|
// Put both input and output into raw
|
|
|
|
if !noStdIn {
|
|
|
|
term.SetRawTerminal(inputStream.Fd())
|
|
|
|
}
|
|
|
|
|
|
|
|
controlPath := filepath.Join(c.state.RunDir, "ctl")
|
|
|
|
controlFile, err := os.OpenFile(controlPath, unix.O_WRONLY, 0)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to open container ctl file: %v")
|
|
|
|
}
|
|
|
|
|
|
|
|
kubecontainer.HandleResizing(resize, func(size remotecommand.TerminalSize) {
|
2017-10-30 18:31:31 +00:00
|
|
|
logrus.Debugf("Received a resize event: %+v", size)
|
2017-10-27 00:26:04 +00:00
|
|
|
_, err := fmt.Fprintf(controlFile, "%d %d %d\n", 1, size.Height, size.Width)
|
|
|
|
if err != nil {
|
2017-10-30 18:31:31 +00:00
|
|
|
logrus.Warnf("Failed to write to control file to resize terminal: %v", err)
|
2017-10-27 00:26:04 +00:00
|
|
|
}
|
|
|
|
})
|
2017-10-30 18:31:31 +00:00
|
|
|
logrus.Debug("connecting to socket ", c.attachSocketPath())
|
2017-10-27 00:26:04 +00:00
|
|
|
|
2017-10-30 18:31:31 +00:00
|
|
|
conn, err := net.DialUnix("unixpacket", nil, &net.UnixAddr{Name: c.attachSocketPath(), Net: "unixpacket"})
|
2017-10-27 00:26:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to connect to container's attach socket: %v")
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
receiveStdoutError := make(chan error)
|
|
|
|
if outputStream != nil || errorStream != nil {
|
|
|
|
go func() {
|
|
|
|
receiveStdoutError <- redirectResponseToOutputStreams(outputStream, errorStream, conn)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
stdinDone := make(chan error)
|
|
|
|
go func() {
|
|
|
|
var err error
|
|
|
|
if inputStream != nil && !noStdIn {
|
|
|
|
_, err = utils.CopyDetachable(conn, inputStream, detachKeys)
|
|
|
|
conn.CloseWrite()
|
|
|
|
}
|
|
|
|
stdinDone <- err
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-receiveStdoutError:
|
|
|
|
return err
|
|
|
|
case err := <-stdinDone:
|
|
|
|
if _, ok := err.(utils.DetachError); ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if outputStream != nil || errorStream != nil {
|
|
|
|
return <-receiveStdoutError
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func redirectResponseToOutputStreams(outputStream, errorStream io.Writer, conn io.Reader) error {
|
|
|
|
var err error
|
|
|
|
buf := make([]byte, 8192+1) /* Sync with conmon STDIO_BUF_SIZE */
|
|
|
|
for {
|
|
|
|
nr, er := conn.Read(buf)
|
|
|
|
if nr > 0 {
|
|
|
|
var dst io.Writer
|
|
|
|
switch buf[0] {
|
|
|
|
case AttachPipeStdout:
|
|
|
|
dst = outputStream
|
|
|
|
case AttachPipeStderr:
|
|
|
|
dst = errorStream
|
|
|
|
default:
|
2017-10-30 18:31:31 +00:00
|
|
|
logrus.Infof("Received unexpected attach type %+d", buf[0])
|
2017-10-27 00:26:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if dst != nil {
|
|
|
|
nw, ew := dst.Write(buf[1:nr])
|
|
|
|
if ew != nil {
|
|
|
|
err = ew
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if nr != nw+1 {
|
|
|
|
err = io.ErrShortWrite
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if er == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if er != nil {
|
|
|
|
err = er
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|