add support for delayed automatic service start

This commit is contained in:
utkarsh-extc
2019-07-23 15:39:27 +05:30
committed by GitHub
parent 7e62a73d66
commit a9b5334b0f

View File

@@ -20,6 +20,9 @@ const (
StartAutomatic = windows.SERVICE_AUTO_START // the service will start by itself whenever the computer reboots
StartDisabled = windows.SERVICE_DISABLED // the service cannot be started
// ServiceDelayedAutoStart types
ServiceDelayedAutoStartTrue = 1
ServiceDelayedAutoStartFalse = 0
// The severity of the error, and action taken,
// if this service fails to start.
ErrorCritical = windows.SERVICE_ERROR_CRITICAL
@@ -43,6 +46,7 @@ type Config struct {
Password string
Description string
SidType uint32 // one of SERVICE_SID_TYPE, the type of sid to use for the service
DelayedAutoStart uint32
}
func toString(p *uint16) string {
@@ -95,6 +99,12 @@ func (s *Service) Config() (Config, error) {
}
p2 := (*windows.SERVICE_DESCRIPTION)(unsafe.Pointer(&b[0]))
b, err = s.queryServiceConfig2(windows.SERVICE_CONFIG_DELAYED_AUTO_START_INFO)
if err != nil {
return Config{}, err
}
p3 := (*windows.SERVICE_DELAYED_AUTO_START_INFO)(unsafe.Pointer(&b[0]))
return Config{
ServiceType: p.ServiceType,
StartType: p.StartType,
@@ -106,6 +116,7 @@ func (s *Service) Config() (Config, error) {
ServiceStartName: toString(p.ServiceStartName),
DisplayName: toString(p.DisplayName),
Description: toString(p2.Description),
DelayedAutoStart: p3.IsDelayedAutoStartUp,
}, nil
}
@@ -119,6 +130,12 @@ func updateSidType(handle windows.Handle, sidType uint32) error {
return windows.ChangeServiceConfig2(handle, windows.SERVICE_CONFIG_SERVICE_SID_INFO, (*byte)(unsafe.Pointer(&sidType)))
}
func updateStartUp(handle windows.Handle, isDelayed uint32) error {
d := windows.SERVICE_DELAYED_AUTO_START_INFO{IsDelayedAutoStartUp: isDelayed}
return windows.ChangeServiceConfig2(handle,
windows.SERVICE_CONFIG_DELAYED_AUTO_START_INFO, (*byte)(unsafe.Pointer(&d)))
}
// UpdateConfig updates service s configuration parameters.
func (s *Service) UpdateConfig(c Config) error {
err := windows.ChangeServiceConfig(s.Handle, c.ServiceType, c.StartType,
@@ -132,6 +149,12 @@ func (s *Service) UpdateConfig(c Config) error {
if err != nil {
return err
}
err = updateStartUp(s.Handle, c.DelayedAutoStart)
if err != nil {
return err
}
return updateDescription(s.Handle, c.Description)
}