windows: add support for automatic delayed start in windows service

Change-Id: Iad33ea0f6627ac98c89dbaab0b41b3dd724c3163
GitHub-Last-Rev: 8764fdbd3200eb38d482cfffb5bd98f565f4395f
GitHub-Pull-Request: golang/sys#36
Reviewed-on: https://go-review.googlesource.com/c/sys/+/187198
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
utkarsh-extc
2019-08-04 05:38:45 +00:00
committed by Alex Brainman
parent cbf593c0f2
commit 51ab0e2dea
4 changed files with 51 additions and 0 deletions
+27
View File
@@ -43,6 +43,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 bool // the service is started after other auto-start services are started plus a short delay
}
func toString(p *uint16) string {
@@ -95,6 +96,16 @@ 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]))
delayedStart := false
if p3.IsDelayedAutoStartUp != 0 {
delayedStart = true
}
return Config{
ServiceType: p.ServiceType,
StartType: p.StartType,
@@ -106,6 +117,7 @@ func (s *Service) Config() (Config, error) {
ServiceStartName: toString(p.ServiceStartName),
DisplayName: toString(p.DisplayName),
Description: toString(p2.Description),
DelayedAutoStart: delayedStart,
}, nil
}
@@ -119,6 +131,15 @@ 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 bool) error {
var d windows.SERVICE_DELAYED_AUTO_START_INFO
if isDelayed {
d.IsDelayedAutoStartUp = 1
}
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 +153,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)
}