mirror of
https://github.com/golang/sys.git
synced 2026-02-08 03:36:03 +03:00
Done with: go get go@1.18 go mod tidy go fix ./... Using go1.21.3. Also update code generators to use only the new go:build lines, not the old +build ones. For golang/go#60268. Change-Id: I6aabc42efb6ab3329981100e1db2263aac5e92a6 Reviewed-on: https://go-review.googlesource.com/c/sys/+/534222 Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
88 lines
2.2 KiB
Go
88 lines
2.2 KiB
Go
// Copyright 2012 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
//go:build windows
|
|
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"golang.org/x/sys/windows/svc"
|
|
"golang.org/x/sys/windows/svc/debug"
|
|
"golang.org/x/sys/windows/svc/eventlog"
|
|
)
|
|
|
|
var elog debug.Log
|
|
|
|
type exampleService struct{}
|
|
|
|
func (m *exampleService) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) {
|
|
const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown | svc.AcceptPauseAndContinue
|
|
changes <- svc.Status{State: svc.StartPending}
|
|
fasttick := time.Tick(500 * time.Millisecond)
|
|
slowtick := time.Tick(2 * time.Second)
|
|
tick := fasttick
|
|
changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
|
|
loop:
|
|
for {
|
|
select {
|
|
case <-tick:
|
|
beep()
|
|
elog.Info(1, "beep")
|
|
case c := <-r:
|
|
switch c.Cmd {
|
|
case svc.Interrogate:
|
|
changes <- c.CurrentStatus
|
|
// Testing deadlock from https://code.google.com/p/winsvc/issues/detail?id=4
|
|
time.Sleep(100 * time.Millisecond)
|
|
changes <- c.CurrentStatus
|
|
case svc.Stop, svc.Shutdown:
|
|
// golang.org/x/sys/windows/svc.TestExample is verifying this output.
|
|
testOutput := strings.Join(args, "-")
|
|
testOutput += fmt.Sprintf("-%d", c.Context)
|
|
elog.Info(1, testOutput)
|
|
break loop
|
|
case svc.Pause:
|
|
changes <- svc.Status{State: svc.Paused, Accepts: cmdsAccepted}
|
|
tick = slowtick
|
|
case svc.Continue:
|
|
changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
|
|
tick = fasttick
|
|
default:
|
|
elog.Error(1, fmt.Sprintf("unexpected control request #%d", c))
|
|
}
|
|
}
|
|
}
|
|
changes <- svc.Status{State: svc.StopPending}
|
|
return
|
|
}
|
|
|
|
func runService(name string, isDebug bool) {
|
|
var err error
|
|
if isDebug {
|
|
elog = debug.New(name)
|
|
} else {
|
|
elog, err = eventlog.Open(name)
|
|
if err != nil {
|
|
return
|
|
}
|
|
}
|
|
defer elog.Close()
|
|
|
|
elog.Info(1, fmt.Sprintf("starting %s service", name))
|
|
run := svc.Run
|
|
if isDebug {
|
|
run = debug.Run
|
|
}
|
|
err = run(name, &exampleService{})
|
|
if err != nil {
|
|
elog.Error(1, fmt.Sprintf("%s service failed: %v", name, err))
|
|
return
|
|
}
|
|
elog.Info(1, fmt.Sprintf("%s service stopped", name))
|
|
}
|