config.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. }
  39. func toString(p *uint16) string {
  40. if p == nil {
  41. return ""
  42. }
  43. return syscall.UTF16ToString((*[4096]uint16)(unsafe.Pointer(p))[:])
  44. }
  45. func toStringSlice(ps *uint16) []string {
  46. if ps == nil {
  47. return nil
  48. }
  49. r := make([]string, 0)
  50. for from, i, p := 0, 0, (*[1 << 24]uint16)(unsafe.Pointer(ps)); true; i++ {
  51. if p[i] == 0 {
  52. // empty string marks the end
  53. if i <= from {
  54. break
  55. }
  56. r = append(r, string(utf16.Decode(p[from:i])))
  57. from = i + 1
  58. }
  59. }
  60. return r
  61. }
  62. // Config retrieves service s configuration paramteres.
  63. func (s *Service) Config() (Config, error) {
  64. var p *windows.QUERY_SERVICE_CONFIG
  65. n := uint32(1024)
  66. for {
  67. b := make([]byte, n)
  68. p = (*windows.QUERY_SERVICE_CONFIG)(unsafe.Pointer(&b[0]))
  69. err := windows.QueryServiceConfig(s.Handle, p, n, &n)
  70. if err == nil {
  71. break
  72. }
  73. if err.(syscall.Errno) != syscall.ERROR_INSUFFICIENT_BUFFER {
  74. return Config{}, err
  75. }
  76. if n <= uint32(len(b)) {
  77. return Config{}, err
  78. }
  79. }
  80. b, err := s.queryServiceConfig2(windows.SERVICE_CONFIG_DESCRIPTION)
  81. if err != nil {
  82. return Config{}, err
  83. }
  84. p2 := (*windows.SERVICE_DESCRIPTION)(unsafe.Pointer(&b[0]))
  85. return Config{
  86. ServiceType: p.ServiceType,
  87. StartType: p.StartType,
  88. ErrorControl: p.ErrorControl,
  89. BinaryPathName: toString(p.BinaryPathName),
  90. LoadOrderGroup: toString(p.LoadOrderGroup),
  91. TagId: p.TagId,
  92. Dependencies: toStringSlice(p.Dependencies),
  93. ServiceStartName: toString(p.ServiceStartName),
  94. DisplayName: toString(p.DisplayName),
  95. Description: toString(p2.Description),
  96. }, nil
  97. }
  98. func updateDescription(handle windows.Handle, desc string) error {
  99. d := windows.SERVICE_DESCRIPTION{Description: toPtr(desc)}
  100. return windows.ChangeServiceConfig2(handle,
  101. windows.SERVICE_CONFIG_DESCRIPTION, (*byte)(unsafe.Pointer(&d)))
  102. }
  103. func updateSidType(handle windows.Handle, sidType uint32) error {
  104. return windows.ChangeServiceConfig2(handle, windows.SERVICE_CONFIG_SERVICE_SID_INFO, (*byte)(unsafe.Pointer(&sidType)))
  105. }
  106. // UpdateConfig updates service s configuration parameters.
  107. func (s *Service) UpdateConfig(c Config) error {
  108. err := windows.ChangeServiceConfig(s.Handle, c.ServiceType, c.StartType,
  109. c.ErrorControl, toPtr(c.BinaryPathName), toPtr(c.LoadOrderGroup),
  110. nil, toStringBlock(c.Dependencies), toPtr(c.ServiceStartName),
  111. toPtr(c.Password), toPtr(c.DisplayName))
  112. if err != nil {
  113. return err
  114. }
  115. err = updateSidType(s.Handle, c.SidType)
  116. if err != nil {
  117. return err
  118. }
  119. return updateDescription(s.Handle, c.Description)
  120. }
  121. // queryServiceConfig2 calls Windows QueryServiceConfig2 with infoLevel parameter and returns retrieved service configuration information.
  122. func (s *Service) queryServiceConfig2(infoLevel uint32) ([]byte, error) {
  123. n := uint32(1024)
  124. for {
  125. b := make([]byte, n)
  126. err := windows.QueryServiceConfig2(s.Handle, infoLevel, &b[0], n, &n)
  127. if err == nil {
  128. return b, nil
  129. }
  130. if err.(syscall.Errno) != syscall.ERROR_INSUFFICIENT_BUFFER {
  131. return nil, err
  132. }
  133. if n <= uint32(len(b)) {
  134. return nil, err
  135. }
  136. }
  137. }