2017-01-23 19:35:18 +00:00
|
|
|
package runc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2017-01-26 18:33:33 +00:00
|
|
|
"path/filepath"
|
2017-01-23 19:35:18 +00:00
|
|
|
|
2017-01-26 18:33:33 +00:00
|
|
|
"github.com/crosbymichael/console"
|
2017-01-23 19:35:18 +00:00
|
|
|
"github.com/opencontainers/runc/libcontainer/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewConsoleSocket creates a new unix socket at the provided path to accept a
|
|
|
|
// pty master created by runc for use by the container
|
|
|
|
func NewConsoleSocket(path string) (*ConsoleSocket, error) {
|
2017-01-26 18:33:33 +00:00
|
|
|
abs, err := filepath.Abs(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
l, err := net.Listen("unix", abs)
|
2017-01-23 19:35:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &ConsoleSocket{
|
|
|
|
l: l,
|
2017-01-26 18:33:33 +00:00
|
|
|
path: abs,
|
2017-01-23 19:35:18 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConsoleSocket is a unix socket that accepts the pty master created by runc
|
|
|
|
type ConsoleSocket struct {
|
|
|
|
path string
|
|
|
|
l net.Listener
|
|
|
|
}
|
|
|
|
|
|
|
|
// Path returns the path to the unix socket on disk
|
|
|
|
func (c *ConsoleSocket) Path() string {
|
|
|
|
return c.path
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReceiveMaster blocks until the socket receives the pty master
|
2017-01-26 18:33:33 +00:00
|
|
|
func (c *ConsoleSocket) ReceiveMaster() (console.Console, error) {
|
2017-01-23 19:35:18 +00:00
|
|
|
conn, err := c.l.Accept()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
unix, ok := conn.(*net.UnixConn)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("received connection which was not a unix socket")
|
|
|
|
}
|
|
|
|
sock, err := unix.File()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
f, err := utils.RecvFd(sock)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-01-26 18:33:33 +00:00
|
|
|
return console.ConsoleFromFile(f)
|
2017-01-23 19:35:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the unix socket
|
|
|
|
func (c *ConsoleSocket) Close() error {
|
|
|
|
return c.l.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// WinSize specifies the console size
|
|
|
|
type WinSize struct {
|
|
|
|
// Width of the console
|
|
|
|
Width uint16
|
|
|
|
// Height of the console
|
|
|
|
Height uint16
|
|
|
|
}
|