config.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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.
  4. // +build windows
  5. package mgr
  6. import (
  7. "syscall"
  8. "unicode/utf16"
  9. "unsafe"
  10. "golang.org/x/sys/windows"
  11. )
  12. const (
  13. // Service start types.
  14. StartManual = windows.SERVICE_DEMAND_START // the service must be started manually
  15. StartAutomatic = windows.SERVICE_AUTO_START // the service will start by itself whenever the computer reboots
  16. StartDisabled = windows.SERVICE_DISABLED // the service cannot be started
  17. // The severity of the error, and action taken,
  18. // if this service fails to start.
  19. ErrorCritical = windows.SERVICE_ERROR_CRITICAL
  20. ErrorIgnore = windows.SERVICE_ERROR_IGNORE
  21. ErrorNormal = windows.SERVICE_ERROR_NORMAL
  22. ErrorSevere = windows.SERVICE_ERROR_SEVERE
  23. )
  24. // TODO(brainman): Password is not returned by windows.QueryServiceConfig, not sure how to get it.
  25. type Config struct {
  26. ServiceType uint32
  27. StartType uint32
  28. ErrorControl uint32
  29. BinaryPathName string // fully qualified path to the service binary file, can also include arguments for an auto-start service
  30. LoadOrderGroup string
  31. TagId uint32
  32. Dependencies []string
  33. ServiceStartName string // name of the account under which the service should run
  34. DisplayName string
  35. Password string
  36. Description string
  37. SidType uint32 // one of SERVICE_SID_TYPE, the type of sid to use for the service
  38. DelayedAutoStart bool // the service is started after other auto-start services are started plus a short delay
  39. }
  40. func toString(p *uint16) string {
  41. if p == nil {
  42. return ""
  43. }
  44. return syscall.UTF16ToString((*[4096]uint16)(unsafe.Pointer(p))[:])
  45. }
  46. func toStringSlice(ps *uint16) []string {
  47. if ps == nil {
  48. return nil
  49. }
  50. r := make([]string, 0)
  51. for from, i, p := 0, 0, (*[1 << 24]uint16)(unsafe.Pointer(ps)); true; i++ {
  52. if p[i] == 0 {
  53. // empty string marks the end
  54. if i <= from {
  55. break
  56. }
  57. r = append(r, string(utf16.Decode(p[from:i])))
  58. from = i + 1
  59. }
  60. }
  61. return r
  62. }
  63. // Config retrieves service s configuration paramteres.
  64. func (s *Service) Config() (Config, error) {
  65. var p *windows.QUERY_SERVICE_CONFIG
  66. n := uint32(1024)
  67. for {
  68. b := make([]byte, n)
  69. p = (*windows.QUERY_SERVICE_CONFIG)(unsafe.Pointer(&b[0]))
  70. err := windows.QueryServiceConfig(s.Handle, p, n, &n)
  71. if err == nil {
  72. break
  73. }
  74. if err.(syscall.Errno) != syscall.ERROR_INSUFFICIENT_BUFFER {
  75. return Config{}, err
  76. }
  77. if n <= uint32(len(b)) {
  78. return Config{}, err
  79. }
  80. }
  81. b, err := s.queryServiceConfig2(windows.SERVICE_CONFIG_DESCRIPTION)
  82. if err != nil {
  83. return Config{}, err
  84. }
  85. p2 := (*windows.SERVICE_DESCRIPTION)(unsafe.Pointer(&b[0]))
  86. b, err = s.queryServiceConfig2(windows.SERVICE_CONFIG_DELAYED_AUTO_START_INFO)
  87. if err != nil {
  88. return Config{}, err
  89. }
  90. p3 := (*windows.SERVICE_DELAYED_AUTO_START_INFO)(unsafe.Pointer(&b[0]))
  91. delayedStart := false
  92. if p3.IsDelayedAutoStartUp != 0 {
  93. delayedStart = true
  94. }
  95. return Config{
  96. ServiceType: p.ServiceType,
  97. StartType: p.StartType,
  98. ErrorControl: p.ErrorControl,
  99. BinaryPathName: toString(p.BinaryPathName),
  100. LoadOrderGroup: toString(p.LoadOrderGroup),
  101. TagId: p.TagId,
  102. Dependencies: toStringSlice(p.Dependencies),
  103. ServiceStartName: toString(p.ServiceStartName),
  104. DisplayName: toString(p.DisplayName),
  105. Description: toString(p2.Description),
  106. DelayedAutoStart: delayedStart,
  107. }, nil
  108. }
  109. func updateDescription(handle windows.Handle, desc string) error {
  110. d := windows.SERVICE_DESCRIPTION{Description: toPtr(desc)}
  111. return windows.ChangeServiceConfig2(handle,
  112. windows.SERVICE_CONFIG_DESCRIPTION, (*byte)(unsafe.Pointer(&d)))
  113. }
  114. func updateSidType(handle windows.Handle, sidType uint32) error {
  115. return windows.ChangeServiceConfig2(handle, windows.SERVICE_CONFIG_SERVICE_SID_INFO, (*byte)(unsafe.Pointer(&sidType)))
  116. }
  117. func updateStartUp(handle windows.Handle, isDelayed bool) error {
  118. var d windows.SERVICE_DELAYED_AUTO_START_INFO
  119. if isDelayed {
  120. d.IsDelayedAutoStartUp = 1
  121. }
  122. return windows.ChangeServiceConfig2(handle,
  123. windows.SERVICE_CONFIG_DELAYED_AUTO_START_INFO, (*byte)(unsafe.Pointer(&d)))
  124. }
  125. // UpdateConfig updates service s configuration parameters.
  126. func (s *Service) UpdateConfig(c Config) error {
  127. err := windows.ChangeServiceConfig(s.Handle, c.ServiceType, c.StartType,
  128. c.ErrorControl, toPtr(c.BinaryPathName), toPtr(c.LoadOrderGroup),
  129. nil, toStringBlock(c.Dependencies), toPtr(c.ServiceStartName),
  130. toPtr(c.Password), toPtr(c.DisplayName))
  131. if err != nil {
  132. return err
  133. }
  134. err = updateSidType(s.Handle, c.SidType)
  135. if err != nil {
  136. return err
  137. }
  138. err = updateStartUp(s.Handle, c.DelayedAutoStart)
  139. if err != nil {
  140. return err
  141. }
  142. return updateDescription(s.Handle, c.Description)
  143. }
  144. // queryServiceConfig2 calls Windows QueryServiceConfig2 with infoLevel parameter and returns retrieved service configuration information.
  145. func (s *Service) queryServiceConfig2(infoLevel uint32) ([]byte, error) {
  146. n := uint32(1024)
  147. for {
  148. b := make([]byte, n)
  149. err := windows.QueryServiceConfig2(s.Handle, infoLevel, &b[0], n, &n)
  150. if err == nil {
  151. return b, nil
  152. }
  153. if err.(syscall.Errno) != syscall.ERROR_INSUFFICIENT_BUFFER {
  154. return nil, err
  155. }
  156. if n <= uint32(len(b)) {
  157. return nil, err
  158. }
  159. }
  160. }