Source code of Windows XP (NT5)
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.

309 lines
8.1 KiB

  1. /*++
  2. Copyright (C) 1997-2001 Microsoft Corporation
  3. Module Name:
  4. SERVUTIL.CPP
  5. Abstract:
  6. Defines various service utilities.
  7. History:
  8. a-davj 04-Mar-97 Created.
  9. --*/
  10. #include "precomp.h"
  11. #include "servutil.h"
  12. //***************************************************************************
  13. //
  14. // BOOL InstallService
  15. //
  16. // DESCRIPTION:
  17. //
  18. // Installs a service
  19. //
  20. // PARAMETERS:
  21. //
  22. // pServiceName short service name
  23. // pDisplayName name displayed to the user
  24. // pBinary full path to the binary
  25. //
  26. // RETURN VALUE:
  27. //
  28. // TRUE if it worked
  29. //
  30. //***************************************************************************
  31. BOOL InstallService(
  32. IN LPCTSTR pServiceName,
  33. IN LPCTSTR pDisplayName,
  34. IN LPCTSTR pBinary)
  35. {
  36. SC_HANDLE schService = NULL;
  37. SC_HANDLE schSCManager;
  38. schSCManager = OpenSCManager(
  39. NULL, // machine (NULL == local)
  40. NULL, // database (NULL == default)
  41. SC_MANAGER_ALL_ACCESS // access required
  42. );
  43. if ( schSCManager )
  44. {
  45. schService = CreateService(
  46. schSCManager, // SCManager database
  47. pServiceName, // name of service
  48. pDisplayName, // name to display
  49. SERVICE_ALL_ACCESS, // desired access
  50. SERVICE_WIN32_OWN_PROCESS, // service type
  51. SERVICE_DEMAND_START, // start type
  52. SERVICE_ERROR_NORMAL, // error control type
  53. pBinary, // service's binary
  54. NULL, // no load ordering group
  55. NULL, // no tag identifier
  56. NULL, // dependencies
  57. NULL, // LocalSystem account
  58. NULL); // no password
  59. if ( schService )
  60. {
  61. CloseServiceHandle(schService);
  62. }
  63. CloseServiceHandle(schSCManager);
  64. }
  65. return schService != NULL;
  66. }
  67. //***************************************************************************
  68. //
  69. // BOOL RemoveService
  70. //
  71. // DESCRIPTION:
  72. //
  73. // Stops and then removes the service.
  74. //
  75. // PARAMETERS:
  76. //
  77. // pServiceName Short service name
  78. //
  79. // RETURN VALUE:
  80. //
  81. // TRUE if it worked
  82. //
  83. //***************************************************************************
  84. BOOL RemoveService(
  85. IN LPCTSTR pServiceName)
  86. {
  87. SC_HANDLE schService;
  88. BOOL bRet = FALSE;
  89. SC_HANDLE schSCManager;
  90. StopService(pServiceName, 15);
  91. schSCManager = OpenSCManager(
  92. NULL, // machine (NULL == local)
  93. NULL, // database (NULL == default)
  94. SC_MANAGER_ALL_ACCESS // access required
  95. );
  96. if ( schSCManager )
  97. {
  98. schService = OpenService(schSCManager, pServiceName, SERVICE_ALL_ACCESS);
  99. if (schService)
  100. {
  101. bRet = DeleteService(schService);
  102. CloseServiceHandle(schService);
  103. }
  104. CloseServiceHandle(schSCManager);
  105. }
  106. return bRet;
  107. }
  108. //***************************************************************************
  109. //
  110. // BOOL StopService
  111. //
  112. // DESCRIPTION:
  113. //
  114. // Stops and then removes the service.
  115. //
  116. // PARAMETERS:
  117. //
  118. // pServiceName short service name
  119. // dwMaxWait max time in seconds to wait
  120. //
  121. // RETURN VALUE:
  122. //
  123. // TRUE if it worked
  124. //
  125. //***************************************************************************
  126. BOOL StopService(
  127. IN LPCTSTR pServiceName,
  128. IN DWORD dwMaxWait)
  129. {
  130. BOOL bRet = FALSE;
  131. SC_HANDLE schService;
  132. SC_HANDLE schSCManager;
  133. DWORD dwCnt;
  134. SERVICE_STATUS ssStatus; // current status of the service
  135. schSCManager = OpenSCManager(
  136. NULL, // machine (NULL == local)
  137. NULL, // database (NULL == default)
  138. SC_MANAGER_ALL_ACCESS // access required
  139. );
  140. if ( schSCManager )
  141. {
  142. schService = OpenService(schSCManager, pServiceName, SERVICE_ALL_ACCESS);
  143. if (schService)
  144. {
  145. // try to stop the service
  146. if ( bRet = ControlService( schService, SERVICE_CONTROL_STOP, &ssStatus ) )
  147. {
  148. for(dwCnt=0; dwCnt < dwMaxWait &&
  149. QueryServiceStatus( schService, &ssStatus ); dwCnt++)
  150. {
  151. if ( ssStatus.dwCurrentState == SERVICE_STOP_PENDING )
  152. Sleep( 1000 );
  153. else
  154. break;
  155. }
  156. }
  157. CloseServiceHandle(schService);
  158. }
  159. CloseServiceHandle(schSCManager);
  160. }
  161. return bRet;
  162. }
  163. //***************************************************************************
  164. //
  165. // BOOL StartService
  166. //
  167. // DESCRIPTION:
  168. //
  169. // Starts the service runnig
  170. //
  171. // PARAMETERS:
  172. //
  173. // pServiceName short service name
  174. // dwMaxWait max time in seconds to wait
  175. //
  176. // RETURN VALUE:
  177. //
  178. // TRUE if it worked
  179. //
  180. //***************************************************************************
  181. BOOL StartService(
  182. IN LPCTSTR pServiceName,
  183. IN DWORD dwMaxWait)
  184. {
  185. DWORD dwCnt;
  186. BOOL bRet = FALSE;
  187. SC_HANDLE schService;
  188. SC_HANDLE schSCManager;
  189. SERVICE_STATUS ssStatus; // current status of the service
  190. schSCManager = OpenSCManager(
  191. NULL, // machine (NULL == local)
  192. NULL, // database (NULL == default)
  193. SC_MANAGER_ALL_ACCESS // access required
  194. );
  195. if ( schSCManager )
  196. {
  197. schService = OpenService(schSCManager, pServiceName, SERVICE_ALL_ACCESS);
  198. if (schService)
  199. {
  200. // try to stop the service
  201. if ( bRet = StartService( schService, 0, NULL ) )
  202. {
  203. for(dwCnt = 0; dwCnt < dwMaxWait &&
  204. QueryServiceStatus( schService, &ssStatus ); dwCnt++)
  205. {
  206. if ( ssStatus.dwCurrentState != SERVICE_RUNNING )
  207. Sleep( 1000 );
  208. else
  209. break;
  210. }
  211. }
  212. CloseServiceHandle(schService);
  213. }
  214. CloseServiceHandle(schSCManager);
  215. }
  216. return bRet;
  217. }
  218. //***************************************************************************
  219. //
  220. // BOOL SetDependency
  221. //
  222. // DESCRIPTION:
  223. //
  224. // Sets a service's dependency list.
  225. //
  226. // PARAMETERS:
  227. //
  228. // pServiceName short service name
  229. // pDependency dependency list
  230. //
  231. // RETURN VALUE:
  232. //
  233. // TRUE if it worked
  234. //
  235. //***************************************************************************
  236. BOOL SetDependency(
  237. IN LPCTSTR pServiceName,
  238. IN LPCTSTR pDependency)
  239. {
  240. BOOL bRet = FALSE;
  241. SC_HANDLE schService;
  242. SC_HANDLE schSCManager;
  243. schSCManager = OpenSCManager(
  244. NULL, // machine (NULL == local)
  245. NULL, // database (NULL == default)
  246. SC_MANAGER_ALL_ACCESS // access required
  247. );
  248. if ( schSCManager )
  249. {
  250. schService = OpenService(schSCManager, pServiceName, SERVICE_ALL_ACCESS);
  251. if (schService)
  252. {
  253. bRet = ChangeServiceConfig(
  254. schService,
  255. SERVICE_NO_CHANGE,
  256. SERVICE_NO_CHANGE,
  257. SERVICE_NO_CHANGE,
  258. NULL,
  259. NULL,
  260. NULL,
  261. pDependency,
  262. NULL,
  263. NULL,
  264. NULL);
  265. CloseServiceHandle(schService);
  266. }
  267. CloseServiceHandle(schSCManager);
  268. }
  269. return bRet;
  270. }