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