2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2022-06-07 17:27:14 +00:00
|
|
|
//go:build windows
|
|
|
|
// +build windows
|
|
|
|
|
|
|
|
package checks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"golang.org/x/sys/windows"
|
|
|
|
"golang.org/x/sys/windows/svc"
|
|
|
|
"golang.org/x/sys/windows/svc/mgr"
|
|
|
|
)
|
|
|
|
|
2022-06-09 14:48:34 +00:00
|
|
|
var (
|
|
|
|
win windowsSystem = windowsOS{}
|
|
|
|
)
|
|
|
|
|
2022-09-21 08:26:47 +00:00
|
|
|
type OSServiceClient struct{}
|
2022-06-07 17:27:14 +00:00
|
|
|
|
|
|
|
func NewOSServiceClient() (*OSServiceClient, error) {
|
2022-09-21 08:26:47 +00:00
|
|
|
return &OSServiceClient{}, nil
|
2022-06-07 17:27:14 +00:00
|
|
|
}
|
|
|
|
|
2022-06-09 14:48:34 +00:00
|
|
|
func (client *OSServiceClient) Check(serviceName string) (err error) {
|
2022-09-21 08:26:47 +00:00
|
|
|
h, err := windows.OpenSCManager(nil, nil, windows.SC_MANAGER_CONNECT)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to connect to Windows service manager: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m := win.getWindowsSvcMgr(h)
|
2022-08-28 16:56:30 +00:00
|
|
|
defer m.Disconnect()
|
|
|
|
|
|
|
|
svcNamePtr, err := syscall.UTF16PtrFromString(serviceName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("service name must not contain NUL bytes: %w", err)
|
|
|
|
}
|
|
|
|
svcHandle, err := win.OpenService(win.getWindowsSvcMgrHandle(m), svcNamePtr, windows.SC_MANAGER_ENUMERATE_SERVICE)
|
2022-06-07 17:27:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error accessing service: %w", err)
|
|
|
|
}
|
2022-06-09 14:48:34 +00:00
|
|
|
service := win.getWindowsSvc(serviceName, svcHandle)
|
2022-08-28 16:56:30 +00:00
|
|
|
defer service.Close()
|
|
|
|
|
2022-06-07 17:27:14 +00:00
|
|
|
status, err := service.Query()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error querying service status: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch status.State {
|
|
|
|
case svc.Running:
|
2022-08-28 16:56:30 +00:00
|
|
|
return nil
|
2022-06-09 14:48:34 +00:00
|
|
|
case svc.Paused, svc.Stopped:
|
2022-08-28 16:56:30 +00:00
|
|
|
err = fmt.Errorf("service status: %v - %w", svcStateString(status.State), ErrOSServiceStatusCritical)
|
2022-06-07 17:27:14 +00:00
|
|
|
default:
|
2022-08-28 16:56:30 +00:00
|
|
|
err = fmt.Errorf("service status: %v", svcStateString(status.State))
|
2022-06-07 17:27:14 +00:00
|
|
|
}
|
2022-06-09 14:48:34 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
type windowsOS struct{}
|
|
|
|
|
|
|
|
func (windowsOS) OpenSCManager(machineName *uint16, databaseName *uint16, access uint32) (handle windows.Handle, err error) {
|
|
|
|
return windows.OpenSCManager(machineName, databaseName, access)
|
|
|
|
}
|
|
|
|
func (windowsOS) OpenService(mgr windows.Handle, serviceName *uint16, access uint32) (handle windows.Handle, err error) {
|
|
|
|
return windows.OpenService(mgr, serviceName, access)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (windowsOS) getWindowsSvcMgr(h windows.Handle) windowsSvcMgr { return &mgr.Mgr{Handle: h} }
|
|
|
|
func (windowsOS) getWindowsSvcMgrHandle(sm windowsSvcMgr) windows.Handle {
|
|
|
|
return sm.(*mgr.Mgr).Handle
|
|
|
|
}
|
|
|
|
|
|
|
|
func (windowsOS) getWindowsSvc(name string, h windows.Handle) windowsSvc {
|
|
|
|
return &mgr.Service{Name: name, Handle: h}
|
|
|
|
}
|
|
|
|
|
|
|
|
type windowsSystem interface {
|
|
|
|
OpenSCManager(machineName *uint16, databaseName *uint16, access uint32) (handle windows.Handle, err error)
|
|
|
|
OpenService(mgr windows.Handle, serviceName *uint16, access uint32) (handle windows.Handle, err error)
|
|
|
|
|
|
|
|
getWindowsSvcMgr(h windows.Handle) windowsSvcMgr
|
|
|
|
getWindowsSvcMgrHandle(sm windowsSvcMgr) windows.Handle
|
|
|
|
getWindowsSvc(name string, h windows.Handle) windowsSvc
|
|
|
|
}
|
|
|
|
|
|
|
|
type windowsSvcMgr interface {
|
|
|
|
Disconnect() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type windowsSvc interface {
|
|
|
|
Close() error
|
|
|
|
Query() (svc.Status, error)
|
2022-06-07 17:27:14 +00:00
|
|
|
}
|
2022-08-28 16:56:30 +00:00
|
|
|
|
|
|
|
// svcStateString converts svc.State (uint32) to human readable string
|
|
|
|
//
|
|
|
|
// source: https://pkg.go.dev/golang.org/x/sys/windows/svc#pkg-constants
|
|
|
|
func svcStateString(state svc.State) string {
|
|
|
|
switch state {
|
|
|
|
case svc.State(windows.SERVICE_STOPPED):
|
|
|
|
return "Stopped"
|
|
|
|
case svc.State(windows.SERVICE_START_PENDING):
|
|
|
|
return "StartPending"
|
|
|
|
case svc.State(windows.SERVICE_STOP_PENDING):
|
|
|
|
return "StopPending"
|
|
|
|
case svc.State(windows.SERVICE_RUNNING):
|
|
|
|
return "Running"
|
|
|
|
case svc.State(windows.SERVICE_CONTINUE_PENDING):
|
|
|
|
return "ContinuePending"
|
|
|
|
case svc.State(windows.SERVICE_PAUSE_PENDING):
|
|
|
|
return "PausePending"
|
|
|
|
case svc.State(windows.SERVICE_PAUSED):
|
|
|
|
return "Paused"
|
|
|
|
default:
|
|
|
|
//if not handled we return the underlying uint32
|
|
|
|
return fmt.Sprintf("%d", state)
|
|
|
|
}
|
|
|
|
}
|