2014-02-19 22:33:25 +00:00
|
|
|
package main
|
2014-02-19 01:52:06 +00:00
|
|
|
|
|
|
|
import (
|
2014-02-19 22:33:25 +00:00
|
|
|
"encoding/json"
|
2014-02-19 01:52:06 +00:00
|
|
|
"fmt"
|
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer"
|
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer/capabilities"
|
2014-02-19 23:33:44 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer/network"
|
2014-02-19 07:13:36 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/system"
|
2014-02-19 23:33:44 +00:00
|
|
|
"io/ioutil"
|
2014-02-19 01:52:06 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2014-02-19 22:33:25 +00:00
|
|
|
func loadContainer() (*libcontainer.Container, error) {
|
|
|
|
f, err := os.Open("container.json")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-02-19 18:44:29 +00:00
|
|
|
}
|
2014-02-19 22:33:25 +00:00
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
var container *libcontainer.Container
|
|
|
|
if err := json.NewDecoder(f).Decode(&container); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return container, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
container, err := loadContainer()
|
2014-02-19 01:52:06 +00:00
|
|
|
if err != nil {
|
2014-02-19 22:33:25 +00:00
|
|
|
log.Fatal(err)
|
2014-02-19 01:52:06 +00:00
|
|
|
}
|
|
|
|
|
2014-02-19 22:33:25 +00:00
|
|
|
if os.Args[1] == "exec" {
|
2014-02-19 22:55:34 +00:00
|
|
|
_, err := execCommand(container)
|
2014-02-19 22:33:25 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
console := os.Args[1]
|
|
|
|
|
|
|
|
if err := setLogFile(container); err != nil {
|
|
|
|
log.Fatal(err)
|
2014-02-19 01:52:06 +00:00
|
|
|
}
|
|
|
|
|
2014-02-19 22:33:25 +00:00
|
|
|
rootfs, err := resolveRootfs()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-02-19 23:54:53 +00:00
|
|
|
var tempVethName string
|
|
|
|
if container.Network != nil {
|
|
|
|
data, err := ioutil.ReadAll(os.Stdin)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error reading from stdin %s", err)
|
|
|
|
}
|
|
|
|
tempVethName = string(data)
|
2014-02-19 23:33:44 +00:00
|
|
|
}
|
|
|
|
|
2014-02-19 22:33:25 +00:00
|
|
|
// close pipes so that we can replace it with the pty
|
|
|
|
os.Stdin.Close()
|
|
|
|
os.Stdout.Close()
|
|
|
|
os.Stderr.Close()
|
|
|
|
|
|
|
|
slave, err := openTerminal(console, syscall.O_RDWR)
|
2014-02-19 01:52:06 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("open terminal %s", err)
|
2014-02-19 22:33:25 +00:00
|
|
|
}
|
|
|
|
if slave.Fd() != 0 {
|
|
|
|
log.Fatalf("slave fd should be 0")
|
2014-02-19 01:52:06 +00:00
|
|
|
}
|
|
|
|
if err := dupSlave(slave); err != nil {
|
|
|
|
log.Fatalf("dup2 slave %s", err)
|
|
|
|
}
|
2014-02-19 07:13:36 +00:00
|
|
|
if _, err := system.Setsid(); err != nil {
|
2014-02-19 01:52:06 +00:00
|
|
|
log.Fatalf("setsid %s", err)
|
|
|
|
}
|
2014-02-19 07:13:36 +00:00
|
|
|
if err := system.Setctty(); err != nil {
|
2014-02-19 01:52:06 +00:00
|
|
|
log.Fatalf("setctty %s", err)
|
|
|
|
}
|
2014-02-19 07:13:36 +00:00
|
|
|
if err := system.ParentDeathSignal(); err != nil {
|
2014-02-19 01:52:06 +00:00
|
|
|
log.Fatalf("parent deth signal %s", err)
|
|
|
|
}
|
2014-02-19 22:33:25 +00:00
|
|
|
if err := setupNewMountNamespace(rootfs, console, container.ReadonlyFs); err != nil {
|
2014-02-19 01:52:06 +00:00
|
|
|
log.Fatalf("setup mount namespace %s", err)
|
|
|
|
}
|
2014-02-19 22:33:25 +00:00
|
|
|
if container.Network != nil {
|
2014-02-19 23:54:53 +00:00
|
|
|
if err := setupNetworking(container.Network, tempVethName); err != nil {
|
2014-02-19 22:33:25 +00:00
|
|
|
log.Fatalf("setup networking %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-19 07:13:36 +00:00
|
|
|
if err := system.Sethostname(container.ID); err != nil {
|
2014-02-19 01:52:06 +00:00
|
|
|
log.Fatalf("sethostname %s", err)
|
|
|
|
}
|
|
|
|
if err := capabilities.DropCapabilities(container); err != nil {
|
|
|
|
log.Fatalf("drop capabilities %s", err)
|
|
|
|
}
|
|
|
|
if err := setupUser(container); err != nil {
|
|
|
|
log.Fatalf("setup user %s", err)
|
|
|
|
}
|
|
|
|
if container.WorkingDir != "" {
|
2014-02-19 07:13:36 +00:00
|
|
|
if err := system.Chdir(container.WorkingDir); err != nil {
|
2014-02-19 01:52:06 +00:00
|
|
|
log.Fatalf("chdir to %s %s", container.WorkingDir, err)
|
|
|
|
}
|
|
|
|
}
|
2014-02-19 07:13:36 +00:00
|
|
|
if err := system.Exec(container.Command.Args[0], container.Command.Args[0:], container.Command.Env); err != nil {
|
2014-02-19 01:52:06 +00:00
|
|
|
log.Fatalf("exec %s", err)
|
|
|
|
}
|
|
|
|
panic("unreachable")
|
|
|
|
}
|
|
|
|
|
2014-02-19 22:33:25 +00:00
|
|
|
func resolveRootfs() (string, error) {
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
rootfs, err := filepath.Abs(cwd)
|
2014-02-19 01:52:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return filepath.EvalSymlinks(rootfs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupUser(container *libcontainer.Container) error {
|
|
|
|
// TODO: honor user passed on container
|
2014-02-19 07:13:36 +00:00
|
|
|
if err := system.Setgroups(nil); err != nil {
|
2014-02-19 01:52:06 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-02-19 07:13:36 +00:00
|
|
|
if err := system.Setresgid(0, 0, 0); err != nil {
|
2014-02-19 01:52:06 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-02-19 07:13:36 +00:00
|
|
|
if err := system.Setresuid(0, 0, 0); err != nil {
|
2014-02-19 01:52:06 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func dupSlave(slave *os.File) error {
|
|
|
|
// we close Stdin,etc so our pty slave should have fd 0
|
|
|
|
if slave.Fd() != 0 {
|
|
|
|
return fmt.Errorf("slave fd not 0 %d", slave.Fd())
|
|
|
|
}
|
2014-02-19 07:13:36 +00:00
|
|
|
if err := system.Dup2(slave.Fd(), 1); err != nil {
|
2014-02-19 01:52:06 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-02-19 07:13:36 +00:00
|
|
|
if err := system.Dup2(slave.Fd(), 2); err != nil {
|
2014-02-19 01:52:06 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-02-19 07:13:36 +00:00
|
|
|
// openTerminal is a clone of os.OpenFile without the O_CLOEXEC addition.
|
2014-02-19 01:52:06 +00:00
|
|
|
func openTerminal(name string, flag int) (*os.File, error) {
|
|
|
|
r, e := syscall.Open(name, flag, 0)
|
|
|
|
if e != nil {
|
|
|
|
return nil, &os.PathError{"open", name, e}
|
|
|
|
}
|
|
|
|
return os.NewFile(uintptr(r), name), nil
|
|
|
|
}
|
2014-02-19 18:44:29 +00:00
|
|
|
|
|
|
|
func setLogFile(container *libcontainer.Container) error {
|
2014-02-19 22:55:34 +00:00
|
|
|
if container.LogFile != "" {
|
|
|
|
f, err := os.OpenFile(container.LogFile, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0655)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.SetOutput(f)
|
2014-02-19 18:44:29 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2014-02-19 22:33:25 +00:00
|
|
|
|
2014-02-19 23:54:53 +00:00
|
|
|
func setupNetworking(config *libcontainer.Network, tempVethName string) error {
|
|
|
|
if err := network.InterfaceDown(tempVethName); err != nil {
|
|
|
|
return fmt.Errorf("interface down %s %s", tempVethName, err)
|
|
|
|
}
|
|
|
|
if err := network.ChangeInterfaceName(tempVethName, "eth0"); err != nil {
|
|
|
|
return fmt.Errorf("change %s to eth0 %s", tempVethName, err)
|
|
|
|
}
|
|
|
|
if err := network.SetInterfaceIp("eth0", config.IP); err != nil {
|
|
|
|
return fmt.Errorf("set eth0 ip %s", err)
|
|
|
|
}
|
|
|
|
if err := network.SetMtu("eth0", config.Mtu); err != nil {
|
|
|
|
return fmt.Errorf("set eth0 mtu to %d %s", config.Mtu, err)
|
|
|
|
}
|
|
|
|
if err := network.InterfaceUp("eth0"); err != nil {
|
|
|
|
return fmt.Errorf("eth0 up %s", err)
|
|
|
|
}
|
|
|
|
if err := network.SetMtu("lo", config.Mtu); err != nil {
|
|
|
|
return fmt.Errorf("set lo mtu to %d %s", config.Mtu, err)
|
|
|
|
}
|
|
|
|
if err := network.InterfaceUp("lo"); err != nil {
|
|
|
|
return fmt.Errorf("lo up %s", err)
|
|
|
|
}
|
|
|
|
if config.Gateway != "" {
|
|
|
|
if err := network.SetDefaultGateway(config.Gateway); err != nil {
|
|
|
|
return fmt.Errorf("set gateway to %s %s", config.Gateway, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2014-02-19 22:33:25 +00:00
|
|
|
}
|