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.
14
"github.com/gabriel-samfira/sys/windows"
18
// Service start types.
19
StartManual = windows.SERVICE_DEMAND_START // the service must be started manually
20
StartAutomatic = windows.SERVICE_AUTO_START // the service will start by itself whenever the computer reboots
21
StartDisabled = windows.SERVICE_DISABLED // the service cannot be started
23
// The severity of the error, and action taken,
24
// if this service fails to start.
25
ErrorCritical = windows.SERVICE_ERROR_CRITICAL
26
ErrorIgnore = windows.SERVICE_ERROR_IGNORE
27
ErrorNormal = windows.SERVICE_ERROR_NORMAL
28
ErrorSevere = windows.SERVICE_ERROR_SEVERE
31
// TODO(brainman): Password is not returned by windows.QueryServiceConfig, not sure how to get it.
37
BinaryPathName string // fully qualified path to the service binary file, can also include arguments for an auto-start service
41
ServiceStartName string // name of the account under which the service should run
47
func toString(p *uint16) string {
51
return syscall.UTF16ToString((*[4096]uint16)(unsafe.Pointer(p))[:])
54
func toStringSlice(ps *uint16) []string {
58
r := make([]string, 0)
59
for from, i, p := 0, 0, (*[1 << 24]uint16)(unsafe.Pointer(ps)); true; i++ {
61
// empty string marks the end
65
r = append(r, string(utf16.Decode(p[from:i])))
72
// Config retrieves service s configuration paramteres.
73
func (s *Service) Config() (Config, error) {
74
var p *windows.QUERY_SERVICE_CONFIG
78
p = (*windows.QUERY_SERVICE_CONFIG)(unsafe.Pointer(&b[0]))
79
err := windows.QueryServiceConfig(s.Handle, p, n, &n)
83
if err.(syscall.Errno) != syscall.ERROR_INSUFFICIENT_BUFFER {
86
if n <= uint32(len(b)) {
91
var p2 *windows.SERVICE_DESCRIPTION
95
p2 = (*windows.SERVICE_DESCRIPTION)(unsafe.Pointer(&b[0]))
96
err := windows.QueryServiceConfig2(s.Handle,
97
windows.SERVICE_CONFIG_DESCRIPTION, &b[0], n, &n)
101
if err.(syscall.Errno) != syscall.ERROR_INSUFFICIENT_BUFFER {
104
if n <= uint32(len(b)) {
110
ServiceType: p.ServiceType,
111
StartType: p.StartType,
112
ErrorControl: p.ErrorControl,
113
BinaryPathName: toString(p.BinaryPathName),
114
LoadOrderGroup: toString(p.LoadOrderGroup),
116
Dependencies: toStringSlice(p.Dependencies),
117
ServiceStartName: toString(p.ServiceStartName),
118
DisplayName: toString(p.DisplayName),
119
Description: toString(p2.Description),
123
func updateDescription(handle windows.Handle, desc string) error {
124
d := windows.SERVICE_DESCRIPTION{toPtr(desc)}
125
return windows.ChangeServiceConfig2(handle,
126
windows.SERVICE_CONFIG_DESCRIPTION, (*byte)(unsafe.Pointer(&d)))
129
// UpdateConfig updates service s configuration parameters.
130
func (s *Service) UpdateConfig(c Config) error {
131
err := windows.ChangeServiceConfig(s.Handle, c.ServiceType, c.StartType,
132
c.ErrorControl, toPtr(c.BinaryPathName), toPtr(c.LoadOrderGroup),
133
nil, toStringBlock(c.Dependencies), toPtr(c.ServiceStartName),
134
toPtr(c.Password), toPtr(c.DisplayName))
138
return updateDescription(s.Handle, c.Description)