2014-08-13 01:46:43 +08:00
|
|
|
package main
|
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-08-13 08:33:20 +08:00
|
|
|
"text/tabwriter"
|
2014-06-05 07:46:30 +08:00
|
|
|
|
|
|
|
"github.com/codegangsta/cli"
|
2014-07-25 05:21:02 +08:00
|
|
|
"github.com/docker/docker/pkg/term"
|
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-06-05 07:46:30 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var execCommand = cli.Command{
|
|
|
|
Name: "exec",
|
|
|
|
Usage: "execute a new command inside a container",
|
|
|
|
Action: execAction,
|
2014-08-13 08:33:20 +08:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.BoolFlag{Name: "list", Usage: "list all registered exec functions"},
|
|
|
|
cli.StringFlag{Name: "func", Value: "exec", Usage: "function name to exec inside a container"},
|
|
|
|
},
|
2014-06-05 07:46:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func execAction(context *cli.Context) {
|
2014-08-13 08:33:20 +08:00
|
|
|
if context.Bool("list") {
|
|
|
|
w := tabwriter.NewWriter(os.Stdout, 10, 1, 3, ' ', 0)
|
|
|
|
fmt.Fprint(w, "NAME\tUSAGE\n")
|
|
|
|
|
|
|
|
for k, f := range argvs {
|
|
|
|
fmt.Fprintf(w, "%s\t%s\n", k, f.Usage)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Flush()
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-07-22 07:25:46 +08:00
|
|
|
var exitCode int
|
|
|
|
|
2014-08-13 03:03:53 +08:00
|
|
|
container, err := loadConfig()
|
2014-07-22 07:25:46 +08:00
|
|
|
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 {
|
2014-08-13 08:33:20 +08:00
|
|
|
exitCode, err = startInExistingContainer(container, state, context.String("func"), context)
|
2014-07-22 07:25:46 +08:00
|
|
|
} else {
|
|
|
|
exitCode, err = startContainer(container, dataPath, []string(context.Args()))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to exec: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Exit(exitCode)
|
|
|
|
}
|
|
|
|
|
2014-08-07 09:44:41 +08:00
|
|
|
// 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
|
2014-08-08 01:47:42 +08:00
|
|
|
// code path will drop us into the path that we can do the final setup of the namespace and exec the users
|
2014-08-07 09:44:41 +08:00
|
|
|
// application.
|
2014-08-13 02:43:12 +08:00
|
|
|
func startInExistingContainer(config *libcontainer.Config, state *libcontainer.State, action string, context *cli.Context) (int, error) {
|
2014-08-07 09:44:41 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2014-08-13 02:43:12 +08:00
|
|
|
return namespaces.ExecIn(config, state, context.Args(), os.Args[0], action, stdin, stdout, stderr, console, startCallback)
|
2014-08-07 09:44:41 +08:00
|
|
|
}
|
|
|
|
|
2014-06-05 07:46:30 +08:00
|
|
|
// 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-09-24 05:04:55 +08:00
|
|
|
createCommand := func(container *libcontainer.Config, console, dataPath, init string, pipe *os.File, args []string) *exec.Cmd {
|
|
|
|
cmd = namespaces.DefaultCreateCommand(container, console, dataPath, init, pipe, args)
|
2014-06-05 07:46:30 +08:00
|
|
|
if logPath != "" {
|
|
|
|
cmd.Env = append(cmd.Env, fmt.Sprintf("log=%s", logPath))
|
|
|
|
}
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2014-07-16 07:26:28 +08:00
|
|
|
var (
|
2014-07-16 09:24:15 +08:00
|
|
|
master *os.File
|
|
|
|
console string
|
|
|
|
err error
|
|
|
|
|
|
|
|
stdin = os.Stdin
|
|
|
|
stdout = os.Stdout
|
|
|
|
stderr = os.Stderr
|
2014-07-16 07:26:28 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
if container.Tty {
|
2014-07-16 09:24:15 +08:00
|
|
|
stdin = nil
|
|
|
|
stdout = nil
|
|
|
|
stderr = nil
|
2014-07-16 07:26:28 +08:00
|
|
|
|
2014-07-16 09:24:15 +08:00
|
|
|
master, console, err = consolepkg.CreateMasterAndConsole()
|
2014-07-16 07:26:28 +08:00
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
go io.Copy(master, os.Stdin)
|
|
|
|
go io.Copy(os.Stdout, master)
|
2014-07-16 08:19:32 +08:00
|
|
|
|
2014-07-16 08:44:18 +08:00
|
|
|
state, err := term.SetRawTerminal(os.Stdin.Fd())
|
2014-07-16 08:19:32 +08:00
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
2014-07-16 08:44:18 +08:00
|
|
|
defer term.RestoreTerminal(os.Stdin.Fd(), state)
|
|
|
|
}
|
2014-07-16 08:19:32 +08:00
|
|
|
|
2014-07-16 08:44:18 +08:00
|
|
|
startCallback := func() {
|
|
|
|
go func() {
|
|
|
|
resizeTty(master)
|
|
|
|
|
|
|
|
for sig := range sigc {
|
|
|
|
switch sig {
|
|
|
|
case syscall.SIGWINCH:
|
|
|
|
resizeTty(master)
|
|
|
|
default:
|
|
|
|
cmd.Process.Signal(sig)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2014-07-16 07:26:28 +08:00
|
|
|
}
|
|
|
|
|
2014-09-24 05:04:55 +08:00
|
|
|
return namespaces.Exec(container, stdin, stdout, stderr, console, dataPath, args, createCommand, startCallback)
|
2014-06-05 07:46:30 +08:00
|
|
|
}
|
2014-07-16 08:44:18 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|