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 ( import (
"crypto/md5" "crypto/md5"
"fmt" "fmt"
"io"
"log" "log"
"os" "os"
"syscall" "syscall"
"github.com/codegangsta/cli" "github.com/codegangsta/cli"
"github.com/docker/docker/pkg/term"
"github.com/docker/libcontainer" "github.com/docker/libcontainer"
"github.com/docker/libcontainer/configs" "github.com/docker/libcontainer/configs"
consolepkg "github.com/docker/libcontainer/console"
) )
var ( var (
@ -50,21 +53,55 @@ func getContainer(context *cli.Context) (libcontainer.Container, error) {
} }
func execAction(context *cli.Context) { func execAction(context *cli.Context) {
var exitCode int var (
master *os.File
console string
err error
process := &libcontainer.ProcessConfig{ sigc = make(chan os.Signal, 10)
Args: context.Args(),
Env: context.StringSlice("env"), stdin = os.Stdin
Stdin: os.Stdin, stdout = os.Stdout
Stdout: os.Stdout, stderr = os.Stderr
Stderr: os.Stderr,
} exitCode int
)
container, err := getContainer(context) container, err := getContainer(context)
if err != nil { if err != nil {
log.Fatal(err) 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) pid, err := container.StartProcess(process)
if err != nil { if err != nil {
log.Fatalf("failed to exec: %s", err) 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) 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() ps, err := p.Wait()
if err != nil { if err != nil {
log.Fatalf("Unable to wait the %d process: %s", pid, err) log.Fatalf("Unable to wait the %d process: %s", pid, err)
@ -92,3 +142,18 @@ func execAction(context *cli.Context) {
os.Exit(exitCode) 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
}
}