]> Cypherpunks repositories - gostls13.git/commitdiff
os: terminate windows processes via handle directly
authorJason A. Donenfeld <Jason@zx2c4.com>
Tue, 25 May 2021 14:23:16 +0000 (16:23 +0200)
committerJason A. Donenfeld <Jason@zx2c4.com>
Fri, 4 Jun 2021 17:21:40 +0000 (17:21 +0000)
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>
src/os/exec_windows.go

index 5710401acdb92d44dca834d2ac9c42fe09b9b2d7..b59a01a75e93d11550715967071ec1696ae6e798 100644 (file)
@@ -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)