Remove terminal handling in libcontainer

Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@docker.com> (github: crosbymichael)
This commit is contained in:
Michael Crosby 2014-07-15 16:26:28 -07:00
parent fb3d909c28
commit d661720fd7
6 changed files with 39 additions and 151 deletions

View File

@ -90,13 +90,16 @@ func CreateMasterAndConsole() (*os.File, string, error) {
if err != nil { if err != nil {
return nil, "", err return nil, "", err
} }
console, err := Ptsname(master) console, err := Ptsname(master)
if err != nil { if err != nil {
return nil, "", err return nil, "", err
} }
if err := Unlockpt(master); err != nil { if err := Unlockpt(master); err != nil {
return nil, "", err return nil, "", err
} }
return master, console, nil return master, console, nil
} }

View File

@ -3,6 +3,7 @@
package namespaces package namespaces
import ( import (
"io"
"os" "os"
"os/exec" "os/exec"
"syscall" "syscall"
@ -11,7 +12,6 @@ import (
"github.com/docker/libcontainer/cgroups" "github.com/docker/libcontainer/cgroups"
"github.com/docker/libcontainer/cgroups/fs" "github.com/docker/libcontainer/cgroups/fs"
"github.com/docker/libcontainer/cgroups/systemd" "github.com/docker/libcontainer/cgroups/systemd"
consolePkg "github.com/docker/libcontainer/console"
"github.com/docker/libcontainer/network" "github.com/docker/libcontainer/network"
"github.com/docker/libcontainer/syncpipe" "github.com/docker/libcontainer/syncpipe"
"github.com/docker/libcontainer/system" "github.com/docker/libcontainer/system"
@ -21,9 +21,8 @@ import (
// Move this to libcontainer package. // Move this to libcontainer package.
// Exec performs setup outside of a namespace so that a container can be // Exec performs setup outside of a namespace so that a container can be
// executed. Exec is a high level function for working with container namespaces. // executed. Exec is a high level function for working with container namespaces.
func Exec(container *libcontainer.Config, term Terminal, rootfs, dataPath string, args []string, createCommand CreateCommand, startCallback func()) (int, error) { func Exec(container *libcontainer.Config, stdin io.Reader, stdout, stderr io.Writer, rootfs, dataPath string, args []string, createCommand CreateCommand, startCallback func()) (int, error) {
var ( var (
master *os.File
console string console string
err error err error
) )
@ -36,20 +35,15 @@ func Exec(container *libcontainer.Config, term Terminal, rootfs, dataPath string
} }
defer syncPipe.Close() defer syncPipe.Close()
if container.Tty { if f, ok := stdin.(*os.File); ok {
master, console, err = consolePkg.CreateMasterAndConsole() console = f.Name()
if err != nil {
return -1, err
}
term.SetMaster(master)
} }
command := createCommand(container, console, rootfs, dataPath, os.Args[0], syncPipe.Child(), args) command := createCommand(container, console, rootfs, dataPath, os.Args[0], syncPipe.Child(), args)
if err := term.Attach(command); err != nil { command.Stdin = stdin
return -1, err command.Stdout = stdout
} command.Stderr = stderr
defer term.Close()
if err := command.Start(); err != nil { if err := command.Start(); err != nil {
return -1, err return -1, err

View File

@ -1,49 +0,0 @@
package namespaces
import (
"io"
"os"
"os/exec"
)
type StdTerminal struct {
stdin io.Reader
stdout, stderr io.Writer
}
func (s *StdTerminal) SetMaster(*os.File) {
// no need to set master on non tty
}
func (s *StdTerminal) Close() error {
return nil
}
func (s *StdTerminal) Resize(h, w int) error {
return nil
}
func (s *StdTerminal) Attach(command *exec.Cmd) error {
inPipe, err := command.StdinPipe()
if err != nil {
return err
}
outPipe, err := command.StdoutPipe()
if err != nil {
return err
}
errPipe, err := command.StderrPipe()
if err != nil {
return err
}
go func() {
defer inPipe.Close()
io.Copy(inPipe, s.stdin)
}()
go io.Copy(s.stdout, outPipe)
go io.Copy(s.stderr, errPipe)
return nil
}

View File

@ -1,29 +0,0 @@
package namespaces
import (
"io"
"os"
"os/exec"
)
type Terminal interface {
io.Closer
SetMaster(*os.File)
Attach(*exec.Cmd) error
Resize(h, w int) error
}
func NewTerminal(stdin io.Reader, stdout, stderr io.Writer, tty bool) Terminal {
if tty {
return &TtyTerminal{
stdin: stdin,
stdout: stdout,
stderr: stderr,
}
}
return &StdTerminal{
stdin: stdin,
stdout: stdout,
stderr: stderr,
}
}

View File

@ -1,56 +0,0 @@
package namespaces
import (
"io"
"os"
"os/exec"
"github.com/dotcloud/docker/pkg/term"
)
type TtyTerminal struct {
stdin io.Reader
stdout, stderr io.Writer
master *os.File
state *term.State
}
func (t *TtyTerminal) Resize(h, w int) error {
return term.SetWinsize(t.master.Fd(), &term.Winsize{Height: uint16(h), Width: uint16(w)})
}
func (t *TtyTerminal) SetMaster(master *os.File) {
t.master = master
}
func (t *TtyTerminal) Attach(command *exec.Cmd) error {
go io.Copy(t.stdout, t.master)
go io.Copy(t.master, t.stdin)
state, err := t.setupWindow(t.master, os.Stdin)
if err != nil {
return err
}
t.state = state
return err
}
// SetupWindow gets the parent window size and sets the master
// pty to the current size and set the parents mode to RAW
func (t *TtyTerminal) setupWindow(master, parent *os.File) (*term.State, error) {
ws, err := term.GetWinsize(parent.Fd())
if err != nil {
return nil, err
}
if err := term.SetWinsize(master.Fd(), ws); err != nil {
return nil, err
}
return term.SetRawTerminal(parent.Fd())
}
func (t *TtyTerminal) Close() error {
term.RestoreTerminal(os.Stdin.Fd(), t.state)
return t.master.Close()
}

View File

@ -2,13 +2,16 @@ package nsinit
import ( import (
"fmt" "fmt"
"io"
"log" "log"
"os" "os"
"os/exec" "os/exec"
"os/signal" "os/signal"
"syscall"
"github.com/codegangsta/cli" "github.com/codegangsta/cli"
"github.com/docker/libcontainer" "github.com/docker/libcontainer"
consolepkg "github.com/docker/libcontainer/console"
"github.com/docker/libcontainer/namespaces" "github.com/docker/libcontainer/namespaces"
) )
@ -34,8 +37,7 @@ func execAction(context *cli.Context) {
if state != nil { if state != nil {
err = namespaces.ExecIn(container, state, []string(context.Args())) err = namespaces.ExecIn(container, state, []string(context.Args()))
} else { } else {
term := namespaces.NewTerminal(os.Stdin, os.Stdout, os.Stderr, container.Tty) exitCode, err = startContainer(container, dataPath, []string(context.Args()))
exitCode, err = startContainer(container, term, dataPath, []string(context.Args()))
} }
if err != nil { if err != nil {
@ -49,7 +51,7 @@ func execAction(context *cli.Context) {
// error. // error.
// //
// Signals sent to the current process will be forwarded to container. // Signals sent to the current process will be forwarded to container.
func startContainer(container *libcontainer.Config, term namespaces.Terminal, dataPath string, args []string) (int, error) { func startContainer(container *libcontainer.Config, dataPath string, args []string) (int, error) {
var ( var (
cmd *exec.Cmd cmd *exec.Cmd
sigc = make(chan os.Signal, 10) sigc = make(chan os.Signal, 10)
@ -73,5 +75,28 @@ func startContainer(container *libcontainer.Config, term namespaces.Terminal, da
}() }()
} }
return namespaces.Exec(container, term, "", dataPath, args, createCommand, startCallback) var (
stdin = os.Stdin
stdout = os.Stdout
stderr = os.Stderr
)
if container.Tty {
master, slavePath, err := consolepkg.CreateMasterAndConsole()
if err != nil {
return -1, err
}
slave, err := consolepkg.OpenTerminal(slavePath, syscall.O_RDWR)
if err != nil {
return -1, err
}
stdin, stdout, stderr = slave, slave, slave
go io.Copy(master, os.Stdin)
go io.Copy(os.Stdout, master)
}
return namespaces.Exec(container, stdin, stdout, stderr, "", dataPath, args, createCommand, startCallback)
} }