2015-06-22 10:31:12 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-06-25 15:19:44 +08:00
|
|
|
"os"
|
2015-06-22 10:31:12 +08:00
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/codegangsta/cli"
|
|
|
|
"github.com/opencontainers/runc/libcontainer"
|
|
|
|
)
|
|
|
|
|
|
|
|
func execContainer(context *cli.Context, spec *LinuxSpec) (int, error) {
|
|
|
|
if len(spec.Processes) != 1 {
|
|
|
|
return -1, fmt.Errorf("runc only supports one(1) process for the container")
|
|
|
|
}
|
|
|
|
config, err := createLibcontainerConfig(spec)
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
2015-06-25 15:19:44 +08:00
|
|
|
if _, err := os.Stat(config.Rootfs); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return -1, fmt.Errorf("Rootfs (%q) does not exist", config.Rootfs)
|
|
|
|
}
|
|
|
|
return -1, err
|
|
|
|
}
|
2015-06-22 10:31:12 +08:00
|
|
|
rootuid, err := config.HostUID()
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
factory, err := loadFactory(context)
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
container, err := factory.Create(context.GlobalString("id"), config)
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
// ensure that the container is always removed if we were the process
|
|
|
|
// that created it.
|
|
|
|
defer destroy(container)
|
|
|
|
process := newProcess(spec.Processes[0])
|
|
|
|
tty, err := newTty(spec.Processes[0].TTY, process, rootuid)
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
handler := newSignalHandler(tty)
|
|
|
|
defer handler.Close()
|
|
|
|
if err := container.Start(process); err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
return handler.forward(process)
|
|
|
|
}
|
|
|
|
|
|
|
|
func destroy(container libcontainer.Container) {
|
|
|
|
status, err := container.Status()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
}
|
|
|
|
if status != libcontainer.Checkpointed {
|
|
|
|
if err := container.Destroy(); err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|