2016-03-18 18:53:27 +00:00
|
|
|
package libcontainerd
|
|
|
|
|
|
|
|
import (
|
2016-10-12 00:11:56 +00:00
|
|
|
"fmt"
|
2016-03-18 18:53:27 +00:00
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
2016-04-18 20:10:15 +00:00
|
|
|
"time"
|
2016-03-18 18:53:27 +00:00
|
|
|
|
|
|
|
"github.com/Microsoft/hcsshim"
|
|
|
|
"github.com/Sirupsen/logrus"
|
2016-09-27 17:26:59 +00:00
|
|
|
"github.com/opencontainers/runtime-spec/specs-go"
|
2016-03-18 18:53:27 +00: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 17:26:59 +00:00
|
|
|
ociSpec specs.Spec
|
2016-05-10 23:02:44 +00:00
|
|
|
|
|
|
|
manualStopRequested bool
|
2016-05-23 23:12:06 +00:00
|
|
|
hcsContainer hcsshim.Container
|
2016-03-18 18:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ctr *container) newProcess(friendlyName string) *process {
|
|
|
|
return &process{
|
|
|
|
processCommon: processCommon{
|
|
|
|
containerID: ctr.containerID,
|
|
|
|
friendlyName: friendlyName,
|
|
|
|
client: ctr.client,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-15 23:51:45 +00:00
|
|
|
// start starts a created container.
|
|
|
|
// Caller needs to lock container ID before calling this method.
|
2016-03-18 18:53:27 +00:00
|
|
|
func (ctr *container) start() error {
|
|
|
|
var err error
|
2016-06-09 01:57:25 +00:00
|
|
|
isServicing := false
|
|
|
|
|
|
|
|
for _, option := range ctr.options {
|
|
|
|
if s, ok := option.(*ServicingOption); ok && s.IsServicing {
|
|
|
|
isServicing = true
|
|
|
|
}
|
|
|
|
}
|
2016-03-18 18:53:27 +00:00
|
|
|
|
2016-04-13 20:34:07 +00: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 22:20:14 +00:00
|
|
|
logrus.Debugln("libcontainerd: starting container ", ctr.containerID)
|
2016-05-23 23:12:06 +00:00
|
|
|
if err = ctr.hcsContainer.Start(); err != nil {
|
2016-07-22 22:20:14 +00:00
|
|
|
logrus.Errorf("libcontainerd: failed to start container: %s", err)
|
2016-05-25 18:08:15 +00:00
|
|
|
if err := ctr.terminate(); err != nil {
|
2016-07-22 22:20:14 +00:00
|
|
|
logrus.Errorf("libcontainerd: failed to cleanup after a failed Start. %s", err)
|
2016-05-25 18:08:15 +00:00
|
|
|
} else {
|
2016-07-22 22:20:14 +00:00
|
|
|
logrus.Debugln("libcontainerd: cleaned up after failed Start by calling Terminate")
|
2016-05-25 18:08:15 +00:00
|
|
|
}
|
2016-03-18 18:53:27 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-05-23 23:12:06 +00: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.
|
|
|
|
createProcessParms := &hcsshim.ProcessConfig{
|
2016-03-18 18:53:27 +00:00
|
|
|
EmulateConsole: ctr.ociSpec.Process.Terminal,
|
|
|
|
WorkingDirectory: ctr.ociSpec.Process.Cwd,
|
2016-06-09 01:57:25 +00:00
|
|
|
CreateStdInPipe: !isServicing,
|
|
|
|
CreateStdOutPipe: !isServicing,
|
|
|
|
CreateStdErrPipe: !ctr.ociSpec.Process.Terminal && !isServicing,
|
2016-03-18 18:53:27 +00:00
|
|
|
}
|
2016-09-22 20:52:04 +00:00
|
|
|
createProcessParms.ConsoleSize[0] = uint(ctr.ociSpec.Process.ConsoleSize.Height)
|
|
|
|
createProcessParms.ConsoleSize[1] = uint(ctr.ociSpec.Process.ConsoleSize.Width)
|
2016-03-18 18:53:27 +00:00
|
|
|
|
|
|
|
// Configure the environment for the process
|
|
|
|
createProcessParms.Environment = setupEnvironmentVariables(ctr.ociSpec.Process.Env)
|
|
|
|
createProcessParms.CommandLine = strings.Join(ctr.ociSpec.Process.Args, " ")
|
|
|
|
|
2016-05-23 23:12:06 +00:00
|
|
|
// Start the command running in the container.
|
2016-08-15 23:51:45 +00:00
|
|
|
newProcess, err := ctr.hcsContainer.CreateProcess(createProcessParms)
|
2016-03-18 18:53:27 +00:00
|
|
|
if err != nil {
|
2016-07-22 22:20:14 +00:00
|
|
|
logrus.Errorf("libcontainerd: CreateProcess() failed %s", err)
|
2016-05-25 18:08:15 +00:00
|
|
|
if err := ctr.terminate(); err != nil {
|
2016-07-22 22:20:14 +00:00
|
|
|
logrus.Errorf("libcontainerd: failed to cleanup after a failed CreateProcess. %s", err)
|
2016-03-18 18:53:27 +00:00
|
|
|
} else {
|
2016-07-22 22:20:14 +00:00
|
|
|
logrus.Debugln("libcontainerd: cleaned up after failed CreateProcess by calling Terminate")
|
2016-03-18 18:53:27 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-08-15 23:51:45 +00:00
|
|
|
pid := newProcess.Pid()
|
|
|
|
|
2016-05-23 23:12:06 +00:00
|
|
|
// Save the hcs Process and PID
|
|
|
|
ctr.process.friendlyName = InitFriendlyName
|
2016-09-21 00:31:24 +00:00
|
|
|
ctr.process.hcsProcess = newProcess
|
2016-05-23 23:12:06 +00:00
|
|
|
|
2016-06-09 01:57:25 +00:00
|
|
|
// If this is a servicing container, wait on the process synchronously here and
|
2016-08-15 21:30:08 +00:00
|
|
|
// if it succeeds, wait for it cleanly shutdown and merge into the parent container.
|
2016-06-09 01:57:25 +00:00
|
|
|
if isServicing {
|
|
|
|
exitCode := ctr.waitProcessExitCode(&ctr.process)
|
|
|
|
|
|
|
|
if exitCode != 0 {
|
2016-10-12 00:11:56 +00: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-09 01:57:25 +00:00
|
|
|
}
|
|
|
|
|
2016-08-15 21:30:08 +00:00
|
|
|
return ctr.hcsContainer.WaitTimeout(time.Minute * 5)
|
2016-06-09 01:57:25 +00:00
|
|
|
}
|
|
|
|
|
2016-05-23 23:12:06 +00:00
|
|
|
var stdout, stderr io.ReadCloser
|
|
|
|
var stdin io.WriteCloser
|
2016-08-15 23:51:45 +00:00
|
|
|
stdin, stdout, stderr, err = newProcess.Stdio()
|
2016-05-23 23:12:06 +00:00
|
|
|
if err != nil {
|
2016-07-22 22:20:14 +00:00
|
|
|
logrus.Errorf("libcontainerd: failed to get stdio pipes: %s", err)
|
2016-05-23 23:12:06 +00:00
|
|
|
if err := ctr.terminate(); err != nil {
|
2016-07-22 22:20:14 +00:00
|
|
|
logrus.Errorf("libcontainerd: failed to cleanup after a failed Stdio. %s", err)
|
2016-05-23 23:12:06 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
iopipe := &IOPipe{Terminal: ctr.ociSpec.Process.Terminal}
|
|
|
|
|
2016-08-15 23:51:45 +00:00
|
|
|
iopipe.Stdin = createStdInCloser(stdin, newProcess)
|
2016-05-23 23:12:06 +00:00
|
|
|
|
2016-03-18 18:53:27 +00:00
|
|
|
// Convert io.ReadClosers to io.Readers
|
|
|
|
if stdout != nil {
|
|
|
|
iopipe.Stdout = openReaderFromPipe(stdout)
|
|
|
|
}
|
|
|
|
if stderr != nil {
|
|
|
|
iopipe.Stderr = openReaderFromPipe(stderr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the PID
|
2016-07-22 22:20:14 +00:00
|
|
|
logrus.Debugf("libcontainerd: process started - PID %d", pid)
|
2016-03-18 18:53:27 +00:00
|
|
|
ctr.systemPid = uint32(pid)
|
|
|
|
|
|
|
|
// Spin up a go routine waiting for exit to handle cleanup
|
2016-05-23 23:12:06 +00:00
|
|
|
go ctr.waitExit(&ctr.process, true)
|
2016-03-18 18:53:27 +00:00
|
|
|
|
|
|
|
ctr.client.appendContainer(ctr)
|
|
|
|
|
|
|
|
if err := ctr.client.backend.AttachStreams(ctr.containerID, *iopipe); err != nil {
|
|
|
|
// 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-02 00:02:38 +00:00
|
|
|
CommonStateInfo: CommonStateInfo{
|
|
|
|
State: StateStart,
|
|
|
|
Pid: ctr.systemPid, // Not sure this is needed? Double-check monitor.go in daemon BUGBUG @jhowardmsft
|
|
|
|
}}
|
2016-08-15 23:51:45 +00:00
|
|
|
logrus.Debugf("libcontainerd: start() completed OK, %+v", si)
|
2016-03-18 18:53:27 +00:00
|
|
|
return ctr.client.backend.StateChanged(ctr.containerID, si)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-06-09 01:57:25 +00: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 18:53:27 +00:00
|
|
|
// Block indefinitely for the process to exit.
|
2016-05-23 23:12:06 +00:00
|
|
|
err := process.hcsProcess.Wait()
|
2016-03-18 18:53:27 +00:00
|
|
|
if err != nil {
|
2016-05-23 23:12:06 +00:00
|
|
|
if herr, ok := err.(*hcsshim.ProcessError); ok && herr.Err != syscall.ERROR_BROKEN_PIPE {
|
2016-07-22 22:20:14 +00:00
|
|
|
logrus.Warnf("libcontainerd: Wait() failed (container may have been killed): %s", err)
|
2016-03-18 18:53:27 +00:00
|
|
|
}
|
|
|
|
// Fall through here, do not return. This ensures we attempt to continue the
|
2016-05-23 23:12:06 +00: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 {
|
|
|
|
if herr, ok := err.(*hcsshim.ProcessError); ok && herr.Err != syscall.ERROR_BROKEN_PIPE {
|
2016-07-22 22:20:14 +00:00
|
|
|
logrus.Warnf("libcontainerd: unable to get exit code from container %s", ctr.containerID)
|
2016-05-23 23:12:06 +00:00
|
|
|
}
|
2016-06-14 17:19:55 +00: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 23:12:06 +00: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 18:53:27 +00:00
|
|
|
// has exited to avoid a container being dropped on the floor.
|
|
|
|
}
|
|
|
|
|
2016-06-09 01:57:25 +00: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 22:20:14 +00:00
|
|
|
logrus.Debugln("libcontainerd: waitExit() on pid", process.systemPid)
|
2016-06-09 01:57:25 +00:00
|
|
|
|
|
|
|
exitCode := ctr.waitProcessExitCode(process)
|
2016-08-15 23:51:45 +00:00
|
|
|
// Lock the container while shutting down
|
|
|
|
ctr.client.lock(ctr.containerID)
|
2016-06-09 01:57:25 +00:00
|
|
|
|
2016-03-18 18:53:27 +00:00
|
|
|
// Assume the container has exited
|
|
|
|
si := StateInfo{
|
2016-04-02 00:02:38 +00:00
|
|
|
CommonStateInfo: CommonStateInfo{
|
|
|
|
State: StateExit,
|
|
|
|
ExitCode: uint32(exitCode),
|
2016-05-23 23:12:06 +00:00
|
|
|
Pid: process.systemPid,
|
|
|
|
ProcessID: process.friendlyName,
|
2016-04-02 00:02:38 +00:00
|
|
|
},
|
|
|
|
UpdatePending: false,
|
2016-03-18 18:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// But it could have been an exec'd process which exited
|
|
|
|
if !isFirstProcessToStart {
|
|
|
|
si.State = StateExitProcess
|
2016-08-15 23:51:45 +00:00
|
|
|
ctr.cleanProcess(process.friendlyName)
|
2016-05-10 23:02:44 +00:00
|
|
|
} else {
|
2016-05-23 23:12:06 +00:00
|
|
|
updatePending, err := ctr.hcsContainer.HasPendingUpdates()
|
2016-04-13 20:34:07 +00:00
|
|
|
if err != nil {
|
2016-07-22 22:20:14 +00:00
|
|
|
logrus.Warnf("libcontainerd: HasPendingUpdates() failed (container may have been killed): %s", err)
|
2016-04-13 20:34:07 +00:00
|
|
|
} else {
|
2016-05-23 23:12:06 +00:00
|
|
|
si.UpdatePending = updatePending
|
2016-04-13 20:34:07 +00:00
|
|
|
}
|
2016-04-02 00:02:38 +00:00
|
|
|
|
2016-07-22 22:20:14 +00:00
|
|
|
logrus.Debugf("libcontainerd: shutting down container %s", ctr.containerID)
|
2016-05-23 23:12:06 +00:00
|
|
|
if err := ctr.shutdown(); err != nil {
|
2016-07-22 22:20:14 +00:00
|
|
|
logrus.Debugf("libcontainerd: failed to shutdown container %s", ctr.containerID)
|
2016-03-18 18:53:27 +00:00
|
|
|
} else {
|
2016-07-22 22:20:14 +00:00
|
|
|
logrus.Debugf("libcontainerd: completed shutting down container %s", ctr.containerID)
|
2016-03-18 18:53:27 +00:00
|
|
|
}
|
2016-05-23 23:12:06 +00:00
|
|
|
if err := ctr.hcsContainer.Close(); err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
}
|
2016-03-18 18:53:27 +00:00
|
|
|
|
|
|
|
// Remove process from list if we have exited
|
|
|
|
if si.State == StateExit {
|
2016-08-15 23:51:45 +00:00
|
|
|
ctr.client.deleteContainer(ctr.containerID)
|
2016-03-18 18:53:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-15 23:51:45 +00:00
|
|
|
if err := process.hcsProcess.Close(); err != nil {
|
|
|
|
logrus.Errorf("libcontainerd: hcsProcess.Close(): %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock here before we call back into the daemon to update state
|
|
|
|
ctr.client.unlock(ctr.containerID)
|
|
|
|
|
2016-03-18 18:53:27 +00:00
|
|
|
// Call into the backend to notify it of the state change.
|
2016-07-22 22:20:14 +00:00
|
|
|
logrus.Debugf("libcontainerd: waitExit() calling backend.StateChanged %+v", si)
|
2016-03-18 18:53:27 +00:00
|
|
|
if err := ctr.client.backend.StateChanged(ctr.containerID, si); err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
}
|
2016-09-21 20:42:26 +00:00
|
|
|
|
|
|
|
logrus.Debugf("libcontainerd: waitExit() completed OK, %+v", si)
|
|
|
|
|
2016-03-18 18:53:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
2016-05-23 23:12:06 +00:00
|
|
|
|
2016-08-15 23:51:45 +00: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 23:12:06 +00:00
|
|
|
func (ctr *container) shutdown() error {
|
|
|
|
const shutdownTimeout = time.Minute * 5
|
|
|
|
err := ctr.hcsContainer.Shutdown()
|
2016-08-04 00:47:43 +00:00
|
|
|
if hcsshim.IsPending(err) {
|
2016-05-23 23:12:06 +00:00
|
|
|
// Explicit timeout to avoid a (remote) possibility that shutdown hangs indefinitely.
|
|
|
|
err = ctr.hcsContainer.WaitTimeout(shutdownTimeout)
|
2016-08-04 00:47:43 +00:00
|
|
|
} else if hcsshim.IsAlreadyStopped(err) {
|
2016-06-20 23:57:08 +00:00
|
|
|
err = nil
|
2016-05-23 23:12:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2016-07-22 22:20:14 +00:00
|
|
|
logrus.Debugf("libcontainerd: error shutting down container %s %v calling terminate", ctr.containerID, err)
|
2016-05-23 23:12:06 +00:00
|
|
|
if err := ctr.terminate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-15 23:51:45 +00:00
|
|
|
// terminate terminates the container in HCS
|
|
|
|
// Caller needs to lock container ID before calling this method.
|
2016-05-23 23:12:06 +00:00
|
|
|
func (ctr *container) terminate() error {
|
|
|
|
const terminateTimeout = time.Minute * 5
|
|
|
|
err := ctr.hcsContainer.Terminate()
|
|
|
|
|
2016-08-04 00:47:43 +00:00
|
|
|
if hcsshim.IsPending(err) {
|
2016-05-23 23:12:06 +00:00
|
|
|
err = ctr.hcsContainer.WaitTimeout(terminateTimeout)
|
2016-08-04 00:47:43 +00:00
|
|
|
} else if hcsshim.IsAlreadyStopped(err) {
|
2016-06-20 23:57:08 +00:00
|
|
|
err = nil
|
2016-05-23 23:12:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2016-07-22 22:20:14 +00:00
|
|
|
logrus.Debugf("libcontainerd: error terminating container %s %v", ctr.containerID, err)
|
2016-05-23 23:12:06 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|