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

Abstract recvtty to a single function #1731

Closed
wants to merge 1 commit into from
Closed
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
39 changes: 2 additions & 37 deletions contrib/cmd/recvtty/recvtty.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"os"
"strings"

"github.com/containerd/console"
"github.com/opencontainers/runc/libcontainer/utils"
"github.com/urfave/cli"
)
Expand Down Expand Up @@ -67,45 +66,11 @@ func bail(err error) {

func handleSingle(path string) error {
// Open a socket.
ln, err := net.Listen("unix", path)
if err != nil {
return err
}
defer ln.Close()

// We only accept a single connection, since we can only really have
// one reader for os.Stdin. Plus this is all a PoC.
conn, err := ln.Accept()
if err != nil {
return err
}
defer conn.Close()

// Close ln, to allow for other instances to take over.
ln.Close()

// Get the fd of the connection.
unixconn, ok := conn.(*net.UnixConn)
if !ok {
return fmt.Errorf("failed to cast to unixconn")
}

socket, err := unixconn.File()
if err != nil {
return err
}
defer socket.Close()

// Get the master file descriptor from runC.
master, err := utils.RecvFd(socket)
if err != nil {
return err
}
c, err := console.ConsoleFromFile(master)
c, err := utils.ConsoleSocket(path)
if err != nil {
return err
}
console.ClearONLCR(c.Fd())
defer c.Close()

// Copy from our stdio to the master fd.
quitChan := make(chan struct{})
Expand Down
49 changes: 49 additions & 0 deletions libcontainer/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import (
"crypto/rand"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net"
"os"
"path/filepath"
"strings"
"unsafe"

"github.com/containerd/console"
"golang.org/x/sys/unix"
)

Expand Down Expand Up @@ -125,3 +128,49 @@ func Annotations(labels []string) (bundle string, userAnnotations map[string]str
func GetIntSize() int {
return int(unsafe.Sizeof(1))
}

// ConsoleSocket opens a socket on the given path and receives the console file descriptors from there
func ConsoleSocket(path string) (console.Console, error) {
// Open a socket.
ln, err := net.Listen("unix", path)
if err != nil {
return nil, err
}
defer ln.Close()

// We only accept a single connection, since we can only really have
// one reader for os.Stdin
conn, err := ln.Accept()
if err != nil {
return nil, err
}
defer conn.Close()

// Close ln, to allow for other instances to take over.
ln.Close()

// Get the fd of the connection.
unixconn, ok := conn.(*net.UnixConn)
if !ok {
return nil, fmt.Errorf("failed to cast to unixconn")
}

socket, err := unixconn.File()
if err != nil {
return nil, err
}
defer socket.Close()

// Get the master file descriptor from runC.
master, err := RecvFd(socket)
if err != nil {
return nil, err
}
c, err := console.ConsoleFromFile(master)
if err != nil {
return nil, err
}
console.ClearONLCR(c.Fd())

return c, nil
}