Debian Codegraph @

windows/svc/mgr/recovery.go

raw
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 msgPointer, err := syscall.UTF16PtrFromString(msg)
103 if err != nil {
104 return err
105 }
106
107 rActions := windows.SERVICE_FAILURE_ACTIONS{
108 RebootMsg: msgPointer,
109 }
110 return windows.ChangeServiceConfig2(s.Handle, windows.SERVICE_CONFIG_FAILURE_ACTIONS, (*byte)(unsafe.Pointer(&rActions)))
111 }
112
113 // RebootMessage is broadcast to server users before rebooting in response to the ComputerReboot service controller action.
114 func (s *Service) RebootMessage() (string, error) {
115 b, err := s.queryServiceConfig2(windows.SERVICE_CONFIG_FAILURE_ACTIONS)
116 if err != nil {
117 return "", err
118 }
119 p := (*windows.SERVICE_FAILURE_ACTIONS)(unsafe.Pointer(&b[0]))
120 return windows.UTF16PtrToString(p.RebootMsg), nil
121 }
122
123 // SetRecoveryCommand sets the command line of the process to execute in response to the RunCommand service controller action.
124 // If cmd is "", the command is deleted and no program is run when the service fails.
125 func (s *Service) SetRecoveryCommand(cmd string) error {
126 cmdPointer, err := syscall.UTF16PtrFromString(cmd)
127 if err != nil {
128 return err
129 }
130
131 rActions := windows.SERVICE_FAILURE_ACTIONS{
132 Command: cmdPointer,
133 }
134 return windows.ChangeServiceConfig2(s.Handle, windows.SERVICE_CONFIG_FAILURE_ACTIONS, (*byte)(unsafe.Pointer(&rActions)))
135 }
136
137 // 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.
138 func (s *Service) RecoveryCommand() (string, error) {
139 b, err := s.queryServiceConfig2(windows.SERVICE_CONFIG_FAILURE_ACTIONS)
140 if err != nil {
141 return "", err
142 }
143 p := (*windows.SERVICE_FAILURE_ACTIONS)(unsafe.Pointer(&b[0]))
144 return windows.UTF16PtrToString(p.Command), nil
145 }
146
147 // SetRecoveryActionsOnNonCrashFailures sets the failure actions flag. If the
148 // flag is set to false, recovery actions will only be performed if the service
149 // terminates without reporting a status of SERVICE_STOPPED. If the flag is set
150 // to true, recovery actions are also performed if the service stops with a
151 // nonzero exit code.
152 func (s *Service) SetRecoveryActionsOnNonCrashFailures(flag bool) error {
153 var setting windows.SERVICE_FAILURE_ACTIONS_FLAG
154 if flag {
155 setting.FailureActionsOnNonCrashFailures = 1
156 }
157 return windows.ChangeServiceConfig2(s.Handle, windows.SERVICE_CONFIG_FAILURE_ACTIONS_FLAG, (*byte)(unsafe.Pointer(&setting)))
158 }
159
160 // RecoveryActionsOnNonCrashFailures returns the current value of the failure
161 // actions flag. If the flag is set to false, recovery actions will only be
162 // performed if the service terminates without reporting a status of
163 // SERVICE_STOPPED. If the flag is set to true, recovery actions are also
164 // performed if the service stops with a nonzero exit code.
165 func (s *Service) RecoveryActionsOnNonCrashFailures() (bool, error) {
166 b, err := s.queryServiceConfig2(windows.SERVICE_CONFIG_FAILURE_ACTIONS_FLAG)
167 if err != nil {
168 return false, err
169 }
170 p := (*windows.SERVICE_FAILURE_ACTIONS_FLAG)(unsafe.Pointer(&b[0]))
171 return p.FailureActionsOnNonCrashFailures != 0, nil
172 }