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