2015-02-10 06:07:18 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/codegangsta/cli"
|
|
|
|
"github.com/docker/docker/pkg/term"
|
|
|
|
"github.com/docker/libcontainer"
|
|
|
|
)
|
|
|
|
|
2015-02-26 06:31:39 +08:00
|
|
|
func newTty(context *cli.Context, p *libcontainer.Process, rootuid int) (*tty, error) {
|
2015-02-10 06:07:18 +08:00
|
|
|
if context.Bool("tty") {
|
2015-02-26 06:31:39 +08:00
|
|
|
console, err := p.NewConsole(rootuid)
|
2015-02-10 06:07:18 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &tty{
|
|
|
|
console: console,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
return &tty{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type tty struct {
|
|
|
|
console libcontainer.Console
|
|
|
|
state *term.State
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tty) Close() error {
|
|
|
|
if t.console != nil {
|
|
|
|
t.console.Close()
|
|
|
|
}
|
|
|
|
if t.state != nil {
|
|
|
|
term.RestoreTerminal(os.Stdin.Fd(), t.state)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-18 13:37:02 +08:00
|
|
|
func (t *tty) attach(process *libcontainer.Process) error {
|
2015-02-10 06:07:18 +08:00
|
|
|
if t.console != nil {
|
2015-02-18 13:37:02 +08:00
|
|
|
go io.Copy(t.console, os.Stdin)
|
|
|
|
go io.Copy(os.Stdout, t.console)
|
|
|
|
state, err := term.SetRawTerminal(os.Stdin.Fd())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
t.state = state
|
2015-02-10 06:07:18 +08:00
|
|
|
process.Stderr = nil
|
|
|
|
process.Stdout = nil
|
|
|
|
process.Stdin = nil
|
|
|
|
}
|
2015-02-18 13:37:02 +08:00
|
|
|
return nil
|
2015-02-10 06:07:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tty) resize() error {
|
|
|
|
if t.console == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ws, err := term.GetWinsize(os.Stdin.Fd())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return term.SetWinsize(t.console.Fd(), ws)
|
|
|
|
}
|