1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 "golang.org/x/sys/windows"
17 // Service start types.
18 StartManual = windows.SERVICE_DEMAND_START // the service must be started manually
19 StartAutomatic = windows.SERVICE_AUTO_START // the service will start by itself whenever the computer reboots
20 StartDisabled = windows.SERVICE_DISABLED // the service cannot be started
22 // The severity of the error, and action taken,
23 // if this service fails to start.
24 ErrorCritical = windows.SERVICE_ERROR_CRITICAL
25 ErrorIgnore = windows.SERVICE_ERROR_IGNORE
26 ErrorNormal = windows.SERVICE_ERROR_NORMAL
27 ErrorSevere = windows.SERVICE_ERROR_SEVERE
30 // TODO(brainman): Password is not returned by windows.QueryServiceConfig, not sure how to get it.
36 BinaryPathName string // fully qualified path to the service binary file, can also include arguments for an auto-start service
40 ServiceStartName string // name of the account under which the service should run
44 SidType uint32 // one of SERVICE_SID_TYPE, the type of sid to use for the service
45 DelayedAutoStart bool // the service is started after other auto-start services are started plus a short delay
48 func toStringSlice(ps *uint16) []string {
49 r := make([]string, 0)
50 p := unsafe.Pointer(ps)
53 s := windows.UTF16PtrToString((*uint16)(p))
59 offset := unsafe.Sizeof(uint16(0)) * (uintptr)(len(s)+1)
60 p = unsafe.Pointer(uintptr(p) + offset)
66 // Config retrieves service s configuration paramteres.
67 func (s *Service) Config() (Config, error) {
68 var p *windows.QUERY_SERVICE_CONFIG
72 p = (*windows.QUERY_SERVICE_CONFIG)(unsafe.Pointer(&b[0]))
73 err := windows.QueryServiceConfig(s.Handle, p, n, &n)
77 if err.(syscall.Errno) != syscall.ERROR_INSUFFICIENT_BUFFER {
80 if n <= uint32(len(b)) {
85 b, err := s.queryServiceConfig2(windows.SERVICE_CONFIG_DESCRIPTION)
89 p2 := (*windows.SERVICE_DESCRIPTION)(unsafe.Pointer(&b[0]))
91 b, err = s.queryServiceConfig2(windows.SERVICE_CONFIG_DELAYED_AUTO_START_INFO)
95 p3 := (*windows.SERVICE_DELAYED_AUTO_START_INFO)(unsafe.Pointer(&b[0]))
97 if p3.IsDelayedAutoStartUp != 0 {
101 b, err = s.queryServiceConfig2(windows.SERVICE_CONFIG_SERVICE_SID_INFO)
105 sidType := *(*uint32)(unsafe.Pointer(&b[0]))
108 ServiceType: p.ServiceType,
109 StartType: p.StartType,
110 ErrorControl: p.ErrorControl,
111 BinaryPathName: windows.UTF16PtrToString(p.BinaryPathName),
112 LoadOrderGroup: windows.UTF16PtrToString(p.LoadOrderGroup),
114 Dependencies: toStringSlice(p.Dependencies),
115 ServiceStartName: windows.UTF16PtrToString(p.ServiceStartName),
116 DisplayName: windows.UTF16PtrToString(p.DisplayName),
117 Description: windows.UTF16PtrToString(p2.Description),
118 DelayedAutoStart: delayedStart,
123 func updateDescription(handle windows.Handle, desc string) error {
124 d := windows.SERVICE_DESCRIPTION{Description: toPtr(desc)}
125 return windows.ChangeServiceConfig2(handle,
126 windows.SERVICE_CONFIG_DESCRIPTION, (*byte)(unsafe.Pointer(&d)))
129 func updateSidType(handle windows.Handle, sidType uint32) error {
130 return windows.ChangeServiceConfig2(handle, windows.SERVICE_CONFIG_SERVICE_SID_INFO, (*byte)(unsafe.Pointer(&sidType)))
133 func updateStartUp(handle windows.Handle, isDelayed bool) error {
134 var d windows.SERVICE_DELAYED_AUTO_START_INFO
136 d.IsDelayedAutoStartUp = 1
138 return windows.ChangeServiceConfig2(handle,
139 windows.SERVICE_CONFIG_DELAYED_AUTO_START_INFO, (*byte)(unsafe.Pointer(&d)))
142 // UpdateConfig updates service s configuration parameters.
143 func (s *Service) UpdateConfig(c Config) error {
144 err := windows.ChangeServiceConfig(s.Handle, c.ServiceType, c.StartType,
145 c.ErrorControl, toPtr(c.BinaryPathName), toPtr(c.LoadOrderGroup),
146 nil, toStringBlock(c.Dependencies), toPtr(c.ServiceStartName),
147 toPtr(c.Password), toPtr(c.DisplayName))
151 err = updateSidType(s.Handle, c.SidType)
156 err = updateStartUp(s.Handle, c.DelayedAutoStart)
161 return updateDescription(s.Handle, c.Description)
164 // queryServiceConfig2 calls Windows QueryServiceConfig2 with infoLevel parameter and returns retrieved service configuration information.
165 func (s *Service) queryServiceConfig2(infoLevel uint32) ([]byte, error) {
169 err := windows.QueryServiceConfig2(s.Handle, infoLevel, &b[0], n, &n)
173 if err.(syscall.Errno) != syscall.ERROR_INSUFFICIENT_BUFFER {
176 if n <= uint32(len(b)) {