service.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. //go:build windows
  5. package windows
  6. const (
  7. SC_MANAGER_CONNECT = 1
  8. SC_MANAGER_CREATE_SERVICE = 2
  9. SC_MANAGER_ENUMERATE_SERVICE = 4
  10. SC_MANAGER_LOCK = 8
  11. SC_MANAGER_QUERY_LOCK_STATUS = 16
  12. SC_MANAGER_MODIFY_BOOT_CONFIG = 32
  13. SC_MANAGER_ALL_ACCESS = 0xf003f
  14. )
  15. const (
  16. SERVICE_KERNEL_DRIVER = 1
  17. SERVICE_FILE_SYSTEM_DRIVER = 2
  18. SERVICE_ADAPTER = 4
  19. SERVICE_RECOGNIZER_DRIVER = 8
  20. SERVICE_WIN32_OWN_PROCESS = 16
  21. SERVICE_WIN32_SHARE_PROCESS = 32
  22. SERVICE_WIN32 = SERVICE_WIN32_OWN_PROCESS | SERVICE_WIN32_SHARE_PROCESS
  23. SERVICE_INTERACTIVE_PROCESS = 256
  24. SERVICE_DRIVER = SERVICE_KERNEL_DRIVER | SERVICE_FILE_SYSTEM_DRIVER | SERVICE_RECOGNIZER_DRIVER
  25. SERVICE_TYPE_ALL = SERVICE_WIN32 | SERVICE_ADAPTER | SERVICE_DRIVER | SERVICE_INTERACTIVE_PROCESS
  26. SERVICE_BOOT_START = 0
  27. SERVICE_SYSTEM_START = 1
  28. SERVICE_AUTO_START = 2
  29. SERVICE_DEMAND_START = 3
  30. SERVICE_DISABLED = 4
  31. SERVICE_ERROR_IGNORE = 0
  32. SERVICE_ERROR_NORMAL = 1
  33. SERVICE_ERROR_SEVERE = 2
  34. SERVICE_ERROR_CRITICAL = 3
  35. SC_STATUS_PROCESS_INFO = 0
  36. SC_ACTION_NONE = 0
  37. SC_ACTION_RESTART = 1
  38. SC_ACTION_REBOOT = 2
  39. SC_ACTION_RUN_COMMAND = 3
  40. SERVICE_STOPPED = 1
  41. SERVICE_START_PENDING = 2
  42. SERVICE_STOP_PENDING = 3
  43. SERVICE_RUNNING = 4
  44. SERVICE_CONTINUE_PENDING = 5
  45. SERVICE_PAUSE_PENDING = 6
  46. SERVICE_PAUSED = 7
  47. SERVICE_NO_CHANGE = 0xffffffff
  48. SERVICE_ACCEPT_STOP = 1
  49. SERVICE_ACCEPT_PAUSE_CONTINUE = 2
  50. SERVICE_ACCEPT_SHUTDOWN = 4
  51. SERVICE_ACCEPT_PARAMCHANGE = 8
  52. SERVICE_ACCEPT_NETBINDCHANGE = 16
  53. SERVICE_ACCEPT_HARDWAREPROFILECHANGE = 32
  54. SERVICE_ACCEPT_POWEREVENT = 64
  55. SERVICE_ACCEPT_SESSIONCHANGE = 128
  56. SERVICE_ACCEPT_PRESHUTDOWN = 256
  57. SERVICE_CONTROL_STOP = 1
  58. SERVICE_CONTROL_PAUSE = 2
  59. SERVICE_CONTROL_CONTINUE = 3
  60. SERVICE_CONTROL_INTERROGATE = 4
  61. SERVICE_CONTROL_SHUTDOWN = 5
  62. SERVICE_CONTROL_PARAMCHANGE = 6
  63. SERVICE_CONTROL_NETBINDADD = 7
  64. SERVICE_CONTROL_NETBINDREMOVE = 8
  65. SERVICE_CONTROL_NETBINDENABLE = 9
  66. SERVICE_CONTROL_NETBINDDISABLE = 10
  67. SERVICE_CONTROL_DEVICEEVENT = 11
  68. SERVICE_CONTROL_HARDWAREPROFILECHANGE = 12
  69. SERVICE_CONTROL_POWEREVENT = 13
  70. SERVICE_CONTROL_SESSIONCHANGE = 14
  71. SERVICE_CONTROL_PRESHUTDOWN = 15
  72. SERVICE_ACTIVE = 1
  73. SERVICE_INACTIVE = 2
  74. SERVICE_STATE_ALL = 3
  75. SERVICE_QUERY_CONFIG = 1
  76. SERVICE_CHANGE_CONFIG = 2
  77. SERVICE_QUERY_STATUS = 4
  78. SERVICE_ENUMERATE_DEPENDENTS = 8
  79. SERVICE_START = 16
  80. SERVICE_STOP = 32
  81. SERVICE_PAUSE_CONTINUE = 64
  82. SERVICE_INTERROGATE = 128
  83. SERVICE_USER_DEFINED_CONTROL = 256
  84. SERVICE_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED | SERVICE_QUERY_CONFIG | SERVICE_CHANGE_CONFIG | SERVICE_QUERY_STATUS | SERVICE_ENUMERATE_DEPENDENTS | SERVICE_START | SERVICE_STOP | SERVICE_PAUSE_CONTINUE | SERVICE_INTERROGATE | SERVICE_USER_DEFINED_CONTROL
  85. SERVICE_RUNS_IN_SYSTEM_PROCESS = 1
  86. SERVICE_CONFIG_DESCRIPTION = 1
  87. SERVICE_CONFIG_FAILURE_ACTIONS = 2
  88. SERVICE_CONFIG_DELAYED_AUTO_START_INFO = 3
  89. SERVICE_CONFIG_FAILURE_ACTIONS_FLAG = 4
  90. SERVICE_CONFIG_SERVICE_SID_INFO = 5
  91. SERVICE_CONFIG_REQUIRED_PRIVILEGES_INFO = 6
  92. SERVICE_CONFIG_PRESHUTDOWN_INFO = 7
  93. SERVICE_CONFIG_TRIGGER_INFO = 8
  94. SERVICE_CONFIG_PREFERRED_NODE = 9
  95. SERVICE_CONFIG_LAUNCH_PROTECTED = 12
  96. SERVICE_SID_TYPE_NONE = 0
  97. SERVICE_SID_TYPE_UNRESTRICTED = 1
  98. SERVICE_SID_TYPE_RESTRICTED = 2 | SERVICE_SID_TYPE_UNRESTRICTED
  99. SC_ENUM_PROCESS_INFO = 0
  100. SERVICE_NOTIFY_STATUS_CHANGE = 2
  101. SERVICE_NOTIFY_STOPPED = 0x00000001
  102. SERVICE_NOTIFY_START_PENDING = 0x00000002
  103. SERVICE_NOTIFY_STOP_PENDING = 0x00000004
  104. SERVICE_NOTIFY_RUNNING = 0x00000008
  105. SERVICE_NOTIFY_CONTINUE_PENDING = 0x00000010
  106. SERVICE_NOTIFY_PAUSE_PENDING = 0x00000020
  107. SERVICE_NOTIFY_PAUSED = 0x00000040
  108. SERVICE_NOTIFY_CREATED = 0x00000080
  109. SERVICE_NOTIFY_DELETED = 0x00000100
  110. SERVICE_NOTIFY_DELETE_PENDING = 0x00000200
  111. SC_EVENT_DATABASE_CHANGE = 0
  112. SC_EVENT_PROPERTY_CHANGE = 1
  113. SC_EVENT_STATUS_CHANGE = 2
  114. SERVICE_START_REASON_DEMAND = 0x00000001
  115. SERVICE_START_REASON_AUTO = 0x00000002
  116. SERVICE_START_REASON_TRIGGER = 0x00000004
  117. SERVICE_START_REASON_RESTART_ON_FAILURE = 0x00000008
  118. SERVICE_START_REASON_DELAYEDAUTO = 0x00000010
  119. SERVICE_DYNAMIC_INFORMATION_LEVEL_START_REASON = 1
  120. )
  121. type ENUM_SERVICE_STATUS struct {
  122. ServiceName *uint16
  123. DisplayName *uint16
  124. ServiceStatus SERVICE_STATUS
  125. }
  126. type SERVICE_STATUS struct {
  127. ServiceType uint32
  128. CurrentState uint32
  129. ControlsAccepted uint32
  130. Win32ExitCode uint32
  131. ServiceSpecificExitCode uint32
  132. CheckPoint uint32
  133. WaitHint uint32
  134. }
  135. type SERVICE_TABLE_ENTRY struct {
  136. ServiceName *uint16
  137. ServiceProc uintptr
  138. }
  139. type QUERY_SERVICE_CONFIG struct {
  140. ServiceType uint32
  141. StartType uint32
  142. ErrorControl uint32
  143. BinaryPathName *uint16
  144. LoadOrderGroup *uint16
  145. TagId uint32
  146. Dependencies *uint16
  147. ServiceStartName *uint16
  148. DisplayName *uint16
  149. }
  150. type SERVICE_DESCRIPTION struct {
  151. Description *uint16
  152. }
  153. type SERVICE_DELAYED_AUTO_START_INFO struct {
  154. IsDelayedAutoStartUp uint32
  155. }
  156. type SERVICE_STATUS_PROCESS struct {
  157. ServiceType uint32
  158. CurrentState uint32
  159. ControlsAccepted uint32
  160. Win32ExitCode uint32
  161. ServiceSpecificExitCode uint32
  162. CheckPoint uint32
  163. WaitHint uint32
  164. ProcessId uint32
  165. ServiceFlags uint32
  166. }
  167. type ENUM_SERVICE_STATUS_PROCESS struct {
  168. ServiceName *uint16
  169. DisplayName *uint16
  170. ServiceStatusProcess SERVICE_STATUS_PROCESS
  171. }
  172. type SERVICE_NOTIFY struct {
  173. Version uint32
  174. NotifyCallback uintptr
  175. Context uintptr
  176. NotificationStatus uint32
  177. ServiceStatus SERVICE_STATUS_PROCESS
  178. NotificationTriggered uint32
  179. ServiceNames *uint16
  180. }
  181. type SERVICE_FAILURE_ACTIONS struct {
  182. ResetPeriod uint32
  183. RebootMsg *uint16
  184. Command *uint16
  185. ActionsCount uint32
  186. Actions *SC_ACTION
  187. }
  188. type SERVICE_FAILURE_ACTIONS_FLAG struct {
  189. FailureActionsOnNonCrashFailures int32
  190. }
  191. type SC_ACTION struct {
  192. Type uint32
  193. Delay uint32
  194. }
  195. type QUERY_SERVICE_LOCK_STATUS struct {
  196. IsLocked uint32
  197. LockOwner *uint16
  198. LockDuration uint32
  199. }
  200. //sys OpenSCManager(machineName *uint16, databaseName *uint16, access uint32) (handle Handle, err error) [failretval==0] = advapi32.OpenSCManagerW
  201. //sys CloseServiceHandle(handle Handle) (err error) = advapi32.CloseServiceHandle
  202. //sys CreateService(mgr Handle, serviceName *uint16, displayName *uint16, access uint32, srvType uint32, startType uint32, errCtl uint32, pathName *uint16, loadOrderGroup *uint16, tagId *uint32, dependencies *uint16, serviceStartName *uint16, password *uint16) (handle Handle, err error) [failretval==0] = advapi32.CreateServiceW
  203. //sys OpenService(mgr Handle, serviceName *uint16, access uint32) (handle Handle, err error) [failretval==0] = advapi32.OpenServiceW
  204. //sys DeleteService(service Handle) (err error) = advapi32.DeleteService
  205. //sys StartService(service Handle, numArgs uint32, argVectors **uint16) (err error) = advapi32.StartServiceW
  206. //sys QueryServiceStatus(service Handle, status *SERVICE_STATUS) (err error) = advapi32.QueryServiceStatus
  207. //sys QueryServiceLockStatus(mgr Handle, lockStatus *QUERY_SERVICE_LOCK_STATUS, bufSize uint32, bytesNeeded *uint32) (err error) = advapi32.QueryServiceLockStatusW
  208. //sys ControlService(service Handle, control uint32, status *SERVICE_STATUS) (err error) = advapi32.ControlService
  209. //sys StartServiceCtrlDispatcher(serviceTable *SERVICE_TABLE_ENTRY) (err error) = advapi32.StartServiceCtrlDispatcherW
  210. //sys SetServiceStatus(service Handle, serviceStatus *SERVICE_STATUS) (err error) = advapi32.SetServiceStatus
  211. //sys ChangeServiceConfig(service Handle, serviceType uint32, startType uint32, errorControl uint32, binaryPathName *uint16, loadOrderGroup *uint16, tagId *uint32, dependencies *uint16, serviceStartName *uint16, password *uint16, displayName *uint16) (err error) = advapi32.ChangeServiceConfigW
  212. //sys QueryServiceConfig(service Handle, serviceConfig *QUERY_SERVICE_CONFIG, bufSize uint32, bytesNeeded *uint32) (err error) = advapi32.QueryServiceConfigW
  213. //sys ChangeServiceConfig2(service Handle, infoLevel uint32, info *byte) (err error) = advapi32.ChangeServiceConfig2W
  214. //sys QueryServiceConfig2(service Handle, infoLevel uint32, buff *byte, buffSize uint32, bytesNeeded *uint32) (err error) = advapi32.QueryServiceConfig2W
  215. //sys EnumServicesStatusEx(mgr Handle, infoLevel uint32, serviceType uint32, serviceState uint32, services *byte, bufSize uint32, bytesNeeded *uint32, servicesReturned *uint32, resumeHandle *uint32, groupName *uint16) (err error) = advapi32.EnumServicesStatusExW
  216. //sys QueryServiceStatusEx(service Handle, infoLevel uint32, buff *byte, buffSize uint32, bytesNeeded *uint32) (err error) = advapi32.QueryServiceStatusEx
  217. //sys NotifyServiceStatusChange(service Handle, notifyMask uint32, notifier *SERVICE_NOTIFY) (ret error) = advapi32.NotifyServiceStatusChangeW
  218. //sys SubscribeServiceChangeNotifications(service Handle, eventType uint32, callback uintptr, callbackCtx uintptr, subscription *uintptr) (ret error) = sechost.SubscribeServiceChangeNotifications?
  219. //sys UnsubscribeServiceChangeNotifications(subscription uintptr) = sechost.UnsubscribeServiceChangeNotifications?
  220. //sys RegisterServiceCtrlHandlerEx(serviceName *uint16, handlerProc uintptr, context uintptr) (handle Handle, err error) = advapi32.RegisterServiceCtrlHandlerExW
  221. //sys QueryServiceDynamicInformation(service Handle, infoLevel uint32, dynamicInfo unsafe.Pointer) (err error) = advapi32.QueryServiceDynamicInformation?
  222. //sys EnumDependentServices(service Handle, activityState uint32, services *ENUM_SERVICE_STATUS, buffSize uint32, bytesNeeded *uint32, servicesReturned *uint32) (err error) = advapi32.EnumDependentServicesW