config.go 5.4 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. //go:build windows
  5. // +build windows
  6. package mgr
  7. import (
  8. "syscall"
  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 toStringSlice(ps *uint16) []string {
  41. r := make([]string, 0)
  42. p := unsafe.Pointer(ps)
  43. for {
  44. s := windows.UTF16PtrToString((*uint16)(p))
  45. if len(s) == 0 {
  46. break
  47. }
  48. r = append(r, s)
  49. offset := unsafe.Sizeof(uint16(0)) * (uintptr)(len(s)+1)
  50. p = unsafe.Pointer(uintptr(p) + offset)
  51. }
  52. return r
  53. }
  54. // Config retrieves service s configuration paramteres.
  55. func (s *Service) Config() (Config, error) {
  56. var p *windows.QUERY_SERVICE_CONFIG
  57. n := uint32(1024)
  58. for {
  59. b := make([]byte, n)
  60. p = (*windows.QUERY_SERVICE_CONFIG)(unsafe.Pointer(&b[0]))
  61. err := windows.QueryServiceConfig(s.Handle, p, n, &n)
  62. if err == nil {
  63. break
  64. }
  65. if err.(syscall.Errno) != syscall.ERROR_INSUFFICIENT_BUFFER {
  66. return Config{}, err
  67. }
  68. if n <= uint32(len(b)) {
  69. return Config{}, err
  70. }
  71. }
  72. b, err := s.queryServiceConfig2(windows.SERVICE_CONFIG_DESCRIPTION)
  73. if err != nil {
  74. return Config{}, err
  75. }
  76. p2 := (*windows.SERVICE_DESCRIPTION)(unsafe.Pointer(&b[0]))
  77. b, err = s.queryServiceConfig2(windows.SERVICE_CONFIG_DELAYED_AUTO_START_INFO)
  78. if err != nil {
  79. return Config{}, err
  80. }
  81. p3 := (*windows.SERVICE_DELAYED_AUTO_START_INFO)(unsafe.Pointer(&b[0]))
  82. delayedStart := false
  83. if p3.IsDelayedAutoStartUp != 0 {
  84. delayedStart = true
  85. }
  86. b, err = s.queryServiceConfig2(windows.SERVICE_CONFIG_SERVICE_SID_INFO)
  87. if err != nil {
  88. return Config{}, err
  89. }
  90. sidType := *(*uint32)(unsafe.Pointer(&b[0]))
  91. return Config{
  92. ServiceType: p.ServiceType,
  93. StartType: p.StartType,
  94. ErrorControl: p.ErrorControl,
  95. BinaryPathName: windows.UTF16PtrToString(p.BinaryPathName),
  96. LoadOrderGroup: windows.UTF16PtrToString(p.LoadOrderGroup),
  97. TagId: p.TagId,
  98. Dependencies: toStringSlice(p.Dependencies),
  99. ServiceStartName: windows.UTF16PtrToString(p.ServiceStartName),
  100. DisplayName: windows.UTF16PtrToString(p.DisplayName),
  101. Description: windows.UTF16PtrToString(p2.Description),
  102. DelayedAutoStart: delayedStart,
  103. SidType: sidType,
  104. }, nil
  105. }
  106. func updateDescription(handle windows.Handle, desc string) error {
  107. d := windows.SERVICE_DESCRIPTION{Description: toPtr(desc)}
  108. return windows.ChangeServiceConfig2(handle,
  109. windows.SERVICE_CONFIG_DESCRIPTION, (*byte)(unsafe.Pointer(&d)))
  110. }
  111. func updateSidType(handle windows.Handle, sidType uint32) error {
  112. return windows.ChangeServiceConfig2(handle, windows.SERVICE_CONFIG_SERVICE_SID_INFO, (*byte)(unsafe.Pointer(&sidType)))
  113. }
  114. func updateStartUp(handle windows.Handle, isDelayed bool) error {
  115. var d windows.SERVICE_DELAYED_AUTO_START_INFO
  116. if isDelayed {
  117. d.IsDelayedAutoStartUp = 1
  118. }
  119. return windows.ChangeServiceConfig2(handle,
  120. windows.SERVICE_CONFIG_DELAYED_AUTO_START_INFO, (*byte)(unsafe.Pointer(&d)))
  121. }
  122. // UpdateConfig updates service s configuration parameters.
  123. func (s *Service) UpdateConfig(c Config) error {
  124. err := windows.ChangeServiceConfig(s.Handle, c.ServiceType, c.StartType,
  125. c.ErrorControl, toPtr(c.BinaryPathName), toPtr(c.LoadOrderGroup),
  126. nil, toStringBlock(c.Dependencies), toPtr(c.ServiceStartName),
  127. toPtr(c.Password), toPtr(c.DisplayName))
  128. if err != nil {
  129. return err
  130. }
  131. err = updateSidType(s.Handle, c.SidType)
  132. if err != nil {
  133. return err
  134. }
  135. err = updateStartUp(s.Handle, c.DelayedAutoStart)
  136. if err != nil {
  137. return err
  138. }
  139. return updateDescription(s.Handle, c.Description)
  140. }
  141. // queryServiceConfig2 calls Windows QueryServiceConfig2 with infoLevel parameter and returns retrieved service configuration information.
  142. func (s *Service) queryServiceConfig2(infoLevel uint32) ([]byte, error) {
  143. n := uint32(1024)
  144. for {
  145. b := make([]byte, n)
  146. err := windows.QueryServiceConfig2(s.Handle, infoLevel, &b[0], n, &n)
  147. if err == nil {
  148. return b, nil
  149. }
  150. if err.(syscall.Errno) != syscall.ERROR_INSUFFICIENT_BUFFER {
  151. return nil, err
  152. }
  153. if n <= uint32(len(b)) {
  154. return nil, err
  155. }
  156. }
  157. }