-
Notifications
You must be signed in to change notification settings - Fork 0
/
triggers_windows.go
77 lines (69 loc) · 2.21 KB
/
triggers_windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package jasper
import (
"syscall"
"github.com/tychoish/grip"
"github.com/tychoish/grip/message"
)
const cleanTerminationSignalTriggerSource = "clean termination trigger"
// makeCleanTerminationSignalTrigger terminates a process so that it will return exit code 0.
func makeCleanTerminationSignalTrigger() SignalTrigger {
return func(info ProcessInfo, sig syscall.Signal) bool {
if sig != syscall.SIGTERM {
return false
}
proc, err := OpenProcess(PROCESS_TERMINATE|PROCESS_QUERY_INFORMATION, false, uint32(info.PID))
if err != nil {
// OpenProcess returns ERROR_INVALID_PARAMETER if the process has already exited.
if err == ERROR_INVALID_PARAMETER {
grip.Debug(message.WrapError(err, message.Fields{
"id": info.ID,
"pid": info.PID,
"source": cleanTerminationSignalTriggerSource,
"message": "did not open process because it has already exited",
}))
} else {
grip.Error(message.WrapError(err, message.Fields{
"id": info.ID,
"pid": info.PID,
"source": cleanTerminationSignalTriggerSource,
"message": "failed to open process",
}))
}
return false
}
defer CloseHandle(proc)
if err := TerminateProcess(proc, 0); err != nil {
// TerminateProcess returns ERROR_ACCESS_DENIED if the process has already died.
if err != ERROR_ACCESS_DENIED {
grip.Error(message.WrapError(err, message.Fields{
"id": info.ID,
"pid": info.PID,
"source": cleanTerminationSignalTriggerSource,
"message": "failed to terminate process",
}))
return false
}
var exitCode uint32
err := GetExitCodeProcess(proc, &exitCode)
if err != nil {
grip.Error(message.WrapError(err, message.Fields{
"id": info.ID,
"pid": info.PID,
"source": cleanTerminationSignalTriggerSource,
"message": "terminate process was sent but failed to get exit code",
}))
return false
}
if exitCode == STILL_ACTIVE {
grip.Error(message.WrapError(err, message.Fields{
"id": info.ID,
"pid": info.PID,
"source": cleanTerminationSignalTriggerSource,
"message": "terminate process was sent but process is still active",
}))
return false
}
}
return true
}
}