From 782c380a72b03ca6110a3b7b5ebda9a989fa9d2c Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Wed, 15 May 2019 12:35:53 +0200 Subject: service: more apis ported upstream Signed-off-by: Jason A. Donenfeld --- service/mksyscall.go | 2 +- service/tunneltracker.go | 42 ++++++------------------------------------ service/zsyscall_windows.go | 30 +++--------------------------- 3 files changed, 10 insertions(+), 64 deletions(-) diff --git a/service/mksyscall.go b/service/mksyscall.go index 417a99fb..d15628ae 100644 --- a/service/mksyscall.go +++ b/service/mksyscall.go @@ -5,4 +5,4 @@ package service -//go:generate go run $GOROOT/src/syscall/mksyscall_windows.go -output zsyscall_windows.go securityapi.go tunneltracker.go +//go:generate go run $GOROOT/src/syscall/mksyscall_windows.go -output zsyscall_windows.go securityapi.go diff --git a/service/tunneltracker.go b/service/tunneltracker.go index 313be830..9e413c22 100644 --- a/service/tunneltracker.go +++ b/service/tunneltracker.go @@ -19,36 +19,6 @@ import ( "golang.zx2c4.com/wireguard/windows/conf" ) -//sys notifyServiceStatusChange(service windows.Handle, notifyMask uint32, notifier *SERVICE_NOTIFY) (ret error) = advapi32.NotifyServiceStatusChangeW -//sys sleepEx(milliseconds uint32, alertable bool) (ret uint32) = kernel32.SleepEx - -const ( - SERVICE_NOTIFY_STATUS_CHANGE = 2 - SERVICE_NOTIFY_STOPPED = 0x00000001 - SERVICE_NOTIFY_START_PENDING = 0x00000002 - SERVICE_NOTIFY_STOP_PENDING = 0x00000004 - SERVICE_NOTIFY_RUNNING = 0x00000008 - SERVICE_NOTIFY_CONTINUE_PENDING = 0x00000010 - SERVICE_NOTIFY_PAUSE_PENDING = 0x00000020 - SERVICE_NOTIFY_PAUSED = 0x00000040 - SERVICE_NOTIFY_CREATED = 0x00000080 - SERVICE_NOTIFY_DELETED = 0x00000100 - SERVICE_NOTIFY_DELETE_PENDING = 0x00000200 - - STATUS_USER_APC = 0x000000C0 - WAIT_IO_COMPLETION = STATUS_USER_APC -) - -type SERVICE_NOTIFY struct { - Version uint32 - NotifyCallback uintptr - Context uintptr - NotificationStatus uint32 - ServiceStatus windows.SERVICE_STATUS_PROCESS - NotificationTriggered uint32 - ServiceNames *uint16 -} - func trackExistingTunnels() error { m, err := serviceManager() if err != nil { @@ -72,7 +42,7 @@ func trackExistingTunnels() error { return nil } -var serviceTrackerCallbackPtr = windows.NewCallback(func(notifier *SERVICE_NOTIFY) uintptr { +var serviceTrackerCallbackPtr = windows.NewCallback(func(notifier *windows.SERVICE_NOTIFY) uintptr { return 0 }) @@ -129,9 +99,9 @@ func trackTunnelService(tunnelName string, service *mgr.Service) { trackedTunnelsLock.Unlock() }() - const serviceNotifications = SERVICE_NOTIFY_RUNNING | SERVICE_NOTIFY_START_PENDING | SERVICE_NOTIFY_STOP_PENDING | SERVICE_NOTIFY_STOPPED | SERVICE_NOTIFY_DELETE_PENDING - notifier := &SERVICE_NOTIFY{ - Version: SERVICE_NOTIFY_STATUS_CHANGE, + const serviceNotifications = windows.SERVICE_NOTIFY_RUNNING | windows.SERVICE_NOTIFY_START_PENDING | windows.SERVICE_NOTIFY_STOP_PENDING | windows.SERVICE_NOTIFY_STOPPED | windows.SERVICE_NOTIFY_DELETE_PENDING + notifier := &windows.SERVICE_NOTIFY{ + Version: windows.SERVICE_NOTIFY_STATUS_CHANGE, NotifyCallback: serviceTrackerCallbackPtr, } @@ -156,11 +126,11 @@ func trackTunnelService(tunnelName string, service *mgr.Service) { defer runtime.UnlockOSThread() lastState := TunnelUnknown for { - err := notifyServiceStatusChange(service.Handle, serviceNotifications, notifier) + err := windows.NotifyServiceStatusChange(service.Handle, serviceNotifications, notifier) switch err { case nil: for { - if sleepEx(uint32(time.Second*3/time.Millisecond), true) == WAIT_IO_COMPLETION { + if windows.SleepEx(uint32(time.Second*3/time.Millisecond), true) == windows.WAIT_IO_COMPLETION { break } else if checkForDisabled() { return diff --git a/service/zsyscall_windows.go b/service/zsyscall_windows.go index cc40ddea..bb1eced3 100644 --- a/service/zsyscall_windows.go +++ b/service/zsyscall_windows.go @@ -38,14 +38,10 @@ func errnoErr(e syscall.Errno) error { var ( modwtsapi32 = windows.NewLazySystemDLL("wtsapi32.dll") - modadvapi32 = windows.NewLazySystemDLL("advapi32.dll") - modkernel32 = windows.NewLazySystemDLL("kernel32.dll") - procWTSQueryUserToken = modwtsapi32.NewProc("WTSQueryUserToken") - procWTSEnumerateSessionsW = modwtsapi32.NewProc("WTSEnumerateSessionsW") - procWTSFreeMemory = modwtsapi32.NewProc("WTSFreeMemory") - procNotifyServiceStatusChangeW = modadvapi32.NewProc("NotifyServiceStatusChangeW") - procSleepEx = modkernel32.NewProc("SleepEx") + procWTSQueryUserToken = modwtsapi32.NewProc("WTSQueryUserToken") + procWTSEnumerateSessionsW = modwtsapi32.NewProc("WTSEnumerateSessionsW") + procWTSFreeMemory = modwtsapi32.NewProc("WTSFreeMemory") ) func wtsQueryUserToken(session uint32, token *windows.Token) (err error) { @@ -76,23 +72,3 @@ func wtsFreeMemory(ptr uintptr) { syscall.Syscall(procWTSFreeMemory.Addr(), 1, uintptr(ptr), 0, 0) return } - -func notifyServiceStatusChange(service windows.Handle, notifyMask uint32, notifier *SERVICE_NOTIFY) (ret error) { - r0, _, _ := syscall.Syscall(procNotifyServiceStatusChangeW.Addr(), 3, uintptr(service), uintptr(notifyMask), uintptr(unsafe.Pointer(notifier))) - if r0 != 0 { - ret = syscall.Errno(r0) - } - return -} - -func sleepEx(milliseconds uint32, alertable bool) (ret uint32) { - var _p0 uint32 - if alertable { - _p0 = 1 - } else { - _p0 = 0 - } - r0, _, _ := syscall.Syscall(procSleepEx.Addr(), 2, uintptr(milliseconds), uintptr(_p0), 0) - ret = uint32(r0) - return -} -- cgit v1.2.3-59-g8ed1b