aboutsummaryrefslogtreecommitdiff
path: root/libpod/container_attach.go
diff options
context:
space:
mode:
authorbaude <bbaude@redhat.com>2017-11-01 13:59:11 -0500
committerbaude <bbaude@redhat.com>2017-11-01 14:19:19 -0500
commit8cf07b2ad1ed8c6646c48a74e9ecbb2bfeecb322 (patch)
tree625fdeaf51ffced387b4ba2e48d93288f3f1b9b2 /libpod/container_attach.go
parentf5019df3f5da9030ce21e5c8ad3d3921a6585e7f (diff)
downloadpodman-8cf07b2ad1ed8c6646c48a74e9ecbb2bfeecb322.tar.gz
podman-8cf07b2ad1ed8c6646c48a74e9ecbb2bfeecb322.tar.bz2
podman-8cf07b2ad1ed8c6646c48a74e9ecbb2bfeecb322.zip
libpod create and run
patched version of the same code that went into crio Signed-off-by: baude <bbaude@redhat.com>
Diffstat (limited to 'libpod/container_attach.go')
-rw-r--r--libpod/container_attach.go142
1 files changed, 142 insertions, 0 deletions
diff --git a/libpod/container_attach.go b/libpod/container_attach.go
new file mode 100644
index 000000000..b2a407027
--- /dev/null
+++ b/libpod/container_attach.go
@@ -0,0 +1,142 @@
+package libpod
+
+import (
+ "fmt"
+ "io"
+ "net"
+ "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) {
+ logrus.Debugf("Received a resize event: %+v", size)
+ _, err := fmt.Fprintf(controlFile, "%d %d %d\n", 1, size.Height, size.Width)
+ if err != nil {
+ logrus.Warnf("Failed to write to control file to resize terminal: %v", err)
+ }
+ })
+ logrus.Debug("connecting to socket ", c.attachSocketPath())
+
+ conn, err := net.DialUnix("unixpacket", nil, &net.UnixAddr{Name: c.attachSocketPath(), Net: "unixpacket"})
+ 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:
+ logrus.Infof("Received unexpected attach type %+d", buf[0])
+ }
+
+ 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
+}