mirror of
https://github.com/golang/sys.git
synced 2026-02-08 03:36:03 +03:00
Notably, the DisplayName field was set to the same thing in both sys.TestExample and mrg.TestMyService, which may explain the collision reported in golang/go#59298. Moreover, the adjective ”my” conveys no information whatsoever — we shouldn't use it in tests or examples. Also skip the tests that install services if GO_BUILDER_NAME is not set, to reduce the likelihood of 'go test all' in a user's working directory being mistaken for a malicious or compromised program. Fixes golang/go#59298. Change-Id: Ib00bf7400bfaa34e1a1d49125c43b97019b53c82 Reviewed-on: https://go-review.googlesource.com/c/sys/+/481015 Reviewed-by: Carlos Amedee <carlos@golang.org> Run-TryBot: Bryan Mills <bcmills@google.com> Reviewed-by: Alex Brainman <alex.brainman@gmail.com> TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Bryan Mills <bcmills@google.com>
81 lines
1.8 KiB
Go
81 lines
1.8 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
|
|
// +build windows
|
|
|
|
// Example service program that beeps.
|
|
//
|
|
// The program demonstrates how to create Windows service and
|
|
// install / remove it on a computer. It also shows how to
|
|
// stop / start / pause / continue any service, and how to
|
|
// write to event log. It also shows how to use debug
|
|
// facilities available in debug package.
|
|
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"strings"
|
|
|
|
"golang.org/x/sys/windows/svc"
|
|
)
|
|
|
|
func usage(errmsg string) {
|
|
fmt.Fprintf(os.Stderr,
|
|
"%s\n\n"+
|
|
"usage: %s <command>\n"+
|
|
" where <command> is one of\n"+
|
|
" install, remove, debug, start, stop, pause or continue.\n",
|
|
errmsg, os.Args[0])
|
|
os.Exit(2)
|
|
}
|
|
|
|
var svcName = "exampleservice"
|
|
|
|
func main() {
|
|
flag.StringVar(&svcName, "name", svcName, "name of the service")
|
|
flag.Parse()
|
|
|
|
inService, err := svc.IsWindowsService()
|
|
if err != nil {
|
|
log.Fatalf("failed to determine if we are running in service: %v", err)
|
|
}
|
|
if inService {
|
|
runService(svcName, false)
|
|
return
|
|
}
|
|
|
|
if len(os.Args) < 2 {
|
|
usage("no command specified")
|
|
}
|
|
|
|
cmd := strings.ToLower(os.Args[1])
|
|
switch cmd {
|
|
case "debug":
|
|
runService(svcName, true)
|
|
return
|
|
case "install":
|
|
err = installService(svcName, "example service")
|
|
case "remove":
|
|
err = removeService(svcName)
|
|
case "start":
|
|
err = startService(svcName)
|
|
case "stop":
|
|
err = controlService(svcName, svc.Stop, svc.Stopped)
|
|
case "pause":
|
|
err = controlService(svcName, svc.Pause, svc.Paused)
|
|
case "continue":
|
|
err = controlService(svcName, svc.Continue, svc.Running)
|
|
default:
|
|
usage(fmt.Sprintf("invalid command %s", cmd))
|
|
}
|
|
if err != nil {
|
|
log.Fatalf("failed to %s %s: %v", cmd, svcName, err)
|
|
}
|
|
return
|
|
}
|