You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

146 lines
4.1 KiB

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