2016-04-13 05:41:09 +08:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2016-09-03 01:31:54 +08:00
|
|
|
"net"
|
2016-04-13 05:41:09 +08:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2016-06-10 18:35:13 +08:00
|
|
|
"strconv"
|
2016-04-13 05:41:09 +08:00
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2016-05-26 02:24:26 +08:00
|
|
|
"github.com/coreos/go-systemd/activation"
|
2016-04-13 05:41:09 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/cgroups/systemd"
|
2017-03-15 00:36:38 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer/configs"
|
2016-04-13 05:41:09 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer/specconv"
|
2017-03-03 04:53:06 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer/utils"
|
2016-04-13 05:41:09 +08:00
|
|
|
"github.com/opencontainers/runtime-spec/specs-go"
|
2016-06-07 02:45:46 +08:00
|
|
|
"github.com/urfave/cli"
|
2016-04-13 05:41:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var errEmptyID = errors.New("container id cannot be empty")
|
|
|
|
|
|
|
|
var container libcontainer.Container
|
|
|
|
|
|
|
|
// loadFactory returns the configured factory instance for execing containers.
|
|
|
|
func loadFactory(context *cli.Context) (libcontainer.Factory, error) {
|
|
|
|
root := context.GlobalString("root")
|
|
|
|
abs, err := filepath.Abs(root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cgroupManager := libcontainer.Cgroupfs
|
|
|
|
if context.GlobalBool("systemd-cgroup") {
|
|
|
|
if systemd.UseSystemd() {
|
|
|
|
cgroupManager = libcontainer.SystemdCgroups
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("systemd cgroup flag passed, but systemd support for managing cgroups is not available")
|
|
|
|
}
|
|
|
|
}
|
2016-07-07 10:58:55 +08:00
|
|
|
return libcontainer.New(abs, cgroupManager, libcontainer.CriuPath(context.GlobalString("criu")))
|
2016-04-13 05:41:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// getContainer returns the specified container instance by loading it from state
|
|
|
|
// with the default factory.
|
|
|
|
func getContainer(context *cli.Context) (libcontainer.Container, error) {
|
|
|
|
id := context.Args().First()
|
|
|
|
if id == "" {
|
|
|
|
return nil, errEmptyID
|
|
|
|
}
|
|
|
|
factory, err := loadFactory(context)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return factory.Load(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func fatalf(t string, v ...interface{}) {
|
|
|
|
fatal(fmt.Errorf(t, v...))
|
|
|
|
}
|
|
|
|
|
|
|
|
func getDefaultImagePath(context *cli.Context) string {
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return filepath.Join(cwd, "checkpoint")
|
|
|
|
}
|
|
|
|
|
|
|
|
// newProcess returns a new libcontainer Process with the arguments from the
|
|
|
|
// spec and stdio from the current process.
|
|
|
|
func newProcess(p specs.Process) (*libcontainer.Process, error) {
|
|
|
|
lp := &libcontainer.Process{
|
|
|
|
Args: p.Args,
|
|
|
|
Env: p.Env,
|
|
|
|
// TODO: fix libcontainer's API to better support uid/gid in a typesafe way.
|
|
|
|
User: fmt.Sprintf("%d:%d", p.User.UID, p.User.GID),
|
|
|
|
Cwd: p.Cwd,
|
|
|
|
Label: p.SelinuxLabel,
|
|
|
|
NoNewPrivileges: &p.NoNewPrivileges,
|
|
|
|
AppArmorProfile: p.ApparmorProfile,
|
|
|
|
}
|
2017-03-15 00:36:38 +08:00
|
|
|
if p.Capabilities != nil {
|
|
|
|
lp.Capabilities = &configs.Capabilities{}
|
|
|
|
lp.Capabilities.Bounding = p.Capabilities.Bounding
|
|
|
|
lp.Capabilities.Effective = p.Capabilities.Effective
|
|
|
|
lp.Capabilities.Inheritable = p.Capabilities.Inheritable
|
|
|
|
lp.Capabilities.Permitted = p.Capabilities.Permitted
|
|
|
|
lp.Capabilities.Ambient = p.Capabilities.Ambient
|
|
|
|
}
|
2016-06-10 18:35:13 +08:00
|
|
|
for _, gid := range p.User.AdditionalGids {
|
|
|
|
lp.AdditionalGroups = append(lp.AdditionalGroups, strconv.FormatUint(uint64(gid), 10))
|
|
|
|
}
|
2016-04-13 05:41:09 +08:00
|
|
|
for _, rlimit := range p.Rlimits {
|
|
|
|
rl, err := createLibContainerRlimit(rlimit)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
lp.Rlimits = append(lp.Rlimits, rl)
|
|
|
|
}
|
|
|
|
return lp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func destroy(container libcontainer.Container) {
|
|
|
|
if err := container.Destroy(); err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-03 23:29:34 +08:00
|
|
|
// setupIO modifies the given process config according to the options.
|
2017-03-03 04:53:06 +08:00
|
|
|
func setupIO(process *libcontainer.Process, rootuid, rootgid int, createTTY, detach bool, sockpath string) (*tty, error) {
|
2016-04-13 05:41:09 +08:00
|
|
|
if createTTY {
|
2016-06-03 23:29:34 +08:00
|
|
|
process.Stdin = nil
|
|
|
|
process.Stdout = nil
|
|
|
|
process.Stderr = nil
|
2017-03-03 04:53:06 +08:00
|
|
|
t := &tty{}
|
|
|
|
if !detach {
|
|
|
|
parent, child, err := utils.NewSockPair("console")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
process.ConsoleSocket = child
|
|
|
|
t.postStart = append(t.postStart, parent, child)
|
|
|
|
t.consoleC = make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
if err := t.recvtty(process, parent); err != nil {
|
|
|
|
t.consoleC <- err
|
|
|
|
}
|
|
|
|
t.consoleC <- nil
|
|
|
|
}()
|
|
|
|
} else {
|
|
|
|
// the caller of runc will handle receiving the console master
|
|
|
|
conn, err := net.Dial("unix", sockpath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
uc, ok := conn.(*net.UnixConn)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("casting to UnixConn failed")
|
|
|
|
}
|
|
|
|
t.postStart = append(t.postStart, uc)
|
|
|
|
socket, err := uc.File()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
t.postStart = append(t.postStart, socket)
|
|
|
|
process.ConsoleSocket = socket
|
|
|
|
}
|
|
|
|
return t, nil
|
2016-04-13 05:41:09 +08:00
|
|
|
}
|
2017-03-02 02:12:54 +08:00
|
|
|
// when runc will detach the caller provides the stdio to runc via runc's 0,1,2
|
|
|
|
// and the container's process inherits runc's stdio.
|
2016-04-13 05:41:09 +08:00
|
|
|
if detach {
|
2017-03-02 02:12:54 +08:00
|
|
|
if err := inheritStdio(process); err != nil {
|
2016-04-13 05:41:09 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &tty{}, nil
|
|
|
|
}
|
2017-03-02 02:12:54 +08:00
|
|
|
return setupProcessPipes(process, rootuid, rootgid)
|
2016-04-13 05:41:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// createPidFile creates a file with the processes pid inside it atomically
|
|
|
|
// it creates a temp file with the paths filename + '.' infront of it
|
|
|
|
// then renames the file
|
|
|
|
func createPidFile(path string, process *libcontainer.Process) error {
|
|
|
|
pid, err := process.Pid()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
tmpDir = filepath.Dir(path)
|
|
|
|
tmpName = filepath.Join(tmpDir, fmt.Sprintf(".%s", filepath.Base(path)))
|
|
|
|
)
|
|
|
|
f, err := os.OpenFile(tmpName, os.O_RDWR|os.O_CREATE|os.O_EXCL|os.O_SYNC, 0666)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = fmt.Fprintf(f, "%d", pid)
|
|
|
|
f.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return os.Rename(tmpName, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func createContainer(context *cli.Context, id string, spec *specs.Spec) (libcontainer.Container, error) {
|
|
|
|
config, err := specconv.CreateLibcontainerConfig(&specconv.CreateOpts{
|
|
|
|
CgroupName: id,
|
|
|
|
UseSystemdCgroup: context.GlobalBool("systemd-cgroup"),
|
|
|
|
NoPivotRoot: context.Bool("no-pivot"),
|
2016-06-04 02:53:07 +08:00
|
|
|
NoNewKeyring: context.Bool("no-new-keyring"),
|
2016-04-13 05:41:09 +08:00
|
|
|
Spec: spec,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
factory, err := loadFactory(context)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return factory.Create(id, config)
|
|
|
|
}
|
|
|
|
|
|
|
|
type runner struct {
|
|
|
|
enableSubreaper bool
|
|
|
|
shouldDestroy bool
|
|
|
|
detach bool
|
|
|
|
listenFDs []*os.File
|
2016-12-14 00:21:36 +08:00
|
|
|
preserveFDs int
|
2016-04-13 05:41:09 +08:00
|
|
|
pidFile string
|
2016-09-03 01:31:54 +08:00
|
|
|
consoleSocket string
|
2016-04-13 05:41:09 +08:00
|
|
|
container libcontainer.Container
|
2016-05-14 07:54:16 +08:00
|
|
|
create bool
|
2017-02-03 21:17:34 +08:00
|
|
|
notifySocket *notifySocket
|
2016-04-13 05:41:09 +08:00
|
|
|
}
|
|
|
|
|
2016-09-03 01:31:54 +08:00
|
|
|
func (r *runner) terminalinfo() *libcontainer.TerminalInfo {
|
|
|
|
return libcontainer.NewTerminalInfo(r.container.ID())
|
|
|
|
}
|
|
|
|
|
2016-04-13 05:41:09 +08:00
|
|
|
func (r *runner) run(config *specs.Process) (int, error) {
|
2017-03-03 04:53:06 +08:00
|
|
|
if err := r.checkTerminal(config); err != nil {
|
|
|
|
r.destroy()
|
|
|
|
return -1, err
|
|
|
|
}
|
2016-04-13 05:41:09 +08:00
|
|
|
process, err := newProcess(*config)
|
|
|
|
if err != nil {
|
|
|
|
r.destroy()
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
if len(r.listenFDs) > 0 {
|
|
|
|
process.Env = append(process.Env, fmt.Sprintf("LISTEN_FDS=%d", len(r.listenFDs)), "LISTEN_PID=1")
|
|
|
|
process.ExtraFiles = append(process.ExtraFiles, r.listenFDs...)
|
|
|
|
}
|
2016-12-14 00:21:36 +08:00
|
|
|
baseFd := 3 + len(process.ExtraFiles)
|
|
|
|
for i := baseFd; i < baseFd+r.preserveFDs; i++ {
|
|
|
|
process.ExtraFiles = append(process.ExtraFiles, os.NewFile(uintptr(i), "PreserveFD:"+strconv.Itoa(i)))
|
|
|
|
}
|
2016-04-13 05:41:09 +08:00
|
|
|
rootuid, err := r.container.Config().HostUID()
|
|
|
|
if err != nil {
|
|
|
|
r.destroy()
|
|
|
|
return -1, err
|
|
|
|
}
|
2016-04-23 21:39:38 +08:00
|
|
|
rootgid, err := r.container.Config().HostGID()
|
|
|
|
if err != nil {
|
|
|
|
r.destroy()
|
|
|
|
return -1, err
|
|
|
|
}
|
2017-03-03 04:53:06 +08:00
|
|
|
var (
|
|
|
|
detach = r.detach || r.create
|
|
|
|
startFn = r.container.Start
|
|
|
|
)
|
2016-06-07 04:15:18 +08:00
|
|
|
if !r.create {
|
|
|
|
startFn = r.container.Run
|
|
|
|
}
|
2016-06-03 23:29:34 +08:00
|
|
|
// Setting up IO is a two stage process. We need to modify process to deal
|
|
|
|
// with detaching containers, and then we get a tty after the container has
|
|
|
|
// started.
|
2017-02-03 21:17:34 +08:00
|
|
|
handler := newSignalHandler(r.enableSubreaper, r.notifySocket)
|
2017-03-03 04:53:06 +08:00
|
|
|
tty, err := setupIO(process, rootuid, rootgid, config.Terminal, detach, r.consoleSocket)
|
2016-06-03 23:29:34 +08:00
|
|
|
if err != nil {
|
|
|
|
r.destroy()
|
|
|
|
return -1, err
|
|
|
|
}
|
2017-01-26 09:02:00 +08:00
|
|
|
defer tty.Close()
|
2017-01-07 08:21:23 +08:00
|
|
|
if err = startFn(process); err != nil {
|
2016-04-13 05:41:09 +08:00
|
|
|
r.destroy()
|
|
|
|
return -1, err
|
|
|
|
}
|
2017-03-03 04:53:06 +08:00
|
|
|
if err := tty.waitConsole(); err != nil {
|
|
|
|
r.terminate(process)
|
|
|
|
r.destroy()
|
|
|
|
return -1, err
|
2016-09-03 01:31:54 +08:00
|
|
|
}
|
2017-01-07 08:21:23 +08:00
|
|
|
if err = tty.ClosePostStart(); err != nil {
|
2016-04-13 05:41:09 +08:00
|
|
|
r.terminate(process)
|
|
|
|
r.destroy()
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
if r.pidFile != "" {
|
2017-01-07 08:21:23 +08:00
|
|
|
if err = createPidFile(r.pidFile, process); err != nil {
|
2016-04-13 05:41:09 +08:00
|
|
|
r.terminate(process)
|
|
|
|
r.destroy()
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
}
|
2017-02-09 19:27:12 +08:00
|
|
|
status, err := handler.forward(process, tty, detach)
|
2016-04-13 05:41:09 +08:00
|
|
|
if err != nil {
|
|
|
|
r.terminate(process)
|
|
|
|
}
|
2017-02-09 19:27:12 +08:00
|
|
|
if detach {
|
|
|
|
return 0, nil
|
|
|
|
}
|
2016-04-13 05:41:09 +08:00
|
|
|
r.destroy()
|
|
|
|
return status, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *runner) destroy() {
|
|
|
|
if r.shouldDestroy {
|
|
|
|
destroy(r.container)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *runner) terminate(p *libcontainer.Process) {
|
2017-01-07 08:21:23 +08:00
|
|
|
_ = p.Signal(syscall.SIGKILL)
|
|
|
|
_, _ = p.Wait()
|
2016-04-13 05:41:09 +08:00
|
|
|
}
|
|
|
|
|
2017-03-03 04:53:06 +08:00
|
|
|
func (r *runner) checkTerminal(config *specs.Process) error {
|
|
|
|
detach := r.detach || r.create
|
|
|
|
// Check command-line for sanity.
|
|
|
|
if detach && config.Terminal && r.consoleSocket == "" {
|
|
|
|
return fmt.Errorf("cannot allocate tty if runc will detach without setting console socket")
|
|
|
|
}
|
|
|
|
if (!detach || !config.Terminal) && r.consoleSocket != "" {
|
|
|
|
return fmt.Errorf("cannot use console socket if runc will not detach or allocate tty")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-13 05:41:09 +08:00
|
|
|
func validateProcessSpec(spec *specs.Process) error {
|
|
|
|
if spec.Cwd == "" {
|
|
|
|
return fmt.Errorf("Cwd property must not be empty")
|
|
|
|
}
|
|
|
|
if !filepath.IsAbs(spec.Cwd) {
|
|
|
|
return fmt.Errorf("Cwd must be an absolute path")
|
|
|
|
}
|
|
|
|
if len(spec.Args) == 0 {
|
|
|
|
return fmt.Errorf("args must not be empty")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-05-26 02:24:26 +08:00
|
|
|
|
|
|
|
func startContainer(context *cli.Context, spec *specs.Spec, create bool) (int, error) {
|
|
|
|
id := context.Args().First()
|
|
|
|
if id == "" {
|
|
|
|
return -1, errEmptyID
|
|
|
|
}
|
2017-02-03 21:17:34 +08:00
|
|
|
|
|
|
|
notifySocket := newNotifySocket(context, os.Getenv("NOTIFY_SOCKET"), id)
|
|
|
|
if notifySocket != nil {
|
|
|
|
notifySocket.setupSpec(context, spec)
|
|
|
|
}
|
|
|
|
|
2016-05-26 02:24:26 +08:00
|
|
|
container, err := createContainer(context, id, spec)
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
2017-02-03 21:17:34 +08:00
|
|
|
|
|
|
|
if notifySocket != nil {
|
|
|
|
notifySocket.setupSocket()
|
|
|
|
}
|
|
|
|
|
2016-05-26 02:24:26 +08:00
|
|
|
// Support on-demand socket activation by passing file descriptors into the container init process.
|
|
|
|
listenFDs := []*os.File{}
|
|
|
|
if os.Getenv("LISTEN_FDS") != "" {
|
|
|
|
listenFDs = activation.Files(false)
|
|
|
|
}
|
|
|
|
r := &runner{
|
|
|
|
enableSubreaper: !context.Bool("no-subreaper"),
|
|
|
|
shouldDestroy: true,
|
|
|
|
container: container,
|
|
|
|
listenFDs: listenFDs,
|
2017-02-03 21:17:34 +08:00
|
|
|
notifySocket: notifySocket,
|
2016-09-03 01:31:54 +08:00
|
|
|
consoleSocket: context.String("console-socket"),
|
2016-10-20 17:57:37 +08:00
|
|
|
detach: context.Bool("detach"),
|
2016-05-26 02:24:26 +08:00
|
|
|
pidFile: context.String("pid-file"),
|
2016-12-14 00:21:36 +08:00
|
|
|
preserveFDs: context.Int("preserve-fds"),
|
2016-05-26 02:24:26 +08:00
|
|
|
create: create,
|
|
|
|
}
|
|
|
|
return r.run(&spec.Process)
|
|
|
|
}
|