2018-03-19 18:57:30 -04:00
|
|
|
package hcsshim
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"syscall"
|
2018-09-05 13:35:51 -04:00
|
|
|
|
|
|
|
"github.com/Microsoft/hcsshim/internal/hns"
|
|
|
|
|
|
|
|
"github.com/Microsoft/hcsshim/internal/hcs"
|
|
|
|
"github.com/Microsoft/hcsshim/internal/hcserror"
|
2018-03-19 18:57:30 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-09-05 13:35:51 -04:00
|
|
|
// ErrComputeSystemDoesNotExist is an error encountered when the container being operated on no longer exists = hcs.exist
|
|
|
|
ErrComputeSystemDoesNotExist = hcs.ErrComputeSystemDoesNotExist
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
// ErrElementNotFound is an error encountered when the object being referenced does not exist
|
2018-09-05 13:35:51 -04:00
|
|
|
ErrElementNotFound = hcs.ErrElementNotFound
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
// ErrElementNotFound is an error encountered when the object being referenced does not exist
|
2018-09-05 13:35:51 -04:00
|
|
|
ErrNotSupported = hcs.ErrNotSupported
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
// ErrInvalidData is an error encountered when the request being sent to hcs is invalid/unsupported
|
|
|
|
// decimal -2147024883 / hex 0x8007000d
|
2018-09-05 13:35:51 -04:00
|
|
|
ErrInvalidData = hcs.ErrInvalidData
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
// ErrHandleClose is an error encountered when the handle generating the notification being waited on has been closed
|
2018-09-05 13:35:51 -04:00
|
|
|
ErrHandleClose = hcs.ErrHandleClose
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
// ErrAlreadyClosed is an error encountered when using a handle that has been closed by the Close method
|
2018-09-05 13:35:51 -04:00
|
|
|
ErrAlreadyClosed = hcs.ErrAlreadyClosed
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
// ErrInvalidNotificationType is an error encountered when an invalid notification type is used
|
2018-09-05 13:35:51 -04:00
|
|
|
ErrInvalidNotificationType = hcs.ErrInvalidNotificationType
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
// ErrInvalidProcessState is an error encountered when the process is not in a valid state for the requested operation
|
2018-09-05 13:35:51 -04:00
|
|
|
ErrInvalidProcessState = hcs.ErrInvalidProcessState
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
// ErrTimeout is an error encountered when waiting on a notification times out
|
2018-09-05 13:35:51 -04:00
|
|
|
ErrTimeout = hcs.ErrTimeout
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
// ErrUnexpectedContainerExit is the error encountered when a container exits while waiting for
|
|
|
|
// a different expected notification
|
2018-09-05 13:35:51 -04:00
|
|
|
ErrUnexpectedContainerExit = hcs.ErrUnexpectedContainerExit
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
// ErrUnexpectedProcessAbort is the error encountered when communication with the compute service
|
|
|
|
// is lost while waiting for a notification
|
2018-09-05 13:35:51 -04:00
|
|
|
ErrUnexpectedProcessAbort = hcs.ErrUnexpectedProcessAbort
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
// ErrUnexpectedValue is an error encountered when hcs returns an invalid value
|
2018-09-05 13:35:51 -04:00
|
|
|
ErrUnexpectedValue = hcs.ErrUnexpectedValue
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
// ErrVmcomputeAlreadyStopped is an error encountered when a shutdown or terminate request is made on a stopped container
|
2018-09-05 13:35:51 -04:00
|
|
|
ErrVmcomputeAlreadyStopped = hcs.ErrVmcomputeAlreadyStopped
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
// ErrVmcomputeOperationPending is an error encountered when the operation is being completed asynchronously
|
2018-09-05 13:35:51 -04:00
|
|
|
ErrVmcomputeOperationPending = hcs.ErrVmcomputeOperationPending
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
// ErrVmcomputeOperationInvalidState is an error encountered when the compute system is not in a valid state for the requested operation
|
2018-09-05 13:35:51 -04:00
|
|
|
ErrVmcomputeOperationInvalidState = hcs.ErrVmcomputeOperationInvalidState
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
// ErrProcNotFound is an error encountered when the the process cannot be found
|
2018-09-05 13:35:51 -04:00
|
|
|
ErrProcNotFound = hcs.ErrProcNotFound
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
// ErrVmcomputeOperationAccessIsDenied is an error which can be encountered when enumerating compute systems in RS1/RS2
|
|
|
|
// builds when the underlying silo might be in the process of terminating. HCS was fixed in RS3.
|
2018-09-05 13:35:51 -04:00
|
|
|
ErrVmcomputeOperationAccessIsDenied = hcs.ErrVmcomputeOperationAccessIsDenied
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
// ErrVmcomputeInvalidJSON is an error encountered when the compute system does not support/understand the messages sent by management
|
2018-09-05 13:35:51 -04:00
|
|
|
ErrVmcomputeInvalidJSON = hcs.ErrVmcomputeInvalidJSON
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
// ErrVmcomputeUnknownMessage is an error encountered guest compute system doesn't support the message
|
2018-09-05 13:35:51 -04:00
|
|
|
ErrVmcomputeUnknownMessage = hcs.ErrVmcomputeUnknownMessage
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
// ErrNotSupported is an error encountered when hcs doesn't support the request
|
2018-09-05 13:35:51 -04:00
|
|
|
ErrPlatformNotSupported = hcs.ErrPlatformNotSupported
|
2018-03-19 18:57:30 -04:00
|
|
|
)
|
|
|
|
|
2018-09-05 13:35:51 -04:00
|
|
|
type EndpointNotFoundError = hns.EndpointNotFoundError
|
|
|
|
type NetworkNotFoundError = hns.NetworkNotFoundError
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
// ProcessError is an error encountered in HCS during an operation on a Process object
|
|
|
|
type ProcessError struct {
|
|
|
|
Process *process
|
|
|
|
Operation string
|
|
|
|
ExtraInfo string
|
|
|
|
Err error
|
2018-09-05 13:35:51 -04:00
|
|
|
Events []hcs.ErrorEvent
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ContainerError is an error encountered in HCS during an operation on a Container object
|
|
|
|
type ContainerError struct {
|
|
|
|
Container *container
|
|
|
|
Operation string
|
|
|
|
ExtraInfo string
|
|
|
|
Err error
|
2018-09-05 13:35:51 -04:00
|
|
|
Events []hcs.ErrorEvent
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ContainerError) Error() string {
|
|
|
|
if e == nil {
|
|
|
|
return "<nil>"
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.Container == nil {
|
|
|
|
return "unexpected nil container for error: " + e.Err.Error()
|
|
|
|
}
|
|
|
|
|
2018-09-05 13:35:51 -04:00
|
|
|
s := "container " + e.Container.system.ID()
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
if e.Operation != "" {
|
|
|
|
s += " encountered an error during " + e.Operation
|
|
|
|
}
|
|
|
|
|
|
|
|
switch e.Err.(type) {
|
|
|
|
case nil:
|
|
|
|
break
|
|
|
|
case syscall.Errno:
|
2018-09-05 13:35:51 -04:00
|
|
|
s += fmt.Sprintf(": failure in a Windows system call: %s (0x%x)", e.Err, hcserror.Win32FromError(e.Err))
|
2018-03-19 18:57:30 -04:00
|
|
|
default:
|
|
|
|
s += fmt.Sprintf(": %s", e.Err.Error())
|
|
|
|
}
|
|
|
|
|
2018-09-05 13:35:51 -04:00
|
|
|
for _, ev := range e.Events {
|
|
|
|
s += "\n" + ev.String()
|
|
|
|
}
|
|
|
|
|
2018-03-19 18:57:30 -04:00
|
|
|
if e.ExtraInfo != "" {
|
|
|
|
s += " extra info: " + e.ExtraInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeContainerError(container *container, operation string, extraInfo string, err error) error {
|
|
|
|
// Don't double wrap errors
|
|
|
|
if _, ok := err.(*ContainerError); ok {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
containerError := &ContainerError{Container: container, Operation: operation, ExtraInfo: extraInfo, Err: err}
|
|
|
|
return containerError
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ProcessError) Error() string {
|
|
|
|
if e == nil {
|
|
|
|
return "<nil>"
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.Process == nil {
|
|
|
|
return "Unexpected nil process for error: " + e.Err.Error()
|
|
|
|
}
|
|
|
|
|
2018-09-05 13:35:51 -04:00
|
|
|
s := fmt.Sprintf("process %d in container %s", e.Process.p.Pid(), e.Process.p.SystemID())
|
2018-03-19 18:57:30 -04:00
|
|
|
if e.Operation != "" {
|
|
|
|
s += " encountered an error during " + e.Operation
|
|
|
|
}
|
|
|
|
|
|
|
|
switch e.Err.(type) {
|
|
|
|
case nil:
|
|
|
|
break
|
|
|
|
case syscall.Errno:
|
2018-09-05 13:35:51 -04:00
|
|
|
s += fmt.Sprintf(": failure in a Windows system call: %s (0x%x)", e.Err, hcserror.Win32FromError(e.Err))
|
2018-03-19 18:57:30 -04:00
|
|
|
default:
|
|
|
|
s += fmt.Sprintf(": %s", e.Err.Error())
|
|
|
|
}
|
|
|
|
|
2018-09-05 13:35:51 -04:00
|
|
|
for _, ev := range e.Events {
|
|
|
|
s += "\n" + ev.String()
|
|
|
|
}
|
|
|
|
|
2018-03-19 18:57:30 -04:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeProcessError(process *process, operation string, extraInfo string, err error) error {
|
|
|
|
// Don't double wrap errors
|
|
|
|
if _, ok := err.(*ProcessError); ok {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
processError := &ProcessError{Process: process, Operation: operation, ExtraInfo: extraInfo, Err: err}
|
|
|
|
return processError
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsNotExist checks if an error is caused by the Container or Process not existing.
|
|
|
|
// Note: Currently, ErrElementNotFound can mean that a Process has either
|
|
|
|
// already exited, or does not exist. Both IsAlreadyStopped and IsNotExist
|
|
|
|
// will currently return true when the error is ErrElementNotFound or ErrProcNotFound.
|
|
|
|
func IsNotExist(err error) bool {
|
|
|
|
if _, ok := err.(EndpointNotFoundError); ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if _, ok := err.(NetworkNotFoundError); ok {
|
|
|
|
return true
|
|
|
|
}
|
2018-09-05 13:35:51 -04:00
|
|
|
return hcs.IsNotExist(getInnerError(err))
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsAlreadyClosed checks if an error is caused by the Container or Process having been
|
|
|
|
// already closed by a call to the Close() method.
|
|
|
|
func IsAlreadyClosed(err error) bool {
|
2018-09-05 13:35:51 -04:00
|
|
|
return hcs.IsAlreadyClosed(getInnerError(err))
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsPending returns a boolean indicating whether the error is that
|
|
|
|
// the requested operation is being completed in the background.
|
|
|
|
func IsPending(err error) bool {
|
2018-09-05 13:35:51 -04:00
|
|
|
return hcs.IsPending(getInnerError(err))
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsTimeout returns a boolean indicating whether the error is caused by
|
|
|
|
// a timeout waiting for the operation to complete.
|
|
|
|
func IsTimeout(err error) bool {
|
2018-09-05 13:35:51 -04:00
|
|
|
return hcs.IsTimeout(getInnerError(err))
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsAlreadyStopped returns a boolean indicating whether the error is caused by
|
|
|
|
// a Container or Process being already stopped.
|
|
|
|
// Note: Currently, ErrElementNotFound can mean that a Process has either
|
|
|
|
// already exited, or does not exist. Both IsAlreadyStopped and IsNotExist
|
|
|
|
// will currently return true when the error is ErrElementNotFound or ErrProcNotFound.
|
|
|
|
func IsAlreadyStopped(err error) bool {
|
2018-09-05 13:35:51 -04:00
|
|
|
return hcs.IsAlreadyStopped(getInnerError(err))
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsNotSupported returns a boolean indicating whether the error is caused by
|
|
|
|
// unsupported platform requests
|
|
|
|
// Note: Currently Unsupported platform requests can be mean either
|
|
|
|
// ErrVmcomputeInvalidJSON, ErrInvalidData, ErrNotSupported or ErrVmcomputeUnknownMessage
|
|
|
|
// is thrown from the Platform
|
|
|
|
func IsNotSupported(err error) bool {
|
2018-09-05 13:35:51 -04:00
|
|
|
return hcs.IsNotSupported(getInnerError(err))
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func getInnerError(err error) error {
|
|
|
|
switch pe := err.(type) {
|
|
|
|
case nil:
|
|
|
|
return nil
|
|
|
|
case *ContainerError:
|
|
|
|
err = pe.Err
|
|
|
|
case *ProcessError:
|
|
|
|
err = pe.Err
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2018-09-05 13:35:51 -04:00
|
|
|
|
|
|
|
func convertSystemError(err error, c *container) error {
|
|
|
|
if serr, ok := err.(*hcs.SystemError); ok {
|
|
|
|
return &ContainerError{Container: c, Operation: serr.Op, ExtraInfo: serr.Extra, Err: serr.Err, Events: serr.Events}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertProcessError(err error, p *process) error {
|
|
|
|
if perr, ok := err.(*hcs.ProcessError); ok {
|
|
|
|
return &ProcessError{Process: p, Operation: perr.Op, Err: perr.Err, Events: perr.Events}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|