Debian Codegraph @

notify/notify_windows.go

raw symbols SCIP
1 // Copyright 2015 Matthew Holt and The Caddy Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package notify
16
17 import (
18 "log"
19 "strings"
20
21 "golang.org/x/sys/windows/svc"
22 )
23
24 // globalStatus store windows service status, it can be
25 // use to notify caddy status.
26 var globalStatus chan<- svc.Status
27
28 // SetGlobalStatus assigns the channel through which status updates
29 // will be sent to the SCM. This is typically provided by the service
30 // handler when the service starts.
31 func SetGlobalStatus(status chan<- svc.Status) {
32 globalStatus = status
33 }
34
35 // Ready notifies the SCM that the service is fully running and ready
36 // to accept stop or shutdown control requests.
37 func Ready() error {
38 if globalStatus != nil {
39 globalStatus <- svc.Status{
40 State: svc.Running,
41 Accepts: svc.AcceptStop | svc.AcceptShutdown,
42 }
43 }
44 return nil
45 }
46
47 // Reloading notifies the SCM that the service is entering a transitional
48 // state.
49 func Reloading() error {
50 if globalStatus != nil {
51 globalStatus <- svc.Status{State: svc.StartPending}
52 }
53 return nil
54 }
55
56 // Stopping notifies the SCM that the service is in the process of stopping.
57 // This allows Windows to track the shutdown transition properly.
58 func Stopping() error {
59 if globalStatus != nil {
60 globalStatus <- svc.Status{State: svc.StopPending}
61 }
62 return nil
63 }
64
65 // Status sends an arbitrary service state to the SCM based on a string
66 // identifier of [svc.State].
67 // The unknown states will be logged.
68 func Status(name string) error {
69 if globalStatus == nil {
70 return nil
71 }
72
73 var state svc.State
74 var accepts svc.Accepted
75 accepts = 0
76
77 switch strings.ToLower(name) {
78 case "stopped":
79 state = svc.Stopped
80 case "start_pending":
81 state = svc.StartPending
82 case "stop_pending":
83 state = svc.StopPending
84 case "running":
85 state = svc.Running
86 accepts = svc.AcceptStop | svc.AcceptShutdown
87 case "continue_pending":
88 state = svc.ContinuePending
89 case "pause_pending":
90 state = svc.PausePending
91 case "paused":
92 state = svc.Paused
93 accepts = svc.AcceptStop | svc.AcceptShutdown | svc.AcceptPauseAndContinue
94 default:
95 log.Printf("unknown state: %s", name)
96 return nil
97 }
98
99 globalStatus <- svc.Status{State: state, Accepts: accepts}
100 return nil
101 }
102
103 // Error notifies the SCM that the service is stopping due to a failure,
104 // including a service-specific exit code.
105 func Error(err error, code int) error {
106 if globalStatus != nil {
107 globalStatus <- svc.Status{
108 State: svc.StopPending,
109 ServiceSpecificExitCode: uint32(code),
110 }
111 }
112
113 return nil
114 }