Debian Codegraph @

windows/svc/mgr/service.go

raw
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 //go:build windows
6
7 package mgr
8
9 import (
10 "syscall"
11 "unsafe"
12
13 "golang.org/x/sys/windows"
14 "golang.org/x/sys/windows/svc"
15 )
16
17 // Service is used to access Windows service.
18 type Service struct {
19 Name string
20 Handle windows.Handle
21 }
22
23 // Delete marks service s for deletion from the service control manager database.
24 func (s *Service) Delete() error {
25 return windows.DeleteService(s.Handle)
26 }
27
28 // Close relinquish access to the service s.
29 func (s *Service) Close() error {
30 return windows.CloseServiceHandle(s.Handle)
31 }
32
33 // Start starts service s.
34 // args will be passed to svc.Handler.Execute.
35 func (s *Service) Start(args ...string) error {
36 var p **uint16
37 if len(args) > 0 {
38 vs := make([]*uint16, len(args))
39 for i := range vs {
40 argPointer, err := syscall.UTF16PtrFromString(args[i])
41 if err != nil {
42 return err
43 }
44 vs[i] = argPointer
45 }
46 p = &vs[0]
47 }
48 return windows.StartService(s.Handle, uint32(len(args)), p)
49 }
50
51 // Control sends state change request c to the service s. It returns the most
52 // recent status the service reported to the service control manager, and an
53 // error if the state change request was not accepted.
54 // Note that the returned service status is only set if the status change
55 // request succeeded, or if it failed with error ERROR_INVALID_SERVICE_CONTROL,
56 // ERROR_SERVICE_CANNOT_ACCEPT_CTRL, or ERROR_SERVICE_NOT_ACTIVE.
57 func (s *Service) Control(c svc.Cmd) (svc.Status, error) {
58 var t windows.SERVICE_STATUS
59 err := windows.ControlService(s.Handle, uint32(c), &t)
60 if err != nil &&
61 err != windows.ERROR_INVALID_SERVICE_CONTROL &&
62 err != windows.ERROR_SERVICE_CANNOT_ACCEPT_CTRL &&
63 err != windows.ERROR_SERVICE_NOT_ACTIVE {
64 return svc.Status{}, err
65 }
66 return svc.Status{
67 State: svc.State(t.CurrentState),
68 Accepts: svc.Accepted(t.ControlsAccepted),
69 }, err
70 }
71
72 // Query returns current status of service s.
73 func (s *Service) Query() (svc.Status, error) {
74 var t windows.SERVICE_STATUS_PROCESS
75 var needed uint32
76 err := windows.QueryServiceStatusEx(s.Handle, windows.SC_STATUS_PROCESS_INFO, (*byte)(unsafe.Pointer(&t)), uint32(unsafe.Sizeof(t)), &needed)
77 if err != nil {
78 return svc.Status{}, err
79 }
80 return svc.Status{
81 State: svc.State(t.CurrentState),
82 Accepts: svc.Accepted(t.ControlsAccepted),
83 ProcessId: t.ProcessId,
84 Win32ExitCode: t.Win32ExitCode,
85 ServiceSpecificExitCode: t.ServiceSpecificExitCode,
86 }, nil
87 }
88
89 // ListDependentServices returns the names of the services dependent on service s, which match the given status.
90 func (s *Service) ListDependentServices(status svc.ActivityStatus) ([]string, error) {
91 var bytesNeeded, returnedServiceCount uint32
92 var services []windows.ENUM_SERVICE_STATUS
93 for {
94 var servicesPtr *windows.ENUM_SERVICE_STATUS
95 if len(services) > 0 {
96 servicesPtr = &services[0]
97 }
98 allocatedBytes := uint32(len(services)) * uint32(unsafe.Sizeof(windows.ENUM_SERVICE_STATUS{}))
99 err := windows.EnumDependentServices(s.Handle, uint32(status), servicesPtr, allocatedBytes, &bytesNeeded,
100 &returnedServiceCount)
101 if err == nil {
102 break
103 }
104 if err != syscall.ERROR_MORE_DATA {
105 return nil, err
106 }
107 if bytesNeeded <= allocatedBytes {
108 return nil, err
109 }
110 // ERROR_MORE_DATA indicates the provided buffer was too small, run the call again after resizing the buffer
111 requiredSliceLen := bytesNeeded / uint32(unsafe.Sizeof(windows.ENUM_SERVICE_STATUS{}))
112 if bytesNeeded%uint32(unsafe.Sizeof(windows.ENUM_SERVICE_STATUS{})) != 0 {
113 requiredSliceLen += 1
114 }
115 services = make([]windows.ENUM_SERVICE_STATUS, requiredSliceLen)
116 }
117 if returnedServiceCount == 0 {
118 return nil, nil
119 }
120
121 // The slice mutated by EnumDependentServices may have a length greater than returnedServiceCount, any elements
122 // past that should be ignored.
123 var dependents []string
124 for i := 0; i < int(returnedServiceCount); i++ {
125 dependents = append(dependents, windows.UTF16PtrToString(services[i].ServiceName))
126 }
127 return dependents, nil
128 }