nsinit: return console

Signed-off-by: Andrey Vagin <avagin@openvz.org>
This commit is contained in:
Andrey Vagin 2014-12-26 16:13:10 +03:00 committed by Andrew Vagin
parent 76d395eff2
commit 46e62c9204
1 changed files with 73 additions and 8 deletions

View File

@ -3,13 +3,16 @@ package main
import (
"crypto/md5"
"fmt"
"io"
"log"
"os"
"syscall"
"github.com/codegangsta/cli"
"github.com/docker/docker/pkg/term"
"github.com/docker/libcontainer"
"github.com/docker/libcontainer/configs"
consolepkg "github.com/docker/libcontainer/console"
)
var (
@ -50,21 +53,55 @@ func getContainer(context *cli.Context) (libcontainer.Container, error) {
}
func execAction(context *cli.Context) {
var exitCode int
var (
master *os.File
console string
err error
process := &libcontainer.ProcessConfig{
Args: context.Args(),
Env: context.StringSlice("env"),
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
}
sigc = make(chan os.Signal, 10)
stdin = os.Stdin
stdout = os.Stdout
stderr = os.Stderr
exitCode int
)
container, err := getContainer(context)
if err != nil {
log.Fatal(err)
}
if container.Config().Tty {
stdin = nil
stdout = nil
stderr = nil
master, console, err = consolepkg.CreateMasterAndConsole()
if err != nil {
log.Fatal(err)
}
go io.Copy(master, os.Stdin)
go io.Copy(os.Stdout, master)
state, err := term.SetRawTerminal(os.Stdin.Fd())
if err != nil {
log.Fatal(err)
}
defer term.RestoreTerminal(os.Stdin.Fd(), state)
}
process := &libcontainer.ProcessConfig{
Args: context.Args(),
Env: context.StringSlice("env"),
Stdin: stdin,
Stdout: stdout,
Stderr: stderr,
Console: console,
}
pid, err := container.StartProcess(process)
if err != nil {
log.Fatalf("failed to exec: %s", err)
@ -75,6 +112,19 @@ func execAction(context *cli.Context) {
log.Fatalf("Unable to find the %d process: %s", pid, err)
}
go func() {
resizeTty(master)
for sig := range sigc {
switch sig {
case syscall.SIGWINCH:
resizeTty(master)
default:
p.Signal(sig)
}
}
}()
ps, err := p.Wait()
if err != nil {
log.Fatalf("Unable to wait the %d process: %s", pid, err)
@ -92,3 +142,18 @@ func execAction(context *cli.Context) {
os.Exit(exitCode)
}
func resizeTty(master *os.File) {
if master == nil {
return
}
ws, err := term.GetWinsize(os.Stdin.Fd())
if err != nil {
return
}
if err := term.SetWinsize(master.Fd(), ws); err != nil {
return
}
}