golang.org/x/sys@v0.20.1-0.20240517151509-673e0f94c16d/windows/svc/mgr/recovery.go (about) 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 5 //go:build windows 6 7 package mgr 8 9 import ( 10 "errors" 11 "syscall" 12 "time" 13 "unsafe" 14 15 "golang.org/x/sys/windows" 16 ) 17 18 const ( 19 // Possible recovery actions that the service control manager can perform. 20 NoAction = windows.SC_ACTION_NONE // no action 21 ComputerReboot = windows.SC_ACTION_REBOOT // reboot the computer 22 ServiceRestart = windows.SC_ACTION_RESTART // restart the service 23 RunCommand = windows.SC_ACTION_RUN_COMMAND // run a command 24 ) 25 26 // RecoveryAction represents an action that the service control manager can perform when service fails. 27 // A service is considered failed when it terminates without reporting a status of SERVICE_STOPPED to the service controller. 28 type RecoveryAction struct { 29 Type int // one of NoAction, ComputerReboot, ServiceRestart or RunCommand 30 Delay time.Duration // the time to wait before performing the specified action 31 } 32 33 // SetRecoveryActions sets actions that service controller performs when service fails and 34 // the time after which to reset the service failure count to zero if there are no failures, in seconds. 35 // Specify INFINITE to indicate that service failure count should never be reset. 36 func (s *Service) SetRecoveryActions(recoveryActions []RecoveryAction, resetPeriod uint32) error { 37 if recoveryActions == nil { 38 return errors.New("recoveryActions cannot be nil") 39 } 40 actions := []windows.SC_ACTION{} 41 for _, a := range recoveryActions { 42 action := windows.SC_ACTION{ 43 Type: uint32(a.Type), 44 Delay: uint32(a.Delay.Nanoseconds() / 1000000), 45 } 46 actions = append(actions, action) 47 } 48 rActions := windows.SERVICE_FAILURE_ACTIONS{ 49 ActionsCount: uint32(len(actions)), 50 Actions: &actions[0], 51 ResetPeriod: resetPeriod, 52 } 53 return windows.ChangeServiceConfig2(s.Handle, windows.SERVICE_CONFIG_FAILURE_ACTIONS, (*byte)(unsafe.Pointer(&rActions))) 54 } 55 56 // RecoveryActions returns actions that service controller performs when service fails. 57 // The service control manager counts the number of times service s has failed since the system booted. 58 // The count is reset to 0 if the service has not failed for ResetPeriod seconds. 59 // When the service fails for the Nth time, the service controller performs the action specified in element [N-1] of returned slice. 60 // If N is greater than slice length, the service controller repeats the last action in the slice. 61 func (s *Service) RecoveryActions() ([]RecoveryAction, error) { 62 b, err := s.queryServiceConfig2(windows.SERVICE_CONFIG_FAILURE_ACTIONS) 63 if err != nil { 64 return nil, err 65 } 66 p := (*windows.SERVICE_FAILURE_ACTIONS)(unsafe.Pointer(&b[0])) 67 if p.Actions == nil { 68 return nil, err 69 } 70 71 actions := unsafe.Slice(p.Actions, int(p.ActionsCount)) 72 var recoveryActions []RecoveryAction 73 for _, action := range actions { 74 recoveryActions = append(recoveryActions, RecoveryAction{Type: int(action.Type), Delay: time.Duration(action.Delay) * time.Millisecond}) 75 } 76 return recoveryActions, nil 77 } 78 79 // ResetRecoveryActions deletes both reset period and array of failure actions. 80 func (s *Service) ResetRecoveryActions() error { 81 actions := make([]windows.SC_ACTION, 1) 82 rActions := windows.SERVICE_FAILURE_ACTIONS{ 83 Actions: &actions[0], 84 } 85 return windows.ChangeServiceConfig2(s.Handle, windows.SERVICE_CONFIG_FAILURE_ACTIONS, (*byte)(unsafe.Pointer(&rActions))) 86 } 87 88 // ResetPeriod is the time after which to reset the service failure 89 // count to zero if there are no failures, in seconds. 90 func (s *Service) ResetPeriod() (uint32, error) { 91 b, err := s.queryServiceConfig2(windows.SERVICE_CONFIG_FAILURE_ACTIONS) 92 if err != nil { 93 return 0, err 94 } 95 p := (*windows.SERVICE_FAILURE_ACTIONS)(unsafe.Pointer(&b[0])) 96 return p.ResetPeriod, nil 97 } 98 99 // SetRebootMessage sets service s reboot message. 100 // If msg is "", the reboot message is deleted and no message is broadcast. 101 func (s *Service) SetRebootMessage(msg string) error { 102 rActions := windows.SERVICE_FAILURE_ACTIONS{ 103 RebootMsg: syscall.StringToUTF16Ptr(msg), 104 } 105 return windows.ChangeServiceConfig2(s.Handle, windows.SERVICE_CONFIG_FAILURE_ACTIONS, (*byte)(unsafe.Pointer(&rActions))) 106 } 107 108 // RebootMessage is broadcast to server users before rebooting in response to the ComputerReboot service controller action. 109 func (s *Service) RebootMessage() (string, error) { 110 b, err := s.queryServiceConfig2(windows.SERVICE_CONFIG_FAILURE_ACTIONS) 111 if err != nil { 112 return "", err 113 } 114 p := (*windows.SERVICE_FAILURE_ACTIONS)(unsafe.Pointer(&b[0])) 115 return windows.UTF16PtrToString(p.RebootMsg), nil 116 } 117 118 // SetRecoveryCommand sets the command line of the process to execute in response to the RunCommand service controller action. 119 // If cmd is "", the command is deleted and no program is run when the service fails. 120 func (s *Service) SetRecoveryCommand(cmd string) error { 121 rActions := windows.SERVICE_FAILURE_ACTIONS{ 122 Command: syscall.StringToUTF16Ptr(cmd), 123 } 124 return windows.ChangeServiceConfig2(s.Handle, windows.SERVICE_CONFIG_FAILURE_ACTIONS, (*byte)(unsafe.Pointer(&rActions))) 125 } 126 127 // 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. 128 func (s *Service) RecoveryCommand() (string, error) { 129 b, err := s.queryServiceConfig2(windows.SERVICE_CONFIG_FAILURE_ACTIONS) 130 if err != nil { 131 return "", err 132 } 133 p := (*windows.SERVICE_FAILURE_ACTIONS)(unsafe.Pointer(&b[0])) 134 return windows.UTF16PtrToString(p.Command), nil 135 } 136 137 // SetRecoveryActionsOnNonCrashFailures sets the failure actions flag. If the 138 // flag is set to false, recovery actions will only be performed if the service 139 // terminates without reporting a status of SERVICE_STOPPED. If the flag is set 140 // to true, recovery actions are also perfomed if the service stops with a 141 // nonzero exit code. 142 func (s *Service) SetRecoveryActionsOnNonCrashFailures(flag bool) error { 143 var setting windows.SERVICE_FAILURE_ACTIONS_FLAG 144 if flag { 145 setting.FailureActionsOnNonCrashFailures = 1 146 } 147 return windows.ChangeServiceConfig2(s.Handle, windows.SERVICE_CONFIG_FAILURE_ACTIONS_FLAG, (*byte)(unsafe.Pointer(&setting))) 148 } 149 150 // RecoveryActionsOnNonCrashFailures returns the current value of the failure 151 // actions flag. If the flag is set to false, recovery actions will only be 152 // performed if the service terminates without reporting a status of 153 // SERVICE_STOPPED. If the flag is set to true, recovery actions are also 154 // perfomed if the service stops with a nonzero exit code. 155 func (s *Service) RecoveryActionsOnNonCrashFailures() (bool, error) { 156 b, err := s.queryServiceConfig2(windows.SERVICE_CONFIG_FAILURE_ACTIONS_FLAG) 157 if err != nil { 158 return false, err 159 } 160 p := (*windows.SERVICE_FAILURE_ACTIONS_FLAG)(unsafe.Pointer(&b[0])) 161 return p.FailureActionsOnNonCrashFailures != 0, nil 162 }