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.

467 lines
10 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. if (hService)
  106. {
  107. CloseServiceHandle( hService );
  108. }
  109. if (hSCM)
  110. {
  111. CloseServiceHandle( hSCM );
  112. }
  113. }
  114. return dwError;
  115. } //StiServiceInstall
  116. DWORD
  117. WINAPI
  118. StiServiceRemove(
  119. VOID
  120. )
  121. /*++
  122. Routine Description:
  123. Service removal function. This function calls SCM to remove the STI service.
  124. Arguments:
  125. None.
  126. Return Value:
  127. Return code. Return zero for success
  128. --*/
  129. {
  130. DWORD dwError = NOERROR;
  131. SC_HANDLE hSCM = NULL;
  132. SC_HANDLE hService = NULL;
  133. SERVICE_STATUS ServiceStatus;
  134. UINT uiRetry = 10;
  135. HKEY hkRun;
  136. __try {
  137. hSCM = ::OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
  138. if (!hSCM) {
  139. dwError = GetLastError();
  140. __leave;
  141. }
  142. hService = OpenService(
  143. hSCM,
  144. STI_SERVICE_NAME,
  145. SERVICE_ALL_ACCESS
  146. );
  147. if (!hService) {
  148. dwError = GetLastError();
  149. __leave;
  150. }
  151. //
  152. // Stop service first
  153. //
  154. if (ControlService( hService, SERVICE_CONTROL_STOP, &ServiceStatus )) {
  155. //
  156. // Wait a little
  157. //
  158. Sleep( STI_STOP_FOR_REMOVE_TIMEOUT );
  159. ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
  160. while( QueryServiceStatus( hService, &ServiceStatus ) &&
  161. (SERVICE_STOP_PENDING == ServiceStatus.dwCurrentState)) {
  162. Sleep( STI_STOP_FOR_REMOVE_TIMEOUT );
  163. if (!uiRetry--) {
  164. break;
  165. }
  166. }
  167. if (ServiceStatus.dwCurrentState != SERVICE_STOPPED) {
  168. dwError = GetLastError();
  169. __leave;
  170. }
  171. }
  172. else {
  173. dwError = GetLastError();
  174. __leave;
  175. }
  176. if (!DeleteService( hService )) {
  177. dwError = GetLastError();
  178. __leave;
  179. }
  180. }
  181. __finally {
  182. if (hService)
  183. {
  184. CloseServiceHandle( hService );
  185. }
  186. if (hSCM)
  187. {
  188. CloseServiceHandle( hSCM );
  189. }
  190. }
  191. //
  192. // Leftovers from Win9x - remove STI monitor from Run section
  193. //
  194. if (RegOpenKey(HKEY_LOCAL_MACHINE, REGSTR_PATH_RUN, &hkRun) == NO_ERROR) {
  195. RegDeleteValue (hkRun, REGSTR_VAL_MONITOR);
  196. RegCloseKey(hkRun);
  197. }
  198. return dwError;
  199. } // StiServiceRemove
  200. BOOL
  201. SetServiceDependency(
  202. LPTSTR ServiceName,
  203. LPTSTR DependentServiceName
  204. )
  205. {
  206. BOOL rVal = FALSE;
  207. SC_HANDLE hSvcMgr = NULL;
  208. SC_HANDLE hService = NULL;
  209. hSvcMgr = OpenSCManager(
  210. NULL,
  211. NULL,
  212. SC_MANAGER_ALL_ACCESS
  213. );
  214. if (!hSvcMgr) {
  215. goto exit;
  216. }
  217. hService = OpenService(
  218. hSvcMgr,
  219. ServiceName,
  220. SERVICE_ALL_ACCESS
  221. );
  222. if (!hService) {
  223. goto exit;
  224. }
  225. if (!ChangeServiceConfig(
  226. hService, // handle to service
  227. SERVICE_NO_CHANGE, // type of service
  228. SERVICE_NO_CHANGE, // when to start service
  229. SERVICE_NO_CHANGE, // severity if service fails to start
  230. NULL, // pointer to service binary file name
  231. NULL, // pointer to load ordering group name
  232. NULL, // pointer to variable to get tag identifier
  233. DependentServiceName, // pointer to array of dependency names
  234. NULL, // pointer to account name of service
  235. NULL, // pointer to password for service account
  236. NULL // pointer to display name
  237. )) {
  238. goto exit;
  239. }
  240. rVal = TRUE;
  241. exit:
  242. if (hService)
  243. {
  244. CloseServiceHandle( hService );
  245. }
  246. if (hSvcMgr)
  247. {
  248. CloseServiceHandle( hSvcMgr );
  249. }
  250. return rVal;
  251. }
  252. BOOL
  253. SetServiceStart(
  254. LPTSTR ServiceName,
  255. DWORD StartType
  256. )
  257. {
  258. BOOL rVal = FALSE;
  259. SC_HANDLE hSvcMgr = NULL;
  260. SC_HANDLE hService = NULL;
  261. hSvcMgr = OpenSCManager(
  262. NULL,
  263. NULL,
  264. SC_MANAGER_ALL_ACCESS
  265. );
  266. if (!hSvcMgr) {
  267. goto exit;
  268. }
  269. hService = OpenService(
  270. hSvcMgr,
  271. ServiceName,
  272. SERVICE_ALL_ACCESS
  273. );
  274. if (!hService) {
  275. goto exit;
  276. }
  277. if (!ChangeServiceConfig(
  278. hService, // handle to service
  279. SERVICE_NO_CHANGE, // type of service
  280. StartType, // when to start service
  281. SERVICE_NO_CHANGE, // severity if service fails to start
  282. NULL, // pointer to service binary file name
  283. NULL, // pointer to load ordering group name
  284. NULL, // pointer to variable to get tag identifier
  285. NULL, // pointer to array of dependency names
  286. NULL, // pointer to account name of service
  287. NULL, // pointer to password for service account
  288. NULL // pointer to display name
  289. ))
  290. {
  291. goto exit;
  292. }
  293. rVal = TRUE;
  294. exit:
  295. if (hService)
  296. {
  297. CloseServiceHandle( hService );
  298. }
  299. if (hSvcMgr)
  300. {
  301. CloseServiceHandle( hSvcMgr );
  302. }
  303. return rVal;
  304. }
  305. /*
  306. BOOL
  307. SetServiceAccount(
  308. LPTSTR ServiceName,
  309. PSECURITY_INFO SecurityInfo
  310. )
  311. {
  312. BOOL rVal = FALSE;
  313. SC_HANDLE hSvcMgr = NULL;
  314. SC_HANDLE hService = NULL;
  315. hSvcMgr = OpenSCManager(
  316. NULL,
  317. NULL,
  318. SC_MANAGER_ALL_ACCESS
  319. );
  320. if (!hSvcMgr) {
  321. goto exit;
  322. }
  323. hService = OpenService(
  324. hSvcMgr,
  325. ServiceName,
  326. SERVICE_ALL_ACCESS
  327. );
  328. if (!hService) {
  329. goto exit;
  330. }
  331. if (!ChangeServiceConfig(
  332. hService, // handle to service
  333. SERVICE_NO_CHANGE, // type of service
  334. SERVICE_NO_CHANGE, // when to start service
  335. SERVICE_NO_CHANGE, // severity if service fails to start
  336. NULL, // pointer to service binary file name
  337. NULL, // pointer to load ordering group name
  338. NULL, // pointer to variable to get tag identifier
  339. NULL, // pointer to array of dependency names
  340. SecurityInfo->AccountName, // pointer to account name of service
  341. SecurityInfo->Password, // pointer to password for service account
  342. NULL // pointer to display name
  343. )) {
  344. goto exit;
  345. }
  346. rVal = TRUE;
  347. exit:
  348. if (hService)
  349. {
  350. CloseServiceHandle( hService );
  351. }
  352. if (hSvcMgr)
  353. {
  354. CloseServiceHandle( hSvcMgr );
  355. }
  356. return rVal;
  357. }
  358. */