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.
15 "golang.org/x/sys/windows"
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
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
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")
40 actions := []windows.SC_ACTION{}
41 for _, a := range recoveryActions {
42 action := windows.SC_ACTION{
44 Delay: uint32(a.Delay.Nanoseconds() / 1000000),
46 actions = append(actions, action)
48 rActions := windows.SERVICE_FAILURE_ACTIONS{
49 ActionsCount: uint32(len(actions)),
51 ResetPeriod: resetPeriod,
53 return windows.ChangeServiceConfig2(s.Handle, windows.SERVICE_CONFIG_FAILURE_ACTIONS, (*byte)(unsafe.Pointer(&rActions)))
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)
66 p := (*windows.SERVICE_FAILURE_ACTIONS)(unsafe.Pointer(&b[0]))
71 var recoveryActions []RecoveryAction
72 actions := (*[1024]windows.SC_ACTION)(unsafe.Pointer(p.Actions))[:p.ActionsCount]
73 for _, action := range actions {
74 recoveryActions = append(recoveryActions, RecoveryAction{Type: int(action.Type), Delay: time.Duration(action.Delay) * time.Millisecond})
76 return recoveryActions, nil
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{
85 return windows.ChangeServiceConfig2(s.Handle, windows.SERVICE_CONFIG_FAILURE_ACTIONS, (*byte)(unsafe.Pointer(&rActions)))
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)
95 p := (*windows.SERVICE_FAILURE_ACTIONS)(unsafe.Pointer(&b[0]))
96 return p.ResetPeriod, nil
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),
105 return windows.ChangeServiceConfig2(s.Handle, windows.SERVICE_CONFIG_FAILURE_ACTIONS, (*byte)(unsafe.Pointer(&rActions)))
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)
114 p := (*windows.SERVICE_FAILURE_ACTIONS)(unsafe.Pointer(&b[0]))
115 return toString(p.RebootMsg), nil
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),
124 return windows.ChangeServiceConfig2(s.Handle, windows.SERVICE_CONFIG_FAILURE_ACTIONS, (*byte)(unsafe.Pointer(&rActions)))
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)
133 p := (*windows.SERVICE_FAILURE_ACTIONS)(unsafe.Pointer(&b[0]))
134 return toString(p.Command), nil