Debian Codegraph @

service_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 caddy
16
17 import (
18 "os"
19 "path/filepath"
20
21 "golang.org/x/sys/windows/svc"
22
23 "github.com/caddyserver/caddy/v2/notify"
24 )
25
26 func init() {
27 isService, err := svc.IsWindowsService()
28 if err != nil || !isService {
29 return
30 }
31
32 // Windows services always start in the system32 directory, try to
33 // switch into the directory where the caddy executable is.
34 execPath, err := os.Executable()
35 if err == nil {
36 _ = os.Chdir(filepath.Dir(execPath))
37 }
38
39 go func() {
40 _ = svc.Run("", runner{})
41 }()
42 }
43
44 type runner struct{}
45
46 func (runner) Execute(args []string, request <-chan svc.ChangeRequest, status chan<- svc.Status) (bool, uint32) {
47 notify.SetGlobalStatus(status)
48 status <- svc.Status{State: svc.StartPending}
49
50 for {
51 req := <-request
52 switch req.Cmd {
53 case svc.Interrogate:
54 status <- req.CurrentStatus
55 case svc.Stop, svc.Shutdown:
56 status <- svc.Status{State: svc.StopPending}
57 exitProcessFromSignal("SIGINT")
58 return false, 0
59 }
60 }
61 }