2015-06-30 07:49:13 +08:00
|
|
|
// +build linux
|
|
|
|
|
2015-06-22 10:31:12 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-08-19 17:28:51 +08:00
|
|
|
"fmt"
|
2015-06-22 10:31:12 +08:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/term"
|
|
|
|
"github.com/opencontainers/runc/libcontainer"
|
|
|
|
)
|
|
|
|
|
2015-07-13 09:08:16 +08:00
|
|
|
// newTty creates a new tty for use with the container. If a tty is not to be
|
2015-06-22 10:31:12 +08:00
|
|
|
// created for the process, pipes are created so that the TTY of the parent
|
|
|
|
// process are not inherited by the container.
|
2016-01-08 06:34:11 +08:00
|
|
|
func newTty(create bool, p *libcontainer.Process, rootuid int, console string) (*tty, error) {
|
2015-06-22 10:31:12 +08:00
|
|
|
if create {
|
2016-01-08 06:34:11 +08:00
|
|
|
return createTty(p, rootuid, console)
|
2015-06-22 10:31:12 +08:00
|
|
|
}
|
2015-09-19 02:35:23 +08:00
|
|
|
return createStdioPipes(p, rootuid)
|
2015-06-22 10:31:12 +08:00
|
|
|
}
|
|
|
|
|
2015-06-24 11:29:50 +08:00
|
|
|
// setup standard pipes so that the TTY of the calling runc process
|
2015-06-22 10:31:12 +08:00
|
|
|
// is not inherited by the container.
|
2015-09-19 02:35:23 +08:00
|
|
|
func createStdioPipes(p *libcontainer.Process, rootuid int) (*tty, error) {
|
2015-12-11 07:46:26 +08:00
|
|
|
i, err := p.InitializeIO(rootuid)
|
2015-06-22 10:31:12 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-12-11 07:46:26 +08:00
|
|
|
t := &tty{
|
|
|
|
closers: []io.Closer{
|
|
|
|
i.Stdin,
|
|
|
|
i.Stdout,
|
|
|
|
i.Stderr,
|
|
|
|
},
|
2015-09-19 02:35:23 +08:00
|
|
|
}
|
2016-02-02 07:00:09 +08:00
|
|
|
go func() {
|
|
|
|
io.Copy(i.Stdin, os.Stdin)
|
|
|
|
i.Stdin.Close()
|
|
|
|
}()
|
2015-12-11 07:46:26 +08:00
|
|
|
go io.Copy(os.Stdout, i.Stdout)
|
|
|
|
go io.Copy(os.Stderr, i.Stderr)
|
2015-06-22 10:31:12 +08:00
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
2016-01-08 06:34:11 +08:00
|
|
|
func createTty(p *libcontainer.Process, rootuid int, consolePath string) (*tty, error) {
|
|
|
|
if consolePath != "" {
|
|
|
|
if err := p.ConsoleFromPath(consolePath); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &tty{}, nil
|
|
|
|
}
|
2015-06-22 10:31:12 +08:00
|
|
|
console, err := p.NewConsole(rootuid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
go io.Copy(console, os.Stdin)
|
|
|
|
go io.Copy(os.Stdout, console)
|
2016-01-08 06:34:11 +08:00
|
|
|
|
2015-06-22 10:31:12 +08:00
|
|
|
state, err := term.SetRawTerminal(os.Stdin.Fd())
|
|
|
|
if err != nil {
|
2015-08-19 17:28:51 +08:00
|
|
|
return nil, fmt.Errorf("failed to set the terminal from the stdin: %v", err)
|
2015-06-22 10:31:12 +08:00
|
|
|
}
|
|
|
|
t := &tty{
|
|
|
|
console: console,
|
|
|
|
state: state,
|
|
|
|
closers: []io.Closer{
|
|
|
|
console,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type tty struct {
|
|
|
|
console libcontainer.Console
|
|
|
|
state *term.State
|
|
|
|
closers []io.Closer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tty) Close() error {
|
|
|
|
for _, c := range t.closers {
|
|
|
|
c.Close()
|
|
|
|
}
|
|
|
|
if t.state != nil {
|
|
|
|
term.RestoreTerminal(os.Stdin.Fd(), t.state)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|