initsystem_windows.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // +build windows
  2. /*
  3. Copyright 2017 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package initsystem
  15. import (
  16. "fmt"
  17. "time"
  18. "golang.org/x/sys/windows/svc"
  19. "golang.org/x/sys/windows/svc/mgr"
  20. )
  21. // WindowsInitSystem is the windows implementation of InitSystem
  22. type WindowsInitSystem struct{}
  23. func (sysd WindowsInitSystem) EnableCommand(service string) string {
  24. return fmt.Sprintf("Set-Service '%s' -StartupType Automatic", service)
  25. }
  26. // Following Windows documentation: https://docs.microsoft.com/en-us/windows/desktop/Services/starting-a-service
  27. func (sysd WindowsInitSystem) ServiceStart(service string) error {
  28. m, err := mgr.Connect()
  29. if err != nil {
  30. return err
  31. }
  32. defer m.Disconnect()
  33. s, err := m.OpenService(service)
  34. if err != nil {
  35. return fmt.Errorf("could not access service %s: %v", service, err)
  36. }
  37. defer s.Close()
  38. // Check if service is already started
  39. status, err := s.Query()
  40. if err != nil {
  41. return fmt.Errorf("could not query service %s: %v", service, err)
  42. }
  43. if status.State != svc.Stopped && status.State != svc.StopPending {
  44. return nil
  45. }
  46. timeout := time.Now().Add(10 * time.Second)
  47. for status.State != svc.Stopped {
  48. if timeout.Before(time.Now()) {
  49. return fmt.Errorf("timeout waiting for %s service to stop", service)
  50. }
  51. time.Sleep(300 * time.Millisecond)
  52. status, err = s.Query()
  53. if err != nil {
  54. return fmt.Errorf("could not retrieve %s service status: %v", service, err)
  55. }
  56. }
  57. // Start the service
  58. err = s.Start("is", "manual-started")
  59. if err != nil {
  60. return fmt.Errorf("could not start service %s: %v", service, err)
  61. }
  62. // Check that the start was successful
  63. status, err = s.Query()
  64. if err != nil {
  65. return fmt.Errorf("could not query service %s: %v", service, err)
  66. }
  67. timeout = time.Now().Add(10 * time.Second)
  68. for status.State != svc.Running {
  69. if timeout.Before(time.Now()) {
  70. return fmt.Errorf("timeout waiting for %s service to start", service)
  71. }
  72. time.Sleep(300 * time.Millisecond)
  73. status, err = s.Query()
  74. if err != nil {
  75. return fmt.Errorf("could not retrieve %s service status: %v", service, err)
  76. }
  77. }
  78. return nil
  79. }
  80. func (sysd WindowsInitSystem) ServiceRestart(service string) error {
  81. if err := sysd.ServiceStop(service); err != nil {
  82. return fmt.Errorf("couldn't stop service %s: %v", service, err)
  83. }
  84. if err := sysd.ServiceStart(service); err != nil {
  85. return fmt.Errorf("couldn't start service %s: %v", service, err)
  86. }
  87. return nil
  88. }
  89. // Following Windows documentation: https://docs.microsoft.com/en-us/windows/desktop/Services/stopping-a-service
  90. func (sysd WindowsInitSystem) ServiceStop(service string) error {
  91. m, err := mgr.Connect()
  92. if err != nil {
  93. return err
  94. }
  95. defer m.Disconnect()
  96. s, err := m.OpenService(service)
  97. if err != nil {
  98. return fmt.Errorf("could not access service %s: %v", service, err)
  99. }
  100. defer s.Close()
  101. // Check if service is already stopped
  102. status, err := s.Query()
  103. if err != nil {
  104. return fmt.Errorf("could not query service %s: %v", service, err)
  105. }
  106. if status.State == svc.Stopped {
  107. return nil
  108. }
  109. // If StopPending, check that service eventually stops
  110. if status.State == svc.StopPending {
  111. timeout := time.Now().Add(10 * time.Second)
  112. for status.State != svc.Stopped {
  113. if timeout.Before(time.Now()) {
  114. return fmt.Errorf("timeout waiting for %s service to stop", service)
  115. }
  116. time.Sleep(300 * time.Millisecond)
  117. status, err = s.Query()
  118. if err != nil {
  119. return fmt.Errorf("could not retrieve %s service status: %v", service, err)
  120. }
  121. }
  122. return nil
  123. }
  124. // Stop the service
  125. status, err = s.Control(svc.Stop)
  126. if err != nil {
  127. return fmt.Errorf("could not stop service %s: %v", service, err)
  128. }
  129. // Check that the stop was successful
  130. status, err = s.Query()
  131. if err != nil {
  132. return fmt.Errorf("could not query service %s: %v", service, err)
  133. }
  134. timeout := time.Now().Add(10 * time.Second)
  135. for status.State != svc.Stopped {
  136. if timeout.Before(time.Now()) {
  137. return fmt.Errorf("timeout waiting for %s service to stop", service)
  138. }
  139. time.Sleep(300 * time.Millisecond)
  140. status, err = s.Query()
  141. if err != nil {
  142. return fmt.Errorf("could not retrieve %s service status: %v", service, err)
  143. }
  144. }
  145. return nil
  146. }
  147. func (sysd WindowsInitSystem) ServiceExists(service string) bool {
  148. m, err := mgr.Connect()
  149. if err != nil {
  150. return false
  151. }
  152. defer m.Disconnect()
  153. s, err := m.OpenService(service)
  154. if err != nil {
  155. return false
  156. }
  157. defer s.Close()
  158. return true
  159. }
  160. func (sysd WindowsInitSystem) ServiceIsEnabled(service string) bool {
  161. m, err := mgr.Connect()
  162. if err != nil {
  163. return false
  164. }
  165. defer m.Disconnect()
  166. s, err := m.OpenService(service)
  167. if err != nil {
  168. return false
  169. }
  170. defer s.Close()
  171. c, err := s.Config()
  172. if err != nil {
  173. return false
  174. }
  175. return c.StartType != mgr.StartDisabled
  176. }
  177. func (sysd WindowsInitSystem) ServiceIsActive(service string) bool {
  178. m, err := mgr.Connect()
  179. if err != nil {
  180. return false
  181. }
  182. defer m.Disconnect()
  183. s, err := m.OpenService(service)
  184. if err != nil {
  185. return false
  186. }
  187. defer s.Close()
  188. status, err := s.Query()
  189. if err != nil {
  190. return false
  191. }
  192. return status.State == svc.Running
  193. }
  194. // GetInitSystem returns an InitSystem for the current system, or nil
  195. // if we cannot detect a supported init system.
  196. // This indicates we will skip init system checks, not an error.
  197. func GetInitSystem() (InitSystem, error) {
  198. m, err := mgr.Connect()
  199. if err != nil {
  200. return nil, fmt.Errorf("no supported init system detected: %v", err)
  201. }
  202. defer m.Disconnect()
  203. return &WindowsInitSystem{}, nil
  204. }