OSDN Git Service

new repo
[bytom/vapor.git] / vendor / golang.org / x / sys / windows / svc / mgr / service.go
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 mgr
8
9 import (
10         "syscall"
11
12         "golang.org/x/sys/windows"
13         "golang.org/x/sys/windows/svc"
14 )
15
16 // TODO(brainman): Use EnumDependentServices to enumerate dependent services.
17
18 // Service is used to access Windows service.
19 type Service struct {
20         Name   string
21         Handle windows.Handle
22 }
23
24 // Delete marks service s for deletion from the service control manager database.
25 func (s *Service) Delete() error {
26         return windows.DeleteService(s.Handle)
27 }
28
29 // Close relinquish access to the service s.
30 func (s *Service) Close() error {
31         return windows.CloseServiceHandle(s.Handle)
32 }
33
34 // Start starts service s.
35 // args will be passed to svc.Handler.Execute.
36 func (s *Service) Start(args ...string) error {
37         var p **uint16
38         if len(args) > 0 {
39                 vs := make([]*uint16, len(args))
40                 for i := range vs {
41                         vs[i] = syscall.StringToUTF16Ptr(args[i])
42                 }
43                 p = &vs[0]
44         }
45         return windows.StartService(s.Handle, uint32(len(args)), p)
46 }
47
48 // Control sends state change request c to the servce s.
49 func (s *Service) Control(c svc.Cmd) (svc.Status, error) {
50         var t windows.SERVICE_STATUS
51         err := windows.ControlService(s.Handle, uint32(c), &t)
52         if err != nil {
53                 return svc.Status{}, err
54         }
55         return svc.Status{
56                 State:   svc.State(t.CurrentState),
57                 Accepts: svc.Accepted(t.ControlsAccepted),
58         }, nil
59 }
60
61 // Query returns current status of service s.
62 func (s *Service) Query() (svc.Status, error) {
63         var t windows.SERVICE_STATUS
64         err := windows.QueryServiceStatus(s.Handle, &t)
65         if err != nil {
66                 return svc.Status{}, err
67         }
68         return svc.Status{
69                 State:   svc.State(t.CurrentState),
70                 Accepts: svc.Accepted(t.ControlsAccepted),
71         }, nil
72 }