Skip to content

Commit

Permalink
windows: add Service.ListDependentServices
Browse files Browse the repository at this point in the history
This method allows a user to list all Windows services which are
dependent upon a given service.

This commit makes use of the EnumDependentServices Windows API call.
Without this, a user would have to iterate through each service on the
system, and check if the given service is listed in each service's
dependencies list.

The implementation of ListDependentServices is mostly the same as
Mgr.ListServices, as the API calls behave in the same way.

Fixes golang/go#56766

Change-Id: I9ec18c97afd02f48deef691ccdd5c26d6501add1
Reviewed-on: https://go-review.googlesource.com/c/sys/+/451363
Reviewed-by: Than McIntosh <[email protected]>
Run-TryBot: Alex Brainman <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Reviewed-by: Benny Siegert <[email protected]>
Reviewed-by: Alex Brainman <[email protected]>
  • Loading branch information
sebsoto authored and alexbrainman committed Apr 14, 2023
1 parent f25ff60 commit dbd8f99
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 2 deletions.
7 changes: 7 additions & 0 deletions windows/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ const (
SERVICE_DYNAMIC_INFORMATION_LEVEL_START_REASON = 1
)

type ENUM_SERVICE_STATUS struct {
ServiceName *uint16
DisplayName *uint16
ServiceStatus SERVICE_STATUS
}

type SERVICE_STATUS struct {
ServiceType uint32
CurrentState uint32
Expand Down Expand Up @@ -245,3 +251,4 @@ type QUERY_SERVICE_LOCK_STATUS struct {
//sys UnsubscribeServiceChangeNotifications(subscription uintptr) = sechost.UnsubscribeServiceChangeNotifications?
//sys RegisterServiceCtrlHandlerEx(serviceName *uint16, handlerProc uintptr, context uintptr) (handle Handle, err error) = advapi32.RegisterServiceCtrlHandlerExW
//sys QueryServiceDynamicInformation(service Handle, infoLevel uint32, dynamicInfo unsafe.Pointer) (err error) = advapi32.QueryServiceDynamicInformation?
//sys EnumDependentServices(service Handle, activityState uint32, services *ENUM_SERVICE_STATUS, buffSize uint32, bytesNeeded *uint32, servicesReturned *uint32) (err error) = advapi32.EnumDependentServicesW
43 changes: 43 additions & 0 deletions windows/svc/mgr/mgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"testing"
"time"

"golang.org/x/sys/windows/svc"
"golang.org/x/sys/windows/svc/mgr"
)

Expand Down Expand Up @@ -294,3 +295,45 @@ func TestMyService(t *testing.T) {

remove(t, s)
}

func TestListDependentServices(t *testing.T) {
m, err := mgr.Connect()
if err != nil {
if errno, ok := err.(syscall.Errno); ok && errno == syscall.ERROR_ACCESS_DENIED {
t.Skip("Skipping test: we don't have rights to manage services.")
}
t.Fatalf("SCM connection failed: %s", err)
}
defer m.Disconnect()

baseServiceName := "testservice1"
dependentServiceName := "testservice2"
install(t, m, baseServiceName, "", mgr.Config{})
baseService, err := m.OpenService(baseServiceName)
if err != nil {
t.Fatalf("OpenService failed: %v", err)
}
defer remove(t, baseService)
install(t, m, dependentServiceName, "", mgr.Config{Dependencies: []string{baseServiceName}})
dependentService, err := m.OpenService(dependentServiceName)
if err != nil {
t.Fatalf("OpenService failed: %v", err)
}
defer remove(t, dependentService)

// test that both the base service and dependent service list the correct dependencies
dependentServices, err := baseService.ListDependentServices(svc.AnyActivity)
if err != nil {
t.Fatalf("baseService.ListDependentServices failed: %v", err)
}
if len(dependentServices) != 1 || dependentServices[0] != dependentServiceName {
t.Errorf("Found %v, instead of expected contents %s", dependentServices, dependentServiceName)
}
dependentServices, err = dependentService.ListDependentServices(svc.AnyActivity)
if err != nil {
t.Fatalf("dependentService.ListDependentServices failed: %v", err)
}
if len(dependentServices) != 0 {
t.Errorf("Found %v, where no service should be listed", dependentService)
}
}
43 changes: 41 additions & 2 deletions windows/svc/mgr/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import (
"golang.org/x/sys/windows/svc"
)

// TODO(brainman): Use EnumDependentServices to enumerate dependent services.

// Service is used to access Windows service.
type Service struct {
Name string
Expand Down Expand Up @@ -76,3 +74,44 @@ func (s *Service) Query() (svc.Status, error) {
ServiceSpecificExitCode: t.ServiceSpecificExitCode,
}, nil
}

// ListDependentServices returns the names of the services dependent on service s, which match the given status.
func (s *Service) ListDependentServices(status svc.ActivityStatus) ([]string, error) {
var bytesNeeded, returnedServiceCount uint32
var services []windows.ENUM_SERVICE_STATUS
for {
var servicesPtr *windows.ENUM_SERVICE_STATUS
if len(services) > 0 {
servicesPtr = &services[0]
}
allocatedBytes := uint32(len(services)) * uint32(unsafe.Sizeof(windows.ENUM_SERVICE_STATUS{}))
err := windows.EnumDependentServices(s.Handle, uint32(status), servicesPtr, allocatedBytes, &bytesNeeded,
&returnedServiceCount)
if err == nil {
break
}
if err != syscall.ERROR_MORE_DATA {
return nil, err
}
if bytesNeeded <= allocatedBytes {
return nil, err
}
// ERROR_MORE_DATA indicates the provided buffer was too small, run the call again after resizing the buffer
requiredSliceLen := bytesNeeded / uint32(unsafe.Sizeof(windows.ENUM_SERVICE_STATUS{}))
if bytesNeeded%uint32(unsafe.Sizeof(windows.ENUM_SERVICE_STATUS{})) != 0 {
requiredSliceLen += 1
}
services = make([]windows.ENUM_SERVICE_STATUS, requiredSliceLen)
}
if returnedServiceCount == 0 {
return nil, nil
}

// The slice mutated by EnumDependentServices may have a length greater than returnedServiceCount, any elements
// past that should be ignored.
var dependents []string
for i := 0; i < int(returnedServiceCount); i++ {
dependents = append(dependents, windows.UTF16PtrToString(services[i].ServiceName))
}
return dependents, nil
}
9 changes: 9 additions & 0 deletions windows/svc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ const (
AcceptPreShutdown = Accepted(windows.SERVICE_ACCEPT_PRESHUTDOWN)
)

// ActivityStatus allows for services to be selected based on active and inactive categories of service state.
type ActivityStatus uint32

const (
Active = ActivityStatus(windows.SERVICE_ACTIVE)
Inactive = ActivityStatus(windows.SERVICE_INACTIVE)
AnyActivity = ActivityStatus(windows.SERVICE_STATE_ALL)
)

// Status combines State and Accepted commands to fully describe running service.
type Status struct {
State State
Expand Down
9 changes: 9 additions & 0 deletions windows/zsyscall_windows.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit dbd8f99

Please sign in to comment.