aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2021-05-25 16:23:16 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2021-06-04 17:21:40 +0000
commit105c5b50e0098720b9e24aea5efa8e161c31db6d (patch)
tree688517c044b7984b120d1d053f42e8c9ad1315d9
parent79cd407f88f640b889df7645bf3e0491ed25eac7 (diff)
downloadgo-105c5b50e0098720b9e24aea5efa8e161c31db6d.tar.gz
go-105c5b50e0098720b9e24aea5efa8e161c31db6d.zip
os: terminate windows processes via handle directly
We already have a handle to the process, so use that for termination, rather than doing a new lookup based on the PID. Change-Id: I2958c1817f12f3dd783412baacbf629049f6956a Reviewed-on: https://go-review.googlesource.com/c/go/+/322509 Trust: Jason A. Donenfeld <Jason@zx2c4.com> Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
-rw-r--r--src/os/exec_windows.go20
1 files changed, 8 insertions, 12 deletions
diff --git a/src/os/exec_windows.go b/src/os/exec_windows.go
index 5710401acd..b59a01a75e 100644
--- a/src/os/exec_windows.go
+++ b/src/os/exec_windows.go
@@ -45,16 +45,6 @@ func (p *Process) wait() (ps *ProcessState, err error) {
return &ProcessState{p.Pid, syscall.WaitStatus{ExitCode: ec}, &u}, nil
}
-func terminateProcess(pid, exitcode int) error {
- h, e := syscall.OpenProcess(syscall.PROCESS_TERMINATE, false, uint32(pid))
- if e != nil {
- return NewSyscallError("OpenProcess", e)
- }
- defer syscall.CloseHandle(h)
- e = syscall.TerminateProcess(h, uint32(exitcode))
- return NewSyscallError("TerminateProcess", e)
-}
-
func (p *Process) signal(sig Signal) error {
handle := atomic.LoadUintptr(&p.handle)
if handle == uintptr(syscall.InvalidHandle) {
@@ -64,9 +54,15 @@ func (p *Process) signal(sig Signal) error {
return ErrProcessDone
}
if sig == Kill {
- err := terminateProcess(p.Pid, 1)
+ var terminationHandle syscall.Handle
+ e := syscall.DuplicateHandle(^syscall.Handle(0), syscall.Handle(handle), ^syscall.Handle(0), &terminationHandle, syscall.PROCESS_TERMINATE, false, 0)
+ if e != nil {
+ return NewSyscallError("DuplicateHandle", e)
+ }
runtime.KeepAlive(p)
- return err
+ defer syscall.CloseHandle(terminationHandle)
+ e = syscall.TerminateProcess(syscall.Handle(terminationHandle), 1)
+ return NewSyscallError("TerminateProcess", e)
}
// TODO(rsc): Handle Interrupt too?
return syscall.Errno(syscall.EWINDOWS)