Browse Source

windows: allow querying service ProcessId

The two functions have the same levels of compatibility, but this latter
one gives us access to the process ID, which is important for things
like WFP whitelisting. The change required is fairly trivial too.

Change-Id: Ifb6b3ee3e897202b9cffa1388c53c25cbcfede61
Reviewed-on: https://go-review.googlesource.com/c/sys/+/173666
Run-TryBot: Jason Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Jason A. Donenfeld 6 years ago
parent
commit
1607263960
2 changed files with 8 additions and 4 deletions
  1. 7 4
      windows/svc/mgr/service.go
  2. 1 0
      windows/svc/service.go

+ 7 - 4
windows/svc/mgr/service.go

@@ -8,6 +8,7 @@ package mgr
 
 
 import (
 import (
 	"syscall"
 	"syscall"
+	"unsafe"
 
 
 	"golang.org/x/sys/windows"
 	"golang.org/x/sys/windows"
 	"golang.org/x/sys/windows/svc"
 	"golang.org/x/sys/windows/svc"
@@ -60,13 +61,15 @@ func (s *Service) Control(c svc.Cmd) (svc.Status, error) {
 
 
 // Query returns current status of service s.
 // Query returns current status of service s.
 func (s *Service) Query() (svc.Status, error) {
 func (s *Service) Query() (svc.Status, error) {
-	var t windows.SERVICE_STATUS
-	err := windows.QueryServiceStatus(s.Handle, &t)
+	var t windows.SERVICE_STATUS_PROCESS
+	var needed uint32
+	err := windows.QueryServiceStatusEx(s.Handle, windows.SC_STATUS_PROCESS_INFO, (*byte)(unsafe.Pointer(&t)), uint32(unsafe.Sizeof(t)), &needed)
 	if err != nil {
 	if err != nil {
 		return svc.Status{}, err
 		return svc.Status{}, err
 	}
 	}
 	return svc.Status{
 	return svc.Status{
-		State:   svc.State(t.CurrentState),
-		Accepts: svc.Accepted(t.ControlsAccepted),
+		State:     svc.State(t.CurrentState),
+		Accepts:   svc.Accepted(t.ControlsAccepted),
+		ProcessId: t.ProcessId,
 	}, nil
 	}, nil
 }
 }

+ 1 - 0
windows/svc/service.go

@@ -72,6 +72,7 @@ type Status struct {
 	Accepts    Accepted
 	Accepts    Accepted
 	CheckPoint uint32 // used to report progress during a lengthy operation
 	CheckPoint uint32 // used to report progress during a lengthy operation
 	WaitHint   uint32 // estimated time required for a pending operation, in milliseconds
 	WaitHint   uint32 // estimated time required for a pending operation, in milliseconds
+	ProcessId  uint32
 }
 }
 
 
 // ChangeRequest is sent to the service Handler to request service status change.
 // ChangeRequest is sent to the service Handler to request service status change.