Browse code

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>

Tobias Klauser authored on 2017/08/21 19:58:09
Showing 4 changed files
... ...
@@ -7,7 +7,6 @@ import (
7 7
 	"path/filepath"
8 8
 
9 9
 	"github.com/docker/docker/libcontainerd"
10
-	"github.com/docker/docker/pkg/system"
11 10
 	"github.com/sirupsen/logrus"
12 11
 	"golang.org/x/sys/windows"
13 12
 )
... ...
@@ -61,8 +60,8 @@ func (cli *DaemonCli) setupConfigReloadTrap() {
61 61
 		sa := windows.SecurityAttributes{
62 62
 			Length: 0,
63 63
 		}
64
-		ev := "Global\\docker-daemon-config-" + fmt.Sprint(os.Getpid())
65
-		if h, _ := system.CreateEvent(&sa, false, false, ev); h != 0 {
64
+		ev, _ := windows.UTF16PtrFromString("Global\\docker-daemon-config-" + fmt.Sprint(os.Getpid()))
65
+		if h, _ := windows.CreateEvent(&sa, 0, 0, ev); h != 0 {
66 66
 			logrus.Debugf("Config reload - waiting signal at %s", ev)
67 67
 			for {
68 68
 				windows.WaitForSingleObject(h, windows.INFINITE)
... ...
@@ -7,7 +7,6 @@ import (
7 7
 
8 8
 	winio "github.com/Microsoft/go-winio"
9 9
 	"github.com/docker/docker/pkg/signal"
10
-	"github.com/docker/docker/pkg/system"
11 10
 	"github.com/sirupsen/logrus"
12 11
 	"golang.org/x/sys/windows"
13 12
 )
... ...
@@ -16,7 +15,7 @@ func (d *Daemon) setupDumpStackTrap(root string) {
16 16
 	// Windows does not support signals like *nix systems. So instead of
17 17
 	// trapping on SIGUSR1 to dump stacks, we wait on a Win32 event to be
18 18
 	// signaled. ACL'd to builtin administrators and local system
19
-	ev := "Global\\docker-daemon-" + fmt.Sprint(os.Getpid())
19
+	ev, _ := windows.UTF16PtrFromString("Global\\docker-daemon-" + fmt.Sprint(os.Getpid()))
20 20
 	sd, err := winio.SddlToSecurityDescriptor("D:P(A;;GA;;;BA)(A;;GA;;;SY)")
21 21
 	if err != nil {
22 22
 		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) {
26 26
 	sa.Length = uint32(unsafe.Sizeof(sa))
27 27
 	sa.InheritHandle = 1
28 28
 	sa.SecurityDescriptor = uintptr(unsafe.Pointer(&sd[0]))
29
-	h, err := system.CreateEvent(&sa, false, false, ev)
29
+	h, err := windows.CreateEvent(&sa, 0, 0, ev)
30 30
 	if h == 0 || err != nil {
31 31
 		logrus.Errorf("failed to create debug stackdump event %s: %s", ev, err.Error())
32 32
 		return
... ...
@@ -3,47 +3,19 @@ package daemon
3 3
 import (
4 4
 	"fmt"
5 5
 	"strconv"
6
-	"syscall"
7
-	"unsafe"
8 6
 
9 7
 	"github.com/go-check/check"
10 8
 	"golang.org/x/sys/windows"
11 9
 )
12 10
 
13
-func openEvent(desiredAccess uint32, inheritHandle bool, name string, proc *windows.LazyProc) (handle windows.Handle, err error) {
14
-	namep, _ := windows.UTF16PtrFromString(name)
15
-	var _p2 uint32
16
-	if inheritHandle {
17
-		_p2 = 1
18
-	}
19
-	r0, _, e1 := proc.Call(uintptr(desiredAccess), uintptr(_p2), uintptr(unsafe.Pointer(namep)))
20
-	handle = windows.Handle(r0)
21
-	if handle == windows.InvalidHandle {
22
-		err = e1
23
-	}
24
-	return
25
-}
26
-
27
-func pulseEvent(handle windows.Handle, proc *windows.LazyProc) (err error) {
28
-	r0, _, _ := proc.Call(uintptr(handle))
29
-	if r0 != 0 {
30
-		err = syscall.Errno(r0)
31
-	}
32
-	return
33
-}
34
-
35 11
 // SignalDaemonDump sends a signal to the daemon to write a dump file
36 12
 func SignalDaemonDump(pid int) {
37
-	modkernel32 := windows.NewLazySystemDLL("kernel32.dll")
38
-	procOpenEvent := modkernel32.NewProc("OpenEventW")
39
-	procPulseEvent := modkernel32.NewProc("PulseEvent")
40
-
41
-	ev := "Global\\docker-daemon-" + strconv.Itoa(pid)
42
-	h2, _ := openEvent(0x0002, false, ev, procOpenEvent)
43
-	if h2 == 0 {
13
+	ev, _ := windows.UTF16PtrFromString("Global\\docker-daemon-" + strconv.Itoa(pid))
14
+	h2, err := windows.OpenEvent(0x0002, false, ev)
15
+	if h2 == 0 || err != nil {
44 16
 		return
45 17
 	}
46
-	pulseEvent(h2, procPulseEvent)
18
+	windows.PulseEvent(h2)
47 19
 }
48 20
 
49 21
 func signalDaemonReload(pid int) error {
50 22
deleted file mode 100644
... ...
@@ -1,85 +0,0 @@
1
-package system
2
-
3
-// This file implements syscalls for Win32 events which are not implemented
4
-// in golang.
5
-
6
-import (
7
-	"syscall"
8
-	"unsafe"
9
-
10
-	"golang.org/x/sys/windows"
11
-)
12
-
13
-var (
14
-	procCreateEvent = modkernel32.NewProc("CreateEventW")
15
-	procOpenEvent   = modkernel32.NewProc("OpenEventW")
16
-	procSetEvent    = modkernel32.NewProc("SetEvent")
17
-	procResetEvent  = modkernel32.NewProc("ResetEvent")
18
-	procPulseEvent  = modkernel32.NewProc("PulseEvent")
19
-)
20
-
21
-// CreateEvent implements win32 CreateEventW func in golang. It will create an event object.
22
-func CreateEvent(eventAttributes *windows.SecurityAttributes, manualReset bool, initialState bool, name string) (handle windows.Handle, err error) {
23
-	namep, _ := windows.UTF16PtrFromString(name)
24
-	var _p1 uint32
25
-	if manualReset {
26
-		_p1 = 1
27
-	}
28
-	var _p2 uint32
29
-	if initialState {
30
-		_p2 = 1
31
-	}
32
-	r0, _, e1 := procCreateEvent.Call(uintptr(unsafe.Pointer(eventAttributes)), uintptr(_p1), uintptr(_p2), uintptr(unsafe.Pointer(namep)))
33
-	use(unsafe.Pointer(namep))
34
-	handle = windows.Handle(r0)
35
-	if handle == windows.InvalidHandle {
36
-		err = e1
37
-	}
38
-	return
39
-}
40
-
41
-// OpenEvent implements win32 OpenEventW func in golang. It opens an event object.
42
-func OpenEvent(desiredAccess uint32, inheritHandle bool, name string) (handle windows.Handle, err error) {
43
-	namep, _ := windows.UTF16PtrFromString(name)
44
-	var _p1 uint32
45
-	if inheritHandle {
46
-		_p1 = 1
47
-	}
48
-	r0, _, e1 := procOpenEvent.Call(uintptr(desiredAccess), uintptr(_p1), uintptr(unsafe.Pointer(namep)))
49
-	use(unsafe.Pointer(namep))
50
-	handle = windows.Handle(r0)
51
-	if handle == windows.InvalidHandle {
52
-		err = e1
53
-	}
54
-	return
55
-}
56
-
57
-// SetEvent implements win32 SetEvent func in golang.
58
-func SetEvent(handle windows.Handle) (err error) {
59
-	return setResetPulse(handle, procSetEvent)
60
-}
61
-
62
-// ResetEvent implements win32 ResetEvent func in golang.
63
-func ResetEvent(handle windows.Handle) (err error) {
64
-	return setResetPulse(handle, procResetEvent)
65
-}
66
-
67
-// PulseEvent implements win32 PulseEvent func in golang.
68
-func PulseEvent(handle windows.Handle) (err error) {
69
-	return setResetPulse(handle, procPulseEvent)
70
-}
71
-
72
-func setResetPulse(handle windows.Handle, proc *windows.LazyProc) (err error) {
73
-	r0, _, _ := proc.Call(uintptr(handle))
74
-	if r0 != 0 {
75
-		err = syscall.Errno(r0)
76
-	}
77
-	return
78
-}
79
-
80
-var temp unsafe.Pointer
81
-
82
-// use ensures a variable is kept alive without the GC freeing while still needed
83
-func use(p unsafe.Pointer) {
84
-	temp = p
85
-}