windows/svc: make IsWindowsService handle parent exit

IsWindowsService fails when parent process has exited.

This change makes IsWindowsService return false instead.

Added a test to verify the change.

Change-Id: Ibb2aa9478e8afd9e35011bbdc0985bdf8f0af9cc
Reviewed-on: https://go-review.googlesource.com/c/sys/+/294729
TryBot-Result: Go Bot <gobot@golang.org>
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
Reviewed-by: Jason A. Donenfeld <Jason@zx2c4.com>
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Trust: Alex Brainman <alex.brainman@gmail.com>
This commit is contained in:
Alex Brainman
2021-02-27 04:07:30 +00:00
parent f36f78243c
commit b0d1d43c01
2 changed files with 78 additions and 0 deletions
+7
View File
@@ -96,6 +96,13 @@ func IsWindowsService() (bool, error) {
var psid uint32
err := windows.ProcessIdToSessionId(uint32(pbi[5]), &psid)
if err != nil {
if err == windows.ERROR_INVALID_PARAMETER {
// This error happens when Windows cannot find process parent.
// Perhaps process parent exited.
// Assume we are not running in a service, because service
// parent process (services.exe) cannot exit.
return false, nil
}
return false, err
}
if psid != 0 {