nsinit: clean up
Signed-off-by: Andrey Vagin <avagin@openvz.org>
This commit is contained in:
parent
ce9d63376f
commit
c406a6b6e0
147
nsinit/exec.go
147
nsinit/exec.go
|
@ -3,20 +3,14 @@ package main
|
|||
import (
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"text/tabwriter"
|
||||
|
||||
"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"
|
||||
"github.com/docker/libcontainer/namespaces"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -106,144 +100,3 @@ func execAction(context *cli.Context) {
|
|||
|
||||
os.Exit(exitCode)
|
||||
}
|
||||
|
||||
// the process for execing a new process inside an existing container is that we have to exec ourself
|
||||
// with the nsenter argument so that the C code can setns an the namespaces that we require. Then that
|
||||
// code path will drop us into the path that we can do the final setup of the namespace and exec the users
|
||||
// application.
|
||||
func startInExistingContainer(config *configs.Config, state *configs.State, action string, context *cli.Context) (int, error) {
|
||||
var (
|
||||
master *os.File
|
||||
console string
|
||||
err error
|
||||
|
||||
sigc = make(chan os.Signal, 10)
|
||||
|
||||
stdin = os.Stdin
|
||||
stdout = os.Stdout
|
||||
stderr = os.Stderr
|
||||
)
|
||||
signal.Notify(sigc)
|
||||
|
||||
if config.Tty {
|
||||
stdin = nil
|
||||
stdout = nil
|
||||
stderr = nil
|
||||
|
||||
master, console, err = consolepkg.CreateMasterAndConsole()
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
go io.Copy(master, os.Stdin)
|
||||
go io.Copy(os.Stdout, master)
|
||||
|
||||
state, err := term.SetRawTerminal(os.Stdin.Fd())
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
defer term.RestoreTerminal(os.Stdin.Fd(), state)
|
||||
}
|
||||
|
||||
startCallback := func(cmd *exec.Cmd) {
|
||||
go func() {
|
||||
resizeTty(master)
|
||||
|
||||
for sig := range sigc {
|
||||
switch sig {
|
||||
case syscall.SIGWINCH:
|
||||
resizeTty(master)
|
||||
default:
|
||||
cmd.Process.Signal(sig)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
return namespaces.ExecIn(config, state, context.Args(), os.Args[0], action, stdin, stdout, stderr, console, startCallback)
|
||||
}
|
||||
|
||||
// startContainer starts the container. Returns the exit status or -1 and an
|
||||
// error.
|
||||
//
|
||||
// Signals sent to the current process will be forwarded to container.
|
||||
func startContainer(container *configs.Config, dataPath string, args []string) (int, error) {
|
||||
var (
|
||||
cmd *exec.Cmd
|
||||
sigc = make(chan os.Signal, 10)
|
||||
)
|
||||
|
||||
signal.Notify(sigc)
|
||||
|
||||
createCommand := func(container *configs.Config, console, dataPath, init string, pipe *os.File, args []string) *exec.Cmd {
|
||||
cmd = namespaces.DefaultCreateCommand(container, console, dataPath, init, pipe, args)
|
||||
if logPath != "" {
|
||||
cmd.Env = append(cmd.Env, fmt.Sprintf("log=%s", logPath))
|
||||
}
|
||||
return cmd
|
||||
}
|
||||
|
||||
var (
|
||||
master *os.File
|
||||
console string
|
||||
err error
|
||||
|
||||
stdin = os.Stdin
|
||||
stdout = os.Stdout
|
||||
stderr = os.Stderr
|
||||
)
|
||||
|
||||
if container.Tty {
|
||||
stdin = nil
|
||||
stdout = nil
|
||||
stderr = nil
|
||||
|
||||
master, console, err = consolepkg.CreateMasterAndConsole()
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
go io.Copy(master, os.Stdin)
|
||||
go io.Copy(os.Stdout, master)
|
||||
|
||||
state, err := term.SetRawTerminal(os.Stdin.Fd())
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
defer term.RestoreTerminal(os.Stdin.Fd(), state)
|
||||
}
|
||||
|
||||
startCallback := func() {
|
||||
go func() {
|
||||
resizeTty(master)
|
||||
|
||||
for sig := range sigc {
|
||||
switch sig {
|
||||
case syscall.SIGWINCH:
|
||||
resizeTty(master)
|
||||
default:
|
||||
cmd.Process.Signal(sig)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
return namespaces.Exec(container, stdin, stdout, stderr, console, dataPath, args, createCommand, startCallback)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue