~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/gabriel-samfira/sys/windows/svc/debug/service.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
// +build windows
 
6
 
 
7
// Package debug provides facilities to execute svc.Handler on console.
 
8
//
 
9
package debug
 
10
 
 
11
import (
 
12
        "os"
 
13
        "os/signal"
 
14
        "syscall"
 
15
 
 
16
        "github.com/gabriel-samfira/sys/windows/svc"
 
17
)
 
18
 
 
19
// Run executes service name by calling appropriate handler function.
 
20
// The process is running on console, unlike real service. Use Ctrl+C to
 
21
// send "Stop" command to your service.
 
22
func Run(name string, handler svc.Handler) error {
 
23
        cmds := make(chan svc.ChangeRequest)
 
24
        changes := make(chan svc.Status)
 
25
 
 
26
        sig := make(chan os.Signal)
 
27
        signal.Notify(sig)
 
28
 
 
29
        go func() {
 
30
                status := svc.Status{State: svc.Stopped}
 
31
                for {
 
32
                        select {
 
33
                        case <-sig:
 
34
                                cmds <- svc.ChangeRequest{svc.Stop, status}
 
35
                        case status = <-changes:
 
36
                        }
 
37
                }
 
38
        }()
 
39
 
 
40
        _, errno := handler.Execute([]string{name}, cmds, changes)
 
41
        if errno != 0 {
 
42
                return syscall.Errno(errno)
 
43
        }
 
44
        return nil
 
45
}