Skip to content

Commit

Permalink
util/winutil: publicize existing functions for opening read-only conn…
Browse files Browse the repository at this point in the history
…ections to the Windows Service Control Manager

We're going to need to access these from code outside winutil.

Updates tailscale#10215

Signed-off-by: Aaron Klotz <aaron@tailscale.com>
  • Loading branch information
dblohm7 committed Dec 22, 2023
1 parent cae6edf commit 5812093
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions util/winutil/svcdiag_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ type walkSvcFunc func(*mgr.Service, mgr.Config)
// walkServices opens the service named rootSvcName and walks its dependency
// graph, invoking callback for each service (including the root itself).
func walkServices(rootSvcName string, callback walkSvcFunc) error {
scm, err := connectToLocalSCMForRead()
scm, err := ConnectToLocalSCMForRead()
if err != nil {
return fmt.Errorf("connecting to Service Control Manager: %w", err)
}
defer scm.Disconnect()

rootSvc, err := openServiceForRead(scm, rootSvcName)
rootSvc, err := OpenServiceForRead(scm, rootSvcName)
if err != nil {
return fmt.Errorf("opening service %q: %w", rootSvcName, err)
}
Expand Down Expand Up @@ -102,7 +102,7 @@ func walkServices(rootSvcName string, callback walkSvcFunc) error {
continue
}

depSvc, err := openServiceForRead(scm, depName)
depSvc, err := OpenServiceForRead(scm, depName)
if err != nil {
return fmt.Errorf("opening service %q: %w", depName, err)
}
Expand Down Expand Up @@ -276,21 +276,21 @@ func makeLogEntry(svc *mgr.Service, status svc.Status, cfg mgr.Config) (entry sv
return entry
}

// connectToLocalSCMForRead connects to the Windows Service Control Manager with
// ConnectToLocalSCMForRead connects to the Windows Service Control Manager with
// read-only access. x/sys/windows/svc/mgr/Connect requests read+write access,
// which requires higher privileges than we want.
func connectToLocalSCMForRead() (*mgr.Mgr, error) {
// which requires Administrative access rights.
func ConnectToLocalSCMForRead() (*mgr.Mgr, error) {
h, err := windows.OpenSCManager(nil, nil, windows.GENERIC_READ)
if err != nil {
return nil, err
}
return &mgr.Mgr{Handle: h}, nil
}

// openServiceForRead opens a service with read-only access.
// OpenServiceForRead opens a service with read-only access.
// x/sys/windows/svc/mgr/(*Mgr).OpenService requests read+write access,
// which requires higher privileges than we want.
func openServiceForRead(scm *mgr.Mgr, svcName string) (*mgr.Service, error) {
// which requires Administrative access rights.
func OpenServiceForRead(scm *mgr.Mgr, svcName string) (*mgr.Service, error) {
svcNamePtr, err := windows.UTF16PtrFromString(svcName)
if err != nil {
return nil, err
Expand Down

0 comments on commit 5812093

Please sign in to comment.