Add windows service install/uninstall, improve service handling

This commit is contained in:
Daniel
2019-07-19 21:27:15 +02:00
parent 51c96959aa
commit 9cc5c789ec
5 changed files with 268 additions and 30 deletions

View File

@@ -1,11 +1,17 @@
package main
// Based on the offical Go examples from
// https://github.com/golang/sys/blob/master/windows/svc/example
// by The Go Authors.
// Original LICENSE (sha256sum: 2d36597f7117c38b006835ae7f537487207d8ec407aa9d9980794b2030cbc067) can be found in vendor/pkg cache directory.
import (
"fmt"
"time"
"github.com/spf13/cobra"
"golang.org/x/sys/windows/svc"
"golang.org/x/sys/windows/svc/debug"
"golang.org/x/sys/windows/svc/eventlog"
)
@@ -29,6 +35,9 @@ var (
// helpers for execution
runError chan error
runWrapper func() error
// eventlog
eventlogger *eventlog.Log
)
func init() {
@@ -50,10 +59,10 @@ func (ws *windowsService) Execute(args []string, changeRequests <-chan svc.Chang
}()
// poll for start completion
var started chan struct{}
started := make(chan struct{})
go func() {
for {
time.Sleep(100 * time.Millisecond)
time.Sleep(10 * time.Millisecond)
if childIsRunning.IsSet() {
close(started)
return
@@ -66,39 +75,41 @@ func (ws *windowsService) Execute(args []string, changeRequests <-chan svc.Chang
case err := <-runError:
// TODO: log error to windows
fmt.Printf("%s start error: %s", logPrefix, err)
eventlogger.Error(4, fmt.Sprintf("failed to start Portmaster Core: %s", err))
changes <- svc.Status{State: svc.Stopped}
return false, 1
case <-started:
// give some more time for enabling packet interception
time.Sleep(500 * time.Millisecond)
changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
fmt.Printf("%s startup complete, entered service running state", logPrefix)
fmt.Printf("%s startup complete, entered service running state\n", logPrefix)
}
// wait for change requests
serviceLoop:
for {
select {
case <-shuttingDown:
// signal that we are shutting down
changes <- svc.Status{State: svc.StopPending}
// wait for program to exit
<-programEnded
return
break serviceLoop
case c := <-changeRequests:
switch c.Cmd {
case svc.Interrogate:
changes <- c.CurrentStatus
case svc.Stop, svc.Shutdown:
changes <- svc.Status{State: svc.StopPending}
initiateShutdown()
// wait for program to exit
<-programEnded
return
break serviceLoop
default:
fmt.Printf("%s unexpected control request: #%d", logPrefix, c)
fmt.Printf("%s unexpected control request: #%d\n", logPrefix, c)
}
}
}
// signal that we are shutting down
changes <- svc.Status{State: svc.StopPending}
// wait for program to exit
<-programEnded
// signal shutdown complete
changes <- svc.Status{State: svc.Stopped}
return
}
@@ -108,6 +119,12 @@ func runService(cmd *cobra.Command, opts *Options) error {
return run(cmd, opts)
}
// check if we are running interactively
isDebug, err := svc.IsAnInteractiveSession()
if err != nil {
return fmt.Errorf("could not determine if running interactively: %s", err)
}
// open eventlog
// TODO: do something useful with eventlog
elog, err := eventlog.Open(serviceName)
@@ -115,9 +132,17 @@ func runService(cmd *cobra.Command, opts *Options) error {
return fmt.Errorf("failed to open eventlog: %s", err)
}
defer elog.Close()
eventlogger = elog
elog.Info(1, fmt.Sprintf("starting %s service", serviceName))
err = svc.Run(serviceName, &windowsService{})
// select run method bas
run := svc.Run
if isDebug {
fmt.Printf("%s WARNING: running interactively, switching to debug execution (no real service).\n", logPrefix)
run = debug.Run
}
// run
err = run(serviceName, &windowsService{})
if err != nil {
elog.Error(3, fmt.Sprintf("%s service failed: %v", serviceName, err))
return fmt.Errorf("failed to start service: %s", err)