Swap usage of LazyDLL and LoadDLL to LazySystemDLL.

Signed-off-by: Darren Stahl <darst@microsoft.com>
(cherry picked from commit 22c83c567f)
Signed-off-by: Victor Vieux <victorvieux@gmail.com>
This commit is contained in:
Darren Stahl 2016-11-21 14:35:06 -08:00 committed by Victor Vieux
parent f9f8a94f42
commit 00fd466e00
8 changed files with 30 additions and 35 deletions

View file

@ -27,7 +27,7 @@ var (
flUnregisterService *bool flUnregisterService *bool
flRunService *bool flRunService *bool
setStdHandle = syscall.NewLazyDLL("kernel32.dll").NewProc("SetStdHandle") setStdHandle = windows.NewLazySystemDLL("kernel32.dll").NewProc("SetStdHandle")
oldStderr syscall.Handle oldStderr syscall.Handle
panicFile *os.File panicFile *os.File

View file

@ -31,7 +31,8 @@ import (
"github.com/docker/docker/pkg/ioutils" "github.com/docker/docker/pkg/ioutils"
"github.com/docker/docker/pkg/longpath" "github.com/docker/docker/pkg/longpath"
"github.com/docker/docker/pkg/reexec" "github.com/docker/docker/pkg/reexec"
"github.com/docker/go-units" units "github.com/docker/go-units"
"golang.org/x/sys/windows"
) )
// filterDriver is an HCSShim driver type for the Windows Filter driver. // filterDriver is an HCSShim driver type for the Windows Filter driver.
@ -115,7 +116,7 @@ func win32FromHresult(hr uintptr) uintptr {
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa364993(v=vs.85).aspx // https://msdn.microsoft.com/en-us/library/windows/desktop/aa364993(v=vs.85).aspx
func getFileSystemType(drive string) (fsType string, hr error) { func getFileSystemType(drive string) (fsType string, hr error) {
var ( var (
modkernel32 = syscall.NewLazyDLL("kernel32.dll") modkernel32 = windows.NewLazySystemDLL("kernel32.dll")
procGetVolumeInformation = modkernel32.NewProc("GetVolumeInformationW") procGetVolumeInformation = modkernel32.NewProc("GetVolumeInformationW")
buf = make([]uint16, 255) buf = make([]uint16, 255)
size = syscall.MAX_PATH + 1 size = syscall.MAX_PATH + 1

View file

@ -21,6 +21,7 @@ import (
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/docker/docker/daemon/logger" "github.com/docker/docker/daemon/logger"
"golang.org/x/sys/windows"
) )
type etwLogs struct { type etwLogs struct {
@ -35,7 +36,12 @@ const (
win32CallSuccess = 0 win32CallSuccess = 0
) )
var win32Lib *syscall.DLL var (
modAdvapi32 = windows.NewLazySystemDLL("Advapi32.dll")
procEventRegister = modAdvapi32.NewProc("EventRegister")
procEventWriteString = modAdvapi32.NewProc("EventWriteString")
procEventUnregister = modAdvapi32.NewProc("EventUnregister")
)
var providerHandle syscall.Handle var providerHandle syscall.Handle
var refCount int var refCount int
var mu sync.Mutex var mu sync.Mutex
@ -106,12 +112,7 @@ func registerETWProvider() error {
defer mu.Unlock() defer mu.Unlock()
if refCount == 0 { if refCount == 0 {
var err error var err error
if win32Lib, err = syscall.LoadDLL("Advapi32.dll"); err != nil {
return err
}
if err = callEventRegister(); err != nil { if err = callEventRegister(); err != nil {
win32Lib.Release()
win32Lib = nil
return err return err
} }
} }
@ -127,8 +128,6 @@ func unregisterETWProvider() {
if callEventUnregister() { if callEventUnregister() {
refCount-- refCount--
providerHandle = syscall.InvalidHandle providerHandle = syscall.InvalidHandle
win32Lib.Release()
win32Lib = nil
} }
// Not returning an error if EventUnregister fails, because etwLogs will continue to work // Not returning an error if EventUnregister fails, because etwLogs will continue to work
} else { } else {
@ -137,17 +136,13 @@ func unregisterETWProvider() {
} }
func callEventRegister() error { func callEventRegister() error {
proc, err := win32Lib.FindProc("EventRegister")
if err != nil {
return err
}
// The provider's GUID is {a3693192-9ed6-46d2-a981-f8226c8363bd} // The provider's GUID is {a3693192-9ed6-46d2-a981-f8226c8363bd}
guid := syscall.GUID{ guid := syscall.GUID{
0xa3693192, 0x9ed6, 0x46d2, 0xa3693192, 0x9ed6, 0x46d2,
[8]byte{0xa9, 0x81, 0xf8, 0x22, 0x6c, 0x83, 0x63, 0xbd}, [8]byte{0xa9, 0x81, 0xf8, 0x22, 0x6c, 0x83, 0x63, 0xbd},
} }
ret, _, _ := proc.Call(uintptr(unsafe.Pointer(&guid)), 0, 0, uintptr(unsafe.Pointer(&providerHandle))) ret, _, _ := procEventRegister.Call(uintptr(unsafe.Pointer(&guid)), 0, 0, uintptr(unsafe.Pointer(&providerHandle)))
if ret != win32CallSuccess { if ret != win32CallSuccess {
errorMessage := fmt.Sprintf("Failed to register ETW provider. Error: %d", ret) errorMessage := fmt.Sprintf("Failed to register ETW provider. Error: %d", ret)
logrus.Error(errorMessage) logrus.Error(errorMessage)
@ -157,11 +152,7 @@ func callEventRegister() error {
} }
func callEventWriteString(message string) error { func callEventWriteString(message string) error {
proc, err := win32Lib.FindProc("EventWriteString") ret, _, _ := procEventWriteString.Call(uintptr(providerHandle), 0, 0, uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(message))))
if err != nil {
return err
}
ret, _, _ := proc.Call(uintptr(providerHandle), 0, 0, uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(message))))
if ret != win32CallSuccess { if ret != win32CallSuccess {
errorMessage := fmt.Sprintf("ETWLogs provider failed to log message. Error: %d", ret) errorMessage := fmt.Sprintf("ETWLogs provider failed to log message. Error: %d", ret)
logrus.Error(errorMessage) logrus.Error(errorMessage)
@ -171,11 +162,7 @@ func callEventWriteString(message string) error {
} }
func callEventUnregister() bool { func callEventUnregister() bool {
proc, err := win32Lib.FindProc("EventUnregister") ret, _, _ := procEventUnregister.Call(uintptr(providerHandle))
if err != nil {
return false
}
ret, _, _ := proc.Call(uintptr(providerHandle))
if ret != win32CallSuccess { if ret != win32CallSuccess {
return false return false
} }

View file

@ -7,9 +7,10 @@ import (
"unsafe" "unsafe"
"github.com/go-check/check" "github.com/go-check/check"
"golang.org/x/sys/windows"
) )
func openEvent(desiredAccess uint32, inheritHandle bool, name string, proc *syscall.LazyProc) (handle syscall.Handle, err error) { func openEvent(desiredAccess uint32, inheritHandle bool, name string, proc *windows.LazyProc) (handle syscall.Handle, err error) {
namep, _ := syscall.UTF16PtrFromString(name) namep, _ := syscall.UTF16PtrFromString(name)
var _p2 uint32 var _p2 uint32
if inheritHandle { if inheritHandle {
@ -23,7 +24,7 @@ func openEvent(desiredAccess uint32, inheritHandle bool, name string, proc *sysc
return return
} }
func pulseEvent(handle syscall.Handle, proc *syscall.LazyProc) (err error) { func pulseEvent(handle syscall.Handle, proc *windows.LazyProc) (err error) {
r0, _, _ := proc.Call(uintptr(handle)) r0, _, _ := proc.Call(uintptr(handle))
if r0 != 0 { if r0 != 0 {
err = syscall.Errno(r0) err = syscall.Errno(r0)
@ -32,7 +33,7 @@ func pulseEvent(handle syscall.Handle, proc *syscall.LazyProc) (err error) {
} }
func signalDaemonDump(pid int) { func signalDaemonDump(pid int) {
modkernel32 := syscall.NewLazyDLL("kernel32.dll") modkernel32 := windows.NewLazySystemDLL("kernel32.dll")
procOpenEvent := modkernel32.NewProc("OpenEventW") procOpenEvent := modkernel32.NewProc("OpenEventW")
procPulseEvent := modkernel32.NewProc("PulseEvent") procPulseEvent := modkernel32.NewProc("PulseEvent")

View file

@ -4,10 +4,12 @@ import (
"fmt" "fmt"
"syscall" "syscall"
"unsafe" "unsafe"
"golang.org/x/sys/windows"
) )
var ( var (
modkernel32 = syscall.NewLazyDLL("kernel32.dll") modkernel32 = windows.NewLazySystemDLL("kernel32.dll")
procGetSystemInfo = modkernel32.NewProc("GetSystemInfo") procGetSystemInfo = modkernel32.NewProc("GetSystemInfo")
) )

View file

@ -4,12 +4,13 @@ package sysinfo
import ( import (
"runtime" "runtime"
"syscall"
"unsafe" "unsafe"
"golang.org/x/sys/windows"
) )
var ( var (
kernel32 = syscall.NewLazyDLL("kernel32.dll") kernel32 = windows.NewLazySystemDLL("kernel32.dll")
getCurrentProcess = kernel32.NewProc("GetCurrentProcess") getCurrentProcess = kernel32.NewProc("GetCurrentProcess")
getProcessAffinityMask = kernel32.NewProc("GetProcessAffinityMask") getProcessAffinityMask = kernel32.NewProc("GetProcessAffinityMask")
) )

View file

@ -6,6 +6,8 @@ package system
import ( import (
"syscall" "syscall"
"unsafe" "unsafe"
"golang.org/x/sys/windows"
) )
var ( var (
@ -67,7 +69,7 @@ func PulseEvent(handle syscall.Handle) (err error) {
return setResetPulse(handle, procPulseEvent) return setResetPulse(handle, procPulseEvent)
} }
func setResetPulse(handle syscall.Handle, proc *syscall.LazyProc) (err error) { func setResetPulse(handle syscall.Handle, proc *windows.LazyProc) (err error) {
r0, _, _ := proc.Call(uintptr(handle)) r0, _, _ := proc.Call(uintptr(handle))
if r0 != 0 { if r0 != 0 {
err = syscall.Errno(r0) err = syscall.Errno(r0)

View file

@ -1,12 +1,13 @@
package system package system
import ( import (
"syscall"
"unsafe" "unsafe"
"golang.org/x/sys/windows"
) )
var ( var (
modkernel32 = syscall.NewLazyDLL("kernel32.dll") modkernel32 = windows.NewLazySystemDLL("kernel32.dll")
procGlobalMemoryStatusEx = modkernel32.NewProc("GlobalMemoryStatusEx") procGlobalMemoryStatusEx = modkernel32.NewProc("GlobalMemoryStatusEx")
) )