2016-03-18 14:53:27 -04:00
|
|
|
package libcontainerd
|
|
|
|
|
|
|
|
import (
|
2017-05-26 19:14:18 -04:00
|
|
|
"encoding/json"
|
2016-10-11 20:11:56 -04:00
|
|
|
"fmt"
|
2016-03-18 14:53:27 -04:00
|
|
|
"io"
|
2016-10-14 19:31:27 -04:00
|
|
|
"io/ioutil"
|
2016-03-18 14:53:27 -04:00
|
|
|
"strings"
|
2016-04-18 16:10:15 -04:00
|
|
|
"time"
|
2016-03-18 14:53:27 -04:00
|
|
|
|
|
|
|
"github.com/Microsoft/hcsshim"
|
2016-09-27 13:26:59 -04:00
|
|
|
"github.com/opencontainers/runtime-spec/specs-go"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-05-23 10:22:32 -04:00
|
|
|
"golang.org/x/sys/windows"
|
2016-03-18 14:53:27 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type container struct {
|
|
|
|
containerCommon
|
|
|
|
|
|
|
|
// Platform specific fields are below here. There are none presently on Windows.
|
|
|
|
options []CreateOption
|
|
|
|
|
|
|
|
// The ociSpec is required, as client.Create() needs a spec,
|
|
|
|
// but can be called from the RestartManager context which does not
|
|
|
|
// otherwise have access to the Spec
|
2016-09-27 13:26:59 -04:00
|
|
|
ociSpec specs.Spec
|
2016-05-10 19:02:44 -04:00
|
|
|
|
2017-08-01 13:00:38 -04:00
|
|
|
isWindows bool
|
2016-05-10 19:02:44 -04:00
|
|
|
manualStopRequested bool
|
2016-05-23 19:12:06 -04:00
|
|
|
hcsContainer hcsshim.Container
|
2016-03-18 14:53:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ctr *container) newProcess(friendlyName string) *process {
|
|
|
|
return &process{
|
|
|
|
processCommon: processCommon{
|
|
|
|
containerID: ctr.containerID,
|
|
|
|
friendlyName: friendlyName,
|
|
|
|
client: ctr.client,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-15 19:51:45 -04:00
|
|
|
// start starts a created container.
|
|
|
|
// Caller needs to lock container ID before calling this method.
|
2016-10-17 17:39:52 -04:00
|
|
|
func (ctr *container) start(attachStdio StdioCallback) error {
|
2016-03-18 14:53:27 -04:00
|
|
|
var err error
|
|
|
|
|
2016-04-13 16:34:07 -04:00
|
|
|
// Start the container. If this is a servicing container, this call will block
|
|
|
|
// until the container is done with the servicing execution.
|
2016-07-22 18:20:14 -04:00
|
|
|
logrus.Debugln("libcontainerd: starting container ", ctr.containerID)
|
2016-05-23 19:12:06 -04:00
|
|
|
if err = ctr.hcsContainer.Start(); err != nil {
|
2016-07-22 18:20:14 -04:00
|
|
|
logrus.Errorf("libcontainerd: failed to start container: %s", err)
|
2017-09-08 16:35:01 -04:00
|
|
|
ctr.debugGCS() // Before terminating!
|
2016-05-25 14:08:15 -04:00
|
|
|
if err := ctr.terminate(); err != nil {
|
2016-07-22 18:20:14 -04:00
|
|
|
logrus.Errorf("libcontainerd: failed to cleanup after a failed Start. %s", err)
|
2016-05-25 14:08:15 -04:00
|
|
|
} else {
|
2016-07-22 18:20:14 -04:00
|
|
|
logrus.Debugln("libcontainerd: cleaned up after failed Start by calling Terminate")
|
2016-05-25 14:08:15 -04:00
|
|
|
}
|
2016-03-18 14:53:27 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-09-07 20:02:17 -04:00
|
|
|
defer ctr.debugGCS()
|
2017-09-08 16:35:01 -04:00
|
|
|
|
2016-05-23 19:12:06 -04:00
|
|
|
// Note we always tell HCS to
|
|
|
|
// create stdout as it's required regardless of '-i' or '-t' options, so that
|
|
|
|
// docker can always grab the output through logs. We also tell HCS to always
|
|
|
|
// create stdin, even if it's not used - it will be closed shortly. Stderr
|
|
|
|
// is only created if it we're not -t.
|
2017-08-01 13:00:38 -04:00
|
|
|
var (
|
|
|
|
emulateConsole bool
|
|
|
|
createStdErrPipe bool
|
|
|
|
)
|
|
|
|
if ctr.ociSpec.Process != nil {
|
|
|
|
emulateConsole = ctr.ociSpec.Process.Terminal
|
|
|
|
createStdErrPipe = !ctr.ociSpec.Process.Terminal && !ctr.ociSpec.Windows.Servicing
|
|
|
|
}
|
|
|
|
|
2016-05-23 19:12:06 -04:00
|
|
|
createProcessParms := &hcsshim.ProcessConfig{
|
2017-08-01 13:00:38 -04:00
|
|
|
EmulateConsole: emulateConsole,
|
2016-03-18 14:53:27 -04:00
|
|
|
WorkingDirectory: ctr.ociSpec.Process.Cwd,
|
2017-08-01 13:00:38 -04:00
|
|
|
CreateStdInPipe: !ctr.ociSpec.Windows.Servicing,
|
|
|
|
CreateStdOutPipe: !ctr.ociSpec.Windows.Servicing,
|
|
|
|
CreateStdErrPipe: createStdErrPipe,
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctr.ociSpec.Process != nil && ctr.ociSpec.Process.ConsoleSize != nil {
|
|
|
|
createProcessParms.ConsoleSize[0] = uint(ctr.ociSpec.Process.ConsoleSize.Height)
|
|
|
|
createProcessParms.ConsoleSize[1] = uint(ctr.ociSpec.Process.ConsoleSize.Width)
|
2016-03-18 14:53:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Configure the environment for the process
|
|
|
|
createProcessParms.Environment = setupEnvironmentVariables(ctr.ociSpec.Process.Env)
|
2017-08-01 13:00:38 -04:00
|
|
|
if ctr.isWindows {
|
2017-06-22 20:15:10 -04:00
|
|
|
createProcessParms.CommandLine = strings.Join(ctr.ociSpec.Process.Args, " ")
|
|
|
|
} else {
|
|
|
|
createProcessParms.CommandArgs = ctr.ociSpec.Process.Args
|
|
|
|
}
|
2016-11-08 14:05:56 -05:00
|
|
|
createProcessParms.User = ctr.ociSpec.Process.User.Username
|
2016-03-18 14:53:27 -04:00
|
|
|
|
2017-08-01 13:00:38 -04:00
|
|
|
// LCOW requires the raw OCI spec passed through HCS and onwards to GCS for the utility VM.
|
|
|
|
if !ctr.isWindows {
|
2017-05-26 19:14:18 -04:00
|
|
|
ociBuf, err := json.Marshal(ctr.ociSpec)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ociRaw := json.RawMessage(ociBuf)
|
|
|
|
createProcessParms.OCISpecification = &ociRaw
|
|
|
|
}
|
|
|
|
|
2016-05-23 19:12:06 -04:00
|
|
|
// Start the command running in the container.
|
2016-08-15 19:51:45 -04:00
|
|
|
newProcess, err := ctr.hcsContainer.CreateProcess(createProcessParms)
|
2016-03-18 14:53:27 -04:00
|
|
|
if err != nil {
|
2016-07-22 18:20:14 -04:00
|
|
|
logrus.Errorf("libcontainerd: CreateProcess() failed %s", err)
|
2016-05-25 14:08:15 -04:00
|
|
|
if err := ctr.terminate(); err != nil {
|
2016-07-22 18:20:14 -04:00
|
|
|
logrus.Errorf("libcontainerd: failed to cleanup after a failed CreateProcess. %s", err)
|
2016-03-18 14:53:27 -04:00
|
|
|
} else {
|
2016-07-22 18:20:14 -04:00
|
|
|
logrus.Debugln("libcontainerd: cleaned up after failed CreateProcess by calling Terminate")
|
2016-03-18 14:53:27 -04:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-08-15 19:51:45 -04:00
|
|
|
pid := newProcess.Pid()
|
|
|
|
|
2016-05-23 19:12:06 -04:00
|
|
|
// Save the hcs Process and PID
|
|
|
|
ctr.process.friendlyName = InitFriendlyName
|
2016-09-20 20:31:24 -04:00
|
|
|
ctr.process.hcsProcess = newProcess
|
2016-05-23 19:12:06 -04:00
|
|
|
|
2016-06-08 21:57:25 -04:00
|
|
|
// If this is a servicing container, wait on the process synchronously here and
|
2016-08-15 17:30:08 -04:00
|
|
|
// if it succeeds, wait for it cleanly shutdown and merge into the parent container.
|
2017-08-01 13:00:38 -04:00
|
|
|
if ctr.ociSpec.Windows.Servicing {
|
2016-06-08 21:57:25 -04:00
|
|
|
exitCode := ctr.waitProcessExitCode(&ctr.process)
|
|
|
|
|
|
|
|
if exitCode != 0 {
|
2016-10-11 20:11:56 -04:00
|
|
|
if err := ctr.terminate(); err != nil {
|
|
|
|
logrus.Warnf("libcontainerd: terminating servicing container %s failed: %s", ctr.containerID, err)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("libcontainerd: servicing container %s returned non-zero exit code %d", ctr.containerID, exitCode)
|
2016-06-08 21:57:25 -04:00
|
|
|
}
|
|
|
|
|
2016-08-15 17:30:08 -04:00
|
|
|
return ctr.hcsContainer.WaitTimeout(time.Minute * 5)
|
2016-06-08 21:57:25 -04:00
|
|
|
}
|
|
|
|
|
2016-05-23 19:12:06 -04:00
|
|
|
var stdout, stderr io.ReadCloser
|
|
|
|
var stdin io.WriteCloser
|
2016-08-15 19:51:45 -04:00
|
|
|
stdin, stdout, stderr, err = newProcess.Stdio()
|
2016-05-23 19:12:06 -04:00
|
|
|
if err != nil {
|
2016-07-22 18:20:14 -04:00
|
|
|
logrus.Errorf("libcontainerd: failed to get stdio pipes: %s", err)
|
2016-05-23 19:12:06 -04:00
|
|
|
if err := ctr.terminate(); err != nil {
|
2016-07-22 18:20:14 -04:00
|
|
|
logrus.Errorf("libcontainerd: failed to cleanup after a failed Stdio. %s", err)
|
2016-05-23 19:12:06 -04:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
iopipe := &IOPipe{Terminal: ctr.ociSpec.Process.Terminal}
|
|
|
|
|
2016-08-15 19:51:45 -04:00
|
|
|
iopipe.Stdin = createStdInCloser(stdin, newProcess)
|
2016-05-23 19:12:06 -04:00
|
|
|
|
2016-03-18 14:53:27 -04:00
|
|
|
// Convert io.ReadClosers to io.Readers
|
|
|
|
if stdout != nil {
|
2016-10-14 19:31:27 -04:00
|
|
|
iopipe.Stdout = ioutil.NopCloser(&autoClosingReader{ReadCloser: stdout})
|
2016-03-18 14:53:27 -04:00
|
|
|
}
|
|
|
|
if stderr != nil {
|
2016-10-14 19:31:27 -04:00
|
|
|
iopipe.Stderr = ioutil.NopCloser(&autoClosingReader{ReadCloser: stderr})
|
2016-03-18 14:53:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save the PID
|
2016-07-22 18:20:14 -04:00
|
|
|
logrus.Debugf("libcontainerd: process started - PID %d", pid)
|
2016-03-18 14:53:27 -04:00
|
|
|
ctr.systemPid = uint32(pid)
|
|
|
|
|
|
|
|
// Spin up a go routine waiting for exit to handle cleanup
|
2016-05-23 19:12:06 -04:00
|
|
|
go ctr.waitExit(&ctr.process, true)
|
2016-03-18 14:53:27 -04:00
|
|
|
|
|
|
|
ctr.client.appendContainer(ctr)
|
|
|
|
|
2016-10-17 17:39:52 -04:00
|
|
|
if err := attachStdio(*iopipe); err != nil {
|
2016-03-18 14:53:27 -04:00
|
|
|
// OK to return the error here, as waitExit will handle tear-down in HCS
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tell the docker engine that the container has started.
|
|
|
|
si := StateInfo{
|
2016-04-01 20:02:38 -04:00
|
|
|
CommonStateInfo: CommonStateInfo{
|
|
|
|
State: StateStart,
|
|
|
|
Pid: ctr.systemPid, // Not sure this is needed? Double-check monitor.go in daemon BUGBUG @jhowardmsft
|
|
|
|
}}
|
2016-08-15 19:51:45 -04:00
|
|
|
logrus.Debugf("libcontainerd: start() completed OK, %+v", si)
|
2016-03-18 14:53:27 -04:00
|
|
|
return ctr.client.backend.StateChanged(ctr.containerID, si)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-06-08 21:57:25 -04:00
|
|
|
// waitProcessExitCode will wait for the given process to exit and return its error code.
|
|
|
|
func (ctr *container) waitProcessExitCode(process *process) int {
|
2016-03-18 14:53:27 -04:00
|
|
|
// Block indefinitely for the process to exit.
|
2016-05-23 19:12:06 -04:00
|
|
|
err := process.hcsProcess.Wait()
|
2016-03-18 14:53:27 -04:00
|
|
|
if err != nil {
|
2017-05-23 10:22:32 -04:00
|
|
|
if herr, ok := err.(*hcsshim.ProcessError); ok && herr.Err != windows.ERROR_BROKEN_PIPE {
|
2016-07-22 18:20:14 -04:00
|
|
|
logrus.Warnf("libcontainerd: Wait() failed (container may have been killed): %s", err)
|
2016-03-18 14:53:27 -04:00
|
|
|
}
|
|
|
|
// Fall through here, do not return. This ensures we attempt to continue the
|
2016-05-23 19:12:06 -04:00
|
|
|
// shutdown in HCS and tell the docker engine that the process/container
|
|
|
|
// has exited to avoid a container being dropped on the floor.
|
|
|
|
}
|
|
|
|
|
|
|
|
exitCode, err := process.hcsProcess.ExitCode()
|
|
|
|
if err != nil {
|
2017-05-23 10:22:32 -04:00
|
|
|
if herr, ok := err.(*hcsshim.ProcessError); ok && herr.Err != windows.ERROR_BROKEN_PIPE {
|
2016-07-22 18:20:14 -04:00
|
|
|
logrus.Warnf("libcontainerd: unable to get exit code from container %s", ctr.containerID)
|
2016-05-23 19:12:06 -04:00
|
|
|
}
|
2016-06-14 13:19:55 -04:00
|
|
|
// Since we got an error retrieving the exit code, make sure that the code we return
|
|
|
|
// doesn't incorrectly indicate success.
|
|
|
|
exitCode = -1
|
|
|
|
|
2016-05-23 19:12:06 -04:00
|
|
|
// Fall through here, do not return. This ensures we attempt to continue the
|
|
|
|
// shutdown in HCS and tell the docker engine that the process/container
|
2016-03-18 14:53:27 -04:00
|
|
|
// has exited to avoid a container being dropped on the floor.
|
|
|
|
}
|
|
|
|
|
2016-06-08 21:57:25 -04:00
|
|
|
return exitCode
|
|
|
|
}
|
|
|
|
|
|
|
|
// waitExit runs as a goroutine waiting for the process to exit. It's
|
|
|
|
// equivalent to (in the linux containerd world) where events come in for
|
|
|
|
// state change notifications from containerd.
|
|
|
|
func (ctr *container) waitExit(process *process, isFirstProcessToStart bool) error {
|
2016-07-22 18:20:14 -04:00
|
|
|
logrus.Debugln("libcontainerd: waitExit() on pid", process.systemPid)
|
2016-06-08 21:57:25 -04:00
|
|
|
|
|
|
|
exitCode := ctr.waitProcessExitCode(process)
|
2017-03-07 19:24:34 -05:00
|
|
|
// Lock the container while removing the process/container from the list
|
2016-08-15 19:51:45 -04:00
|
|
|
ctr.client.lock(ctr.containerID)
|
2016-06-08 21:57:25 -04:00
|
|
|
|
2017-03-07 19:24:34 -05:00
|
|
|
if !isFirstProcessToStart {
|
|
|
|
ctr.cleanProcess(process.friendlyName)
|
|
|
|
} else {
|
|
|
|
ctr.client.deleteContainer(ctr.containerID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock here so other threads are unblocked
|
|
|
|
ctr.client.unlock(ctr.containerID)
|
|
|
|
|
2016-03-18 14:53:27 -04:00
|
|
|
// Assume the container has exited
|
|
|
|
si := StateInfo{
|
2016-04-01 20:02:38 -04:00
|
|
|
CommonStateInfo: CommonStateInfo{
|
|
|
|
State: StateExit,
|
|
|
|
ExitCode: uint32(exitCode),
|
2016-05-23 19:12:06 -04:00
|
|
|
Pid: process.systemPid,
|
|
|
|
ProcessID: process.friendlyName,
|
2016-04-01 20:02:38 -04:00
|
|
|
},
|
|
|
|
UpdatePending: false,
|
2016-03-18 14:53:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// But it could have been an exec'd process which exited
|
|
|
|
if !isFirstProcessToStart {
|
|
|
|
si.State = StateExitProcess
|
2016-05-10 19:02:44 -04:00
|
|
|
} else {
|
2017-05-26 19:14:18 -04:00
|
|
|
// Pending updates is only applicable for WCOW
|
2017-08-01 13:00:38 -04:00
|
|
|
if ctr.isWindows {
|
2017-05-26 19:14:18 -04:00
|
|
|
updatePending, err := ctr.hcsContainer.HasPendingUpdates()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Warnf("libcontainerd: HasPendingUpdates() failed (container may have been killed): %s", err)
|
|
|
|
} else {
|
|
|
|
si.UpdatePending = updatePending
|
|
|
|
}
|
2016-04-13 16:34:07 -04:00
|
|
|
}
|
2016-04-01 20:02:38 -04:00
|
|
|
|
2016-07-22 18:20:14 -04:00
|
|
|
logrus.Debugf("libcontainerd: shutting down container %s", ctr.containerID)
|
2016-05-23 19:12:06 -04:00
|
|
|
if err := ctr.shutdown(); err != nil {
|
2016-07-22 18:20:14 -04:00
|
|
|
logrus.Debugf("libcontainerd: failed to shutdown container %s", ctr.containerID)
|
2016-03-18 14:53:27 -04:00
|
|
|
} else {
|
2016-07-22 18:20:14 -04:00
|
|
|
logrus.Debugf("libcontainerd: completed shutting down container %s", ctr.containerID)
|
2016-03-18 14:53:27 -04:00
|
|
|
}
|
2016-05-23 19:12:06 -04:00
|
|
|
if err := ctr.hcsContainer.Close(); err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
}
|
2016-03-18 14:53:27 -04:00
|
|
|
}
|
|
|
|
|
2016-08-15 19:51:45 -04:00
|
|
|
if err := process.hcsProcess.Close(); err != nil {
|
|
|
|
logrus.Errorf("libcontainerd: hcsProcess.Close(): %v", err)
|
|
|
|
}
|
|
|
|
|
2016-03-18 14:53:27 -04:00
|
|
|
// Call into the backend to notify it of the state change.
|
2016-07-22 18:20:14 -04:00
|
|
|
logrus.Debugf("libcontainerd: waitExit() calling backend.StateChanged %+v", si)
|
2016-03-18 14:53:27 -04:00
|
|
|
if err := ctr.client.backend.StateChanged(ctr.containerID, si); err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
}
|
2016-09-21 16:42:26 -04:00
|
|
|
|
|
|
|
logrus.Debugf("libcontainerd: waitExit() completed OK, %+v", si)
|
|
|
|
|
2016-03-18 14:53:27 -04:00
|
|
|
return nil
|
|
|
|
}
|
2016-05-23 19:12:06 -04:00
|
|
|
|
2016-08-15 19:51:45 -04:00
|
|
|
// cleanProcess removes process from the map.
|
|
|
|
// Caller needs to lock container ID before calling this method.
|
|
|
|
func (ctr *container) cleanProcess(id string) {
|
|
|
|
delete(ctr.processes, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// shutdown shuts down the container in HCS
|
|
|
|
// Caller needs to lock container ID before calling this method.
|
2016-05-23 19:12:06 -04:00
|
|
|
func (ctr *container) shutdown() error {
|
|
|
|
const shutdownTimeout = time.Minute * 5
|
|
|
|
err := ctr.hcsContainer.Shutdown()
|
2016-08-03 20:47:43 -04:00
|
|
|
if hcsshim.IsPending(err) {
|
2016-05-23 19:12:06 -04:00
|
|
|
// Explicit timeout to avoid a (remote) possibility that shutdown hangs indefinitely.
|
|
|
|
err = ctr.hcsContainer.WaitTimeout(shutdownTimeout)
|
2016-08-03 20:47:43 -04:00
|
|
|
} else if hcsshim.IsAlreadyStopped(err) {
|
2016-06-20 19:57:08 -04:00
|
|
|
err = nil
|
2016-05-23 19:12:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2016-07-22 18:20:14 -04:00
|
|
|
logrus.Debugf("libcontainerd: error shutting down container %s %v calling terminate", ctr.containerID, err)
|
2016-05-23 19:12:06 -04:00
|
|
|
if err := ctr.terminate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-15 19:51:45 -04:00
|
|
|
// terminate terminates the container in HCS
|
|
|
|
// Caller needs to lock container ID before calling this method.
|
2016-05-23 19:12:06 -04:00
|
|
|
func (ctr *container) terminate() error {
|
|
|
|
const terminateTimeout = time.Minute * 5
|
|
|
|
err := ctr.hcsContainer.Terminate()
|
|
|
|
|
2016-08-03 20:47:43 -04:00
|
|
|
if hcsshim.IsPending(err) {
|
2016-05-23 19:12:06 -04:00
|
|
|
err = ctr.hcsContainer.WaitTimeout(terminateTimeout)
|
2016-08-03 20:47:43 -04:00
|
|
|
} else if hcsshim.IsAlreadyStopped(err) {
|
2016-06-20 19:57:08 -04:00
|
|
|
err = nil
|
2016-05-23 19:12:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2016-07-22 18:20:14 -04:00
|
|
|
logrus.Debugf("libcontainerd: error terminating container %s %v", ctr.containerID, err)
|
2016-05-23 19:12:06 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|