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"
|
2017-07-25 22:42:46 +08:00
|
|
|
"os/signal"
|
2016-02-27 04:14:47 +08:00
|
|
|
"sync"
|
2015-06-22 10:31:12 +08:00
|
|
|
|
2017-05-20 01:18:43 +08:00
|
|
|
"github.com/containerd/console"
|
2015-06-22 10:31:12 +08:00
|
|
|
"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 {
|
2017-05-20 01:18:43 +08:00
|
|
|
epoller *console.Epoller
|
|
|
|
console *console.EpollConsole
|
2017-07-25 22:42:46 +08:00
|
|
|
stdin console.Console
|
2016-06-03 23:29:34 +08:00
|
|
|
closers []io.Closer
|
|
|
|
postStart []io.Closer
|
|
|
|
wg sync.WaitGroup
|
2017-03-03 04:53:06 +08:00
|
|
|
consoleC chan error
|
2016-06-03 23:29:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tty) copyIO(w io.Writer, r io.ReadCloser) {
|
|
|
|
defer t.wg.Done()
|
|
|
|
io.Copy(w, r)
|
|
|
|
r.Close()
|
|
|
|
}
|
|
|
|
|
2017-03-02 02:12:54 +08:00
|
|
|
// setup pipes for the process so that advanced features like c/r are able to easily checkpoint
|
|
|
|
// and restore the process's IO without depending on a host specific path or device
|
|
|
|
func setupProcessPipes(p *libcontainer.Process, rootuid, rootgid int) (*tty, error) {
|
2016-04-23 21:39:38 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-03-02 02:12:54 +08:00
|
|
|
func inheritStdio(process *libcontainer.Process) error {
|
2016-06-03 23:29:34 +08:00
|
|
|
process.Stdin = os.Stdin
|
|
|
|
process.Stdout = os.Stdout
|
|
|
|
process.Stderr = os.Stderr
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-21 09:55:36 +08:00
|
|
|
func (t *tty) recvtty(process *libcontainer.Process, socket *os.File) (Err error) {
|
2017-03-03 04:53:06 +08:00
|
|
|
f, err := utils.RecvFd(socket)
|
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
|
|
|
}
|
2017-05-20 01:18:43 +08:00
|
|
|
cons, err := console.ConsoleFromFile(f)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-09-26 01:23:56 +08:00
|
|
|
console.ClearONLCR(cons.Fd())
|
2017-05-20 01:18:43 +08:00
|
|
|
epoller, err := console.NewEpoller()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
epollConsole, err := epoller.Add(cons)
|
|
|
|
if err != nil {
|
2017-06-08 12:08:44 +08:00
|
|
|
return err
|
|
|
|
}
|
2018-09-21 09:55:36 +08:00
|
|
|
defer func() {
|
|
|
|
if Err != nil {
|
|
|
|
epollConsole.Close()
|
|
|
|
}
|
|
|
|
}()
|
2017-05-20 01:18:43 +08:00
|
|
|
go epoller.Wait()
|
|
|
|
go io.Copy(epollConsole, os.Stdin)
|
2017-03-03 04:53:06 +08:00
|
|
|
t.wg.Add(1)
|
2017-05-20 01:18:43 +08:00
|
|
|
go t.copyIO(os.Stdout, epollConsole)
|
|
|
|
|
2017-07-25 22:42:46 +08:00
|
|
|
// set raw mode to stdin and also handle interrupt
|
|
|
|
stdin, err := console.ConsoleFromFile(os.Stdin)
|
2017-03-03 04:53:06 +08:00
|
|
|
if err != nil {
|
2017-07-25 22:42:46 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := stdin.SetRaw(); err != nil {
|
2017-03-03 04:53:06 +08:00
|
|
|
return fmt.Errorf("failed to set the terminal from the stdin: %v", err)
|
2015-06-22 10:31:12 +08:00
|
|
|
}
|
2017-07-25 22:42:46 +08:00
|
|
|
go handleInterrupt(stdin)
|
|
|
|
|
2017-05-20 01:18:43 +08:00
|
|
|
t.epoller = epoller
|
2017-07-25 22:42:46 +08:00
|
|
|
t.stdin = stdin
|
2017-05-20 01:18:43 +08:00
|
|
|
t.console = epollConsole
|
|
|
|
t.closers = []io.Closer{epollConsole}
|
2016-06-03 23:29:34 +08:00
|
|
|
return nil
|
2015-06-22 10:31:12 +08:00
|
|
|
}
|
|
|
|
|
2017-07-25 22:42:46 +08:00
|
|
|
func handleInterrupt(c console.Console) {
|
|
|
|
sigchan := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigchan, os.Interrupt)
|
|
|
|
<-sigchan
|
|
|
|
c.Reset()
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2017-03-03 04:53:06 +08:00
|
|
|
func (t *tty) waitConsole() error {
|
|
|
|
if t.consoleC != nil {
|
|
|
|
return <-t.consoleC
|
2016-09-03 01:31:54 +08:00
|
|
|
}
|
2017-03-03 04:53:06 +08:00
|
|
|
return nil
|
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
|
|
|
|
}
|
|
|
|
|
2018-09-07 11:37:40 +08:00
|
|
|
// Close closes all open fds for the tty and/or restores the original
|
2016-02-27 04:14:47 +08:00
|
|
|
// 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()
|
|
|
|
}
|
2017-05-20 01:18:43 +08:00
|
|
|
// the process is gone at this point, shutting down the console if we have
|
|
|
|
// one and wait for all IO to be finished
|
|
|
|
if t.console != nil && t.epoller != nil {
|
|
|
|
t.console.Shutdown(t.epoller.CloseConsole)
|
|
|
|
}
|
2016-02-27 04:14:47 +08:00
|
|
|
t.wg.Wait()
|
2015-06-22 10:31:12 +08:00
|
|
|
for _, c := range t.closers {
|
|
|
|
c.Close()
|
|
|
|
}
|
2017-07-25 22:42:46 +08:00
|
|
|
if t.stdin != nil {
|
|
|
|
t.stdin.Reset()
|
2015-06-22 10:31:12 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tty) resize() error {
|
|
|
|
if t.console == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2017-05-20 01:18:43 +08:00
|
|
|
return t.console.ResizeFrom(console.Current())
|
2015-06-22 10:31:12 +08:00
|
|
|
}
|