Skip to content

Commit e0659b9

Browse files
author
Guillaume GILL
committed
x/sys/windows/svc: Update IsWindowsService() to be aligned with .NET 6
The current IsWindowsService() function was inherited from the legacy .NET framework and not compatible inside windows containers. We need to update it so it works inside containers. References to: dotnet/runtime#52416 Fixes golang/go#56335
1 parent 5726498 commit e0659b9

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

windows/svc/security.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,9 @@ func IsAnInteractiveSession() (bool, error) {
6868
// as a Windows service.
6969
func IsWindowsService() (bool, error) {
7070
// The below technique looks a bit hairy, but it's actually
71-
// exactly what the .NET framework does for the similarly named function:
72-
// https://github.com/dotnet/extensions/blob/f4066026ca06984b07e90e61a6390ac38152ba93/src/Hosting/WindowsServices/src/WindowsServiceHelpers.cs#L26-L31
73-
// Specifically, it looks up whether the parent process has session ID zero
74-
// and is called "services".
71+
// exactly what the .NET runtime (>= 6.0.10) does for the similarly named function:
72+
// https://github.com/dotnet/runtime/blob/36bf84fc4a89209f4fdbc1fc201e81afd8be49b0/src/libraries/Microsoft.Extensions.Hosting.WindowsServices/src/WindowsServiceHelpers.cs#L20-L33
73+
// Specifically, it looks up whether the parent process is called "services".
7574

7675
var currentProcess windows.PROCESS_BASIC_INFORMATION
7776
infoSize := uint32(unsafe.Sizeof(currentProcess))
@@ -91,7 +90,7 @@ func IsWindowsService() (bool, error) {
9190
}
9291
for ; ; parentProcess = (*windows.SYSTEM_PROCESS_INFORMATION)(unsafe.Pointer(uintptr(unsafe.Pointer(parentProcess)) + uintptr(parentProcess.NextEntryOffset))) {
9392
if parentProcess.UniqueProcessID == currentProcess.InheritedFromUniqueProcessId {
94-
return parentProcess.SessionID == 0 && strings.EqualFold("services.exe", parentProcess.ImageName.String()), nil
93+
return strings.EqualFold("services.exe", parentProcess.ImageName.String()), nil
9594
}
9695
if parentProcess.NextEntryOffset == 0 {
9796
break

0 commit comments

Comments
 (0)