Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

oci: use /proc/self/fd/FD to open unix socket #8933

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 13 additions & 16 deletions libpod/oci_attach_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ const (
AttachPipeStderr = 3
)

func openUnixSocket(path string) (*net.UnixConn, error) {
fd, err := unix.Open(path, unix.O_PATH, 0)
if err != nil {
return nil, err
}
defer unix.Close(fd)
return net.DialUnix("unixpacket", nil, &net.UnixAddr{Name: fmt.Sprintf("/proc/self/fd/%d", fd), Net: "unixpacket"})
}

// Attach to the given container
// Does not check if state is appropriate
// started is only required if startContainer is true
Expand All @@ -52,11 +61,10 @@ func (c *Container) attach(streams *define.AttachStreams, keys string, resize <-
if err != nil {
return err
}
socketPath := buildSocketPath(attachSock)

conn, err := net.DialUnix("unixpacket", nil, &net.UnixAddr{Name: socketPath, Net: "unixpacket"})
conn, err := openUnixSocket(attachSock)
if err != nil {
return errors.Wrapf(err, "failed to connect to container's attach socket: %v", socketPath)
return errors.Wrapf(err, "failed to connect to container's attach socket: %v", attachSock)
}
defer func() {
if err := conn.Close(); err != nil {
Expand Down Expand Up @@ -124,17 +132,16 @@ func (c *Container) attachToExec(streams *define.AttachStreams, keys *string, se
if err != nil {
return err
}
socketPath := buildSocketPath(sockPath)

// 2: read from attachFd that the parent process has set up the console socket
if _, err := readConmonPipeData(attachFd, ""); err != nil {
return err
}

// 2: then attach
conn, err := net.DialUnix("unixpacket", nil, &net.UnixAddr{Name: socketPath, Net: "unixpacket"})
conn, err := openUnixSocket(sockPath)
if err != nil {
return errors.Wrapf(err, "failed to connect to container's attach socket: %v", socketPath)
return errors.Wrapf(err, "failed to connect to container's attach socket: %v", sockPath)
}
defer func() {
if err := conn.Close(); err != nil {
Expand Down Expand Up @@ -182,16 +189,6 @@ func registerResizeFunc(resize <-chan remotecommand.TerminalSize, bundlePath str
})
}

func buildSocketPath(socketPath string) string {
maxUnixLength := unixPathLength()
if maxUnixLength < len(socketPath) {
socketPath = socketPath[0:maxUnixLength]
}

logrus.Debug("connecting to socket ", socketPath)
return socketPath
}

func setupStdioChannels(streams *define.AttachStreams, conn *net.UnixConn, detachKeys []byte) (chan error, chan error) {
receiveStdoutError := make(chan error)
go func() {
Expand Down
11 changes: 0 additions & 11 deletions libpod/oci_attach_linux_cgo.go

This file was deleted.

7 changes: 0 additions & 7 deletions libpod/oci_attach_linux_nocgo.go

This file was deleted.

6 changes: 2 additions & 4 deletions libpod/oci_conmon_exec_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package libpod

import (
"fmt"
"net"
"net/http"
"os"
"os/exec"
Expand Down Expand Up @@ -512,17 +511,16 @@ func attachExecHTTP(c *Container, sessionID string, r *http.Request, w http.Resp
if err != nil {
return err
}
socketPath := buildSocketPath(sockPath)

// 2: read from attachFd that the parent process has set up the console socket
if _, err := readConmonPipeData(pipes.attachPipe, ""); err != nil {
return err
}

// 2: then attach
conn, err := net.DialUnix("unixpacket", nil, &net.UnixAddr{Name: socketPath, Net: "unixpacket"})
conn, err := openUnixSocket(sockPath)
if err != nil {
return errors.Wrapf(err, "failed to connect to container's attach socket: %v", socketPath)
return errors.Wrapf(err, "failed to connect to container's attach socket: %v", sockPath)
}
defer func() {
if err := conn.Close(); err != nil {
Expand Down
7 changes: 3 additions & 4 deletions libpod/oci_conmon_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,13 +529,12 @@ func (r *ConmonOCIRuntime) HTTPAttach(ctr *Container, req *http.Request, w http.
if err != nil {
return err
}
socketPath := buildSocketPath(attachSock)

var conn *net.UnixConn
if streamAttach {
newConn, err := net.DialUnix("unixpacket", nil, &net.UnixAddr{Name: socketPath, Net: "unixpacket"})
newConn, err := openUnixSocket(attachSock)
if err != nil {
return errors.Wrapf(err, "failed to connect to container's attach socket: %v", socketPath)
return errors.Wrapf(err, "failed to connect to container's attach socket: %v", attachSock)
}
conn = newConn
defer func() {
Expand All @@ -544,7 +543,7 @@ func (r *ConmonOCIRuntime) HTTPAttach(ctr *Container, req *http.Request, w http.
}
}()

logrus.Debugf("Successfully connected to container %s attach socket %s", ctr.ID(), socketPath)
logrus.Debugf("Successfully connected to container %s attach socket %s", ctr.ID(), attachSock)
}

detachString := ctr.runtime.config.Engine.DetachKeys
Expand Down