Use event functions from golang.org/x/sys/windows

Use CreateEvent, OpenEvent (which both map to the respective *EventW
function) and PulseEvent from golang.org/x/sys instead of local copies.

Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
This commit is contained in:
Tobias Klauser 2017-08-21 12:58:09 +02:00
parent df58f40a47
commit e942513ac4
4 changed files with 8 additions and 123 deletions

View File

@ -7,7 +7,6 @@ import (
"path/filepath"
"github.com/docker/docker/libcontainerd"
"github.com/docker/docker/pkg/system"
"github.com/sirupsen/logrus"
"golang.org/x/sys/windows"
)
@ -61,8 +60,8 @@ func (cli *DaemonCli) setupConfigReloadTrap() {
sa := windows.SecurityAttributes{
Length: 0,
}
ev := "Global\\docker-daemon-config-" + fmt.Sprint(os.Getpid())
if h, _ := system.CreateEvent(&sa, false, false, ev); h != 0 {
ev, _ := windows.UTF16PtrFromString("Global\\docker-daemon-config-" + fmt.Sprint(os.Getpid()))
if h, _ := windows.CreateEvent(&sa, 0, 0, ev); h != 0 {
logrus.Debugf("Config reload - waiting signal at %s", ev)
for {
windows.WaitForSingleObject(h, windows.INFINITE)

View File

@ -7,7 +7,6 @@ import (
winio "github.com/Microsoft/go-winio"
"github.com/docker/docker/pkg/signal"
"github.com/docker/docker/pkg/system"
"github.com/sirupsen/logrus"
"golang.org/x/sys/windows"
)
@ -16,7 +15,7 @@ func (d *Daemon) setupDumpStackTrap(root string) {
// Windows does not support signals like *nix systems. So instead of
// trapping on SIGUSR1 to dump stacks, we wait on a Win32 event to be
// signaled. ACL'd to builtin administrators and local system
ev := "Global\\docker-daemon-" + fmt.Sprint(os.Getpid())
ev, _ := windows.UTF16PtrFromString("Global\\docker-daemon-" + fmt.Sprint(os.Getpid()))
sd, err := winio.SddlToSecurityDescriptor("D:P(A;;GA;;;BA)(A;;GA;;;SY)")
if err != nil {
logrus.Errorf("failed to get security descriptor for debug stackdump event %s: %s", ev, err.Error())
@ -26,7 +25,7 @@ func (d *Daemon) setupDumpStackTrap(root string) {
sa.Length = uint32(unsafe.Sizeof(sa))
sa.InheritHandle = 1
sa.SecurityDescriptor = uintptr(unsafe.Pointer(&sd[0]))
h, err := system.CreateEvent(&sa, false, false, ev)
h, err := windows.CreateEvent(&sa, 0, 0, ev)
if h == 0 || err != nil {
logrus.Errorf("failed to create debug stackdump event %s: %s", ev, err.Error())
return

View File

@ -3,47 +3,19 @@ package daemon
import (
"fmt"
"strconv"
"syscall"
"unsafe"
"github.com/go-check/check"
"golang.org/x/sys/windows"
)
func openEvent(desiredAccess uint32, inheritHandle bool, name string, proc *windows.LazyProc) (handle windows.Handle, err error) {
namep, _ := windows.UTF16PtrFromString(name)
var _p2 uint32
if inheritHandle {
_p2 = 1
}
r0, _, e1 := proc.Call(uintptr(desiredAccess), uintptr(_p2), uintptr(unsafe.Pointer(namep)))
handle = windows.Handle(r0)
if handle == windows.InvalidHandle {
err = e1
}
return
}
func pulseEvent(handle windows.Handle, proc *windows.LazyProc) (err error) {
r0, _, _ := proc.Call(uintptr(handle))
if r0 != 0 {
err = syscall.Errno(r0)
}
return
}
// SignalDaemonDump sends a signal to the daemon to write a dump file
func SignalDaemonDump(pid int) {
modkernel32 := windows.NewLazySystemDLL("kernel32.dll")
procOpenEvent := modkernel32.NewProc("OpenEventW")
procPulseEvent := modkernel32.NewProc("PulseEvent")
ev := "Global\\docker-daemon-" + strconv.Itoa(pid)
h2, _ := openEvent(0x0002, false, ev, procOpenEvent)
if h2 == 0 {
ev, _ := windows.UTF16PtrFromString("Global\\docker-daemon-" + strconv.Itoa(pid))
h2, err := windows.OpenEvent(0x0002, false, ev)
if h2 == 0 || err != nil {
return
}
pulseEvent(h2, procPulseEvent)
windows.PulseEvent(h2)
}
func signalDaemonReload(pid int) error {

View File

@ -1,85 +0,0 @@
package system
// This file implements syscalls for Win32 events which are not implemented
// in golang.
import (
"syscall"
"unsafe"
"golang.org/x/sys/windows"
)
var (
procCreateEvent = modkernel32.NewProc("CreateEventW")
procOpenEvent = modkernel32.NewProc("OpenEventW")
procSetEvent = modkernel32.NewProc("SetEvent")
procResetEvent = modkernel32.NewProc("ResetEvent")
procPulseEvent = modkernel32.NewProc("PulseEvent")
)
// CreateEvent implements win32 CreateEventW func in golang. It will create an event object.
func CreateEvent(eventAttributes *windows.SecurityAttributes, manualReset bool, initialState bool, name string) (handle windows.Handle, err error) {
namep, _ := windows.UTF16PtrFromString(name)
var _p1 uint32
if manualReset {
_p1 = 1
}
var _p2 uint32
if initialState {
_p2 = 1
}
r0, _, e1 := procCreateEvent.Call(uintptr(unsafe.Pointer(eventAttributes)), uintptr(_p1), uintptr(_p2), uintptr(unsafe.Pointer(namep)))
use(unsafe.Pointer(namep))
handle = windows.Handle(r0)
if handle == windows.InvalidHandle {
err = e1
}
return
}
// OpenEvent implements win32 OpenEventW func in golang. It opens an event object.
func OpenEvent(desiredAccess uint32, inheritHandle bool, name string) (handle windows.Handle, err error) {
namep, _ := windows.UTF16PtrFromString(name)
var _p1 uint32
if inheritHandle {
_p1 = 1
}
r0, _, e1 := procOpenEvent.Call(uintptr(desiredAccess), uintptr(_p1), uintptr(unsafe.Pointer(namep)))
use(unsafe.Pointer(namep))
handle = windows.Handle(r0)
if handle == windows.InvalidHandle {
err = e1
}
return
}
// SetEvent implements win32 SetEvent func in golang.
func SetEvent(handle windows.Handle) (err error) {
return setResetPulse(handle, procSetEvent)
}
// ResetEvent implements win32 ResetEvent func in golang.
func ResetEvent(handle windows.Handle) (err error) {
return setResetPulse(handle, procResetEvent)
}
// PulseEvent implements win32 PulseEvent func in golang.
func PulseEvent(handle windows.Handle) (err error) {
return setResetPulse(handle, procPulseEvent)
}
func setResetPulse(handle windows.Handle, proc *windows.LazyProc) (err error) {
r0, _, _ := proc.Call(uintptr(handle))
if r0 != 0 {
err = syscall.Errno(r0)
}
return
}
var temp unsafe.Pointer
// use ensures a variable is kept alive without the GC freeing while still needed
func use(p unsafe.Pointer) {
temp = p
}