2014-02-22 03:29:21 -05:00
|
|
|
package nsinit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2014-02-25 00:11:52 -05:00
|
|
|
// CommandFactory takes the container's configuration and options passed by the
|
|
|
|
// parent processes and creates an *exec.Cmd that will be used to fork/exec the
|
|
|
|
// namespaced init process
|
2014-02-22 03:29:21 -05:00
|
|
|
type CommandFactory interface {
|
2014-02-25 00:11:52 -05:00
|
|
|
Create(container *libcontainer.Container, console string, syncFd uintptr, args []string) *exec.Cmd
|
2014-02-22 03:29:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type DefaultCommandFactory struct{}
|
|
|
|
|
|
|
|
// Create will return an exec.Cmd with the Cloneflags set to the proper namespaces
|
|
|
|
// defined on the container's configuration and use the current binary as the init with the
|
|
|
|
// args provided
|
2014-02-25 00:11:52 -05:00
|
|
|
func (c *DefaultCommandFactory) Create(container *libcontainer.Container, console string, pipe uintptr, args []string) *exec.Cmd {
|
2014-02-22 03:29:21 -05:00
|
|
|
// get our binary name so we can always reexec ourself
|
|
|
|
name := os.Args[0]
|
|
|
|
command := exec.Command(name, append([]string{
|
|
|
|
"-console", console,
|
|
|
|
"-pipe", fmt.Sprint(pipe),
|
|
|
|
"init"}, args...)...)
|
|
|
|
|
|
|
|
command.SysProcAttr = &syscall.SysProcAttr{
|
|
|
|
Cloneflags: uintptr(GetNamespaceFlags(container.Namespaces)),
|
|
|
|
}
|
|
|
|
command.Env = container.Env
|
|
|
|
return command
|
|
|
|
}
|