Debian Codegraph @

svc_windows.go

raw symbols SCIP
1 // Copyright 2015 Daniel Theophanes.
2 // Use of this source code is governed by a zlib-style
3 // license that can be found in the LICENSE file.package service
4
5 //+build windows
6
7 package minwinsvc
8
9 import (
10 "os"
11 "sync"
12
13 "golang.org/x/sys/windows/svc"
14 )
15
16 var (
17 onExit func()
18 guard sync.Mutex
19 )
20
21 func init() {
22 interactive, err := svc.IsAnInteractiveSession()
23 if err != nil {
24 panic(err)
25 }
26 if interactive {
27 return
28 }
29 go func() {
30 _ = svc.Run("", runner{})
31
32 guard.Lock()
33 f := onExit
34 guard.Unlock()
35
36 // Don't hold this lock in user code.
37 if f != nil {
38 f()
39 }
40 // Make sure we exit.
41 os.Exit(0)
42 }()
43 }
44
45 func setOnExit(f func()) {
46 guard.Lock()
47 onExit = f
48 guard.Unlock()
49 }
50
51 type runner struct{}
52
53 func (runner) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (bool, uint32) {
54 const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown
55 changes <- svc.Status{State: svc.StartPending}
56
57 changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
58 for {
59 c := <-r
60 switch c.Cmd {
61 case svc.Interrogate:
62 changes <- c.CurrentStatus
63 case svc.Stop, svc.Shutdown:
64 changes <- svc.Status{State: svc.StopPending}
65 return false, 0
66 }
67 }
68
69 return false, 0
70 }