2014-07-02 03:51:02 +08:00
|
|
|
package nsinit
|
2014-06-05 07:46:30 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-07-16 07:26:28 +08:00
|
|
|
"io"
|
2014-06-05 07:46:30 +08:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"os/signal"
|
2014-07-16 07:26:28 +08:00
|
|
|
"syscall"
|
2014-06-05 07:46:30 +08:00
|
|
|
|
|
|
|
"github.com/codegangsta/cli"
|
2014-06-10 23:14:16 +08:00
|
|
|
"github.com/docker/libcontainer"
|
2014-07-16 07:26:28 +08:00
|
|
|
consolepkg "github.com/docker/libcontainer/console"
|
2014-06-10 23:14:16 +08:00
|
|
|
"github.com/docker/libcontainer/namespaces"
|
2014-07-16 08:19:32 +08:00
|
|
|
"github.com/dotcloud/docker/pkg/term"
|
2014-06-05 07:46:30 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var execCommand = cli.Command{
|
|
|
|
Name: "exec",
|
|
|
|
Usage: "execute a new command inside a container",
|
|
|
|
Action: execAction,
|
|
|
|
}
|
|
|
|
|
|
|
|
func execAction(context *cli.Context) {
|
2014-06-25 07:54:50 +08:00
|
|
|
var exitCode int
|
2014-06-06 05:28:09 +08:00
|
|
|
|
|
|
|
container, err := loadContainer()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-06-05 07:46:30 +08:00
|
|
|
|
2014-06-25 08:17:41 +08:00
|
|
|
state, err := libcontainer.GetState(dataPath)
|
2014-06-25 07:54:50 +08:00
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
log.Fatalf("unable to read state.json: %s", err)
|
2014-06-05 07:46:30 +08:00
|
|
|
}
|
|
|
|
|
2014-06-25 07:54:50 +08:00
|
|
|
if state != nil {
|
|
|
|
err = namespaces.ExecIn(container, state, []string(context.Args()))
|
2014-06-05 07:46:30 +08:00
|
|
|
} else {
|
2014-07-16 07:26:28 +08:00
|
|
|
exitCode, err = startContainer(container, dataPath, []string(context.Args()))
|
2014-06-05 07:46:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2014-07-16 07:26:28 +08:00
|
|
|
func startContainer(container *libcontainer.Config, dataPath string, args []string) (int, error) {
|
2014-06-05 07:46:30 +08:00
|
|
|
var (
|
|
|
|
cmd *exec.Cmd
|
|
|
|
sigc = make(chan os.Signal, 10)
|
|
|
|
)
|
|
|
|
|
|
|
|
signal.Notify(sigc)
|
|
|
|
|
2014-06-24 07:54:35 +08:00
|
|
|
createCommand := func(container *libcontainer.Config, console, rootfs, dataPath, init string, pipe *os.File, args []string) *exec.Cmd {
|
2014-06-05 07:46:30 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2014-07-16 07:26:28 +08:00
|
|
|
var (
|
|
|
|
stdin = os.Stdin
|
|
|
|
stdout = os.Stdout
|
|
|
|
stderr = os.Stderr
|
|
|
|
)
|
|
|
|
|
|
|
|
if container.Tty {
|
|
|
|
master, slavePath, err := consolepkg.CreateMasterAndConsole()
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
2014-07-16 08:19:32 +08:00
|
|
|
slave, err := consolepkg.OpenTerminal(slavePath, syscall.O_RDWR|syscall.O_NOCTTY)
|
2014-07-16 07:26:28 +08:00
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
stdin, stdout, stderr = slave, slave, slave
|
|
|
|
|
|
|
|
go io.Copy(master, os.Stdin)
|
|
|
|
go io.Copy(os.Stdout, master)
|
2014-07-16 08:19:32 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2014-07-16 07:26:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return namespaces.Exec(container, stdin, stdout, stderr, "", dataPath, args, createCommand, startCallback)
|
2014-06-05 07:46:30 +08:00
|
|
|
}
|