github.com/google/fleetspeak@v0.1.15-0.20240426164851-4f31f62c1aea/fleetspeak/src/client/entry/entry_windows.go (about) 1 //go:build windows 2 3 package entry 4 5 import ( 6 "os" 7 "time" 8 9 log "github.com/golang/glog" 10 "golang.org/x/sys/windows/svc" 11 ) 12 13 type fleetspeakService struct { 14 innerMain func() 15 } 16 17 func (m *fleetspeakService) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (svcSpecificEC bool, errno uint32) { 18 const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown 19 changes <- svc.Status{State: svc.StartPending} 20 changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted} 21 timer := time.Tick(2 * time.Second) 22 go m.innerMain() 23 loop: 24 for { 25 select { 26 case <-timer: 27 case c := <-r: 28 switch c.Cmd { 29 case svc.Interrogate: 30 changes <- c.CurrentStatus 31 case svc.Stop, svc.Shutdown: 32 break loop 33 case svc.Pause: 34 changes <- svc.Status{State: svc.Paused, Accepts: cmdsAccepted} 35 case svc.Continue: 36 changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted} 37 default: 38 } 39 } 40 } 41 changes <- svc.Status{State: svc.StopPending} 42 43 log.Info("Stopping the service.") 44 os.Exit(2) 45 return 46 } 47 48 // RunMain starts the application. 49 func RunMain(innerMain func(), windowsServiceName string) { 50 isIntSess, err := svc.IsAnInteractiveSession() 51 if err != nil { 52 log.Fatalf("failed to determine if we are running in an interactive session: %v", err) 53 } 54 if isIntSess { 55 innerMain() 56 } else { 57 svc.Run(windowsServiceName, &fleetspeakService{innerMain}) 58 } 59 }