2015-05-27 16:15:14 -04:00
|
|
|
// Shim for the Host Compute Service (HSC) to manage Windows Server
|
|
|
|
// containers and Hyper-V containers.
|
|
|
|
|
|
|
|
package hcsshim
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2016-02-01 18:09:35 -05:00
|
|
|
//go:generate go run mksyscall_windows.go -output zhcsshim.go hcsshim.go
|
|
|
|
|
|
|
|
//sys coTaskMemFree(buffer unsafe.Pointer) = ole32.CoTaskMemFree
|
|
|
|
|
|
|
|
//sys activateLayer(info *driverInfo, id string) (hr error) = vmcompute.ActivateLayer?
|
|
|
|
//sys copyLayer(info *driverInfo, srcId string, dstId string, descriptors []WC_LAYER_DESCRIPTOR) (hr error) = vmcompute.CopyLayer?
|
|
|
|
//sys createLayer(info *driverInfo, id string, parent string) (hr error) = vmcompute.CreateLayer?
|
|
|
|
//sys createSandboxLayer(info *driverInfo, id string, parent string, descriptors []WC_LAYER_DESCRIPTOR) (hr error) = vmcompute.CreateSandboxLayer?
|
|
|
|
//sys deactivateLayer(info *driverInfo, id string) (hr error) = vmcompute.DeactivateLayer?
|
|
|
|
//sys destroyLayer(info *driverInfo, id string) (hr error) = vmcompute.DestroyLayer?
|
|
|
|
//sys exportLayer(info *driverInfo, id string, path string, descriptors []WC_LAYER_DESCRIPTOR) (hr error) = vmcompute.ExportLayer?
|
|
|
|
//sys getLayerMountPath(info *driverInfo, id string, length *uintptr, buffer *uint16) (hr error) = vmcompute.GetLayerMountPath?
|
|
|
|
//sys getBaseImages(buffer **uint16) (hr error) = vmcompute.GetBaseImages?
|
|
|
|
//sys importLayer(info *driverInfo, id string, path string, descriptors []WC_LAYER_DESCRIPTOR) (hr error) = vmcompute.ImportLayer?
|
|
|
|
//sys layerExists(info *driverInfo, id string, exists *uint32) (hr error) = vmcompute.LayerExists?
|
|
|
|
//sys nameToGuid(name string, guid *GUID) (hr error) = vmcompute.NameToGuid?
|
|
|
|
//sys prepareLayer(info *driverInfo, id string, descriptors []WC_LAYER_DESCRIPTOR) (hr error) = vmcompute.PrepareLayer?
|
|
|
|
//sys unprepareLayer(info *driverInfo, id string) (hr error) = vmcompute.UnprepareLayer?
|
|
|
|
|
2016-03-02 17:17:13 -05:00
|
|
|
//sys importLayerBegin(info *driverInfo, id string, descriptors []WC_LAYER_DESCRIPTOR, context *uintptr) (hr error) = vmcompute.ImportLayerBegin?
|
|
|
|
//sys importLayerNext(context uintptr, fileName string, fileInfo *winio.FileBasicInfo) (hr error) = vmcompute.ImportLayerNext?
|
|
|
|
//sys importLayerWrite(context uintptr, buffer []byte) (hr error) = vmcompute.ImportLayerWrite?
|
|
|
|
//sys importLayerEnd(context uintptr) (hr error) = vmcompute.ImportLayerEnd?
|
|
|
|
|
|
|
|
//sys exportLayerBegin(info *driverInfo, id string, descriptors []WC_LAYER_DESCRIPTOR, context *uintptr) (hr error) = vmcompute.ExportLayerBegin?
|
|
|
|
//sys exportLayerNext(context uintptr, fileName **uint16, fileInfo *winio.FileBasicInfo, fileSize *int64, deleted *uint32) (hr error) = vmcompute.ExportLayerNext?
|
|
|
|
//sys exportLayerRead(context uintptr, buffer []byte, bytesRead *uint32) (hr error) = vmcompute.ExportLayerRead?
|
|
|
|
//sys exportLayerEnd(context uintptr) (hr error) = vmcompute.ExportLayerEnd?
|
|
|
|
|
2016-02-01 18:09:35 -05:00
|
|
|
//sys createComputeSystem(id string, configuration string) (hr error) = vmcompute.CreateComputeSystem?
|
|
|
|
//sys createProcessWithStdHandlesInComputeSystem(id string, paramsJson string, pid *uint32, stdin *syscall.Handle, stdout *syscall.Handle, stderr *syscall.Handle) (hr error) = vmcompute.CreateProcessWithStdHandlesInComputeSystem?
|
|
|
|
//sys resizeConsoleInComputeSystem(id string, pid uint32, height uint16, width uint16, flags uint32) (hr error) = vmcompute.ResizeConsoleInComputeSystem?
|
|
|
|
//sys shutdownComputeSystem(id string, timeout uint32) (hr error) = vmcompute.ShutdownComputeSystem?
|
|
|
|
//sys startComputeSystem(id string) (hr error) = vmcompute.StartComputeSystem?
|
|
|
|
//sys terminateComputeSystem(id string) (hr error) = vmcompute.TerminateComputeSystem?
|
|
|
|
//sys terminateProcessInComputeSystem(id string, pid uint32) (hr error) = vmcompute.TerminateProcessInComputeSystem?
|
|
|
|
//sys waitForProcessInComputeSystem(id string, pid uint32, timeout uint32, exitCode *uint32) (hr error) = vmcompute.WaitForProcessInComputeSystem?
|
|
|
|
|
|
|
|
//sys _hnsCall(method string, path string, object string, response **uint16) (hr error) = vmcompute.HNSCall?
|
2015-10-12 19:34:03 -04:00
|
|
|
|
2016-02-01 18:09:35 -05:00
|
|
|
const (
|
2015-10-12 19:34:03 -04:00
|
|
|
// Specific user-visible exit codes
|
|
|
|
WaitErrExecFailed = 32767
|
|
|
|
|
2016-02-03 15:11:21 -05:00
|
|
|
ERROR_GEN_FAILURE = syscall.Errno(31)
|
|
|
|
ERROR_SHUTDOWN_IN_PROGRESS = syscall.Errno(1115)
|
|
|
|
WSAEINVAL = syscall.Errno(10022)
|
2015-10-12 19:34:03 -04:00
|
|
|
|
|
|
|
// Timeout on wait calls
|
|
|
|
TimeoutInfinite = 0xFFFFFFFF
|
2015-05-27 16:15:14 -04:00
|
|
|
)
|
|
|
|
|
2016-02-03 15:11:21 -05:00
|
|
|
type HcsError struct {
|
2016-02-01 18:09:35 -05:00
|
|
|
title string
|
|
|
|
rest string
|
2016-02-03 15:11:21 -05:00
|
|
|
Err error
|
2016-02-01 18:09:35 -05:00
|
|
|
}
|
2015-05-27 16:15:14 -04:00
|
|
|
|
2016-03-02 17:17:13 -05:00
|
|
|
func makeError(err error, title, rest string) error {
|
|
|
|
// Pass through DLL errors directly since they do not originate from HCS.
|
|
|
|
if _, ok := err.(*syscall.DLLError); ok {
|
|
|
|
return err
|
2016-02-03 15:11:21 -05:00
|
|
|
}
|
|
|
|
return &HcsError{title, rest, err}
|
2015-07-16 15:07:20 -04:00
|
|
|
}
|
|
|
|
|
2016-03-02 17:17:13 -05:00
|
|
|
func makeErrorf(err error, title, format string, a ...interface{}) error {
|
2016-02-03 15:11:21 -05:00
|
|
|
return makeError(err, title, fmt.Sprintf(format, a...))
|
2016-02-01 18:09:35 -05:00
|
|
|
}
|
2015-07-16 15:07:20 -04:00
|
|
|
|
2016-02-03 15:11:21 -05:00
|
|
|
func win32FromError(err error) uint32 {
|
|
|
|
if herr, ok := err.(*HcsError); ok {
|
|
|
|
return win32FromError(herr.Err)
|
|
|
|
}
|
|
|
|
if code, ok := err.(syscall.Errno); ok {
|
2016-03-02 17:17:13 -05:00
|
|
|
return uint32(code)
|
2016-02-03 15:11:21 -05:00
|
|
|
}
|
|
|
|
return uint32(ERROR_GEN_FAILURE)
|
2015-05-27 16:15:14 -04:00
|
|
|
}
|
|
|
|
|
2016-03-02 17:17:13 -05:00
|
|
|
func win32FromHresult(hr uintptr) uintptr {
|
2016-02-03 15:11:21 -05:00
|
|
|
if hr&0x1fff0000 == 0x00070000 {
|
|
|
|
return hr & 0xffff
|
2016-02-01 18:09:35 -05:00
|
|
|
}
|
2016-02-03 15:11:21 -05:00
|
|
|
return hr
|
2016-02-01 18:09:35 -05:00
|
|
|
}
|
2015-05-27 16:15:14 -04:00
|
|
|
|
2016-02-03 15:11:21 -05:00
|
|
|
func (e *HcsError) Error() string {
|
2016-03-02 17:17:13 -05:00
|
|
|
s := e.title
|
|
|
|
if len(s) > 0 && s[len(s)-1] != ' ' {
|
|
|
|
s += " "
|
|
|
|
}
|
|
|
|
s += fmt.Sprintf("failed in Win32: %s (0x%x)", e.Err, win32FromError(e.Err))
|
|
|
|
if e.rest != "" {
|
|
|
|
if e.rest[0] != ' ' {
|
|
|
|
s += " "
|
|
|
|
}
|
|
|
|
s += e.rest
|
|
|
|
}
|
|
|
|
return s
|
2016-02-01 18:09:35 -05:00
|
|
|
}
|
2015-05-27 16:15:14 -04:00
|
|
|
|
2016-02-01 18:09:35 -05:00
|
|
|
func convertAndFreeCoTaskMemString(buffer *uint16) string {
|
|
|
|
str := syscall.UTF16ToString((*[1 << 30]uint16)(unsafe.Pointer(buffer))[:])
|
|
|
|
coTaskMemFree(unsafe.Pointer(buffer))
|
|
|
|
return str
|
2015-05-27 16:15:14 -04:00
|
|
|
}
|