recovery.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright 2018 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. "errors"
  8. "syscall"
  9. "time"
  10. "unsafe"
  11. "golang.org/x/sys/windows"
  12. )
  13. const (
  14. // Possible recovery actions that the service control manager can perform.
  15. NoAction = windows.SC_ACTION_NONE // no action
  16. ComputerReboot = windows.SC_ACTION_REBOOT // reboot the computer
  17. ServiceRestart = windows.SC_ACTION_RESTART // restart the service
  18. RunCommand = windows.SC_ACTION_RUN_COMMAND // run a command
  19. )
  20. // RecoveryAction represents an action that the service control manager can perform when service fails.
  21. // A service is considered failed when it terminates without reporting a status of SERVICE_STOPPED to the service controller.
  22. type RecoveryAction struct {
  23. Type int // one of NoAction, ComputerReboot, ServiceRestart or RunCommand
  24. Delay time.Duration // the time to wait before performing the specified action
  25. }
  26. // SetRecoveryActions sets actions that service controller performs when service fails and
  27. // the time after which to reset the service failure count to zero if there are no failures, in seconds.
  28. // Specify INFINITE to indicate that service failure count should never be reset.
  29. func (s *Service) SetRecoveryActions(recoveryActions []RecoveryAction, resetPeriod uint32) error {
  30. if recoveryActions == nil {
  31. return errors.New("recoveryActions cannot be nil")
  32. }
  33. actions := []windows.SC_ACTION{}
  34. for _, a := range recoveryActions {
  35. action := windows.SC_ACTION{
  36. Type: uint32(a.Type),
  37. Delay: uint32(a.Delay.Nanoseconds() / 1000000),
  38. }
  39. actions = append(actions, action)
  40. }
  41. rActions := windows.SERVICE_FAILURE_ACTIONS{
  42. ActionsCount: uint32(len(actions)),
  43. Actions: &actions[0],
  44. ResetPeriod: resetPeriod,
  45. }
  46. return windows.ChangeServiceConfig2(s.Handle, windows.SERVICE_CONFIG_FAILURE_ACTIONS, (*byte)(unsafe.Pointer(&rActions)))
  47. }
  48. // RecoveryActions returns actions that service controller performs when service fails.
  49. // The service control manager counts the number of times service s has failed since the system booted.
  50. // The count is reset to 0 if the service has not failed for ResetPeriod seconds.
  51. // When the service fails for the Nth time, the service controller performs the action specified in element [N-1] of returned slice.
  52. // If N is greater than slice length, the service controller repeats the last action in the slice.
  53. func (s *Service) RecoveryActions() ([]RecoveryAction, error) {
  54. b, err := s.queryServiceConfig2(windows.SERVICE_CONFIG_FAILURE_ACTIONS)
  55. if err != nil {
  56. return nil, err
  57. }
  58. p := (*windows.SERVICE_FAILURE_ACTIONS)(unsafe.Pointer(&b[0]))
  59. if p.Actions == nil {
  60. return nil, err
  61. }
  62. var recoveryActions []RecoveryAction
  63. actions := (*[1024]windows.SC_ACTION)(unsafe.Pointer(p.Actions))[:p.ActionsCount]
  64. for _, action := range actions {
  65. recoveryActions = append(recoveryActions, RecoveryAction{Type: int(action.Type), Delay: time.Duration(action.Delay) * time.Millisecond})
  66. }
  67. return recoveryActions, nil
  68. }
  69. // ResetRecoveryActions deletes both reset period and array of failure actions.
  70. func (s *Service) ResetRecoveryActions() error {
  71. actions := make([]windows.SC_ACTION, 1)
  72. rActions := windows.SERVICE_FAILURE_ACTIONS{
  73. Actions: &actions[0],
  74. }
  75. return windows.ChangeServiceConfig2(s.Handle, windows.SERVICE_CONFIG_FAILURE_ACTIONS, (*byte)(unsafe.Pointer(&rActions)))
  76. }
  77. // ResetPeriod is the time after which to reset the service failure
  78. // count to zero if there are no failures, in seconds.
  79. func (s *Service) ResetPeriod() (uint32, error) {
  80. b, err := s.queryServiceConfig2(windows.SERVICE_CONFIG_FAILURE_ACTIONS)
  81. if err != nil {
  82. return 0, err
  83. }
  84. p := (*windows.SERVICE_FAILURE_ACTIONS)(unsafe.Pointer(&b[0]))
  85. return p.ResetPeriod, nil
  86. }
  87. // SetRebootMessage sets service s reboot message.
  88. // If msg is "", the reboot message is deleted and no message is broadcast.
  89. func (s *Service) SetRebootMessage(msg string) error {
  90. rActions := windows.SERVICE_FAILURE_ACTIONS{
  91. RebootMsg: syscall.StringToUTF16Ptr(msg),
  92. }
  93. return windows.ChangeServiceConfig2(s.Handle, windows.SERVICE_CONFIG_FAILURE_ACTIONS, (*byte)(unsafe.Pointer(&rActions)))
  94. }
  95. // RebootMessage is broadcast to server users before rebooting in response to the ComputerReboot service controller action.
  96. func (s *Service) RebootMessage() (string, error) {
  97. b, err := s.queryServiceConfig2(windows.SERVICE_CONFIG_FAILURE_ACTIONS)
  98. if err != nil {
  99. return "", err
  100. }
  101. p := (*windows.SERVICE_FAILURE_ACTIONS)(unsafe.Pointer(&b[0]))
  102. return toString(p.RebootMsg), nil
  103. }
  104. // SetRecoveryCommand sets the command line of the process to execute in response to the RunCommand service controller action.
  105. // If cmd is "", the command is deleted and no program is run when the service fails.
  106. func (s *Service) SetRecoveryCommand(cmd string) error {
  107. rActions := windows.SERVICE_FAILURE_ACTIONS{
  108. Command: syscall.StringToUTF16Ptr(cmd),
  109. }
  110. return windows.ChangeServiceConfig2(s.Handle, windows.SERVICE_CONFIG_FAILURE_ACTIONS, (*byte)(unsafe.Pointer(&rActions)))
  111. }
  112. // RecoveryCommand is the command line of the process to execute in response to the RunCommand service controller action. This process runs under the same account as the service.
  113. func (s *Service) RecoveryCommand() (string, error) {
  114. b, err := s.queryServiceConfig2(windows.SERVICE_CONFIG_FAILURE_ACTIONS)
  115. if err != nil {
  116. return "", err
  117. }
  118. p := (*windows.SERVICE_FAILURE_ACTIONS)(unsafe.Pointer(&b[0]))
  119. return toString(p.Command), nil
  120. }