123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- package minwinsvc
- import (
- "os"
- "sync"
- "golang.org/x/sys/windows/svc"
- )
- var (
- onExit func()
- guard sync.Mutex
- )
- func init() {
- interactive, err := svc.IsAnInteractiveSession()
- if err != nil {
- panic(err)
- }
-
-
- if interactive || len(os.Getenv("SSH_ORIGINAL_COMMAND")) > 0 {
- return
- }
- go func() {
- _ = svc.Run("", runner{})
- guard.Lock()
- f := onExit
- guard.Unlock()
-
- if f != nil {
- f()
- }
-
- os.Exit(0)
- }()
- }
- func setOnExit(f func()) {
- guard.Lock()
- onExit = f
- guard.Unlock()
- }
- type runner struct{}
- func (runner) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (bool, uint32) {
- const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown
- changes <- svc.Status{State: svc.StartPending}
- changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
- for {
- c := <-r
- switch c.Cmd {
- case svc.Interrogate:
- changes <- c.CurrentStatus
- case svc.Stop, svc.Shutdown:
- changes <- svc.Status{State: svc.StopPending}
- return false, 0
- }
- }
- return false, 0
- }
|