Skip to content

Commit

Permalink
[windows][cws] Add api for killing a process.
Browse files Browse the repository at this point in the history
Adds api (not called anywhere by this PR) to kill a process by its pid.

Testing instructions will be included in the PR which consumes this API.
  • Loading branch information
derekwbrown committed May 2, 2024
1 parent e3b2bc7 commit 79c355d
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions pkg/util/winutil/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,3 +399,35 @@ func getProcessStartTimeAsNs(pid uint64) (uint64, error) {
}
return uint64(creation.Nanoseconds()), nil
}

// KillProcess kills the process with the given PID, supplying the given return code
func KillProcess(pid int, returnCode uint32) error {
/*
* Open the process with PROCESS_TERMINATE rights. This will fail
* if the process is not owned by the current user, or the user does not
* have admin rights
*/
h, err := windows.OpenProcess(windows.PROCESS_TERMINATE, false, uint32(pid))
if err != nil {
return fmt.Errorf("Error opening process %v", err)
}
/*
* if the handle is successfully opened, must be closed to avoid handle leaks
*/
defer windows.Close(h)
/*
* terminate the process; the process will exit with the given return code
*
* See https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-terminateprocess for
* more information.
*
* A couple of notes:
* - This function will return immediately. The process itself will not be closed until all I/O is completed or cancelled
* - We could block and wait for the process to terminate, but this is not done here.
*/
err = windows.TerminateProcess(h, returnCode)
if err != nil {
return fmt.Errorf("Error terminating process %v", err)
}
return nil
}

0 comments on commit 79c355d

Please sign in to comment.