Skip to content

Commit

Permalink
Merge pull request #874 from dcantah/expand-winapi
Browse files Browse the repository at this point in the history
Add new winapi bindings for job containers
  • Loading branch information
dcantah authored Sep 14, 2020
2 parents 1a9588f + 12eee6e commit 6dd55e7
Show file tree
Hide file tree
Showing 13 changed files with 323 additions and 88 deletions.
8 changes: 6 additions & 2 deletions internal/processorinfo/processor_count.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package processorinfo

import "runtime"
import (
"runtime"

"github.com/Microsoft/hcsshim/internal/winapi"
)

// ProcessorCount calls the win32 API function GetMaximumProcessorCount
// to get the total number of logical processors on the system. If this
// fails it will fall back to runtime.NumCPU
func ProcessorCount() int32 {
if amount := getActiveProcessorCount(ALL_PROCESSOR_GROUPS); amount != 0 {
if amount := winapi.GetActiveProcessorCount(winapi.ALL_PROCESSOR_GROUPS); amount != 0 {
return int32(amount)
}
return int32(runtime.NumCPU())
Expand Down
49 changes: 0 additions & 49 deletions internal/processorinfo/zsyscall_windows.go

This file was deleted.

7 changes: 5 additions & 2 deletions internal/winapi/errors.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package winapi

import "syscall"

//sys RtlNtStatusToDosError(status uint32) (winerr error) = ntdll.RtlNtStatusToDosError

const (
STATUS_REPARSE_POINT_ENCOUNTERED = 0xC000050B
ERROR_NO_MORE_ITEMS = 0x103
STATUS_REPARSE_POINT_ENCOUNTERED = 0xC000050B
ERROR_NO_MORE_ITEMS = 0x103
ERROR_MORE_DATA syscall.Errno = 234
)

func NTSuccess(status uint32) bool {
Expand Down
File renamed without changes.
4 changes: 0 additions & 4 deletions internal/winapi/heapalloc.go

This file was deleted.

120 changes: 120 additions & 0 deletions internal/winapi/jobobject.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package winapi

import (
"golang.org/x/sys/windows"
)

// Messages that can be received from an assigned io completion port.
// https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-jobobject_associate_completion_port
const (
JOB_OBJECT_MSG_END_OF_JOB_TIME = 1
JOB_OBJECT_MSG_END_OF_PROCESS_TIME = 2
JOB_OBJECT_MSG_ACTIVE_PROCESS_LIMIT = 3
JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO = 4
JOB_OBJECT_MSG_NEW_PROCESS = 6
JOB_OBJECT_MSG_EXIT_PROCESS = 7
JOB_OBJECT_MSG_ABNORMAL_EXIT_PROCESS = 8
JOB_OBJECT_MSG_PROCESS_MEMORY_LIMIT = 9
JOB_OBJECT_MSG_JOB_MEMORY_LIMIT = 10
JOB_OBJECT_MSG_NOTIFICATION_LIMIT = 11
)

// IO limit flags
//
// https://docs.microsoft.com/en-us/windows/win32/api/jobapi2/ns-jobapi2-jobobject_io_rate_control_information
const JOB_OBJECT_IO_RATE_CONTROL_ENABLE = 0x1

// https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-jobobject_cpu_rate_control_information
const (
JOB_OBJECT_CPU_RATE_CONTROL_ENABLE = 1 << iota
JOB_OBJECT_CPU_RATE_CONTROL_WEIGHT_BASED
JOB_OBJECT_CPU_RATE_CONTROL_HARD_CAP
JOB_OBJECT_CPU_RATE_CONTROL_NOTIFY
JOB_OBJECT_CPU_RATE_CONTROL_MIN_MAX_RATE
)

// JobObjectInformationClass values. Used for a call to QueryInformationJobObject
//
// https://docs.microsoft.com/en-us/windows/win32/api/jobapi2/nf-jobapi2-queryinformationjobobject
const (
JobObjectBasicAccountingInformation uint32 = 1
JobObjectBasicProcessIdList uint32 = 3
JobObjectBasicAndIoAccountingInformation uint32 = 8
JobObjectLimitViolationInformation uint32 = 13
JobObjectNotificationLimitInformation2 uint32 = 33
)

// https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-jobobject_basic_limit_information
type JOBOBJECT_BASIC_LIMIT_INFORMATION struct {
PerProcessUserTimeLimit int64
PerJobUserTimeLimit int64
LimitFlags uint32
MinimumWorkingSetSize uintptr
MaximumWorkingSetSize uintptr
ActiveProcessLimit uint32
Affinity uintptr
PriorityClass uint32
SchedulingClass uint32
}

// https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-jobobject_cpu_rate_control_information
type JOBOBJECT_CPU_RATE_CONTROL_INFORMATION struct {
ControlFlags uint32
Rate uint32
}

// https://docs.microsoft.com/en-us/windows/win32/api/jobapi2/ns-jobapi2-jobobject_io_rate_control_information
type JOBOBJECT_IO_RATE_CONTROL_INFORMATION struct {
MaxIops int64
MaxBandwidth int64
ReservationIops int64
BaseIOSize uint32
VolumeName string
ControlFlags uint32
}

// https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-jobobject_basic_process_id_list
type JOBOBJECT_BASIC_PROCESS_ID_LIST struct {
NumberOfAssignedProcesses uint32
NumberOfProcessIdsInList uint32
ProcessIdList [1]uintptr
}

// https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-jobobject_associate_completion_port
type JOBOBJECT_ASSOCIATE_COMPLETION_PORT struct {
CompletionKey uintptr
CompletionPort windows.Handle
}

// BOOL IsProcessInJob(
// HANDLE ProcessHandle,
// HANDLE JobHandle,
// PBOOL Result
// );
//
//sys IsProcessInJob(procHandle windows.Handle, jobHandle windows.Handle, result *bool) (err error) = kernel32.IsProcessInJob

// BOOL QueryInformationJobObject(
// HANDLE hJob,
// JOBOBJECTINFOCLASS JobObjectInformationClass,
// LPVOID lpJobObjectInformation,
// DWORD cbJobObjectInformationLength,
// LPDWORD lpReturnLength
// );
//
//sys QueryInformationJobObject(jobHandle windows.Handle, infoClass uint32, jobObjectInfo uintptr, jobObjectInformationLength uint32, lpReturnLength *uint32) (err error) = kernel32.QueryInformationJobObject

// HANDLE OpenJobObjectW(
// DWORD dwDesiredAccess,
// BOOL bInheritHandle,
// LPCWSTR lpName
// );
//
//sys OpenJobObject(desiredAccess uint32, inheritHandle bool, lpName *uint16) (handle windows.Handle, err error) = kernel32.OpenJobObjectW

// DWORD SetIoRateControlInformationJobObject(
// HANDLE hJob,
// JOBOBJECT_IO_RATE_CONTROL_INFORMATION *IoRateControlInfo
// );
//
//sys SetIoRateControlInformationJobObject(jobHandle windows.Handle, ioRateControlInfo *JOBOBJECT_IO_RATE_CONTROL_INFORMATION) (ret uint32, err error) = kernel32.SetIoRateControlInformationJobObject
30 changes: 30 additions & 0 deletions internal/winapi/logon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package winapi

// BOOL LogonUserA(
// LPCWSTR lpszUsername,
// LPCWSTR lpszDomain,
// LPCWSTR lpszPassword,
// DWORD dwLogonType,
// DWORD dwLogonProvider,
// PHANDLE phToken
// );
//
//sys LogonUser(username *uint16, domain *uint16, password *uint16, logonType uint32, logonProvider uint32, token *windows.Token) (err error) = advapi32.LogonUserW

// Logon types
const (
LOGON32_LOGON_INTERACTIVE uint32 = 2
LOGON32_LOGON_NETWORK uint32 = 3
LOGON32_LOGON_BATCH uint32 = 4
LOGON32_LOGON_SERVICE uint32 = 5
LOGON32_LOGON_UNLOCK uint32 = 7
LOGON32_LOGON_NETWORK_CLEARTEXT uint32 = 8
LOGON32_LOGON_NEW_CREDENTIALS uint32 = 9
)

// Logon providers
const (
LOGON32_PROVIDER_DEFAULT uint32 = 0
LOGON32_PROVIDER_WINNT40 uint32 = 2
LOGON32_PROVIDER_WINNT50 uint32 = 3
)
11 changes: 11 additions & 0 deletions internal/winapi/memory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package winapi

// VOID RtlMoveMemory(
// _Out_ VOID UNALIGNED *Destination,
// _In_ const VOID UNALIGNED *Source,
// _In_ SIZE_T Length
// );
//sys RtlMoveMemory(destination *byte, source *byte, length uintptr) (err error) = kernel32.RtlMoveMemory

//sys LocalAlloc(flags uint32, size int) (ptr uintptr) = kernel32.LocalAlloc
//sys LocalFree(ptr uintptr) = kernel32.LocalFree
11 changes: 11 additions & 0 deletions internal/winapi/path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package winapi

// DWORD SearchPathW(
// LPCWSTR lpPath,
// LPCWSTR lpFileName,
// LPCWSTR lpExtension,
// DWORD nBufferLength,
// LPWSTR lpBuffer,
// LPWSTR *lpFilePart
// );
//sys SearchPath(lpPath *uint16, lpFileName *uint16, lpExtension *uint16, nBufferLength uint32, lpBuffer *uint16, lpFilePath **uint16) (size uint32, err error) = kernel32.SearchPathW
3 changes: 3 additions & 0 deletions internal/winapi/process.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package winapi

const PROCESS_ALL_ACCESS uint32 = 2097151
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package processorinfo

//go:generate go run ../../mksyscall_windows.go -output zsyscall_windows.go syscall.go

//sys getActiveProcessorCount(groupNumber uint16) (amount uint32) = kernel32.GetActiveProcessorCount
package winapi

// Get count from all processor groups.
// https://docs.microsoft.com/en-us/windows/win32/procthread/processor-groups
const ALL_PROCESSOR_GROUPS = 0xFFFF

//sys GetActiveProcessorCount(groupNumber uint16) (amount uint32) = kernel32.GetActiveProcessorCount
6 changes: 3 additions & 3 deletions internal/winapi/winapi.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*Package winapi contains various low-level bindings to Windows APIs. It can
be thought of as an extension to golang.org/x/sys/windows. */
// Package winapi contains various low-level bindings to Windows APIs. It can
// be thought of as an extension to golang.org/x/sys/windows.
package winapi

//go:generate go run ..\..\mksyscall_windows.go -output zsyscall_windows.go devices.go heapalloc.go ntfs.go errors.go
//go:generate go run ..\..\mksyscall_windows.go -output zsyscall_windows.go jobobject.go path.go logon.go memory.go processor.go devices.go filesystem.go errors.go
Loading

0 comments on commit 6dd55e7

Please sign in to comment.