runc/nsinit/exec.go

117 lines
2.5 KiB
Go
Raw Normal View History

package nsinit
import (
"fmt"
"io"
"log"
"os"
"os/exec"
"os/signal"
"syscall"
"github.com/codegangsta/cli"
2014-06-10 23:14:16 +08:00
"github.com/docker/libcontainer"
consolepkg "github.com/docker/libcontainer/console"
2014-06-10 23:14:16 +08:00
"github.com/docker/libcontainer/namespaces"
"github.com/dotcloud/docker/pkg/term"
)
var execCommand = cli.Command{
Name: "exec",
Usage: "execute a new command inside a container",
Action: execAction,
}
func execAction(context *cli.Context) {
var exitCode int
container, err := loadContainer()
if err != nil {
log.Fatal(err)
}
state, err := libcontainer.GetState(dataPath)
if err != nil && !os.IsNotExist(err) {
log.Fatalf("unable to read state.json: %s", err)
}
if state != nil {
err = namespaces.ExecIn(container, state, []string(context.Args()))
} else {
exitCode, err = startContainer(container, dataPath, []string(context.Args()))
}
if err != nil {
log.Fatalf("failed to exec: %s", err)
}
os.Exit(exitCode)
}
// 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 *libcontainer.Config, dataPath string, args []string) (int, error) {
var (
cmd *exec.Cmd
sigc = make(chan os.Signal, 10)
)
signal.Notify(sigc)
createCommand := func(container *libcontainer.Config, console, rootfs, dataPath, init string, pipe *os.File, args []string) *exec.Cmd {
cmd = namespaces.DefaultCreateCommand(container, console, rootfs, dataPath, init, pipe, args)
if logPath != "" {
cmd.Env = append(cmd.Env, fmt.Sprintf("log=%s", logPath))
}
return cmd
}
startCallback := func() {
go func() {
for sig := range sigc {
cmd.Process.Signal(sig)
}
}()
}
var (
stdin = os.Stdin
stdout = os.Stdout
stderr = os.Stderr
)
if container.Tty {
master, slavePath, err := consolepkg.CreateMasterAndConsole()
if err != nil {
return -1, err
}
slave, err := consolepkg.OpenTerminal(slavePath, syscall.O_RDWR|syscall.O_NOCTTY)
if err != nil {
return -1, err
}
stdin, stdout, stderr = slave, slave, slave
go io.Copy(master, os.Stdin)
go io.Copy(os.Stdout, master)
ws, err := term.GetWinsize(os.Stdin.Fd())
if err != nil {
return -1, err
}
if err := term.SetWinsize(master.Fd(), ws); err != nil {
return -1, err
}
if _, err := term.SetRawTerminal(os.Stdin.Fd()); err != nil {
return -1, err
}
}
return namespaces.Exec(container, stdin, stdout, stderr, "", dataPath, args, createCommand, startCallback)
}