-
Notifications
You must be signed in to change notification settings - Fork 0
/
ps_windows.go
91 lines (76 loc) · 1.82 KB
/
ps_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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// https://gist.github.com/hallazzang/76f3970bfc949831808bbebc8ca15209
package main
import (
"os"
"os/exec"
"unsafe"
"golang.org/x/sys/windows"
)
type windowsProcess struct {
Pid int
Handle uintptr
}
type ProcessExitGroup windows.Handle
func NewProcessExitGroup() (ProcessExitGroup, error) {
handle, err := windows.CreateJobObject(nil, nil)
if err != nil {
return 0, err
}
info := windows.JOBOBJECT_EXTENDED_LIMIT_INFORMATION{
BasicLimitInformation: windows.JOBOBJECT_BASIC_LIMIT_INFORMATION{
LimitFlags: windows.JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE,
},
}
if _, err := windows.SetInformationJobObject(
handle,
windows.JobObjectExtendedLimitInformation,
uintptr(unsafe.Pointer(&info)),
uint32(unsafe.Sizeof(info))); err != nil {
return 0, err
}
return ProcessExitGroup(handle), nil
}
func (g ProcessExitGroup) Dispose() error {
return windows.CloseHandle(windows.Handle(g))
}
func (g ProcessExitGroup) AddProcess(p *os.Process) error {
return windows.AssignProcessToJobObject(
windows.Handle(g),
windows.Handle((*windowsProcess)(unsafe.Pointer(p)).Handle))
}
func start(command string, arguments ...string) error {
processMutex.Lock()
defer processMutex.Unlock()
if process != nil {
if err := kill(false); err != nil {
return err
}
}
group, err := NewProcessExitGroup()
if err != nil {
return err
}
defer group.Dispose()
process = exec.Command(command, arguments...)
process.Stdout = os.Stdout
process.Stdin = os.Stdin
process.Stderr = os.Stderr
if err := group.AddProcess(process.Process); err != nil {
return err
}
return process.Start()
}
func kill(lock bool) error {
if lock {
processMutex.Lock()
defer processMutex.Unlock()
}
if process == nil {
return nil
}
if err := process.Process.Kill(); err != nil {
return err
}
_, err := process.Process.Wait()
return err
}