golang.org/x/sys@v0.20.1-0.20240517151509-673e0f94c16d/windows/svc/debug/service.go (about) 1 // Copyright 2012 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 //go:build windows 6 7 // Package debug provides facilities to execute svc.Handler on console. 8 package debug 9 10 import ( 11 "os" 12 "os/signal" 13 "syscall" 14 15 "golang.org/x/sys/windows/svc" 16 ) 17 18 // Run executes service name by calling appropriate handler function. 19 // The process is running on console, unlike real service. Use Ctrl+C to 20 // send "Stop" command to your service. 21 func Run(name string, handler svc.Handler) error { 22 cmds := make(chan svc.ChangeRequest) 23 changes := make(chan svc.Status) 24 25 sig := make(chan os.Signal, 1) 26 signal.Notify(sig) 27 28 go func() { 29 status := svc.Status{State: svc.Stopped} 30 for { 31 select { 32 case <-sig: 33 cmds <- svc.ChangeRequest{Cmd: svc.Stop, CurrentStatus: status} 34 case status = <-changes: 35 } 36 } 37 }() 38 39 _, errno := handler.Execute([]string{name}, cmds, changes) 40 if errno != 0 { 41 return syscall.Errno(errno) 42 } 43 return nil 44 }