@@ -18,9 +18,9 @@ package mobycli
18
18
19
19
import (
20
20
"fmt"
21
- "os"
22
- "syscall"
23
21
"unsafe"
22
+
23
+ "golang.org/x/sys/windows"
24
24
)
25
25
26
26
func init () {
@@ -29,81 +29,28 @@ func init() {
29
29
}
30
30
}
31
31
32
- var (
33
- kernel32 = syscall .NewLazyDLL ("kernel32.dll" )
34
- )
35
-
36
- type jobObjectExtendedLimitInformation struct {
37
- BasicLimitInformation struct {
38
- PerProcessUserTimeLimit uint64
39
- PerJobUserTimeLimit uint64
40
- LimitFlags uint32
41
- MinimumWorkingSetSize uintptr
42
- MaximumWorkingSetSize uintptr
43
- ActiveProcessLimit uint32
44
- Affinity uintptr
45
- PriorityClass uint32
46
- SchedulingClass uint32
47
- }
48
- IoInfo struct {
49
- ReadOperationCount uint64
50
- WriteOperationCount uint64
51
- OtherOperationCount uint64
52
- ReadTransferCount uint64
53
- WriteTransferCount uint64
54
- OtherTransferCount uint64
55
- }
56
- ProcessMemoryLimit uintptr
57
- JobMemoryLimit uintptr
58
- PeakProcessMemoryUsed uintptr
59
- PeakJobMemoryUsed uintptr
60
- }
61
-
62
32
// killSubProcessesOnClose will ensure on windows that all child processes of the current process are killed if parent is killed.
63
33
func killSubProcessesOnClose () error {
64
- job , err := createJobObject ( )
34
+ job , err := windows . CreateJobObject ( nil , nil )
65
35
if err != nil {
66
36
return err
67
37
}
68
- info := jobObjectExtendedLimitInformation {}
69
- info .BasicLimitInformation .LimitFlags = 0x2000
70
- if err := setInformationJobObject (job , info ); err != nil {
71
- _ = syscall .CloseHandle (job )
38
+ info := windows.JOBOBJECT_EXTENDED_LIMIT_INFORMATION {
39
+ BasicLimitInformation : windows.JOBOBJECT_BASIC_LIMIT_INFORMATION {
40
+ LimitFlags : windows .JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE ,
41
+ },
42
+ }
43
+ if _ , err := windows .SetInformationJobObject (
44
+ job ,
45
+ windows .JobObjectExtendedLimitInformation ,
46
+ uintptr (unsafe .Pointer (& info )),
47
+ uint32 (unsafe .Sizeof (info ))); err != nil {
48
+ _ = windows .CloseHandle (job )
72
49
return err
73
50
}
74
- proc , err := syscall .GetCurrentProcess ()
75
- if err != nil {
76
- _ = syscall .CloseHandle (job )
51
+ if err := windows .AssignProcessToJobObject (job , windows .CurrentProcess ()); err != nil {
52
+ _ = windows .CloseHandle (job )
77
53
return err
78
54
}
79
- if err := assignProcessToJobObject (job , proc ); err != nil {
80
- _ = syscall .CloseHandle (job )
81
- return err
82
- }
83
- return nil
84
- }
85
-
86
- func createJobObject () (syscall.Handle , error ) {
87
- res , _ , err := kernel32 .NewProc ("CreateJobObjectW" ).Call (uintptr (unsafe .Pointer (nil )), uintptr (unsafe .Pointer (nil )))
88
- if res == 0 {
89
- return syscall .InvalidHandle , os .NewSyscallError ("CreateJobObject" , err )
90
- }
91
- return syscall .Handle (res ), nil
92
- }
93
-
94
- func setInformationJobObject (job syscall.Handle , info jobObjectExtendedLimitInformation ) error {
95
- infoClass := uint32 (9 )
96
- res , _ , err := kernel32 .NewProc ("SetInformationJobObject" ).Call (uintptr (job ), uintptr (infoClass ), uintptr (unsafe .Pointer (& info )), uintptr (uint32 (unsafe .Sizeof (info ))))
97
- if res == 0 {
98
- return os .NewSyscallError ("SetInformationJobObject" , err )
99
- }
100
- return nil
101
- }
102
-
103
- func assignProcessToJobObject (job syscall.Handle , process syscall.Handle ) error {
104
- res , _ , err := kernel32 .NewProc ("AssignProcessToJobObject" ).Call (uintptr (job ), uintptr (process ))
105
- if res == 0 {
106
- return os .NewSyscallError ("AssignProcessToJobObject" , err )
107
- }
108
55
return nil
109
56
}
0 commit comments