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"
|
2016-02-27 04:14:47 +08:00
|
|
|
"sync"
|
2016-06-03 23:29:34 +08:00
|
|
|
"syscall"
|
2015-06-22 10:31:12 +08:00
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/term"
|
|
|
|
"github.com/opencontainers/runc/libcontainer"
|
2016-09-03 01:31:54 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer/utils"
|
2015-06-22 10:31:12 +08:00
|
|
|
)
|
|
|
|
|
2016-06-03 23:29:34 +08:00
|
|
|
type tty struct {
|
|
|
|
console libcontainer.Console
|
|
|
|
state *term.State
|
|
|
|
closers []io.Closer
|
|
|
|
postStart []io.Closer
|
|
|
|
wg sync.WaitGroup
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tty) copyIO(w io.Writer, r io.ReadCloser) {
|
|
|
|
defer t.wg.Done()
|
|
|
|
io.Copy(w, r)
|
|
|
|
r.Close()
|
|
|
|
}
|
|
|
|
|
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.
|
2016-04-23 21:39:38 +08:00
|
|
|
func createStdioPipes(p *libcontainer.Process, rootuid, rootgid int) (*tty, error) {
|
|
|
|
i, err := p.InitializeIO(rootuid, rootgid)
|
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-27 04:14:47 +08:00
|
|
|
// add the process's io to the post start closers if they support close
|
|
|
|
for _, cc := range []interface{}{
|
|
|
|
p.Stdin,
|
|
|
|
p.Stdout,
|
|
|
|
p.Stderr,
|
|
|
|
} {
|
|
|
|
if c, ok := cc.(io.Closer); ok {
|
|
|
|
t.postStart = append(t.postStart, c)
|
|
|
|
}
|
|
|
|
}
|
2016-02-02 07:00:09 +08:00
|
|
|
go func() {
|
|
|
|
io.Copy(i.Stdin, os.Stdin)
|
|
|
|
i.Stdin.Close()
|
|
|
|
}()
|
2016-02-27 04:14:47 +08:00
|
|
|
t.wg.Add(2)
|
|
|
|
go t.copyIO(os.Stdout, i.Stdout)
|
|
|
|
go t.copyIO(os.Stderr, i.Stderr)
|
2015-06-22 10:31:12 +08:00
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
2016-06-03 23:29:34 +08:00
|
|
|
func dupStdio(process *libcontainer.Process, rootuid, rootgid int) error {
|
|
|
|
process.Stdin = os.Stdin
|
|
|
|
process.Stdout = os.Stdout
|
|
|
|
process.Stderr = os.Stderr
|
|
|
|
for _, fd := range []uintptr{
|
|
|
|
os.Stdin.Fd(),
|
|
|
|
os.Stdout.Fd(),
|
|
|
|
os.Stderr.Fd(),
|
|
|
|
} {
|
|
|
|
if err := syscall.Fchown(int(fd), rootuid, rootgid); err != nil {
|
|
|
|
return err
|
2016-01-08 06:34:11 +08:00
|
|
|
}
|
|
|
|
}
|
2016-06-03 23:29:34 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tty) recvtty(process *libcontainer.Process, detach bool) error {
|
|
|
|
console, err := process.GetConsole()
|
2015-06-22 10:31:12 +08:00
|
|
|
if err != nil {
|
2016-06-03 23:29:34 +08:00
|
|
|
return err
|
2015-06-22 10:31:12 +08:00
|
|
|
}
|
2016-01-08 06:34:11 +08:00
|
|
|
|
2016-06-03 23:29:34 +08:00
|
|
|
if !detach {
|
|
|
|
go io.Copy(console, os.Stdin)
|
|
|
|
t.wg.Add(1)
|
|
|
|
go t.copyIO(os.Stdout, console)
|
|
|
|
|
|
|
|
state, err := term.SetRawTerminal(os.Stdin.Fd())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to set the terminal from the stdin: %v", err)
|
|
|
|
}
|
|
|
|
t.state = state
|
2015-06-22 10:31:12 +08:00
|
|
|
}
|
|
|
|
|
2016-06-03 23:29:34 +08:00
|
|
|
t.console = console
|
|
|
|
t.closers = []io.Closer{console}
|
|
|
|
return nil
|
2015-06-22 10:31:12 +08:00
|
|
|
}
|
|
|
|
|
2016-09-03 01:31:54 +08:00
|
|
|
func (t *tty) sendtty(socket *os.File, ti *libcontainer.TerminalInfo) error {
|
|
|
|
if t.console == nil {
|
|
|
|
return fmt.Errorf("tty.console not set")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a fake file to contain the terminal info.
|
|
|
|
console := os.NewFile(t.console.File().Fd(), ti.String())
|
2017-01-07 07:57:31 +08:00
|
|
|
return utils.SendFd(socket, console)
|
2016-09-03 01:31:54 +08:00
|
|
|
}
|
|
|
|
|
2016-02-27 04:14:47 +08:00
|
|
|
// ClosePostStart closes any fds that are provided to the container and dup2'd
|
|
|
|
// so that we no longer have copy in our process.
|
|
|
|
func (t *tty) ClosePostStart() error {
|
|
|
|
for _, c := range t.postStart {
|
|
|
|
c.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes all open fds for the tty and/or restores the orignal
|
|
|
|
// stdin state to what it was prior to the container execution
|
2015-06-22 10:31:12 +08:00
|
|
|
func (t *tty) Close() error {
|
2016-02-27 04:14:47 +08:00
|
|
|
// ensure that our side of the fds are always closed
|
|
|
|
for _, c := range t.postStart {
|
|
|
|
c.Close()
|
|
|
|
}
|
|
|
|
// wait for the copy routines to finish before closing the fds
|
|
|
|
t.wg.Wait()
|
2015-06-22 10:31:12 +08:00
|
|
|
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
|
|
|
|
}
|
2016-09-03 01:31:54 +08:00
|
|
|
return term.SetWinsize(t.console.File().Fd(), ws)
|
2015-06-22 10:31:12 +08:00
|
|
|
}
|