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.

427 lines
9.4 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. service.cpp
  5. Abstract:
  6. This file provides access to the service control
  7. manager for starting, stopping, adding, and removing
  8. services.
  9. Environment:
  10. WIN32 User Mode
  11. Author:
  12. Vlad Sadovsky (vlads) 17-Apr-1998
  13. --*/
  14. #include "cplusinc.h"
  15. #include "sticomm.h"
  16. #include <stisvc.h>
  17. #include <eventlog.h>
  18. DWORD
  19. SetServiceSecurity(
  20. LPTSTR AccountName
  21. );
  22. //
  23. // Installation routines.
  24. //
  25. DWORD
  26. WINAPI
  27. StiServiceInstall(
  28. BOOL UseLocalSystem,
  29. BOOL DemandStart,
  30. LPTSTR lpszUserName,
  31. LPTSTR lpszUserPassword
  32. )
  33. /*++
  34. Routine Description:
  35. Service installation function.
  36. Calls SCM to install STI service, which is running in user security context
  37. BUGBUG Review
  38. Arguments:
  39. Return Value:
  40. None.
  41. --*/
  42. {
  43. DWORD dwError = NOERROR;
  44. SC_HANDLE hSCM = NULL;
  45. SC_HANDLE hService = NULL;
  46. __try {
  47. hSCM = ::OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
  48. if (!hSCM) {
  49. dwError = GetLastError();
  50. __leave;
  51. }
  52. //
  53. // If service already exists - bail out quickly
  54. //
  55. hService = OpenService(
  56. hSCM,
  57. STI_SERVICE_NAME,
  58. SERVICE_ALL_ACCESS
  59. );
  60. if (hService) {
  61. dwError = NOERROR;
  62. __leave;
  63. }
  64. //
  65. // If use local system - set security
  66. //
  67. if (!UseLocalSystem) {
  68. #ifdef LATER
  69. dwError = SetServiceSecurity( lpszUserName );
  70. if (dwError) {
  71. dwError = ERROR_SERVICE_LOGON_FAILED ;
  72. __leave;
  73. }
  74. #endif
  75. }
  76. hService = CreateService(
  77. hSCM,
  78. STI_SERVICE_NAME,
  79. STI_DISPLAY_NAME,
  80. SERVICE_ALL_ACCESS,
  81. STI_SVC_SERVICE_TYPE,
  82. DemandStart ? SERVICE_DEMAND_START : SERVICE_AUTO_START,
  83. SERVICE_ERROR_NORMAL,
  84. STI_IMAGE_NAME,
  85. NULL,
  86. NULL,
  87. NULL, //STI_SERVICE_DEPENDENCY,
  88. UseLocalSystem ? NULL : lpszUserName,
  89. UseLocalSystem ? NULL : lpszUserPassword
  90. );
  91. if (!hService) {
  92. dwError = GetLastError();
  93. __leave;
  94. }
  95. //
  96. // Add registry settings for event logging
  97. //
  98. RegisterStiEventSources();
  99. //
  100. // Start service
  101. //
  102. dwError = StartService(hService,0,(LPCTSTR *)NULL);
  103. }
  104. __finally {
  105. CloseServiceHandle( hService );
  106. CloseServiceHandle( hSCM );
  107. }
  108. return dwError;
  109. } //StiServiceInstall
  110. DWORD
  111. WINAPI
  112. StiServiceRemove(
  113. VOID
  114. )
  115. /*++
  116. Routine Description:
  117. Service removal function. This function calls SCM to remove the STI service.
  118. Arguments:
  119. None.
  120. Return Value:
  121. Return code. Return zero for success
  122. --*/
  123. {
  124. DWORD dwError = NOERROR;
  125. SC_HANDLE hSCM = NULL;
  126. SC_HANDLE hService = NULL;
  127. SERVICE_STATUS ServiceStatus;
  128. UINT uiRetry = 10;
  129. HKEY hkRun;
  130. __try {
  131. hSCM = ::OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
  132. if (!hSCM) {
  133. dwError = GetLastError();
  134. __leave;
  135. }
  136. hService = OpenService(
  137. hSCM,
  138. STI_SERVICE_NAME,
  139. SERVICE_ALL_ACCESS
  140. );
  141. if (!hService) {
  142. dwError = GetLastError();
  143. __leave;
  144. }
  145. //
  146. // Stop service first
  147. //
  148. if (ControlService( hService, SERVICE_CONTROL_STOP, &ServiceStatus )) {
  149. //
  150. // Wait a little
  151. //
  152. Sleep( STI_STOP_FOR_REMOVE_TIMEOUT );
  153. ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
  154. while( QueryServiceStatus( hService, &ServiceStatus ) &&
  155. (SERVICE_STOP_PENDING == ServiceStatus.dwCurrentState)) {
  156. Sleep( STI_STOP_FOR_REMOVE_TIMEOUT );
  157. if (!uiRetry--) {
  158. break;
  159. }
  160. }
  161. if (ServiceStatus.dwCurrentState != SERVICE_STOPPED) {
  162. dwError = GetLastError();
  163. __leave;
  164. }
  165. }
  166. else {
  167. dwError = GetLastError();
  168. __leave;
  169. }
  170. if (!DeleteService( hService )) {
  171. dwError = GetLastError();
  172. __leave;
  173. }
  174. }
  175. __finally {
  176. CloseServiceHandle( hService );
  177. CloseServiceHandle( hSCM );
  178. }
  179. //
  180. // Leftovers from Win9x - remove STI monitor from Run section
  181. //
  182. if (RegOpenKey(HKEY_LOCAL_MACHINE, REGSTR_PATH_RUN, &hkRun) == NO_ERROR) {
  183. RegDeleteValue (hkRun, REGSTR_VAL_MONITOR);
  184. RegCloseKey(hkRun);
  185. }
  186. return dwError;
  187. } // StiServiceRemove
  188. BOOL
  189. SetServiceDependency(
  190. LPTSTR ServiceName,
  191. LPTSTR DependentServiceName
  192. )
  193. {
  194. BOOL rVal = FALSE;
  195. SC_HANDLE hSvcMgr;
  196. SC_HANDLE hService;
  197. hSvcMgr = OpenSCManager(
  198. NULL,
  199. NULL,
  200. SC_MANAGER_ALL_ACCESS
  201. );
  202. if (!hSvcMgr) {
  203. goto exit;
  204. }
  205. hService = OpenService(
  206. hSvcMgr,
  207. ServiceName,
  208. SERVICE_ALL_ACCESS
  209. );
  210. if (!hService) {
  211. goto exit;
  212. }
  213. if (!ChangeServiceConfig(
  214. hService, // handle to service
  215. SERVICE_NO_CHANGE, // type of service
  216. SERVICE_NO_CHANGE, // when to start service
  217. SERVICE_NO_CHANGE, // severity if service fails to start
  218. NULL, // pointer to service binary file name
  219. NULL, // pointer to load ordering group name
  220. NULL, // pointer to variable to get tag identifier
  221. DependentServiceName, // pointer to array of dependency names
  222. NULL, // pointer to account name of service
  223. NULL, // pointer to password for service account
  224. NULL // pointer to display name
  225. )) {
  226. goto exit;
  227. }
  228. rVal = TRUE;
  229. exit:
  230. CloseServiceHandle( hService );
  231. CloseServiceHandle( hSvcMgr );
  232. return rVal;
  233. }
  234. BOOL
  235. SetServiceStart(
  236. LPTSTR ServiceName,
  237. DWORD StartType
  238. )
  239. {
  240. BOOL rVal = FALSE;
  241. SC_HANDLE hSvcMgr;
  242. SC_HANDLE hService;
  243. hSvcMgr = OpenSCManager(
  244. NULL,
  245. NULL,
  246. SC_MANAGER_ALL_ACCESS
  247. );
  248. if (!hSvcMgr) {
  249. goto exit;
  250. }
  251. hService = OpenService(
  252. hSvcMgr,
  253. ServiceName,
  254. SERVICE_ALL_ACCESS
  255. );
  256. if (!hService) {
  257. goto exit;
  258. }
  259. if (!ChangeServiceConfig(
  260. hService, // handle to service
  261. SERVICE_NO_CHANGE, // type of service
  262. StartType, // when to start service
  263. SERVICE_NO_CHANGE, // severity if service fails to start
  264. NULL, // pointer to service binary file name
  265. NULL, // pointer to load ordering group name
  266. NULL, // pointer to variable to get tag identifier
  267. NULL, // pointer to array of dependency names
  268. NULL, // pointer to account name of service
  269. NULL, // pointer to password for service account
  270. NULL // pointer to display name
  271. ))
  272. {
  273. goto exit;
  274. }
  275. rVal = TRUE;
  276. exit:
  277. CloseServiceHandle( hService );
  278. CloseServiceHandle( hSvcMgr );
  279. return rVal;
  280. }
  281. /*
  282. BOOL
  283. SetServiceAccount(
  284. LPTSTR ServiceName,
  285. PSECURITY_INFO SecurityInfo
  286. )
  287. {
  288. BOOL rVal = FALSE;
  289. SC_HANDLE hSvcMgr;
  290. SC_HANDLE hService;
  291. hSvcMgr = OpenSCManager(
  292. NULL,
  293. NULL,
  294. SC_MANAGER_ALL_ACCESS
  295. );
  296. if (!hSvcMgr) {
  297. goto exit;
  298. }
  299. hService = OpenService(
  300. hSvcMgr,
  301. ServiceName,
  302. SERVICE_ALL_ACCESS
  303. );
  304. if (!hService) {
  305. goto exit;
  306. }
  307. if (!ChangeServiceConfig(
  308. hService, // handle to service
  309. SERVICE_NO_CHANGE, // type of service
  310. SERVICE_NO_CHANGE, // when to start service
  311. SERVICE_NO_CHANGE, // severity if service fails to start
  312. NULL, // pointer to service binary file name
  313. NULL, // pointer to load ordering group name
  314. NULL, // pointer to variable to get tag identifier
  315. NULL, // pointer to array of dependency names
  316. SecurityInfo->AccountName, // pointer to account name of service
  317. SecurityInfo->Password, // pointer to password for service account
  318. NULL // pointer to display name
  319. )) {
  320. goto exit;
  321. }
  322. rVal = TRUE;
  323. exit:
  324. CloseServiceHandle( hService );
  325. CloseServiceHandle( hSvcMgr );
  326. return rVal;
  327. }
  328. */