Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit a691cc9

Browse files
committed
make windows "kill children process" code simpler
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 487bf96 commit a691cc9

File tree

2 files changed

+17
-69
lines changed

2 files changed

+17
-69
lines changed

cli/mobycli/job_windows.go

Lines changed: 16 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ package mobycli
1818

1919
import (
2020
"fmt"
21-
"os"
22-
"syscall"
2321
"unsafe"
22+
23+
"golang.org/x/sys/windows"
2424
)
2525

2626
func init() {
@@ -29,81 +29,28 @@ func init() {
2929
}
3030
}
3131

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-
6232
// killSubProcessesOnClose will ensure on windows that all child processes of the current process are killed if parent is killed.
6333
func killSubProcessesOnClose() error {
64-
job, err := createJobObject()
34+
job, err := windows.CreateJobObject(nil, nil)
6535
if err != nil {
6636
return err
6737
}
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)
7249
return err
7350
}
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)
7753
return err
7854
}
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-
}
10855
return nil
10956
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ require (
5656
github.com/valyala/fasttemplate v1.2.1 // indirect
5757
golang.org/x/oauth2 v0.0.0-20201109201403-9fd604954f58
5858
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
59+
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4
5960
google.golang.org/grpc v1.33.2
6061
google.golang.org/protobuf v1.25.0
6162
gopkg.in/ini.v1 v1.62.0

0 commit comments

Comments
 (0)