Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

337 lines
11 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997.
  5. //
  6. // File: N C S V C . H
  7. //
  8. // Contents: This file contains CService and CServiceManager, wrapper
  9. // classes to the Win32 Service APIs.
  10. //
  11. // Notes: Note that not all functionality is currently extended through
  12. // these classes.
  13. //
  14. // Author: mikemi 6 Mar 1997
  15. //
  16. //----------------------------------------------------------------------------
  17. #pragma once
  18. #ifndef _NCSVC_H_
  19. #define _NCSVC_H_
  20. // DaveA - 4/21/00 - temporary hack to get bvts to work until final
  21. // solution is found. 15 seconds was not long enough
  22. // for the workstation and dependent services to stop.
  23. // Bug 95996. Two minute timeout will be used instead.
  24. const DWORD c_dwDefaultWaitServiceStop = 120000;
  25. // NTRAID9:105797@20001201#deonb REBOOT: FPS ask for reboot when installing it.
  26. // Changing service start timeout value to 60 seconds instead of 15 seconds as FPS
  27. // requires more time to start all it's dependent services.
  28. const DWORD c_dwDefaultWaitServiceStart = 60000;
  29. struct CSFLAGS
  30. {
  31. // These two fields define the 'control' to be applied. NULL both
  32. // to not apply a control.
  33. //
  34. BOOL fStart; // TRUE to start the service. FALSE to use dwControl.
  35. DWORD dwControl; // 0 to do nothing. SERVICE_CONTROL_ flag otherwise.
  36. // These two fields define the wait behavior to be applied. NULL both
  37. // to not apply a wait.
  38. //
  39. DWORD dwMaxWaitMilliseconds; // How long to wait in milliseconds.
  40. // Zero to not wait.
  41. DWORD dwStateToWaitFor; // Service state flag like SERVICE_STOPPED
  42. // If TRUE, ignore services that are demand start or disabled.
  43. //
  44. BOOL fIgnoreDisabledAndDemandStart;
  45. };
  46. HRESULT
  47. HrSvcQueryStatus (
  48. PCWSTR pszService,
  49. DWORD* pdwState);
  50. HRESULT
  51. HrQueryServiceConfigWithAlloc (
  52. SC_HANDLE hService,
  53. LPQUERY_SERVICE_CONFIG* ppConfig);
  54. HRESULT
  55. HrChangeServiceStartType (
  56. PCWSTR szServiceName,
  57. DWORD dwStartType);
  58. HRESULT
  59. HrChangeServiceStartTypeOptional (
  60. PCWSTR szServiceName,
  61. DWORD dwStartType);
  62. class CService
  63. {
  64. friend class CServiceManager;
  65. public:
  66. CService()
  67. {
  68. _schandle = NULL;
  69. };
  70. ~CService()
  71. {
  72. Close();
  73. };
  74. VOID Close();
  75. HRESULT HrControl (DWORD dwControl);
  76. HRESULT HrRequestStop ();
  77. HRESULT HrQueryServiceConfig (LPQUERY_SERVICE_CONFIG* ppConfig)
  78. {
  79. HRESULT hr = HrQueryServiceConfigWithAlloc (_schandle, ppConfig);
  80. TraceError ("CService::HrQueryServiceConfig", hr);
  81. return hr;
  82. }
  83. HRESULT HrQueryState ( DWORD* pdwState );
  84. HRESULT HrQueryStartType ( DWORD* pdwStartType );
  85. HRESULT HrSetStartType ( DWORD dwStartType )
  86. {
  87. AssertH(_schandle != NULL );
  88. if (::ChangeServiceConfig( _schandle,
  89. SERVICE_NO_CHANGE,
  90. dwStartType,
  91. SERVICE_NO_CHANGE,
  92. NULL,
  93. NULL,
  94. NULL,
  95. NULL,
  96. NULL,
  97. NULL,
  98. NULL))
  99. return S_OK;
  100. else
  101. return ::HrFromLastWin32Error();
  102. }
  103. HRESULT HrSetImagePath(IN PCWSTR pszImagePath)
  104. {
  105. AssertH(pszImagePath);
  106. AssertH(_schandle != NULL );
  107. if (::ChangeServiceConfig( _schandle,
  108. SERVICE_NO_CHANGE, // ServiceType
  109. SERVICE_NO_CHANGE, // StartType
  110. SERVICE_NO_CHANGE, // ErrorControl
  111. pszImagePath, // BinaryPathName
  112. NULL, // LoadOredrGroup
  113. NULL, // TagId
  114. NULL, // Dependencies
  115. NULL, // ServiceStartName
  116. NULL, // Password
  117. NULL)) // DisplayName
  118. return S_OK;
  119. else
  120. return ::HrFromLastWin32Error();
  121. }
  122. HRESULT HrSetServiceRestartRecoveryOption(IN SERVICE_FAILURE_ACTIONS *psfa);
  123. HRESULT HrSetDependencies(IN PCWSTR mszDependencyList)
  124. {
  125. AssertH(_schandle != NULL );
  126. if (::ChangeServiceConfig( _schandle,
  127. SERVICE_NO_CHANGE, // ServiceType
  128. SERVICE_NO_CHANGE, // StartType
  129. SERVICE_NO_CHANGE, // ErrorControl
  130. NULL, // BinaryPathName
  131. NULL, // LoadOredrGroup
  132. NULL, // TagId
  133. mszDependencyList, // Dependencies
  134. NULL, // ServiceStartName
  135. NULL, // Password
  136. NULL)) // DisplayName
  137. return S_OK;
  138. else
  139. return ::HrFromLastWin32Error();
  140. }
  141. HRESULT HrSetDisplayName(IN PCWSTR mszDisplayName)
  142. {
  143. AssertH(_schandle != NULL );
  144. if (::ChangeServiceConfig( _schandle,
  145. SERVICE_NO_CHANGE, // ServiceType
  146. SERVICE_NO_CHANGE, // StartType
  147. SERVICE_NO_CHANGE, // ErrorControl
  148. NULL, // BinaryPathName
  149. NULL, // LoadOredrGroup
  150. NULL, // TagId
  151. NULL, // Dependencies
  152. NULL, // ServiceStartName
  153. NULL, // Password
  154. mszDisplayName)) // DisplayName
  155. {
  156. return S_OK;
  157. }
  158. else
  159. {
  160. return ::HrFromLastWin32Error();
  161. }
  162. }
  163. HRESULT HrSetServiceObjectSecurity(
  164. SECURITY_INFORMATION dwSecurityInformation,
  165. PSECURITY_DESCRIPTOR lpSecurityDescriptor)
  166. {
  167. AssertH(_schandle != NULL );
  168. if (::SetServiceObjectSecurity( _schandle,
  169. dwSecurityInformation, lpSecurityDescriptor))
  170. {
  171. return S_OK;
  172. }
  173. else
  174. {
  175. return ::HrFromLastWin32Error();
  176. }
  177. }
  178. private:
  179. SC_HANDLE _schandle;
  180. };
  181. enum CSLOCK
  182. {
  183. NO_LOCK,
  184. WITH_LOCK,
  185. };
  186. class CServiceManager
  187. {
  188. public:
  189. CServiceManager()
  190. {
  191. _schandle = NULL;
  192. _sclock = NULL;
  193. };
  194. ~CServiceManager();
  195. SC_HANDLE Handle () const
  196. {
  197. return _schandle;
  198. }
  199. HRESULT HrOpen( CSLOCK eLock = NO_LOCK,
  200. DWORD dwDesiredAccess = SC_MANAGER_ALL_ACCESS,
  201. PCWSTR pszMachineName = NULL,
  202. PCWSTR pszDatabaseName = NULL );
  203. HRESULT HrControlServicesAndWait (
  204. UINT cServices,
  205. const PCWSTR* apszServices,
  206. const CSFLAGS* pFlags);
  207. HRESULT HrStartServicesNoWait (UINT cServices, const PCWSTR* apszServices);
  208. HRESULT HrStartServicesAndWait(UINT cServices, const PCWSTR* apszServices, DWORD dwWaitMilliseconds = c_dwDefaultWaitServiceStart);
  209. HRESULT HrStopServicesNoWait (UINT cServices, const PCWSTR* apszServices);
  210. HRESULT HrStopServicesAndWait (UINT cServices, const PCWSTR* apszServices, DWORD dwWaitMilliseconds = c_dwDefaultWaitServiceStop);
  211. HRESULT HrStartServiceNoWait (PCWSTR pszService)
  212. {
  213. return HrStartServicesNoWait (1, &pszService);
  214. }
  215. HRESULT HrStartServiceAndWait(PCWSTR pszService, DWORD dwWaitMilliseconds = c_dwDefaultWaitServiceStart)
  216. {
  217. return HrStartServicesAndWait (1, &pszService, dwWaitMilliseconds);
  218. }
  219. HRESULT HrStopServiceNoWait (PCWSTR pszService)
  220. {
  221. return HrStopServicesNoWait (1, &pszService);
  222. }
  223. HRESULT HrStopServiceAndWait (PCWSTR pszService, DWORD dwWaitMilliseconds = c_dwDefaultWaitServiceStop)
  224. {
  225. return HrStopServicesAndWait (1, &pszService, dwWaitMilliseconds);
  226. }
  227. VOID Close();
  228. HRESULT HrLock();
  229. VOID Unlock();
  230. HRESULT HrQueryLocked (BOOL* pfLocked);
  231. HRESULT HrOpenService (
  232. CService* pcsService,
  233. PCWSTR pszServiceName,
  234. CSLOCK eLock = NO_LOCK,
  235. DWORD dwScmAccess = SC_MANAGER_ALL_ACCESS,
  236. DWORD dwSvcAccess = SERVICE_ALL_ACCESS);
  237. HRESULT HrCreateService (CService* pcsService,
  238. PCWSTR pszServiceName,
  239. PCWSTR pszDisplayName,
  240. DWORD dwServiceType,
  241. DWORD dwStartType,
  242. DWORD dwErrorControl,
  243. PCWSTR pszBinaryPathName,
  244. PCWSTR pslzDependencies = NULL,
  245. PCWSTR pszLoadOrderGroup = NULL,
  246. PDWORD pdwTagId = NULL,
  247. DWORD dwDesiredAccess = SERVICE_ALL_ACCESS,
  248. PCWSTR pszServiceStartName = NULL,
  249. PCWSTR pszPassword = NULL,
  250. PCWSTR pszDescription = NULL);
  251. enum SERVICE_START_CRITERIA
  252. {
  253. SERVICE_NO_CRITERIA, // Start the service regardless
  254. SERVICE_ONLY_AUTO_START // Only start the service if it is of type
  255. // Auto-Start
  256. };
  257. enum DEPENDENCY_ADDREMOVE
  258. {
  259. DEPENDENCY_ADD,
  260. DEPENDENCY_REMOVE
  261. };
  262. HRESULT HrAddServiceDependency(PCWSTR szServiceName, PCWSTR szDependency)
  263. {
  264. return HrAddRemoveServiceDependency(szServiceName,
  265. szDependency,
  266. DEPENDENCY_ADD);
  267. }
  268. HRESULT HrRemoveServiceDependency(PCWSTR szServiceName, PCWSTR szDependency)
  269. {
  270. return HrAddRemoveServiceDependency(szServiceName,
  271. szDependency,
  272. DEPENDENCY_REMOVE);
  273. }
  274. HRESULT HrAddRemoveServiceDependency(PCWSTR szServiceName,
  275. PCWSTR szDependency,
  276. DEPENDENCY_ADDREMOVE enumFlag);
  277. private:
  278. SC_HANDLE _schandle;
  279. SC_LOCK _sclock;
  280. };
  281. #endif // _NCSVC_H_